menunode.class.inc.php 9.8 KB

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