PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Bengine/Game/Menu.php

https://github.com/Basti-sama/Bengine
PHP | 251 lines | 169 code | 13 blank | 69 comment | 11 complexity | 460c1742ab31c35c238f1f6734452da9 MD5 | raw file
  1. <?php
  2. /**
  3. * Menu class: Generates the menu by XML.
  4. *
  5. * @package Bengine
  6. * @copyright Copyright protected by / Urheberrechtlich geschützt durch "Sebastian Noll" <snoll@4ym.org>
  7. * @version $Id: Menu.php 8 2010-10-17 20:55:04Z secretchampion $
  8. */
  9. class Bengine_Game_Menu implements IteratorAggregate
  10. {
  11. /**
  12. * Holds the menu items.
  13. *
  14. * @var array
  15. */
  16. protected $menu = array();
  17. /**
  18. * @var array
  19. */
  20. protected $data = array();
  21. /**
  22. * Constructor.
  23. *
  24. * @return \Bengine_Game_Menu
  25. */
  26. public function __construct()
  27. {
  28. $meta = Game::getMeta();
  29. $this->setData($meta["config"]["menu"]);
  30. $this->generateMenu();
  31. }
  32. /**
  33. * Generates the menu items.
  34. *
  35. * @return Bengine_Game_Menu
  36. */
  37. protected function generateMenu()
  38. {
  39. $this->menu = array();
  40. uasort($this->data, array($this, "sort"));
  41. foreach($this->data as $first)
  42. {
  43. uasort($first["items"], array($this, "sort"));
  44. $subMenu = array();
  45. foreach($first["items"] as $second)
  46. {
  47. $subMenu[] = array(
  48. "attributes" => isset($second["attributes"]) ? $this->getHtmlAttributesFromArray($second["attributes"]) : "",
  49. "link" => $this->getLink($second)
  50. );
  51. }
  52. $this->menu[] = array(
  53. "attributes" => isset($first["attributes"]) ? $this->getHtmlAttributesFromArray($first["attributes"]) : "",
  54. "link" => isset($first["label"]) ? $this->getLabel($first["label"]) : "",
  55. "children" => $subMenu
  56. );
  57. }
  58. Hook::event("GenerateMenu", array(&$this->menu, $this));
  59. return $this;
  60. }
  61. /**
  62. * @param array $x
  63. * @param array $y
  64. * @return bool
  65. */
  66. protected function sort(array $x, array $y)
  67. {
  68. if(!isset($x["sort"]))
  69. {
  70. return -1;
  71. }
  72. if(!isset($y["sort"]))
  73. {
  74. return 1;
  75. }
  76. return $x["sort"] < $y["sort"] ? -1 : 1;
  77. }
  78. /**
  79. * Generates the label of a menu item.
  80. *
  81. * @param array|string $labelConfig
  82. * @return string
  83. */
  84. protected function getLabel($labelConfig)
  85. {
  86. if(is_array($labelConfig))
  87. {
  88. if(isset($labelConfig["special"]))
  89. {
  90. $params = isset($labelConfig["special"]["params"]) ? $labelConfig["special"]["params"] : null;
  91. $label = $this->func($labelConfig["special"]["function"], $params);
  92. }
  93. else
  94. {
  95. $label = isset($labelConfig["value"]) ? $labelConfig["value"] : "";
  96. if(!empty($item["label"]["noLangVar"]))
  97. {
  98. $label = Core::getLang()->get($label);
  99. }
  100. }
  101. }
  102. else
  103. {
  104. $label = Core::getLang()->get($labelConfig);
  105. }
  106. return $label;
  107. }
  108. /**
  109. * Generates a link from XML element.
  110. *
  111. * @param array $item
  112. * @throws Recipe_Exception_Generic
  113. * @return string
  114. */
  115. protected function getLink(array $item)
  116. {
  117. $title = "";
  118. $class = "";
  119. $attachments = "";
  120. if(isset($item["link"]["attributes"]))
  121. {
  122. if(isset($item["link"]["attributes"]["title"]))
  123. {
  124. $title = $item["link"]["attributes"]["title"];
  125. unset($item["link"]["attributes"]["title"]);
  126. }
  127. if(isset($item["link"]["attributes"]["class"]))
  128. {
  129. $class = $item["link"]["attributes"]["class"];
  130. unset($item["link"]["attributes"]["class"]);
  131. }
  132. $attachments = $this->getHtmlAttributesFromArray($item["link"]["attributes"]);
  133. }
  134. $name = $this->getLabel($item["label"]);
  135. $refdir = isset($item["link"]["external"]) ? $item["link"]["external"] : false;
  136. if(!is_array($item["link"]))
  137. {
  138. throw new Recipe_Exception_Generic("Menu link must be an array.");
  139. }
  140. if(isset($item["link"]["href"]))
  141. {
  142. $link = $item["link"]["href"];
  143. }
  144. else
  145. {
  146. $package = isset($item["link"]["package"]) ? $item["link"]["package"] : "game";
  147. $controller = $item["link"]["controller"];
  148. $action = isset($item["link"]["action"]) ? "/".$item["link"]["action"] : "";
  149. $link = $package."/".SID."/".$controller.$action;
  150. }
  151. return Link::get($link, $name, $title, $class, $attachments, false, false, $refdir);
  152. }
  153. /**
  154. * Returns the menu items.
  155. *
  156. * @return array
  157. */
  158. public function getMenu()
  159. {
  160. if($this->menu === null)
  161. {
  162. $this->generateMenu();
  163. }
  164. return $this->menu;
  165. }
  166. /**
  167. * @param string $function
  168. * @param string $param
  169. *
  170. * @return string
  171. */
  172. protected function func($function, $param = null)
  173. {
  174. $function = strtolower($function);
  175. switch($function)
  176. {
  177. case "version":
  178. return "v".Game::getVersion();
  179. break;
  180. case "const":
  181. return constant($param);
  182. break;
  183. case "config":
  184. return Core::getConfig()->get($param);
  185. break;
  186. case "user":
  187. return Core::getUser()->get($param);
  188. break;
  189. case "cookie":
  190. return Core::getRequest()->getCOOKIE($param);
  191. break;
  192. case "image":
  193. return Image::getImage($param, "");
  194. break;
  195. case "callback":
  196. return call_user_func_array($param["function"], $param["arguments"]);
  197. break;
  198. }
  199. return "";
  200. }
  201. /**
  202. * Retrieves an external iterator.
  203. *
  204. * @return ArrayIterator
  205. */
  206. public function getIterator()
  207. {
  208. return new ArrayIterator($this->getMenu());
  209. }
  210. /**
  211. * @param array $data
  212. */
  213. public function setData(array $data)
  214. {
  215. $this->data = $data;
  216. }
  217. /**
  218. * @return array
  219. */
  220. public function getData()
  221. {
  222. return $this->data;
  223. }
  224. /**
  225. * @param array $assoc
  226. * @return string
  227. */
  228. protected function getHtmlAttributesFromArray(array $assoc)
  229. {
  230. $attributes = array();
  231. foreach($assoc as $attribute => $value)
  232. {
  233. $attributes[] = "$attribute=\"$value\"";
  234. }
  235. return " ".implode(" ", $attributes);
  236. }
  237. }
  238. ?>