/includes/classes/requesthandler.php
PHP | 303 lines | 181 code | 34 blank | 88 comment | 4 complexity | 88f2ca5baa03d3c858ddb5762852168e MD5 | raw file
- <?php
- /**
- * RequestHandler
- *
- * Handles a request
- */
- class RequestHandler
- {
- private $request;
- /**
- * Initialise request handler
- *
- * @param Array $request The request arguments (basically $_GET)
- */
- public function __construct($request)
- {
- $this->request = $request;
- }
- /**
- * Handle the request
- */
- public function handle()
- {
- $data = $this->getPageData();
- $html = render("theme/templates/html.tpl.php", $data);
- echo $html;
- }
- /**
- * Get the page data (dummy data)
- *
- * @return array Page data
- */
- private function getPageData()
- {
- $page = $this->getRequestedUrl();
- $data = array(
- "title" => ucfirst(text($page)),
- "content" => array(
- "#main-menu" => $this->getMainMenu(),
- "#secondary-menu" => $this->getSecondaryMenu(),
- "#overlay" => array(
- "profile" => render("theme/templates/content/overlay-profile.tpl.php"),
- "notifications" => render("theme/templates/content/overlay-notifications.tpl.php"),
- "teams" => render("theme/templates/content/overlay-teams.tpl.php"),
- "phonebook" => render("theme/templates/content/overlay-phonebook.tpl.php"),
- ),
- "#tertiary-menu" => '',
- "#widgets" => array(),
- )
- );
- switch($page)
- {
- case "home":
- $data["title"] = "Timeline";
- $data["content"]["#content"] = render("theme/templates/content/timeline.tpl.php", array("messages" => $this->getDummyTimelineItems()));
- $data["content"]["#tertiary-menu"] = $this->getTertiaryMenu();
- $data["content"]["#widgets"] = $this->getDummyWidgets();
- break;
- case "blogs":
- case "planner":
- case "documents":
- $data["content"]["#content"] = render("theme/templates/content/{$page}.tpl.php");
- $data["content"]["#tertiary-menu"] = $this->getTertiaryMenu();
- $data["content"]["#widgets"] = $this->getDummyWidgets();
- break;
- default:
- $data["content"]["#content"] = str_repeat("body content ", 130);
- break;
- }
- return $data;
- }
- /**
- * Get Secondary menu (dummy implementation)
- *
- * @return array Array with items for the menu
- */
- private function getSecondaryMenu()
- {
- $params = array(
- "profile" => $this->getRandomProfile(),
- );
- return render("theme/templates/content/secondary-menu.tpl.php", $params);
- }
- /**
- * Get Main menu (dummy implementation)
- *
- * @return array Array with items for the menu
- */
- private function getMainMenu()
- {
- $menuList = array(
- "/" => "home",
- "/getstuffdone" => "Get stuff done",
- "/news" => "news",
- );
- return $this->createMenuItemsArray($menuList);
- }
- /**
- * Get Tertiary menu (dummy implementation)
- *
- * @return array Array with items for the menu
- */
- private function getTertiaryMenu()
- {
- $active = $this->getRequestedUrl();
- return render("theme/templates/content/tertiary-menu.tpl.php", array("active" => $active));
- }
- /**
- * Create an array of menu items based on a menuList array
- *
- * @param $menuList
- * @return array
- */
- private function createMenuItemsArray($menuList)
- {
- $menu = array();
- foreach ($menuList as $path => $label)
- {
- $menu[] = $this->createMenuItem($path, $label, false);
- }
- return $menu;
- }
- /**
- * Create a menu item
- *
- * @param String $path The path/url for this menu item
- * @param String $label The name for this menu item
- * @param bool|FALSE $isActive Wether this menu item is currently active
- * @return array
- */
- private function createMenuItem($path, $label, $icon=false, $isActive=false)
- {
- $class = strtolower(preg_replace('/[^[a-zA-Z0-9]]/', '-', $label));
- $classes = array();
- $classes[] = "menu-item";
- $classes[] = "menu-item-".$class;
- if ($icon)
- $classes[] = "icon-".$class;
- return array(
- "path" => $path,
- "label" => $label,
- "active" => $isActive,
- "classes" => implode(' ', $classes),
- );
- }
- /**
- * Get the requested URL
- *
- * @return string
- */
- private function getRequestedUrl()
- {
- return array_key_exists("q", $this->request) ? $this->request["q"] : "home";
- }
- /**
- * Get some dummy timeline items
- *
- * @return array
- */
- private function getDummyTimelineItems()
- {
- $randomPostDates = $this->generateRandomPostDateList();
- $items = array();
- for ($i=1; $i<10; $i++)
- {
- $items[] = array(
- "profile" => $this->getRandomProfile(),
- "text" => file_get_contents('http://loripsum.net/api'),
- "post-date" => $this->getRandomItemFromArray($randomPostDates)
- );
- }
- return $items;
- }
- /**
- * Generate a list of random postdates
- *
- * @return array
- */
- private function generateRandomPostDateList()
- {
- $randomPostDates = array();
- for($i=0; $i<10; $i++)
- {
- $randomPostDates[] = "1 hour ago";
- $randomPostDates[] = rand(2,59) ." minutes ago";
- $randomPostDates[] = rand(2,date("H")) ." hours ago";
- $yesterdaystamp = mktime(rand(0,23),rand(0,59),0, date("m"), date("d"),date("Y"));
- $randomPostDates[] = "yesterday at ".date("H:i", $yesterdaystamp);
- $randomDateStamp = mktime(12, 0, 0, date("m"), date("d") - rand(1,100),date("Y"));
- $randomPostDates[] = "on ".date("d-m-Y", $randomDateStamp);
- }
- return $randomPostDates;
- }
- /**
- * Get a random item from the given array
- *
- * @param Array $options Options to choose from
- * @return mixed
- */
- private function getRandomItemFromArray($options)
- {
- return $options[rand(0,count($options)-1)];
- }
- /**
- * Get a random dummy profile
- *
- * A profile contains:
- * - User id
- * - username
- * - Path to the profile image
- *
- * @return array
- */
- private function getRandomProfile()
- {
- $id = rand(1,5);
- switch($id)
- {
- case 1:
- $name = "Dennis Luitwieler";
- break;
- default:
- $name = "Random User #".$id;
- break;
- }
- return array(
- "id" => $id,
- "image" => "/theme/images/profiles/". $id . ".png",
- "username" => $name
- );
- }
- /**
- * Get some dummy widgets
- *
- * @return array
- */
- private function getDummyWidgets()
- {
- $widgets = array();
- $widgets[] = array(
- "content" => $this->getDummyFindColleaguesWidget(),
- );
- $widgets[] = array(
- "content" => $this->getDummyFindColleaguesWidget(),
- );
- $widgets[] = array(
- "content" => $this->getDummyFindColleaguesWidget(),
- );
- return $widgets;
- }
- /**
- * Get the dummy widget for finding colleagues
- *
- * @return string A Rendered Widget (HTML)
- */
- private function getDummyFindColleaguesWidget()
- {
- $profiles = array();
- for ($i=0; $i<rand(7,14); $i++)
- {
- $profiles[]= $this->getRandomProfile();
- }
- $params = array(
- "title" => "Find someone",
- "profiles" => $profiles
- );
- return render('theme/templates/content/widgets/find-colleagues.tpl.php', $params);
- }
- }