/library/Fisma/Yui/Menu.php

https://bitbucket.org/khuongduybui/openfisma · PHP · 142 lines · 42 code · 12 blank · 88 comment · 3 complexity · 4423cb6aec7c6d6fa16ed7eabcc8c77a MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright (c) 2008 Endeavor Systems, Inc.
  4. *
  5. * This file is part of OpenFISMA.
  6. *
  7. * OpenFISMA is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
  9. * version.
  10. *
  11. * OpenFISMA is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  12. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with OpenFISMA. If not, see
  16. * {@link http://www.gnu.org/licenses/}.
  17. */
  18. /**
  19. * An object which represents a menu in YUI
  20. *
  21. * This object is designed to be easily converted to JSON, so it's JSON members are exposed in PHP as public members.
  22. *
  23. * This design constraint makes this class a little difficult to understand, so refer to the unit tests if you are
  24. * trying to figure out how this works. Also see the phpdoc on $submenu.
  25. *
  26. * @author Mark E. Haase <mhaase@endeavorsystems.com>
  27. * @copyright (c) Endeavor Systems, Inc. 2009 {@link http://www.endeavorsystems.com}
  28. * @license http://www.openfisma.org/content/license GPLv3
  29. * @package Fisma
  30. * @subpackage Fisma_Yui
  31. */
  32. class Fisma_Yui_Menu
  33. {
  34. /**
  35. * Holds menu title
  36. *
  37. * @var string
  38. */
  39. public $text;
  40. /**
  41. * Stores submenu id and item data
  42. *
  43. * Example:
  44. *
  45. * 'id' => 'uniqueMenuIdGoesHere'
  46. * 'itemdata' => array(
  47. * 0 => array(
  48. * new MenuItem('Item 1'),
  49. * new MenuItem('Item 2')
  50. * ),
  51. * 1 => array(
  52. * new MenuItem('Item 3')
  53. * )
  54. * )
  55. *
  56. * The example above would draw the following menus:
  57. *
  58. * ------
  59. * Item 1
  60. * Item 2
  61. * ------
  62. * Item 3
  63. * ------
  64. *
  65. * @var array
  66. */
  67. public $submenu = array();
  68. public $pull;
  69. /**
  70. * To perform initialization as a default constructor
  71. *
  72. * @param string $title The specified menu title
  73. * @return void
  74. */
  75. function __construct($title, $pull = null)
  76. {
  77. $this->text = $title;
  78. $this->pull = $pull;
  79. $this->submenu['id'] = uniqid();
  80. // itemdata is an array of arrays. Create one empty inner array to begin with.
  81. $this->submenu['itemdata'] = array();
  82. $this->submenu['itemdata'][0] = array();
  83. }
  84. /**
  85. * Append a specified submenu item to the submenu data array of the menu
  86. *
  87. * @param Fisma_Yui_MenuItem $item The specified submenu item
  88. * @return void
  89. * @todo is it necessary to support Fisma_Yui_Menu based cascading menu in the future?
  90. */
  91. public function add($item)
  92. {
  93. $currentMenu = count($this->submenu['itemdata']) - 1;
  94. $this->submenu['itemdata'][$currentMenu][] = $item;
  95. }
  96. /**
  97. * Add a menu separator immediately after the menu item which was most recently added
  98. *
  99. */
  100. public function addSeparator()
  101. {
  102. $this->submenu['itemdata'][] = array();
  103. }
  104. /**
  105. * Remove empty groups (superfluous separators)
  106. */
  107. public function removeEmptyGroups()
  108. {
  109. foreach ($this->submenu['itemdata'] as $key => $group) {
  110. if (count($group) == 0) {
  111. unset($this->submenu['itemdata'][$key]);
  112. }
  113. }
  114. $this->submenu['itemdata'] = array_values($this->submenu['itemdata']);
  115. }
  116. /**
  117. * Check whether the menu is empty or not
  118. *
  119. * @return bool
  120. */
  121. public function isEmpty()
  122. {
  123. foreach ($this->submenu['itemdata'] as $group) {
  124. if (count($group) > 0) {
  125. return false;
  126. }
  127. }
  128. return true; // didn't find any items
  129. }
  130. }