/libraries/piwi/Widget/Container/MenuBar.php

https://github.com/jaws-project/jaws · PHP · 322 lines · 225 code · 35 blank · 62 comment · 49 complexity · 31e6fd3a3e5eadb7180c35c5f47c3620 MD5 · raw file

  1. <?php
  2. /**
  3. * MenuBar.php - MenuBar Class
  4. * @version $Id $
  5. * @author Pablo Fischer <pablo@pablo.com.mx>
  6. *
  7. * <c> Jonathan Hernandez 2004
  8. * <c> Pablo Fischer 2004
  9. * <c> Piwi
  10. */
  11. require_once PIWI_PATH . '/Widget/Container/Container.php';
  12. require_once PIWI_PATH . '/Widget/Container/MenuItem.php';
  13. define('MENUBAR_REQ_PARAMS', 0);
  14. class MenuBar extends Container
  15. {
  16. /**
  17. * Public constructor
  18. *
  19. * @param string $id The ID of the div that will have the menubar.
  20. * if you are using another menubar, you should give it another ID and
  21. * another CSS sheet!
  22. * @access public
  23. */
  24. function __construct($id = 'menubar')
  25. {
  26. $this->_id = $id;
  27. $this->_paths = array();
  28. $this->_class = 'ddmx';
  29. if (empty($this->_id)) {
  30. die("Menubar should have an id!");
  31. }
  32. parent::init();
  33. }
  34. /**
  35. * Add a MenuItem
  36. *
  37. * @param string $path Path of the menuitem
  38. * @param string $url What to do?
  39. * @param string $icon The icon
  40. * @access public
  41. */
  42. function add($path, $url = '', $icon = '')
  43. {
  44. array_push($this->_paths, $path);
  45. $explodedPath = explode('/',$path);
  46. $numElements = count($explodedPath);
  47. $i = 0;
  48. foreach ($explodedPath as $item) {
  49. if ($numElements == 1) {
  50. if (!isset($this->_items[$item])) {
  51. $this->_items[$item] = new MenuItem($item, $url, $icon);
  52. }
  53. }
  54. if ($i == $numElements-1) {
  55. if (!empty($explodedPath[$i-1])) {
  56. $this->addTo($explodedPath[$i-1], $this->_items, $path, $item, $url, $icon);
  57. }
  58. }
  59. $i++;
  60. }
  61. }
  62. /**
  63. * Add a menu item to a certain menupath
  64. *
  65. * @param string $findme Text to find
  66. * @param array $items Items to read
  67. * @param string $title Text of the item
  68. * @param string $url URL of the item
  69. * @param string $icon Icon of the item
  70. * @access public
  71. */
  72. function addTo($findme, $items, $path, $title, $url, $icon)
  73. {
  74. $i = 0;
  75. $founded = false;
  76. foreach ($items as $key => $item) {
  77. if ($item->getValue() == $findme) {
  78. $items[$key]->add($title, $url, $icon);
  79. $founded = true;
  80. break;
  81. }
  82. $i++;
  83. }
  84. if (!$founded) {
  85. foreach ($items as $item) {
  86. //sometimes the computers are SOOO stupid that they confuse an array with a scalar :-P
  87. if (is_array($path)) {
  88. $path = implode('/', $path);
  89. }
  90. $path = explode('/', $path);
  91. //Are we in this path?...
  92. if ($path[0] == $item->getValue()) {
  93. if (count($item->getItems()) > 0) {
  94. unset($path[0]);
  95. $path = implode('/', $path);
  96. if ($this->addTo($findme, $item->getItems(), $path, $title, $url, $icon)) {
  97. $founded = true;
  98. break;
  99. } else {
  100. $founded = false;
  101. }
  102. }
  103. } else {
  104. $founded = false;
  105. }
  106. }
  107. }
  108. return $founded;
  109. }
  110. /**
  111. * Read the childs menuitems of a menuitem
  112. *
  113. * @param array $items Items to read
  114. * @access private
  115. */
  116. function getChildItems ($childs, $spacing = ' ')
  117. {
  118. $sspacing = $spacing;
  119. $dspacing = $spacing."".$spacing;
  120. $xhtml = "\n".$sspacing."<div class=\"section\">\n";
  121. foreach ($childs as $o) {
  122. $action = $o->getAction();
  123. $text = $o->getValue();
  124. $icon = $o->getIcon();
  125. if ($text != '-') {
  126. $items = $o->getItems();
  127. if (count($items) > 0) {
  128. if (!empty($action)) {
  129. $xhtml.= $sspacing."<a class=\"item2 arrow\" href=\"".$action."\">";
  130. } else {
  131. $xhtml.= $sspacing."<a class=\"item2 arrow\" href=\"javascript:void(0);\">";
  132. }
  133. if (!empty ($icon)) {
  134. $xhtml.= "<img src=\"".$icon."\" alt=\"".$text."\" />&nbsp;";
  135. }
  136. $xhtml.= $text."</a>";
  137. if (count($items) > 0) {
  138. $xhtml.= $this->getChildItems($items, $dspacing);
  139. } else {
  140. $xhtml.= "\n";
  141. }
  142. } else {
  143. if (!empty($action)) {
  144. $xhtml.= $sspacing."<a class=\"item2\" href=\"".$action."\">";
  145. } else {
  146. $xhtml.= $sspacing."<a class=\"item2\" href=\"javascript:void(0);\">";
  147. }
  148. if (!empty($icon)) {
  149. $xhtml.= "<img src=\"".$icon."\" alt=\"".$text."\" />&nbsp;";
  150. }
  151. $xhtml.= $text."</a>";
  152. if (count($items) > 0) {
  153. $xhtml.= $this->getChildItems($items, $dspacing);
  154. } else {
  155. $xhtml.= "\n";
  156. }
  157. }
  158. }
  159. }
  160. $xhtml.= $spacing."</div>\n";
  161. return $xhtml;
  162. }
  163. /**
  164. * Get piwiXML Items
  165. *
  166. * @param array $childs Childs to read and add
  167. * @param boolean $for_top_level The child items are for a top_level menu? *
  168. * @access private
  169. */
  170. function getXmlChildItems($childs, $for_top_level = false)
  171. {
  172. if (!$for_top_level) {
  173. $this->_PiwiXML->openElement('menu');
  174. }
  175. foreach ($childs as $o) {
  176. $action = $o->getAction();
  177. $text = $o->getValue();
  178. $icon = $o->getIcon();
  179. if ($text != '-') {
  180. $items = $o->getItems();
  181. if (count($items) > 0) {
  182. $this->_PiwiXML->openElement('menu');
  183. $this->_PiwiXML->addAttribute('label', $text);
  184. if (!empty($action)) {
  185. $this->_PiwiXML->addAttribute('action', $action);
  186. }
  187. if (!empty($icon)) {
  188. $this->_PiwiXML->addAttribute('icon', $icon);
  189. }
  190. $this->getXmlChildItems($items, true);
  191. $this->_PiwiXML->closeElement('menu');
  192. } else {
  193. $this->_PiwiXML->openElement('menuitem', true);
  194. $this->_PiwiXML->addAttribute('label', $text);
  195. if (!empty($action)) {
  196. $this->_PiwiXML->addAttribute('action', $action);
  197. }
  198. if (!empty($icon)) {
  199. $this->_PiwiXML->addAttribute('icon', $icon);
  200. }
  201. $this->_PiwiXML->closeElement('menuitem');
  202. }
  203. } else {
  204. $this->_PiwiXML->openElement('menuseparator', true);
  205. $this->_PiwiXML->closeElement('menuseparator');
  206. }
  207. }
  208. if (!$for_top_level) {
  209. $this->_PiwiXML->closeElement('menu');
  210. }
  211. }
  212. /**
  213. * Build the piwiXML data.
  214. *
  215. * Based in XUL menus
  216. *
  217. * @access public
  218. */
  219. function buildPiwiXML ()
  220. {
  221. $this->buildBasicPiwiXML();
  222. $this->_PiwiXML->addAttribute('name', $this->_id);
  223. foreach ($this->_items as $o) {
  224. $action = $o->getAction();
  225. $text = $o->getValue();
  226. $icon = $o->getIcon();
  227. $this->_PiwiXML->openElement('menu');
  228. if (!empty($action)) {
  229. $this->_PiwiXML->addAttribute('action', $action);
  230. }
  231. if (!empty($icon)) {
  232. $this->_PiwiXML->addAttribute('icon', $icon);
  233. }
  234. $this->_PiwiXML->addAttribute('label', $text);
  235. $items = $o->getItems();
  236. if (count($items) > 0) {
  237. $this->getXmlChildItems($items, true);
  238. }
  239. $this->_PiwiXML->closeElement('menu');
  240. }
  241. $this->_PiwiXML->closeElement($this->getClassName());
  242. }
  243. /**
  244. * Build the XHTML and JS Menu
  245. *
  246. * @access public
  247. */
  248. function buildXHTML()
  249. {
  250. $this->addFile(PIWI_URL . 'piwidata/js/mygosumenu/ie5.js');
  251. $this->addFile(PIWI_URL . 'piwidata/js/mygosumenu/1.1/DropDownMenuX.js');
  252. $this->_XHTML = "<table id=\"".$this->_id."\" class=\"".$this->_class."\">\n";
  253. $this->_XHTML.= "<tr>\n";
  254. $spacing = ' ';
  255. foreach ($this->_items as $o) {
  256. $this->_XHTML.= " <td>\n";
  257. $action = $o->getAction();
  258. $text = $o->getValue();
  259. $icon = $o->getIcon();
  260. if (!empty($action)) {
  261. $this->_XHTML.= $spacing."<a class=\"item1\" href=\"".$action."\">";
  262. } else {
  263. $this->_XHTML.= $spacing."<a href=\"javascript:void(0);\" class=\"item1\">";
  264. }
  265. if (!empty($icon)) {
  266. $this->_XHTML.= "<img src=\"".$icon."\" alt=\"".$text."\" />&nbsp;";
  267. }
  268. $this->_XHTML.= $text;
  269. $this->_XHTML.= "</a>";
  270. $items = $o->getItems();
  271. if (count($items) > 0) {
  272. $this->_XHTML.= $this->getChildItems($items, $spacing);
  273. }
  274. $this->_XHTML.= " </td>\n";
  275. }
  276. $this->_XHTML.= "</tr>\n";
  277. $this->_XHTML.= "</table>\n";
  278. $this->_XHTML.= "<script type=\"text/javascript\">\n";
  279. $this->_XHTML.= "var ".$this->_id."_menu = new DropDownMenuX('".$this->_id."');\n";
  280. $this->_XHTML.= $this->_id."_menu.delay.show = 0;\n";
  281. $this->_XHTML.= $this->_id."_menu.delay.hide = 400;\n";
  282. $this->_XHTML.= $this->_id."_menu.position.levelX.left = 2;\n";
  283. $this->_XHTML.= $this->_id."_menu.init();\n";
  284. $this->_XHTML.= "</script>\n";
  285. }
  286. }
  287. ?>