shortcut.class.inc.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Persistent class Shortcut and derived
  20. * Shortcuts of any kind
  21. *
  22. * @copyright Copyright (C) 2010-2012 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. abstract class Shortcut extends cmdbAbstractObject
  26. {
  27. public static function Init()
  28. {
  29. $aParams = array
  30. (
  31. "category" => "core/cmdb,view_in_gui,application",
  32. "key_type" => "autoincrement",
  33. "name_attcode" => "name",
  34. "state_attcode" => "",
  35. "reconc_keys" => array(),
  36. "db_table" => "priv_shortcut",
  37. "db_key_field" => "id",
  38. "db_finalclass_field" => "realclass",
  39. "display_template" => "",
  40. );
  41. MetaModel::Init_Params($aParams);
  42. //MetaModel::Init_InheritAttributes();
  43. MetaModel::Init_AddAttribute(new AttributeExternalKey("user_id", array("targetclass"=>"User", "allowed_values"=>null, "sql"=>"user_id", "is_null_allowed"=>true, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));
  44. MetaModel::Init_AddAttribute(new AttributeString("name", array("allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  45. MetaModel::Init_AddAttribute(new AttributeText("context", array("allowed_values"=>null, "sql"=>"context", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  46. // Display lists
  47. MetaModel::Init_SetZListItems('details', array('name', 'context')); // Attributes to be displayed for the complete details
  48. MetaModel::Init_SetZListItems('list', array('name')); // Attributes to be displayed for a list
  49. // Search criteria
  50. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  51. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  52. }
  53. abstract public function RenderContent(WebPage $oPage, $aExtraParams = array());
  54. protected function OnInsert()
  55. {
  56. $this->Set('user_id', UserRights::GetUserId());
  57. }
  58. public function StartRenameDialog($oPage)
  59. {
  60. $oPage->add('<div id="shortcut_rename_dlg">');
  61. $oForm = new DesignerForm();
  62. $sDefault = $this->Get('name');
  63. $oField = new DesignerTextField('name', Dict::S('Class:Shortcut/Attribute:name'), $sDefault);
  64. $oField->SetMandatory(true);
  65. $oForm->AddField($oField);
  66. $oForm->Render($oPage);
  67. $oPage->add('</div>');
  68. $sDialogTitle = Dict::S('UI:ShortcutRenameDlg:Title');
  69. $sOkButtonLabel = Dict::S('UI:Button:Ok');
  70. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  71. $iShortcut = $this->GetKey();
  72. $oPage->add_ready_script(
  73. <<<EOF
  74. function ShortcutRenameOK()
  75. {
  76. var oForm = $(this).find('form');
  77. var sFormId = oForm.attr('id');
  78. var oParams = null;
  79. var aErrors = ValidateForm(sFormId, false);
  80. if (aErrors.length == 0)
  81. {
  82. oParams = ReadFormParams(sFormId);
  83. }
  84. oParams.operation = 'shortcut_rename_go';
  85. oParams.id = $iShortcut;
  86. var me = $(this);
  87. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', oParams, function(data) {
  88. me.dialog( "close" );
  89. me.remove();
  90. $('body').append(data);
  91. });
  92. }
  93. $('#shortcut_rename_dlg form').bind('submit', function() { return false; });
  94. $('#shortcut_rename_dlg').dialog({
  95. width: 400,
  96. modal: true,
  97. title: '$sDialogTitle',
  98. buttons: [
  99. { text: "$sOkButtonLabel", click: ShortcutRenameOK},
  100. { text: "$sCancelButtonLabel", click: function() {
  101. $(this).dialog( "close" ); $(this).remove();
  102. } },
  103. ],
  104. close: function() { $(this).remove(); }
  105. });
  106. EOF
  107. );
  108. }
  109. }
  110. class ShortcutOQL extends Shortcut
  111. {
  112. public static function Init()
  113. {
  114. $aParams = array
  115. (
  116. "category" => "core/cmdb,view_in_gui,application",
  117. "key_type" => "autoincrement",
  118. "name_attcode" => "name",
  119. "state_attcode" => "",
  120. "reconc_keys" => array(),
  121. "db_table" => "priv_shortcut_oql",
  122. "db_key_field" => "id",
  123. "db_finalclass_field" => "",
  124. "display_template" => "",
  125. );
  126. MetaModel::Init_Params($aParams);
  127. MetaModel::Init_InheritAttributes();
  128. MetaModel::Init_AddAttribute(new AttributeOQL("oql", array("allowed_values"=>null, "sql"=>"oql", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  129. // Display lists
  130. MetaModel::Init_SetZListItems('details', array('name', 'context', 'oql')); // Attributes to be displayed for the complete details
  131. MetaModel::Init_SetZListItems('list', array('name')); // Attributes to be displayed for a list
  132. // Search criteria
  133. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  134. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  135. }
  136. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  137. {
  138. $oPage->set_title($this->Get('name'));
  139. $bSearchPane = true;
  140. $bSearchOpen = false;
  141. try
  142. {
  143. OQLMenuNode::RenderOQLSearch($this->Get('oql'), $this->Get('name'), 'shortcut_'.$this->GetKey(), $bSearchPane, $bSearchOpen, $oPage, $aExtraParams);
  144. }
  145. catch (Exception $e)
  146. {
  147. throw new Exception("The OQL shortcut '".$this->Get('name')."' (id: ".$this->GetKey().") could not be displayed: ".$e->getMessage());
  148. }
  149. }
  150. public static function GetCreationForm($sOQL = null)
  151. {
  152. $oForm = new DesignerForm();
  153. // Find a unique default name
  154. // -> The class of the query + an index if necessary
  155. if ($sOQL == null)
  156. {
  157. $sDefault = '';
  158. }
  159. else
  160. {
  161. $oBMSearch = new DBObjectSearch('Shortcut');
  162. $oBMSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
  163. $oBMSet = new DBObjectSet($oBMSearch);
  164. $aNames = $oBMSet->GetColumnAsArray('name');
  165. $oSearch = DBObjectSearch::FromOQL($sOQL);
  166. $sDefault = utils::MakeUniqueName($oSearch->GetClass(), $aNames);
  167. }
  168. $oField = new DesignerTextField('name', Dict::S('Class:Shortcut/Attribute:name'), $sDefault);
  169. $oField->SetMandatory(true);
  170. $oForm->AddField($oField);
  171. //$oField = new DesignerLongTextField('oql', Dict::S('Class:Shortcut/Attribute:oql'), $sOQL);
  172. //$oField->SetMandatory();
  173. $oField = new DesignerHiddenField('oql', '', $sOQL);
  174. $oForm->AddField($oField);
  175. return $oForm;
  176. }
  177. public static function GetCreationDlgFromOQL($oPage, $sOQL)
  178. {
  179. $oPage->add('<div id="shortcut_creation_dlg">');
  180. $oForm = self::GetCreationForm($sOQL);
  181. $oForm->Render($oPage);
  182. $oPage->add('</div>');
  183. $sDialogTitle = Dict::S('UI:ShortcutListDlg:Title');
  184. $sOkButtonLabel = Dict::S('UI:Button:Ok');
  185. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  186. $oAppContext = new ApplicationContext();
  187. $sContext = $oAppContext->GetForLink();
  188. $oPage->add_ready_script(
  189. <<<EOF
  190. function ShortcutCreationOK()
  191. {
  192. var oForm = $('#shortcut_creation_dlg form');
  193. var sFormId = oForm.attr('id');
  194. var oParams = null;
  195. var aErrors = ValidateForm(sFormId, false);
  196. if (aErrors.length == 0)
  197. {
  198. oParams = ReadFormParams(sFormId);
  199. }
  200. oParams.operation = 'shortcut_list_create';
  201. var me = $('#shortcut_creation_dlg');
  202. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?$sContext', oParams, function(data) {
  203. me.dialog( "close" );
  204. me.remove();
  205. $('body').append(data);
  206. });
  207. }
  208. $('#shortcut_creation_dlg form').bind('submit', function() { ShortcutCreationOK(); return false; });
  209. $('#shortcut_creation_dlg').dialog({
  210. width: 400,
  211. modal: true,
  212. title: '$sDialogTitle',
  213. buttons: [
  214. { text: "$sOkButtonLabel", click: ShortcutCreationOK },
  215. { text: "$sCancelButtonLabel", click: function() {
  216. $(this).dialog( "close" ); $(this).remove();
  217. } },
  218. ],
  219. close: function() { $(this).remove(); }
  220. });
  221. EOF
  222. );
  223. }
  224. }
  225. ?>