menunode.class.inc.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. require_once('../core/attributedef.class.inc.php');
  3. require_once('../core/filterdef.class.inc.php');
  4. require_once('../core/stimulus.class.inc.php');
  5. require_once('../core/MyHelpers.class.inc.php');
  6. require_once('../core/cmdbsource.class.inc.php');
  7. require_once('../core/sqlquery.class.inc.php');
  8. require_once('../core/dbobject.class.php');
  9. require_once('../core/dbobjectsearch.class.php');
  10. require_once('../core/dbobjectset.class.php');
  11. require_once('../application/displayblock.class.inc.php');
  12. /**
  13. * This class manages en entries in the menu tree on the left of the iTop pages
  14. */
  15. class menuNode extends DBObject
  16. {
  17. public static function Init()
  18. {
  19. $aParams = array
  20. (
  21. "category" => "gui",
  22. "name" => "menuNode",
  23. "description" => "Main menu configuration elements",
  24. "key_type" => "autoincrement",
  25. "key_label" => "",
  26. "name_attcode" => "name",
  27. "state_attcode" => "",
  28. "reconc_keys" => array(),
  29. "db_table" => "priv_menunode",
  30. "db_key_field" => "id",
  31. "db_finalclass_field" => "",
  32. );
  33. MetaModel::Init_Params($aParams);
  34. // MetaModel::Init_AddAttribute(new AttributeExternalKey("change", array("label"=>"change", "description"=>"change", "allowed_values"=>null, "sql"=>"changeid", "targetclass"=>"CMDBChange", "jointype"=>"closed")));
  35. // MetaModel::Init_AddAttribute(new AttributeExternalField("date", array("label"=>"date", "description"=>"date and time of the change", "allowed_values"=>null, "extkey_attcode"=>"change", "target_attcode"=>"date")));
  36. MetaModel::Init_AddAttribute(new AttributeString("name", array("label"=>"Menu Name", "description"=>"Short name for this menu", "allowed_values"=>null, "sql"=>"name", "default_value"=>"", "is_null_allowed"=>false, "on_target_delete"=>DEL_MANUAL, "depends_on"=>array())));
  37. MetaModel::Init_AddAttribute(new AttributeString("label", array("label"=>"Menu Description", "description"=>"Long description for this menu", "allowed_values"=>null, "sql"=>"label", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  38. MetaModel::Init_AddAttribute(new AttributeString("hyperlink", array("label"=>"Hyperlink", "description"=>"Hyperlink to the page", "allowed_values"=>null, "sql"=>"hyperlink", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  39. MetaModel::Init_AddAttribute(new AttributeString("icon_path", array("label"=>"Menu Icon", "description"=>"Path to the icon o the menu", "allowed_values"=>null, "sql"=>"icon_path", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  40. MetaModel::Init_AddAttribute(new AttributeText("template", array("label"=>"Template", "description"=>"HTML template for the view", "allowed_values"=>null, "sql"=>"template", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  41. MetaModel::Init_AddAttribute(new AttributeEnum("type", array("label"=>"Type", "description"=>"Type of menu", "allowed_values"=>new ValueSetEnum('application,user,administrator'), "sql"=>"type", "default_value"=>"application", "is_null_allowed"=>false, "depends_on"=>array())));
  42. MetaModel::Init_AddAttribute(new AttributeInteger("rank", array("label"=>"Display rank", "description"=>"Sort order for displaying the menu", "allowed_values"=>null, "sql"=>"rank", "default_value" => 999, "is_null_allowed"=>false, "depends_on"=>array())));
  43. MetaModel::Init_AddAttribute(new AttributeExternalKey("parent_id", array("label"=>"Parent Menu Item", "description"=>"Parent Menu Item", "allowed_values"=>null, "sql"=>"parent_id", "targetclass"=>"menuNode", "is_null_allowed"=>true, "on_target_delete"=>DEL_MANUAL, "depends_on"=>array())));
  44. MetaModel::Init_AddAttribute(new AttributeExternalField("parent_name", array("label"=>"Parent Menu Item", "description"=>"Parent Menu Item", "allowed_values"=>null, "extkey_attcode"=>"parent_id", "target_attcode"=>"name")));
  45. MetaModel::Init_AddAttribute(new AttributeInteger("user_id", array("label"=>"Owner of the menu", "description"=>"User who owns this menu (for user defined menus)", "allowed_values"=>null, "sql"=>"user_id", "default_value" => 0, "is_null_allowed"=>false, "depends_on"=>array())));
  46. MetaModel::Init_AddFilterFromAttribute("label");
  47. MetaModel::Init_AddFilterFromAttribute("parent_id");
  48. MetaModel::Init_AddFilterFromAttribute("rank");
  49. MetaModel::Init_AddFilterFromAttribute("type");
  50. MetaModel::Init_AddFilterFromAttribute("user_id");
  51. MetaModel::Init_SetZListItems('details', array('parent_id', 'name', 'label', 'hyperlink', 'template', 'rank', 'type')); // Attributes to be displayed for the complete details
  52. MetaModel::Init_SetZListItems('list', array('parent_id', 'name', 'label', 'rank', 'type')); // Attributes to be displayed for a list
  53. }
  54. public function IsVisible()
  55. {
  56. return true;
  57. }
  58. public function GetMenuName()
  59. {
  60. return $this->Get('name');
  61. }
  62. public function GetMenuIcon()
  63. {
  64. return $this->Get('icon_path');
  65. }
  66. public function GetMenuLabel()
  67. {
  68. return $this->Get('label');
  69. }
  70. public function GetMenuLink($aExtraParams)
  71. {
  72. $aExtraParams['menu'] = $this->GetKey(); // Make sure we overwrite the current menu id (if any)
  73. $aParams = array();
  74. foreach($aExtraParams as $sName => $sValue)
  75. {
  76. $aParams[] = urlencode($sName)."=".urlencode($sValue);
  77. }
  78. return $this->Get('hyperlink')."?".implode("&", $aParams);
  79. }
  80. public function GetChildNodesSet($sType = null)
  81. {
  82. $aParams = array();
  83. if ($sType == 'user')
  84. {
  85. $sSelectChilds = 'SELECT menuNode AS m WHERE m.parent_id = :parent AND type = :type AND m.user_id = :user';
  86. $aParams = array('parent' => $this->GetKey(), 'type' => $sType, 'user' => UserRights::GetUserId());
  87. }
  88. elseif ($sType != null)
  89. {
  90. $sSelectChilds = 'SELECT menuNode AS m WHERE m.parent_id = :parent AND type = :type';
  91. $aParams = array('parent' => $this->GetKey(), 'type' => $sType);
  92. }
  93. else
  94. {
  95. $sSelectChilds = 'SELECT menuNode AS m WHERE m.parent_id = :parent';
  96. $aParams = array('parent' => $this->GetKey());
  97. }
  98. $oSearchFilter = DBObjectSearch::FromOQL($sSelectChilds);
  99. $oSet = new CMDBObjectSet($oSearchFilter, array('rank' => true), $aParams);
  100. return $oSet;
  101. }
  102. public function RenderContent(web_page $oPage, $aExtraParams = array())
  103. {
  104. $sTemplate = $this->Get('template');
  105. $oTemplate = new DisplayTemplate($sTemplate);
  106. $oTemplate->Render($oPage, $aExtraParams);
  107. //$this->ProcessTemplate($sTemplate, $oPage, $aExtraParams);
  108. }
  109. public function DisplayMenu(iTopWebPage $oP, $sType, $aExtraParams)
  110. {
  111. $oP->AddToMenu("<li><a href=\"".$this->GetMenuLink($aExtraParams)."\" title=\"".$this->GetMenuLabel()."\">".$this->GetMenuName()."</a>");
  112. $oSet = $this->GetChildNodesSet($sType);
  113. if ($oSet->Count() > 0)
  114. {
  115. $oP->AddToMenu("\n<ul>\n");
  116. while($oChildNode = $oSet->Fetch())
  117. {
  118. $oChildNode->DisplayMenu($oP, $sType, $aExtraParams);
  119. }
  120. $oP->AddToMenu("</ul>\n");
  121. }
  122. $oP->AddToMenu("</li>\n");
  123. }
  124. static public function DisplayCreationForm(web_page $oP, $sClass, $sFilter, $aExtraParams = array())
  125. {
  126. $oFilter = DBObjectSearch::unserialize($sFilter);
  127. $oP->p('Create a new menu item for: '.$oFilter->__DescribeHTML());
  128. $oP->add('<form action="UniversalSearch.php" method="post">');
  129. $oP->add('<input type="hidden" name="operation" value="add_menu">');
  130. $oP->add('<input type="hidden" name="filter" value="'.$sFilter.'">');
  131. $oP->add('<input type="hidden" name="class" value="'.$sClass.'">');
  132. $oP->p('Menu Label: <input type="text" name="label" size="30">');
  133. $oP->p('Description: <input type="text" name="description" size="30">');
  134. $oP->add('<p>Insert after: <select name="previous_node_id">');
  135. $aNodes = self::GetMenuAsArray(null, 'user');
  136. foreach($aNodes as $aNodeDesc)
  137. {
  138. $oP->add('<option value="'.$aNodeDesc['id'].'">'.str_repeat('&nbsp;&nbsp;&nbsp;', $aNodeDesc['depth']).$aNodeDesc['label'].'</option>');
  139. }
  140. $oP->add('</select></p>');
  141. $oP->p('<input type="checkbox" name="child_item" value="1"> Create as a child menu item');
  142. $oP->p('<input type="submit" value=" Ok "> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" class="jqmClose" value="Cancel">');
  143. $oP->add('</form>');
  144. }
  145. static public function GetMenuAsArray($oRootNode = null, $sType = 'application', $iDepth = 0)
  146. {
  147. $aNodes = array();
  148. if (is_object($oRootNode))
  149. {
  150. $oChildSet = $oRootNode->GetChildNodesSet($sType);
  151. while($oNode = $oChildSet->Fetch())
  152. {
  153. $aNodes[] = array('depth' => $iDepth, 'id' => $oNode->GetKey(), 'label' => $oNode->GetName());
  154. $aNodes = array_merge($aNodes, self::GetMenuAsArray($oNode, $sType, $iDepth+1));
  155. }
  156. }
  157. else
  158. {
  159. $oSearchFilter = new DbObjectSearch("menuNode");
  160. $oSearchFilter->AddCondition('parent_id', 0, '=');
  161. if ($sType != null)
  162. {
  163. $oSearchFilter->AddCondition('type', $sType, '=');
  164. if ($sType == 'user')
  165. {
  166. $oSearchFilter->AddCondition('user_id', UserRights::GetUserId(), '=');
  167. }
  168. }
  169. $oRootSet = new CMDBObjectSet($oSearchFilter, array('rank' => true));
  170. while($oNode = $oRootSet->Fetch())
  171. {
  172. $aNodes[] = array('depth' => $iDepth, 'id' => $oNode->GetKey(), 'label' => $oNode->GetName());
  173. $aNodes = array_merge($aNodes, self::GetMenuAsArray($oNode, $sType, $iDepth+1));
  174. }
  175. }
  176. return $aNodes;
  177. }
  178. /**
  179. * Returns a set of all the nodes following the current node in the tree
  180. * (i.e. nodes with the same parent but with a greater rank)
  181. */
  182. public function GetNextNodesSet($sType = 'application')
  183. {
  184. $oSearchFilter = new DBObjectSearch("menuNode");
  185. $oSearchFilter->AddCondition('parent_id', $this->Get('parent_id'));
  186. $oSearchFilter->AddCondition('rank', $this->Get('rank'), '>');
  187. if ($sType != null)
  188. {
  189. $oSearchFilter->AddCondition('type', $sType, '=');
  190. if ($sType == 'user')
  191. {
  192. $oSearchFilter->AddCondition('user_id', UserRights::GetUserId(), '=');
  193. }
  194. }
  195. $oSet = new DBObjectSet($oSearchFilter, array('rank'=> true)); // Order by rank (true means ascending)
  196. return $oSet;
  197. }
  198. }
  199. ?>