shortcut.class.inc.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 DBObject implements iDisplay
  26. {
  27. public static function Init()
  28. {
  29. $aParams = array
  30. (
  31. "category" => "gui,view_in_gui",
  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. // Minimual implementation of iDisplay: to make the shortcut be listable
  110. //
  111. public static function MapContextParam($sContextParam)
  112. {
  113. return (($sContextParam == 'menu') ? null : $sContextParam);
  114. }
  115. public function GetHilightClass()
  116. {
  117. return HILIGHT_CLASS_NONE;
  118. }
  119. public static function GetUIPage()
  120. {
  121. return '';
  122. }
  123. function DisplayDetails(WebPage $oPage, $bEditMode = false)
  124. {
  125. }
  126. function DisplayBareProperties(WebPage $oPage, $bEditMode = false, $sPrefix = '', $aExtraParams = array())
  127. {
  128. }
  129. // End of the minimal implementation of iDisplay
  130. }
  131. class ShortcutOQL extends Shortcut
  132. {
  133. public static function Init()
  134. {
  135. $aParams = array
  136. (
  137. "category" => "gui,view_in_gui",
  138. "key_type" => "autoincrement",
  139. "name_attcode" => "name",
  140. "state_attcode" => "",
  141. "reconc_keys" => array(),
  142. "db_table" => "priv_shortcut_oql",
  143. "db_key_field" => "id",
  144. "db_finalclass_field" => "",
  145. "display_template" => "",
  146. );
  147. MetaModel::Init_Params($aParams);
  148. MetaModel::Init_InheritAttributes();
  149. MetaModel::Init_AddAttribute(new AttributeOQL("oql", array("allowed_values"=>null, "sql"=>"oql", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  150. // Display lists
  151. MetaModel::Init_SetZListItems('details', array('name', 'context', 'oql')); // Attributes to be displayed for the complete details
  152. MetaModel::Init_SetZListItems('list', array('name')); // Attributes to be displayed for a list
  153. // Search criteria
  154. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  155. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  156. }
  157. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  158. {
  159. $oPage->set_title($this->Get('name'));
  160. $bSearchPane = true;
  161. $bSearchOpen = false;
  162. try
  163. {
  164. OQLMenuNode::RenderOQLSearch($this->Get('oql'), $this->Get('name'), 'shortcut_'.$this->GetKey(), $bSearchPane, $bSearchOpen, $oPage, $aExtraParams);
  165. }
  166. catch (Exception $e)
  167. {
  168. throw new Exception("The OQL shortcut '".$this->Get('name')."' (id: ".$this->GetKey().") could not be displayed: ".$e->getMessage());
  169. }
  170. }
  171. public static function GetCreationForm($sOQL = null)
  172. {
  173. $oForm = new DesignerForm();
  174. // Find a unique default name
  175. // -> The class of the query + an index if necessary
  176. if ($sOQL == null)
  177. {
  178. $sDefault = '';
  179. }
  180. else
  181. {
  182. $oBMSearch = new DBObjectSearch('Shortcut');
  183. $oBMSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
  184. $oBMSet = new DBObjectSet($oBMSearch);
  185. $aNames = $oBMSet->GetColumnAsArray('name');
  186. $oSearch = DBObjectSearch::FromOQL($sOQL);
  187. $sDefault = utils::MakeUniqueName($oSearch->GetClass(), $aNames);
  188. }
  189. $oField = new DesignerTextField('name', Dict::S('Class:Shortcut/Attribute:name'), $sDefault);
  190. $oField->SetMandatory(true);
  191. $oForm->AddField($oField);
  192. //$oField = new DesignerLongTextField('oql', Dict::S('Class:Shortcut/Attribute:oql'), $sOQL);
  193. //$oField->SetMandatory();
  194. $oField = new DesignerHiddenField('oql', '', $sOQL);
  195. $oForm->AddField($oField);
  196. return $oForm;
  197. }
  198. public static function GetCreationDlgFromOQL($oPage, $sOQL)
  199. {
  200. $oPage->add('<div id="shortcut_creation_dlg">');
  201. $oForm = self::GetCreationForm($sOQL);
  202. $oForm->Render($oPage);
  203. $oPage->add('</div>');
  204. $sDialogTitle = Dict::S('UI:ShortcutListDlg:Title');
  205. $sOkButtonLabel = Dict::S('UI:Button:Ok');
  206. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  207. $oAppContext = new ApplicationContext();
  208. $sContext = $oAppContext->GetForLink();
  209. $oPage->add_ready_script(
  210. <<<EOF
  211. function ShortcutCreationOK()
  212. {
  213. var oForm = $('#shortcut_creation_dlg form');
  214. var sFormId = oForm.attr('id');
  215. var oParams = null;
  216. var aErrors = ValidateForm(sFormId, false);
  217. if (aErrors.length == 0)
  218. {
  219. oParams = ReadFormParams(sFormId);
  220. }
  221. oParams.operation = 'shortcut_list_create';
  222. var me = $('#shortcut_creation_dlg');
  223. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?$sContext', oParams, function(data) {
  224. me.dialog( "close" );
  225. me.remove();
  226. $('body').append(data);
  227. });
  228. }
  229. $('#shortcut_creation_dlg form').bind('submit', function() { ShortcutCreationOK(); return false; });
  230. $('#shortcut_creation_dlg').dialog({
  231. width: 400,
  232. modal: true,
  233. title: '$sDialogTitle',
  234. buttons: [
  235. { text: "$sOkButtonLabel", click: ShortcutCreationOK },
  236. { text: "$sCancelButtonLabel", click: function() {
  237. $(this).dialog( "close" ); $(this).remove();
  238. } },
  239. ],
  240. close: function() { $(this).remove(); }
  241. });
  242. EOF
  243. );
  244. }
  245. }
  246. ?>