shortcut.class.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. // Copyright (C) 2010-2013 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. return array();
  129. }
  130. // End of the minimal implementation of iDisplay
  131. }
  132. class ShortcutOQL extends Shortcut
  133. {
  134. public static function Init()
  135. {
  136. $aParams = array
  137. (
  138. "category" => "gui,view_in_gui",
  139. "key_type" => "autoincrement",
  140. "name_attcode" => "name",
  141. "state_attcode" => "",
  142. "reconc_keys" => array(),
  143. "db_table" => "priv_shortcut_oql",
  144. "db_key_field" => "id",
  145. "db_finalclass_field" => "",
  146. "display_template" => "",
  147. );
  148. MetaModel::Init_Params($aParams);
  149. MetaModel::Init_InheritAttributes();
  150. MetaModel::Init_AddAttribute(new AttributeOQL("oql", array("allowed_values"=>null, "sql"=>"oql", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  151. MetaModel::Init_AddAttribute(new AttributeEnum("auto_reload", array("allowed_values"=>new ValueSetEnum('none,custom'), "sql"=>"auto_reload", "default_value"=>"none", "is_null_allowed"=>false, "depends_on"=>array())));
  152. MetaModel::Init_AddAttribute(new AttributeInteger("auto_reload_sec", array("allowed_values"=>null, "sql"=>"auto_reload_sec", "default_value"=>60, "is_null_allowed"=>false, "depends_on"=>array())));
  153. // Display lists
  154. MetaModel::Init_SetZListItems('details', array('name', 'context', 'oql')); // Attributes to be displayed for the complete details
  155. MetaModel::Init_SetZListItems('list', array('name')); // Attributes to be displayed for a list
  156. // Search criteria
  157. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  158. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  159. }
  160. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  161. {
  162. $oPage->set_title($this->Get('name'));
  163. switch($this->Get('auto_reload'))
  164. {
  165. case 'custom':
  166. $iRate = (int)$this->Get('auto_reload_sec');
  167. if ($iRate > 0)
  168. {
  169. // Must a string otherwise it can be evaluated to 'true' and defaults to "standard" refresh rate!
  170. $aExtraParams['auto_reload'] = (string)$iRate;
  171. }
  172. break;
  173. default:
  174. case 'none':
  175. }
  176. $bSearchPane = true;
  177. $bSearchOpen = false;
  178. try
  179. {
  180. OQLMenuNode::RenderOQLSearch($this->Get('oql'), $this->Get('name'), 'shortcut_'.$this->GetKey(), $bSearchPane, $bSearchOpen, $oPage, $aExtraParams);
  181. }
  182. catch (Exception $e)
  183. {
  184. throw new Exception("The OQL shortcut '".$this->Get('name')."' (id: ".$this->GetKey().") could not be displayed: ".$e->getMessage());
  185. }
  186. }
  187. public function CloneTableSettings($sTableSettings)
  188. {
  189. $aTableSettings = json_decode($sTableSettings, true);
  190. $oFilter = DBObjectSearch::FromOQL($this->Get('oql'));
  191. $oCustomSettings = new DataTableSettings($oFilter->GetSelectedClasses());
  192. $oCustomSettings->iDefaultPageSize = $aTableSettings['iPageSize'];
  193. $oCustomSettings->aColumns = $aTableSettings['oColumns'];
  194. $oCustomSettings->Save('shortcut_'.$this->GetKey());
  195. }
  196. public static function GetCreationForm($sOQL = null, $sTableSettings = null)
  197. {
  198. $oForm = new DesignerForm();
  199. // Find a unique default name
  200. // -> The class of the query + an index if necessary
  201. if ($sOQL == null)
  202. {
  203. $sDefault = '';
  204. }
  205. else
  206. {
  207. $oBMSearch = new DBObjectSearch('Shortcut');
  208. $oBMSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
  209. $oBMSet = new DBObjectSet($oBMSearch);
  210. $aNames = $oBMSet->GetColumnAsArray('name');
  211. $oSearch = DBObjectSearch::FromOQL($sOQL);
  212. $sDefault = utils::MakeUniqueName($oSearch->GetClass(), $aNames);
  213. }
  214. $oField = new DesignerTextField('name', Dict::S('Class:Shortcut/Attribute:name'), $sDefault);
  215. $oField->SetMandatory(true);
  216. $oForm->AddField($oField);
  217. /*
  218. $oField = new DesignerComboField('auto_reload', Dict::S('Class:ShortcutOQL/Attribute:auto_reload'), 'none');
  219. $oAttDef = MetaModel::GetAttributeDef(__class__, 'auto_reload');
  220. $oField->SetAllowedValues($oAttDef->GetAllowedValues());
  221. $oField->SetMandatory(true);
  222. $oForm->AddField($oField);
  223. */
  224. $oField = new DesignerBooleanField('auto_reload', Dict::S('Class:ShortcutOQL/Attribute:auto_reload'), false);
  225. $oForm->AddField($oField);
  226. $oField = new DesignerIntegerField('auto_reload_sec', Dict::S('Class:ShortcutOQL/Attribute:auto_reload_sec'), MetaModel::GetConfig()->GetStandardReloadInterval());
  227. $oField->SetBoundaries(MetaModel::GetConfig()->Get('min_reload_interval'), null); // no upper limit
  228. $oField->SetMandatory(false);
  229. $oForm->AddField($oField);
  230. $oField = new DesignerHiddenField('oql', '', $sOQL);
  231. $oForm->AddField($oField);
  232. $oField = new DesignerHiddenField('table_settings', '', $sTableSettings);
  233. $oForm->AddField($oField);
  234. return $oForm;
  235. }
  236. public static function GetCreationDlgFromOQL($oPage, $sOQL, $sTableSettings)
  237. {
  238. $oPage->add('<div id="shortcut_creation_dlg">');
  239. $oForm = self::GetCreationForm($sOQL, $sTableSettings);
  240. $oForm->Render($oPage);
  241. $oPage->add('</div>');
  242. $sDialogTitle = Dict::S('UI:ShortcutListDlg:Title');
  243. $sOkButtonLabel = Dict::S('UI:Button:Ok');
  244. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  245. $oAppContext = new ApplicationContext();
  246. $sContext = $oAppContext->GetForLink();
  247. $sRateTitle = addslashes(Dict::Format('Class:ShortcutOQL/Attribute:auto_reload_sec/tip', MetaModel::GetConfig()->Get('min_reload_interval')));
  248. $oPage->add_ready_script(
  249. <<<EOF
  250. // Note: the title gets deleted by the validation mechanism
  251. $("#attr_auto_reload_sec").tooltip({items: 'input', content: '$sRateTitle'});
  252. $("#attr_auto_reload_sec").prop('disabled', !$('#attr_auto_reload').is(':checked'));
  253. $('#attr_auto_reload').change( function(ev) {
  254. $("#attr_auto_reload_sec").prop('disabled', !$(this).is(':checked'));
  255. } );
  256. function ShortcutCreationOK()
  257. {
  258. var oForm = $('#shortcut_creation_dlg form');
  259. var sFormId = oForm.attr('id');
  260. var oParams = null;
  261. var aErrors = ValidateForm(sFormId, false);
  262. if (aErrors.length == 0)
  263. {
  264. oParams = ReadFormParams(sFormId);
  265. }
  266. oParams.operation = 'shortcut_list_create';
  267. var me = $('#shortcut_creation_dlg');
  268. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?$sContext', oParams, function(data) {
  269. me.dialog( "close" );
  270. me.remove();
  271. $('body').append(data);
  272. });
  273. }
  274. $('#shortcut_creation_dlg form').bind('submit', function() { ShortcutCreationOK(); return false; });
  275. $('#shortcut_creation_dlg').dialog({
  276. width: 400,
  277. modal: true,
  278. title: '$sDialogTitle',
  279. buttons: [
  280. { text: "$sOkButtonLabel", click: ShortcutCreationOK },
  281. { text: "$sCancelButtonLabel", click: function() {
  282. $(this).dialog( "close" ); $(this).remove();
  283. } },
  284. ],
  285. close: function() { $(this).remove(); }
  286. });
  287. EOF
  288. );
  289. }
  290. }
  291. ?>