menunode.class.inc.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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, "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'), "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, "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 AttributeExternalKey("user_id", array("label"=>"Owner of the menu", "description"=>"User who owns this menu (for user defined menus)", "allowed_values"=>null, "sql"=>"user_id", "targetclass"=>"UserRightsMatrixUsers", "is_null_allowed"=>true, "depends_on"=>array('type'))));
  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)
  81. {
  82. $oSearchFilter = new DBObjectSearch("menuNode");
  83. $oSearchFilter->AddCondition('parent_id', $this->GetKey(), '=');
  84. $oSearchFilter->AddCondition('type', $sType, '=');
  85. if ($sType == 'user')
  86. {
  87. $oSearchFilter->AddCondition('user_id', UserRights::GetUserId(), '=');
  88. }
  89. $oSet = new CMDBObjectSet($oSearchFilter, array('rank' => true));
  90. return $oSet;
  91. }
  92. public function RenderContent(web_page $oPage, $aExtraParams = array())
  93. {
  94. $sTemplate = $this->Get('template');
  95. $this->ProcessTemplate($sTemplate, $oPage, $aExtraParams);
  96. }
  97. protected function ProcessTemplate($sTemplate, web_page $oPage, $aExtraParams = array())
  98. {
  99. $iStartPos = stripos($sTemplate, '<'.DisplayBlock::TAG_BLOCK.' ',0);
  100. $index = 0;
  101. while(($iStartPos = stripos($sTemplate, '<'.DisplayBlock::TAG_BLOCK.' ',0)) !== false)
  102. {
  103. $iEndPos = stripos($sTemplate, '</'.DisplayBlock::TAG_BLOCK.'>', $iStartPos);
  104. $sBlockDefinition = substr($sTemplate, $iStartPos, $iEndPos - $iStartPos + strlen('</'.DisplayBlock::TAG_BLOCK.'>'));
  105. $oBlock = DisplayBlock::FromTemplate($sBlockDefinition);
  106. $oPage->add(substr($sTemplate, 0, $iStartPos));
  107. if ($oBlock) // Protects agains invalid XML templates
  108. {
  109. $oBlock->Display($oPage, "block{$index}", $aExtraParams); // Values from $aExtraParams have precedence over $aParams
  110. }
  111. $index++;
  112. $sTemplate = substr($sTemplate, $iEndPos + strlen('</'.DisplayBlock::TAG_BLOCK.'>'));
  113. }
  114. // What remains is purely static (without any block inside), just output as it is
  115. $oPage->add($sTemplate);
  116. }
  117. public function DisplayMenu(iTopWebPage $oP, $sType, $aExtraParams)
  118. {
  119. $oP->AddToMenu("<li><a href=\"".$this->GetMenuLink($aExtraParams)."\" title=\"".$this->GetMenuLabel()."\">".$this->GetMenuName()."</a>");
  120. $oSet = $this->GetChildNodesSet($sType);
  121. if ($oSet->Count() > 0)
  122. {
  123. $oP->AddToMenu("\n<ul>\n");
  124. while($oChildNode = $oSet->Fetch())
  125. {
  126. $oChildNode->DisplayMenu($oP, $sType, $aExtraParams);
  127. }
  128. $oP->AddToMenu("</ul>\n");
  129. }
  130. $oP->AddToMenu("</li>\n");
  131. }
  132. static public function DisplayCreationForm(web_page $oP, $sClass, $sFilter, $aExtraParams = array())
  133. {
  134. $oFilter = DBObjectSearch::unserialize($sFilter);
  135. $oP->p('Create a new menu item for: '.$oFilter->__DescribeHTML());
  136. $oP->add('<form action="UniversalSearch.php" method="post">');
  137. $oP->add('<input type="hidden" name="operation" value="add_menu">');
  138. $oP->add('<input type="hidden" name="filter" value="'.$sFilter.'">');
  139. $oP->add('<input type="hidden" name="class" value="'.$sClass.'">');
  140. $oP->p('Menu Label: <input type="text" name="label" size="30">');
  141. $oP->p('Description: <input type="text" name="description" size="30">');
  142. $oP->add('<p>Insert after: <select name="previous_node_id">');
  143. $aNodes = self::GetMenuAsArray(null, 'user');
  144. foreach($aNodes as $aNodeDesc)
  145. {
  146. $oP->add('<option value="'.$aNodeDesc['id'].'">'.str_repeat('&nbsp;&nbsp;&nbsp;', $aNodeDesc['depth']).$aNodeDesc['label'].'</option>');
  147. }
  148. $oP->add('</select></p>');
  149. $oP->p('<input type="checkbox" name="child_item" value="1"> Create as a child menu item');
  150. $oP->p('<input type="submit" value=" Ok "> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" class="jqmClose" value="Cancel">');
  151. $oP->add('</form>');
  152. }
  153. static public function GetMenuAsArray($oRootNode = null, $sType = 'application', $iDepth = 0)
  154. {
  155. $aNodes = array();
  156. if (is_object($oRootNode))
  157. {
  158. $oChildSet = $oRootNode->GetChildNodesSet($sType);
  159. while($oNode = $oChildSet->Fetch())
  160. {
  161. $aNodes[] = array('depth' => $iDepth, 'id' => $oNode->GetKey(), 'label' => $oNode->GetName());
  162. $aNodes = array_merge($aNodes, self::GetMenuAsArray($oNode, $sType, $iDepth+1));
  163. }
  164. }
  165. else
  166. {
  167. $oSearchFilter = new DbObjectSearch("menuNode");
  168. $oSearchFilter->AddCondition('parent_id', 0, '=');
  169. $oSearchFilter->AddCondition('type', $sType, '=');
  170. if ($sType == 'user')
  171. {
  172. $oSearchFilter->AddCondition('user_id', UserRights::GetUserId(), '=');
  173. }
  174. $oRootSet = new CMDBObjectSet($oSearchFilter, array('rank' => true));
  175. while($oNode = $oRootSet->Fetch())
  176. {
  177. $aNodes[] = array('depth' => $iDepth, 'id' => $oNode->GetKey(), 'label' => $oNode->GetName());
  178. $aNodes = array_merge($aNodes, self::GetMenuAsArray($oNode, $sType, $iDepth+1));
  179. }
  180. }
  181. return $aNodes;
  182. }
  183. /**
  184. * Returns a set of all the nodes following the current node in the tree
  185. * (i.e. nodes with the same parent but with a greater rank)
  186. */
  187. public function GetNextNodesSet($sType = 'application')
  188. {
  189. $oSearchFilter = new DBObjectSearch("menuNode");
  190. $oSearchFilter->AddCondition('parent_id', $this->Get('parent_id'));
  191. $oSearchFilter->AddCondition('rank', $this->Get('rank'), '>');
  192. $oSearchFilter->AddCondition('type', $sType, '=');
  193. if ($sType == 'user')
  194. {
  195. $oSearchFilter->AddCondition('user_id', UserRights::GetUserId(), '=');
  196. }
  197. $oSet = new DBObjectSet($oSearchFilter, array('rank'=> true)); // Order by rank (true means ascending)
  198. return $oSet;
  199. }
  200. }
  201. ?>