PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/classes/requesthandler.php

https://gitlab.com/dennisluitwieler/itch
PHP | 303 lines | 181 code | 34 blank | 88 comment | 4 complexity | 88f2ca5baa03d3c858ddb5762852168e MD5 | raw file
  1. <?php
  2. /**
  3. * RequestHandler
  4. *
  5. * Handles a request
  6. */
  7. class RequestHandler
  8. {
  9. private $request;
  10. /**
  11. * Initialise request handler
  12. *
  13. * @param Array $request The request arguments (basically $_GET)
  14. */
  15. public function __construct($request)
  16. {
  17. $this->request = $request;
  18. }
  19. /**
  20. * Handle the request
  21. */
  22. public function handle()
  23. {
  24. $data = $this->getPageData();
  25. $html = render("theme/templates/html.tpl.php", $data);
  26. echo $html;
  27. }
  28. /**
  29. * Get the page data (dummy data)
  30. *
  31. * @return array Page data
  32. */
  33. private function getPageData()
  34. {
  35. $page = $this->getRequestedUrl();
  36. $data = array(
  37. "title" => ucfirst(text($page)),
  38. "content" => array(
  39. "#main-menu" => $this->getMainMenu(),
  40. "#secondary-menu" => $this->getSecondaryMenu(),
  41. "#overlay" => array(
  42. "profile" => render("theme/templates/content/overlay-profile.tpl.php"),
  43. "notifications" => render("theme/templates/content/overlay-notifications.tpl.php"),
  44. "teams" => render("theme/templates/content/overlay-teams.tpl.php"),
  45. "phonebook" => render("theme/templates/content/overlay-phonebook.tpl.php"),
  46. ),
  47. "#tertiary-menu" => '',
  48. "#widgets" => array(),
  49. )
  50. );
  51. switch($page)
  52. {
  53. case "home":
  54. $data["title"] = "Timeline";
  55. $data["content"]["#content"] = render("theme/templates/content/timeline.tpl.php", array("messages" => $this->getDummyTimelineItems()));
  56. $data["content"]["#tertiary-menu"] = $this->getTertiaryMenu();
  57. $data["content"]["#widgets"] = $this->getDummyWidgets();
  58. break;
  59. case "blogs":
  60. case "planner":
  61. case "documents":
  62. $data["content"]["#content"] = render("theme/templates/content/{$page}.tpl.php");
  63. $data["content"]["#tertiary-menu"] = $this->getTertiaryMenu();
  64. $data["content"]["#widgets"] = $this->getDummyWidgets();
  65. break;
  66. default:
  67. $data["content"]["#content"] = str_repeat("body content ", 130);
  68. break;
  69. }
  70. return $data;
  71. }
  72. /**
  73. * Get Secondary menu (dummy implementation)
  74. *
  75. * @return array Array with items for the menu
  76. */
  77. private function getSecondaryMenu()
  78. {
  79. $params = array(
  80. "profile" => $this->getRandomProfile(),
  81. );
  82. return render("theme/templates/content/secondary-menu.tpl.php", $params);
  83. }
  84. /**
  85. * Get Main menu (dummy implementation)
  86. *
  87. * @return array Array with items for the menu
  88. */
  89. private function getMainMenu()
  90. {
  91. $menuList = array(
  92. "/" => "home",
  93. "/getstuffdone" => "Get stuff done",
  94. "/news" => "news",
  95. );
  96. return $this->createMenuItemsArray($menuList);
  97. }
  98. /**
  99. * Get Tertiary menu (dummy implementation)
  100. *
  101. * @return array Array with items for the menu
  102. */
  103. private function getTertiaryMenu()
  104. {
  105. $active = $this->getRequestedUrl();
  106. return render("theme/templates/content/tertiary-menu.tpl.php", array("active" => $active));
  107. }
  108. /**
  109. * Create an array of menu items based on a menuList array
  110. *
  111. * @param $menuList
  112. * @return array
  113. */
  114. private function createMenuItemsArray($menuList)
  115. {
  116. $menu = array();
  117. foreach ($menuList as $path => $label)
  118. {
  119. $menu[] = $this->createMenuItem($path, $label, false);
  120. }
  121. return $menu;
  122. }
  123. /**
  124. * Create a menu item
  125. *
  126. * @param String $path The path/url for this menu item
  127. * @param String $label The name for this menu item
  128. * @param bool|FALSE $isActive Wether this menu item is currently active
  129. * @return array
  130. */
  131. private function createMenuItem($path, $label, $icon=false, $isActive=false)
  132. {
  133. $class = strtolower(preg_replace('/[^[a-zA-Z0-9]]/', '-', $label));
  134. $classes = array();
  135. $classes[] = "menu-item";
  136. $classes[] = "menu-item-".$class;
  137. if ($icon)
  138. $classes[] = "icon-".$class;
  139. return array(
  140. "path" => $path,
  141. "label" => $label,
  142. "active" => $isActive,
  143. "classes" => implode(' ', $classes),
  144. );
  145. }
  146. /**
  147. * Get the requested URL
  148. *
  149. * @return string
  150. */
  151. private function getRequestedUrl()
  152. {
  153. return array_key_exists("q", $this->request) ? $this->request["q"] : "home";
  154. }
  155. /**
  156. * Get some dummy timeline items
  157. *
  158. * @return array
  159. */
  160. private function getDummyTimelineItems()
  161. {
  162. $randomPostDates = $this->generateRandomPostDateList();
  163. $items = array();
  164. for ($i=1; $i<10; $i++)
  165. {
  166. $items[] = array(
  167. "profile" => $this->getRandomProfile(),
  168. "text" => file_get_contents('http://loripsum.net/api'),
  169. "post-date" => $this->getRandomItemFromArray($randomPostDates)
  170. );
  171. }
  172. return $items;
  173. }
  174. /**
  175. * Generate a list of random postdates
  176. *
  177. * @return array
  178. */
  179. private function generateRandomPostDateList()
  180. {
  181. $randomPostDates = array();
  182. for($i=0; $i<10; $i++)
  183. {
  184. $randomPostDates[] = "1 hour ago";
  185. $randomPostDates[] = rand(2,59) ." minutes ago";
  186. $randomPostDates[] = rand(2,date("H")) ." hours ago";
  187. $yesterdaystamp = mktime(rand(0,23),rand(0,59),0, date("m"), date("d"),date("Y"));
  188. $randomPostDates[] = "yesterday at ".date("H:i", $yesterdaystamp);
  189. $randomDateStamp = mktime(12, 0, 0, date("m"), date("d") - rand(1,100),date("Y"));
  190. $randomPostDates[] = "on ".date("d-m-Y", $randomDateStamp);
  191. }
  192. return $randomPostDates;
  193. }
  194. /**
  195. * Get a random item from the given array
  196. *
  197. * @param Array $options Options to choose from
  198. * @return mixed
  199. */
  200. private function getRandomItemFromArray($options)
  201. {
  202. return $options[rand(0,count($options)-1)];
  203. }
  204. /**
  205. * Get a random dummy profile
  206. *
  207. * A profile contains:
  208. * - User id
  209. * - username
  210. * - Path to the profile image
  211. *
  212. * @return array
  213. */
  214. private function getRandomProfile()
  215. {
  216. $id = rand(1,5);
  217. switch($id)
  218. {
  219. case 1:
  220. $name = "Dennis Luitwieler";
  221. break;
  222. default:
  223. $name = "Random User #".$id;
  224. break;
  225. }
  226. return array(
  227. "id" => $id,
  228. "image" => "/theme/images/profiles/". $id . ".png",
  229. "username" => $name
  230. );
  231. }
  232. /**
  233. * Get some dummy widgets
  234. *
  235. * @return array
  236. */
  237. private function getDummyWidgets()
  238. {
  239. $widgets = array();
  240. $widgets[] = array(
  241. "content" => $this->getDummyFindColleaguesWidget(),
  242. );
  243. $widgets[] = array(
  244. "content" => $this->getDummyFindColleaguesWidget(),
  245. );
  246. $widgets[] = array(
  247. "content" => $this->getDummyFindColleaguesWidget(),
  248. );
  249. return $widgets;
  250. }
  251. /**
  252. * Get the dummy widget for finding colleagues
  253. *
  254. * @return string A Rendered Widget (HTML)
  255. */
  256. private function getDummyFindColleaguesWidget()
  257. {
  258. $profiles = array();
  259. for ($i=0; $i<rand(7,14); $i++)
  260. {
  261. $profiles[]= $this->getRandomProfile();
  262. }
  263. $params = array(
  264. "title" => "Find someone",
  265. "profiles" => $profiles
  266. );
  267. return render('theme/templates/content/widgets/find-colleagues.tpl.php', $params);
  268. }
  269. }