Advertisement
AlphaSmack

Untitled

Apr 18th, 2014
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.50 KB | None | 0 0
  1. <?php  
  2. class Router {  
  3.  
  4.     function __construct() {  
  5.     }  
  6.  
  7.     public function handle() {
  8.         try {
  9.             $r = array();  
  10.             if ($_SERVER['REQUEST_METHOD'] == 'GET') {  
  11.                 $this->handleGet($r);  
  12.                 echo json_encode($r);  
  13.             } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {  
  14.                 $this->handlePost($r);  
  15.                 echo json_encode($r);  
  16.             }
  17.         }
  18.         catch (ErrorException $e) {
  19.             echo $e->getMessage();
  20.         }
  21.     }  
  22.  
  23.     public static function get($obj, $key) {  
  24.         if (isset($obj[$key])) {  
  25.             return mysql_escape_string($obj[$key]);  
  26.         }  
  27.         throw new ErrorException();  
  28.     }  
  29.  
  30.     public function handleGet(&$r) {  
  31.          
  32.         if(isset($_GET['component'])){  
  33.             $this->handleComponent($r);  
  34.             return;    }  
  35.         elseif(isset($_GET['templates'])){  
  36.             $template = new TemplateLoader();  
  37.             $template->handle($r);  
  38.             return;}  
  39.         else{  
  40.             throw new ErrorException("Component Error");  
  41.         }  
  42.         return;  
  43.     }  
  44.      
  45.     public function handleComponent(&$r) {  
  46.         $r['req'] = array();  
  47.         $component = self::get($_GET, 'component');  
  48.         $r['req']['component'] = $component;  
  49.         $r['req']['target'] = self::get($_GET, 'target');  
  50.          
  51.         if ($component == 'main') {  
  52.             //http://localhost/example/?component=main  
  53.             $r["images"] = Main::$data -> get3Images();  
  54.             $r["updates"] = array();  
  55.             $r["comments"] = array();  
  56.              
  57.             $r["template"] = 'main';  
  58.             return;  
  59.         }  
  60.         if ($component == 'depots') {  
  61.             //http://localhost/example/?component=depots&location=1  
  62.             $location = self::get($_GET, 'location');  
  63.             $r['req']['location'] = $location;  
  64.             $r["location"] = $location;  
  65.             $r["depots"] = array( array("id" => "1", "name" => "park1", ));  
  66.             return;  
  67.         }  
  68.         if ($component == 'depot_buses') {  
  69.             //http://localhost/example/?component=depot_buses&depot=1  
  70.             $depot = self::get($_GET, 'depot');  
  71.             $r['req']['depot'] = $depot;  
  72.              
  73.             $r["depot"] = $depot;  
  74.             $r["buses"] = array("buses" => 'array()');  
  75.             return;  
  76.         }  
  77.         if ($component == 'search') {  
  78.             $r["search"] = array();  
  79.             return;  
  80.         }  
  81.         if ($component == 'bus') {  
  82.             $id = self::get($_GET, 'id');  
  83.             $r['req']['id'] = $id;  
  84.              
  85.             $bus = Main::$data->getBus($id);  
  86.             $r['bus'] = $bus;  
  87.              
  88.             $images = Main::$data->getBusImages($id);  
  89.             $r['images'] = $images;  
  90.             $r['template'] = 'bus';  
  91.             return;  
  92.         }  
  93.         if ($component == 'gallery') {  
  94.             //http://localhost/example/?component=gallery  
  95.             $r['gallery'] = Main::$data->getGallery();  
  96.             return;  
  97.         }  
  98.         if ($component == 'comments') {  
  99.             //http://localhost/example/?component=comments  
  100.             $r["comments"] = array( array('id' => 1, 'bus' => 1, 'text' => 'text1'), array('id' => 2, 'bus' => 3, 'text' => 'text2'), array('id' => 3, 'bus' => 4, 'text' => 'text3'));  
  101.             return;  
  102.         }  
  103.  
  104.         if ($component == 'locations') {  
  105.             //http://localhost/example/?component=locations  
  106.             $r["locations"] = Main::$data -> getLocations();  
  107.             $r["template"] = 'locations';  
  108.             return;  
  109.         }  
  110.  
  111.         throw new ErrorException("component error");  
  112.     }  
  113.  
  114.     public function handlePost(&$r) {  
  115.         $component = self::get($_POST, 'component');  
  116.         $r['req']['component'] = $component;  
  117.         $r['req']['target'] = self::get($_POST, 'target');  
  118.         if ($component == 'bus') {  
  119.             $fields = array(  
  120.             "id",  
  121.             "location",  
  122.             "service_type",  
  123.             "depot",  
  124.             "model",  
  125.             "state",  
  126.             "plate_nr",  
  127.             "built",  
  128.             "page_views",  
  129.             );  
  130.              
  131.             $bus = array();  
  132.             foreach ($fields as $field) {  
  133.                 $bus[$field] = self::get($_POST, $field);  
  134.             }  
  135.             $r['req']['bus'] = $bus;  
  136.             $id = mysql_real_escape_string($r['req']['bus']['id']);  
  137.             $count = Main::$data->getSingleCell("select count(1) from bus where id = '$id'");  
  138.              
  139.             if($count > 1){  
  140.                 throw new ErrorException("post error");  
  141.             }  
  142.              
  143.             if($count == 1){  
  144.                 Main::$data->updateBus($bus);  
  145.             }  
  146.              
  147.             if($count == 0){  
  148.                 $id = Main::$data->updateBus($bus,TRUE);  
  149.                 $r['location'] = "component=bus&target=content&id=$id";  
  150.             }  
  151.              
  152.             $bus = Main::$data->getBus($id);  
  153.             $r['bus'] = $bus;  
  154.              
  155.             $images = Main::$data->getBusImages($id);  
  156.             $r['images'] = $images;  
  157.              
  158.             $r['template'] = 'bus';  
  159.              
  160.             return;  
  161.         }  
  162.         throw new ErrorException("post error");  
  163.     }  
  164.  
  165. }  
  166. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement