menunode.class.inc.php 8.8 KB

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