shortcut.class.inc.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. 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. // Display lists
  152. MetaModel::Init_SetZListItems('details', array('name', 'context', 'oql')); // Attributes to be displayed for the complete details
  153. MetaModel::Init_SetZListItems('list', array('name')); // Attributes to be displayed for a list
  154. // Search criteria
  155. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  156. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  157. }
  158. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  159. {
  160. $oPage->set_title($this->Get('name'));
  161. $bSearchPane = true;
  162. $bSearchOpen = false;
  163. try
  164. {
  165. OQLMenuNode::RenderOQLSearch($this->Get('oql'), $this->Get('name'), 'shortcut_'.$this->GetKey(), $bSearchPane, $bSearchOpen, $oPage, $aExtraParams);
  166. }
  167. catch (Exception $e)
  168. {
  169. throw new Exception("The OQL shortcut '".$this->Get('name')."' (id: ".$this->GetKey().") could not be displayed: ".$e->getMessage());
  170. }
  171. }
  172. public static function GetCreationForm($sOQL = null)
  173. {
  174. $oForm = new DesignerForm();
  175. // Find a unique default name
  176. // -> The class of the query + an index if necessary
  177. if ($sOQL == null)
  178. {
  179. $sDefault = '';
  180. }
  181. else
  182. {
  183. $oBMSearch = new DBObjectSearch('Shortcut');
  184. $oBMSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
  185. $oBMSet = new DBObjectSet($oBMSearch);
  186. $aNames = $oBMSet->GetColumnAsArray('name');
  187. $oSearch = DBObjectSearch::FromOQL($sOQL);
  188. $sDefault = utils::MakeUniqueName($oSearch->GetClass(), $aNames);
  189. }
  190. $oField = new DesignerTextField('name', Dict::S('Class:Shortcut/Attribute:name'), $sDefault);
  191. $oField->SetMandatory(true);
  192. $oForm->AddField($oField);
  193. //$oField = new DesignerLongTextField('oql', Dict::S('Class:Shortcut/Attribute:oql'), $sOQL);
  194. //$oField->SetMandatory();
  195. $oField = new DesignerHiddenField('oql', '', $sOQL);
  196. $oForm->AddField($oField);
  197. return $oForm;
  198. }
  199. public static function GetCreationDlgFromOQL($oPage, $sOQL)
  200. {
  201. $oPage->add('<div id="shortcut_creation_dlg">');
  202. $oForm = self::GetCreationForm($sOQL);
  203. $oForm->Render($oPage);
  204. $oPage->add('</div>');
  205. $sDialogTitle = Dict::S('UI:ShortcutListDlg:Title');
  206. $sOkButtonLabel = Dict::S('UI:Button:Ok');
  207. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  208. $oAppContext = new ApplicationContext();
  209. $sContext = $oAppContext->GetForLink();
  210. $oPage->add_ready_script(
  211. <<<EOF
  212. function ShortcutCreationOK()
  213. {
  214. var oForm = $('#shortcut_creation_dlg form');
  215. var sFormId = oForm.attr('id');
  216. var oParams = null;
  217. var aErrors = ValidateForm(sFormId, false);
  218. if (aErrors.length == 0)
  219. {
  220. oParams = ReadFormParams(sFormId);
  221. }
  222. oParams.operation = 'shortcut_list_create';
  223. var me = $('#shortcut_creation_dlg');
  224. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?$sContext', oParams, function(data) {
  225. me.dialog( "close" );
  226. me.remove();
  227. $('body').append(data);
  228. });
  229. }
  230. $('#shortcut_creation_dlg form').bind('submit', function() { ShortcutCreationOK(); return false; });
  231. $('#shortcut_creation_dlg').dialog({
  232. width: 400,
  233. modal: true,
  234. title: '$sDialogTitle',
  235. buttons: [
  236. { text: "$sOkButtonLabel", click: ShortcutCreationOK },
  237. { text: "$sCancelButtonLabel", click: function() {
  238. $(this).dialog( "close" ); $(this).remove();
  239. } },
  240. ],
  241. close: function() { $(this).remove(); }
  242. });
  243. EOF
  244. );
  245. }
  246. }
  247. ?>