menunode.class.inc.php 9.0 KB

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