menunode.class.inc.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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)
  124. {
  125. $oP->AddToMenu("<li><a href=\"".$this->GetMenuLink($aExtraParams)."\" title=\"".$this->GetMenuLabel()."\">".$this->GetMenuName()."</a>");
  126. $oSet = $this->GetChildNodesSet($sType);
  127. if ($oSet->Count() > 0)
  128. {
  129. $oP->AddToMenu("\n<ul>\n");
  130. while($oChildNode = $oSet->Fetch())
  131. {
  132. $oChildNode->DisplayMenu($oP, $sType, $aExtraParams);
  133. }
  134. $oP->AddToMenu("</ul>\n");
  135. }
  136. $oP->AddToMenu("</li>\n");
  137. }
  138. static public function DisplayCreationForm(WebPage $oP, $sClass, $sFilter, $aExtraParams = array())
  139. {
  140. $oFilter = DBObjectSearch::unserialize($sFilter);
  141. $oP->p('Create a new menu item for: '.$oFilter->__DescribeHTML());
  142. $oP->add('<form action="UniversalSearch.php" method="post">');
  143. $oP->add('<input type="hidden" name="operation" value="add_menu">');
  144. $oP->add('<input type="hidden" name="filter" value="'.$sFilter.'">');
  145. $oP->add('<input type="hidden" name="class" value="'.$sClass.'">');
  146. $oP->p('Menu Label: <input type="text" name="label" size="30">');
  147. $oP->p('Description: <input type="text" name="description" size="30">');
  148. $oP->add('<p>Insert after: <select name="previous_node_id">');
  149. $aNodes = self::GetMenuAsArray(null, 'user');
  150. foreach($aNodes as $aNodeDesc)
  151. {
  152. $oP->add('<option value="'.$aNodeDesc['id'].'">'.str_repeat('&nbsp;&nbsp;&nbsp;', $aNodeDesc['depth']).$aNodeDesc['label'].'</option>');
  153. }
  154. $oP->add('</select></p>');
  155. $oP->p('<input type="checkbox" name="child_item" value="1"> Create as a child menu item');
  156. $oP->p('<input type="submit" value=" Ok "> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" class="jqmClose" value="Cancel">');
  157. $oP->add('</form>');
  158. }
  159. static public function GetMenuAsArray($oRootNode = null, $sType = 'application', $iDepth = 0)
  160. {
  161. $aNodes = array();
  162. if (is_object($oRootNode))
  163. {
  164. $oChildSet = $oRootNode->GetChildNodesSet($sType);
  165. while($oNode = $oChildSet->Fetch())
  166. {
  167. $aNodes[] = array('depth' => $iDepth, 'id' => $oNode->GetKey(), 'label' => $oNode->GetName());
  168. $aNodes = array_merge($aNodes, self::GetMenuAsArray($oNode, $sType, $iDepth+1));
  169. }
  170. }
  171. else
  172. {
  173. $oSearchFilter = new DbObjectSearch("menuNode");
  174. $oSearchFilter->AddCondition('parent_id', 0, '=');
  175. if ($sType != null)
  176. {
  177. $oSearchFilter->AddCondition('type', $sType, '=');
  178. if ($sType == 'user')
  179. {
  180. $oSearchFilter->AddCondition('user_id', UserRights::GetUserId(), '=');
  181. }
  182. }
  183. $oRootSet = new CMDBObjectSet($oSearchFilter, array('rank' => true));
  184. while($oNode = $oRootSet->Fetch())
  185. {
  186. $aNodes[] = array('depth' => $iDepth, 'id' => $oNode->GetKey(), 'label' => $oNode->GetName());
  187. $aNodes = array_merge($aNodes, self::GetMenuAsArray($oNode, $sType, $iDepth+1));
  188. }
  189. }
  190. return $aNodes;
  191. }
  192. /**
  193. * Returns a set of all the nodes following the current node in the tree
  194. * (i.e. nodes with the same parent but with a greater rank)
  195. */
  196. public function GetNextNodesSet($sType = 'application')
  197. {
  198. $oSearchFilter = new DBObjectSearch("menuNode");
  199. $oSearchFilter->AddCondition('parent_id', $this->Get('parent_id'));
  200. $oSearchFilter->AddCondition('rank', $this->Get('rank'), '>');
  201. if ($sType != null)
  202. {
  203. $oSearchFilter->AddCondition('type', $sType, '=');
  204. if ($sType == 'user')
  205. {
  206. $oSearchFilter->AddCondition('user_id', UserRights::GetUserId(), '=');
  207. }
  208. }
  209. $oSet = new DBObjectSet($oSearchFilter, array('rank'=> true)); // Order by rank (true means ascending)
  210. return $oSet;
  211. }
  212. }
  213. ?>