Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Router {
- function __construct() {
- }
- public function handle() {
- try {
- $r = array();
- if ($_SERVER['REQUEST_METHOD'] == 'GET') {
- $this->handleGet($r);
- echo json_encode($r);
- } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- $this->handlePost($r);
- echo json_encode($r);
- }
- }
- catch (ErrorException $e) {
- echo $e->getMessage();
- }
- }
- public static function get($obj, $key) {
- if (isset($obj[$key])) {
- return mysql_escape_string($obj[$key]);
- }
- throw new ErrorException();
- }
- public function handleGet(&$r) {
- if(isset($_GET['component'])){
- $this->handleComponent($r);
- return; }
- elseif(isset($_GET['templates'])){
- $template = new TemplateLoader();
- $template->handle($r);
- return;}
- else{
- throw new ErrorException("Component Error");
- }
- return;
- }
- public function handleComponent(&$r) {
- $r['req'] = array();
- $component = self::get($_GET, 'component');
- $r['req']['component'] = $component;
- $r['req']['target'] = self::get($_GET, 'target');
- if ($component == 'main') {
- //http://localhost/example/?component=main
- $r["images"] = Main::$data -> get3Images();
- $r["updates"] = array();
- $r["comments"] = array();
- $r["template"] = 'main';
- return;
- }
- if ($component == 'depots') {
- //http://localhost/example/?component=depots&location=1
- $location = self::get($_GET, 'location');
- $r['req']['location'] = $location;
- $r["location"] = $location;
- $r["depots"] = array( array("id" => "1", "name" => "park1", ));
- return;
- }
- if ($component == 'depot_buses') {
- //http://localhost/example/?component=depot_buses&depot=1
- $depot = self::get($_GET, 'depot');
- $r['req']['depot'] = $depot;
- $r["depot"] = $depot;
- $r["buses"] = array("buses" => 'array()');
- return;
- }
- if ($component == 'search') {
- $r["search"] = array();
- return;
- }
- if ($component == 'bus') {
- $id = self::get($_GET, 'id');
- $r['req']['id'] = $id;
- $bus = Main::$data->getBus($id);
- $r['bus'] = $bus;
- $images = Main::$data->getBusImages($id);
- $r['images'] = $images;
- $r['template'] = 'bus';
- return;
- }
- if ($component == 'gallery') {
- //http://localhost/example/?component=gallery
- $r['gallery'] = Main::$data->getGallery();
- return;
- }
- if ($component == 'comments') {
- //http://localhost/example/?component=comments
- $r["comments"] = array( array('id' => 1, 'bus' => 1, 'text' => 'text1'), array('id' => 2, 'bus' => 3, 'text' => 'text2'), array('id' => 3, 'bus' => 4, 'text' => 'text3'));
- return;
- }
- if ($component == 'locations') {
- //http://localhost/example/?component=locations
- $r["locations"] = Main::$data -> getLocations();
- $r["template"] = 'locations';
- return;
- }
- throw new ErrorException("component error");
- }
- public function handlePost(&$r) {
- $component = self::get($_POST, 'component');
- $r['req']['component'] = $component;
- $r['req']['target'] = self::get($_POST, 'target');
- if ($component == 'bus') {
- $fields = array(
- "id",
- "location",
- "service_type",
- "depot",
- "model",
- "state",
- "plate_nr",
- "built",
- "page_views",
- );
- $bus = array();
- foreach ($fields as $field) {
- $bus[$field] = self::get($_POST, $field);
- }
- $r['req']['bus'] = $bus;
- $id = mysql_real_escape_string($r['req']['bus']['id']);
- $count = Main::$data->getSingleCell("select count(1) from bus where id = '$id'");
- if($count > 1){
- throw new ErrorException("post error");
- }
- if($count == 1){
- Main::$data->updateBus($bus);
- }
- if($count == 0){
- $id = Main::$data->updateBus($bus,TRUE);
- $r['location'] = "component=bus&target=content&id=$id";
- }
- $bus = Main::$data->getBus($id);
- $r['bus'] = $bus;
- $images = Main::$data->getBusImages($id);
- $r['images'] = $images;
- $r['template'] = 'bus';
- return;
- }
- throw new ErrorException("post error");
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement