router.class.php 7.86 KB
<?php

class router {
 /*
 * @the registry
 */
 private $tpl;

 /*
 * @the controller path
 */
 private $path;
 
 private $path_controller;
 
 private $modules;
 
 static $modul;
 
 private $modules_default = 'default';
 
 private $admin = false;

 private $args = array();
 
 private $rules = array();
 
 private $start = array();
 
 public $getParam = array();
 
 public $postParam = array();
 
 private $error = array();

 public $file;

 public $controller = 'index';

 public $action = 'index';

 public $action_params;
 
 public $langs;  public $lang;

 function __construct() {

 }
 
 function setParams($tpl,&$error,$url,$this_url,&$navig,&$meta){
        $this->tpl = $tpl;
        $this->error = &$error;
        $this->url = $url;
        $this->this_url = $this_url;
        $this->navig = &$navig;
        $this->meta = &$meta;
        $this->getParams();
        $this->postParams();
 }

 /**
 *
 * @set controller directory path
 *
 * @param string $path
 *
 * @return void
 *
 */
 function setPath($path,$modules,$controller_path) {

        /*** check if path i sa directory ***/
        if (is_dir($path) == false)
        {
                throw new Exception ('Invalid controller path: `' . $path . '`');
        }
        /*** set the path ***/
        $this->path = $path;
        $this->modules_default = $modules;
        $this->modules = $this->modules_default;
        $this->path_controller = $controller_path;
}

 /**
 *
 * @load the controller
 *
 * @access public
 *
 * @return void
 *
 */
 public function loader()
 {
        /*** check the route ***/

        $parts = $this->getRouter();

        if($this->Rule($parts) == false){
        $this->getModule($parts); 
        if (is_readable($this->file) == false && !is_dir($this->path .'/'. $this->modules)){
         $this->getController($parts);
        }}
      /*
        if(isset($this->start[$this->modules]) && count($parts)==0){
         $this->controller = $this->start[$this->modules]['controller'];
         $this->action = $this->start[$this->modules]['action'];
         print_r($this->start[$this->modules]);
        }   */

        /*** if the file is not there diaf ***/
        if (is_readable($this->file) == false)
        {
                $this->file = $this->path .'/'. $this->modules .'/'. $this->path_controller . '/error404.php';
                $this->controller = 'error404';
        }
    self::$modul = $this->modules;
   return array('path'=>$this->path .'/'. $this->modules, 'controller'=>$this->controller);
 }
 
public function run(){
        /*** include the controller ***/

        include_once $this->file;

        $class = $this->getClassController();
        $controller = new $class($this->tpl,$this->error,$this->getParam,$this->postParam,$this->lang,$this->url,$this->this_url,$this->navig,$this->meta);

        /*** check if the action is callable ***/
        if (is_callable(array($controller, $this->action)) == false)
        {
          $action = 'index';
        }
        else
        {
                $action = $this->action;
        }

        if(is_callable(array($controller, 'preDispatch'))){
         $controller->preDispatch($action);
        }

        /*** run the action ***/
        call_user_func_array(array($controller, $action), $this->args);
         Blocks::$load = $controller->loadBlock;
        $this->tpl->controllerAction($this->path , $this->modules, $this->controller, $action);
        $this->tpl->setAction();
        
      /*  if($controller->error404 == true){
         $this->file = $this->path .'/'. $this->modules .'/'. $this->path_controller . '/error404.php';
         $this->controller = 'error404';
         $this->run();
        }  */
}

public function setLangs($langs,$lang){
 $this->langs = $langs;
 $this->lang = $lang;
}

public function setStart($start){
 $this->start = $start;
}

public function setRule($rules){
 $this->rules = $rules;
}

private function Rule($parts){
 if(in_array($parts[0],$this->langs)){$this->lang = $parts[0];unset($parts[0]);}
 $index = implode('/',$parts);

 foreach($this->rules as $key=>$rule){
 //print"/$key/i <br>";
 if( preg_match("/^$key$/i",$index,$matches) ){
 // print_r($matches);
  if( preg_match('/^\$([0-9]+)$/',$rule['modul'],$mat) ) $this->modules = $matches[$mat[1]];
  else $this->modules = $rule['modul'];

  if( preg_match('/^\$([0-9]+)$/',$rule['controller'],$mat) ) $this->controller = $matches[$mat[1]];
  else $this->controller = $rule['controller'];
  
  if( preg_match('/^\$([0-9]+)$/',$rule['action'],$mat) ) $this->action = $matches[$mat[1]];
  else $this->action = $rule['action'];

  $args = array();
  foreach($rule['args'] as $_args){
   if( preg_match('/^\$([0-9]+)$/',$_args,$mat) )$args[] = $matches[$mat[1]];
   else $args[] = $_args;
  }

  $this->getArgs($args);
  $this->getFileController();
  return true;
 }
 }
 /*
 if(isset($this->rules[$index])){
  $this->modules = $this->rules[$index]['modul'];
  $this->controller = $this->rules[$index]['controller'];
  $this->action = $this->rules[$index]['action'];
  $this->getArgs($this->rules[$index]['args']);
  $this->getFileController();
  return true;
 }   */
 return false;
}

private function getParams($data = array()){
 // $this->getParam = array_merge($this->trim($_GET),$this->getParam);
 $this->getParam = $this->trim($_GET);
}

private function postParams($data = array()){
  $this->postParam = $this->trim($_POST);
}

private function trim(&$data){
  foreach($data as $key=>$value){
   if(is_array($data[$key]))$this->trim($data[$key]);
   else $data[$key] = trim($value);
  }
  return $data;
}

 /**
 *
 * @get the controller
 *
 * @access private
 *
 * @return void
 *
 */
private function getRouter(){
 $route = (empty($_GET['rt'])) ? '' : $_GET['rt'];
 $parts = explode('/', $route);
 $parts_array = array();
 foreach($parts as $part)
 {
    if (!empty($part))
    {
        $parts_array[] = trim($part);
    }
 }
 return $parts_array;
}

private function getArgs($parts){
// $parts = array_chunk($parts, 2);
 $this->args = array();
 foreach($parts as $part)
 {//print $part;
  $this->args[] = trim($part);
 }//print_r($this->args);
}

private function setArgsGet($parts,$prefix = "_"){
 foreach($parts as $part){
  if(strpos($part,$prefix)){
   $r = explode($prefix, $part);
   $this->getParam[$r[0]] = trim($r[1]);
   $_GET[$r[0]] = trim($r[1]);
  }else{$this->args[] = trim($part);}
 }
}

private function getModule($parts){
 $index = 0; 
 if(isset($parts[0]) && strlen($parts[0])>0){
  if(in_array($parts[0],$this->langs)){$this->lang = $parts[0];$index++;}
 }
 if(isset($parts[0+$index]) && strlen($parts[0+$index])>0){
  $this->modules = $parts[0+$index];
 }
 if(isset($parts[1+$index]) && strlen($parts[1+$index])){
  $this->controller = $parts[1+$index];
 }
 if(isset($parts[2+$index]) && strlen($parts[2+$index])){
  $this->action = $parts[2+$index];
 }
 if(isset($parts[3+$index]) && strlen($parts[3+$index])){
//  $this->setArgsGet(array_slice($parts, 3+$index));
  $this->getArgs(array_slice($parts, 3+$index));
 }
 $this->getFileController();
}

private function getController($parts){
 $this->modules = $this->modules_default;
 $index = 0;
 if(isset($parts[0]) && strlen($parts[0])>0){
  if(in_array($parts[0],$this->langs)){$this->lang = $parts[0];$index++;}
 }
 if(isset($parts[0+$index]) && strlen($parts[0+$index])){
  $this->controller = $parts[0+$index];
 }
 if(isset($parts[1+$index]) && strlen($parts[1+$index])){
  $this->action = $parts[1+$index];
 }
 if(isset($parts[2+$index]) && strlen($parts[2+$index])){
 // $this->setArgsGet(array_slice($parts, 3+$index));
  $this->getArgs(array_slice($parts, 2+$index));
 }
 $this->getFileController();
}

private function getFileController(){
 $this->file = $this->path .'/'. $this->modules .'/'. $this->path_controller .'/'. $this->controller . 'Controller.php';
}

private function getClassController(){
 if($this->modules == $this->modules_default)return $this->controller . 'Controller';
 else return $this->modules . '_' . $this->controller . 'Controller';
}
 


}

?>