menunode.class.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Persistent class menuNode
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once('../core/attributedef.class.inc.php');
  25. require_once('../core/filterdef.class.inc.php');
  26. require_once('../core/stimulus.class.inc.php');
  27. require_once('../core/MyHelpers.class.inc.php');
  28. require_once('../core/cmdbsource.class.inc.php');
  29. require_once('../core/sqlquery.class.inc.php');
  30. require_once('../core/dbobject.class.php');
  31. require_once('../core/dbobjectsearch.class.php');
  32. require_once('../core/dbobjectset.class.php');
  33. require_once('../application/displayblock.class.inc.php');
  34. /**
  35. * This class manages en entries in the menu tree on the left of the iTop pages
  36. */
  37. class menuNode extends DBObject
  38. {
  39. public static function Init()
  40. {
  41. $aParams = array
  42. (
  43. "category" => "gui",
  44. "key_type" => "autoincrement",
  45. "name_attcode" => "name",
  46. "state_attcode" => "",
  47. "reconc_keys" => array(),
  48. "db_table" => "priv_menunode",
  49. "db_key_field" => "id",
  50. "db_finalclass_field" => "",
  51. );
  52. MetaModel::Init_Params($aParams);
  53. // MetaModel::Init_AddAttribute(new AttributeExternalKey("change", array("allowed_values"=>null, "sql"=>"changeid", "targetclass"=>"CMDBChange", "jointype"=>"closed")));
  54. // MetaModel::Init_AddAttribute(new AttributeExternalField("date", array("allowed_values"=>null, "extkey_attcode"=>"change", "target_attcode"=>"date")));
  55. 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())));
  56. MetaModel::Init_AddAttribute(new AttributeString("label", array("allowed_values"=>null, "sql"=>"label", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  57. MetaModel::Init_AddAttribute(new AttributeString("hyperlink", array("allowed_values"=>null, "sql"=>"hyperlink", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  58. MetaModel::Init_AddAttribute(new AttributeString("icon_path", array("allowed_values"=>null, "sql"=>"icon_path", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  59. MetaModel::Init_AddAttribute(new AttributeText("template", array("allowed_values"=>null, "sql"=>"template", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  60. 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())));
  61. MetaModel::Init_AddAttribute(new AttributeInteger("rank", array("allowed_values"=>null, "sql"=>"rank", "default_value" => 999, "is_null_allowed"=>false, "depends_on"=>array())));
  62. 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())));
  63. MetaModel::Init_AddAttribute(new AttributeExternalField("parent_name", array("allowed_values"=>null, "extkey_attcode"=>"parent_id", "target_attcode"=>"name")));
  64. MetaModel::Init_AddAttribute(new AttributeInteger("user_id", array("allowed_values"=>null, "sql"=>"user_id", "default_value" => 0, "is_null_allowed"=>false, "depends_on"=>array())));
  65. MetaModel::Init_SetZListItems('details', array('parent_id', 'name', 'label', 'hyperlink', 'template', 'rank', 'type')); // Attributes to be displayed for the complete details
  66. MetaModel::Init_SetZListItems('list', array('parent_id', 'label', 'rank', 'type')); // Attributes to be displayed for a list
  67. }
  68. public function IsVisible()
  69. {
  70. return true;
  71. }
  72. public function GetMenuName()
  73. {
  74. return Dict::S($this->Get('name'));
  75. }
  76. public function GetMenuIcon()
  77. {
  78. return $this->Get('icon_path');
  79. }
  80. public function GetMenuLabel()
  81. {
  82. return Dict::S($this->Get('label'));
  83. }
  84. public function GetMenuLink($aExtraParams)
  85. {
  86. $aExtraParams['menu'] = $this->GetKey(); // Make sure we overwrite the current menu id (if any)
  87. $aParams = array();
  88. foreach($aExtraParams as $sName => $sValue)
  89. {
  90. $aParams[] = urlencode($sName)."=".urlencode($sValue);
  91. }
  92. return $this->Get('hyperlink')."?".implode("&", $aParams);
  93. }
  94. public function GetChildNodesSet($sType = null)
  95. {
  96. $aParams = array();
  97. if ($sType == 'user')
  98. {
  99. $sSelectChilds = 'SELECT menuNode AS m WHERE m.parent_id = :parent AND type = :type AND m.user_id = :user';
  100. $aParams = array('parent' => $this->GetKey(), 'type' => $sType, 'user' => UserRights::GetUserId());
  101. }
  102. elseif ($sType != null)
  103. {
  104. $sSelectChilds = 'SELECT menuNode AS m WHERE m.parent_id = :parent AND type = :type';
  105. $aParams = array('parent' => $this->GetKey(), 'type' => $sType);
  106. }
  107. else
  108. {
  109. $sSelectChilds = 'SELECT menuNode AS m WHERE m.parent_id = :parent';
  110. $aParams = array('parent' => $this->GetKey());
  111. }
  112. $oSearchFilter = DBObjectSearch::FromOQL($sSelectChilds);
  113. $oSet = new CMDBObjectSet($oSearchFilter, array('rank' => true), $aParams);
  114. return $oSet;
  115. }
  116. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  117. {
  118. $sTemplate = $this->Get('template');
  119. $oTemplate = new DisplayTemplate($sTemplate);
  120. $oTemplate->Render($oPage, $aExtraParams);
  121. //$this->ProcessTemplate($sTemplate, $oPage, $aExtraParams);
  122. }
  123. public function DisplayMenu(iTopWebPage $oP, $sType, $aExtraParams, $bRootLevel = true, $iActiveNodeId = -1)
  124. {
  125. $bActive = false;
  126. if ($bRootLevel)
  127. {
  128. $oP->AddToMenu("<h3><a href=\"".$this->GetMenuLink($aExtraParams)."\" title=\"".$this->GetMenuLabel()."\">".$this->GetMenuName()."</a></h3>\n");
  129. $oP->AddToMenu("\n<div>\n");
  130. $oP->AddToMenu("<p><a href=\"".$this->GetMenuLink($aExtraParams)."\" title=\"".$this->GetMenuLabel()."\">".$this->GetMenuLabel()."</a></p>\n");
  131. $oSet = $this->GetChildNodesSet($sType);
  132. if ($oSet->Count() > 0)
  133. {
  134. $oP->AddToMenu("\n<ul>\n");
  135. while($oChildNode = $oSet->Fetch())
  136. {
  137. $bActive |= $oChildNode->DisplayMenu($oP, $sType, $aExtraParams, false /* ! RootLevel */, $iActiveNodeId);
  138. }
  139. $oP->AddToMenu("</ul>\n");
  140. }
  141. $oP->AddToMenu("\n</div>\n");
  142. }
  143. else
  144. {
  145. $oP->AddToMenu("<li><a href=\"".$this->GetMenuLink($aExtraParams)."\" title=\"".$this->GetMenuLabel()."\">".$this->GetMenuName()."</a>");
  146. $oSet = $this->GetChildNodesSet($sType);
  147. if ($oSet->Count() > 0)
  148. {
  149. $oP->AddToMenu("\n<ul>\n");
  150. while($oChildNode = $oSet->Fetch())
  151. {
  152. $bActive |= $oChildNode->DisplayMenu($oP, $sType, $aExtraParams, false /* ! RootLevel */, $iActiveNodeId);
  153. }
  154. $oP->AddToMenu("</ul>\n");
  155. }
  156. $oP->AddToMenu("</li>\n");
  157. }
  158. $bResult = ($iActiveNodeId == $this->GetKey()) | $bActive;
  159. return $bResult;
  160. }
  161. static public function DisplayCreationForm(WebPage $oP, $sClass, $sFilter, $aExtraParams = array())
  162. {
  163. $oFilter = DBObjectSearch::unserialize($sFilter);
  164. $oP->p('Create a new menu item for: '.$oFilter->__DescribeHTML());
  165. $oP->add('<form action="UniversalSearch.php" method="post">');
  166. $oP->add('<input type="hidden" name="operation" value="add_menu">');
  167. $oP->add('<input type="hidden" name="filter" value="'.$sFilter.'">');
  168. $oP->add('<input type="hidden" name="class" value="'.$sClass.'">');
  169. $oP->p('Menu Label: <input type="text" name="label" size="30">');
  170. $oP->p('Description: <input type="text" name="description" size="30">');
  171. $oP->add('<p>Insert after: <select name="previous_node_id">');
  172. $aNodes = self::GetMenuAsArray(null, 'user');
  173. foreach($aNodes as $aNodeDesc)
  174. {
  175. $oP->add('<option value="'.$aNodeDesc['id'].'">'.str_repeat('&nbsp;&nbsp;&nbsp;', $aNodeDesc['depth']).$aNodeDesc['label'].'</option>');
  176. }
  177. $oP->add('</select></p>');
  178. $oP->p('<input type="checkbox" name="child_item" value="1"> Create as a child menu item');
  179. $oP->p('<input type="submit" value=" Ok "> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" class="jqmClose" value="Cancel">');
  180. $oP->add('</form>');
  181. }
  182. static public function GetMenuAsArray($oRootNode = null, $sType = 'application', $iDepth = 0)
  183. {
  184. $aNodes = array();
  185. if (is_object($oRootNode))
  186. {
  187. $oChildSet = $oRootNode->GetChildNodesSet($sType);
  188. while($oNode = $oChildSet->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. else
  195. {
  196. $oSearchFilter = new DbObjectSearch("menuNode");
  197. $oSearchFilter->AddCondition('parent_id', 0, '=');
  198. if ($sType != null)
  199. {
  200. $oSearchFilter->AddCondition('type', $sType, '=');
  201. if ($sType == 'user')
  202. {
  203. $oSearchFilter->AddCondition('user_id', UserRights::GetUserId(), '=');
  204. }
  205. }
  206. $oRootSet = new CMDBObjectSet($oSearchFilter, array('rank' => true));
  207. while($oNode = $oRootSet->Fetch())
  208. {
  209. $aNodes[] = array('depth' => $iDepth, 'id' => $oNode->GetKey(), 'label' => $oNode->GetName());
  210. $aNodes = array_merge($aNodes, self::GetMenuAsArray($oNode, $sType, $iDepth+1));
  211. }
  212. }
  213. return $aNodes;
  214. }
  215. /**
  216. * Returns a set of all the nodes following the current node in the tree
  217. * (i.e. nodes with the same parent but with a greater rank)
  218. */
  219. public function GetNextNodesSet($sType = 'application')
  220. {
  221. $oSearchFilter = new DBObjectSearch("menuNode");
  222. $oSearchFilter->AddCondition('parent_id', $this->Get('parent_id'));
  223. $oSearchFilter->AddCondition('rank', $this->Get('rank'), '>');
  224. if ($sType != null)
  225. {
  226. $oSearchFilter->AddCondition('type', $sType, '=');
  227. if ($sType == 'user')
  228. {
  229. $oSearchFilter->AddCondition('user_id', UserRights::GetUserId(), '=');
  230. }
  231. }
  232. $oSet = new DBObjectSet($oSearchFilter, array('rank'=> true)); // Order by rank (true means ascending)
  233. return $oSet;
  234. }
  235. }
  236. ?>