menunode.class.inc.php 11 KB

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