preferences.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. * User preferences page
  20. * Displays / edit some user preferences
  21. *
  22. * @copyright Copyright (C) 2010-2012 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. require_once('../approot.inc.php');
  26. require_once(APPROOT.'/application/application.inc.php');
  27. require_once(APPROOT.'/application/itopwebpage.class.inc.php');
  28. require_once(APPROOT.'/application/startup.inc.php');
  29. /**
  30. * Displays the user's changeable preferences
  31. * @param $oP WebPage The web page used for the output
  32. */
  33. function DisplayPreferences($oP)
  34. {
  35. $oAppContext = new ApplicationContext();
  36. $sURL = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?'.$oAppContext->GetForLink();
  37. $oP->add('<div class="page_header"><h1><img style="vertical-align:middle" src="../images/preferences.png"/>&nbsp;'.Dict::S('UI:Preferences')."</h1></div>\n");
  38. $oP->add('<div id="user_prefs" style="max-width:800px; min-width:400px;">');
  39. //////////////////////////////////////////////////////////////////////////
  40. //
  41. // User Language selection
  42. //
  43. //////////////////////////////////////////////////////////////////////////
  44. $oP->add('<fieldset><legend>'.Dict::S('UI:FavoriteLanguage').'</legend>');
  45. $oP->add('<form method="post">');
  46. $aLanguages = Dict::GetLanguages();
  47. $aSortedlang = array();
  48. foreach($aLanguages as $sCode => $aLang)
  49. {
  50. $aSortedlang[$aLang['description']] = $sCode;
  51. }
  52. ksort($aSortedlang);
  53. $oP->add('<p>'.Dict::S('UI:Favorites:SelectYourLanguage').' <select name="language">');
  54. foreach($aSortedlang as $sCode)
  55. {
  56. $sSelected = ($sCode == Dict::GetUserLanguage()) ? 'selected' : '';
  57. $oP->add('<option value="'.$sCode.'" '.$sSelected.'/>'.$aLanguages[$sCode]['description'].' ('.$aLanguages[$sCode]['localized_description'].')</option>');
  58. }
  59. $oP->add('</select></p>');
  60. $oP->add('<input type="hidden" name="operation" value="apply_language"/>');
  61. $oP->add($oAppContext->GetForForm());
  62. $oP->add('<p><input type="button" onClick="window.location.href=\''.$sURL.'\'" value="'.Dict::S('UI:Button:Cancel').'"/>');
  63. $oP->add('&nbsp;&nbsp;');
  64. $oP->add('<input type="submit" value="'.Dict::S('UI:Button:Apply').'"/></p>');
  65. $oP->add('</form>');
  66. $oP->add('</fieldset>');
  67. //////////////////////////////////////////////////////////////////////////
  68. //
  69. // Other (miscellaneous) settings
  70. //
  71. //////////////////////////////////////////////////////////////////////////
  72. $oP->add('<fieldset><legend>'.Dict::S('UI:FavoriteOtherSettings').'</legend>');
  73. $oP->add('<form method="post" onsubmit="return ValidateOtherSettings()">');
  74. $iDefaultPageSize = appUserPreferences::GetPref('default_page_size', MetaModel::GetConfig()->GetMinDisplayLimit());
  75. $oP->add('<p>'.Dict::Format('UI:Favorites:Default_X_ItemsPerPage', '<input id="default_page_size" name="default_page_size" type="text" size="3" value="'.$iDefaultPageSize.'"/><span id="v_default_page_size"></span>').'</p>');
  76. $oP->add('<input type="hidden" name="operation" value="apply_others"/>');
  77. $oP->add($oAppContext->GetForForm());
  78. $oP->add('<p><input type="button" onClick="window.location.href=\''.$sURL.'\'" value="'.Dict::S('UI:Button:Cancel').'"/>');
  79. $oP->add('&nbsp;&nbsp;');
  80. $oP->add('<input id="other_submit" type="submit" value="'.Dict::S('UI:Button:Apply').'"/></p>');
  81. $oP->add('</form>');
  82. $oP->add('</fieldset>');
  83. $oP->add_script(
  84. <<<EOF
  85. function ValidateOtherSettings()
  86. {
  87. var sPageLength = $('#default_page_size').val();
  88. var iPageLength = parseInt(sPageLength , 10);
  89. if (/^[0-9]+$/.test(sPageLength) && (iPageLength > 0))
  90. {
  91. $('#v_default_page_size').html('');
  92. $('#other_submit').removeAttr('disabled');
  93. return true;
  94. }
  95. else
  96. {
  97. $('#v_default_page_size').html('<img src="../images/validation_error.png"/>');
  98. $('#other_submit').attr('disabled', 'disabled');
  99. return false;
  100. }
  101. }
  102. EOF
  103. );
  104. //////////////////////////////////////////////////////////////////////////
  105. //
  106. // Favorite Organizations
  107. //
  108. //////////////////////////////////////////////////////////////////////////
  109. $oP->add('<fieldset><legend>'.Dict::S('UI:FavoriteOrganizations').'</legend>');
  110. $oP->p(Dict::S('UI:FavoriteOrganizations+'));
  111. $oP->add('<form method="post">');
  112. // Favorite organizations: the organizations listed in the drop-down menu
  113. $sOQL = ApplicationMenu::GetFavoriteSiloQuery();
  114. $oFilter = DBObjectSearch::FromOQL($sOQL);
  115. $oBlock = new DisplayBlock($oFilter, 'list', false);
  116. $oBlock->Display($oP, 1, array('menu' => false, 'selection_mode' => true, 'selection_type' => 'multiple', 'cssCount'=> '.selectedCount', 'table_id' => 'user_prefs'));
  117. $oP->add($oAppContext->GetForForm());
  118. $oP->add('<input type="hidden" name="operation" value="apply"/>');
  119. $oP->add('<p><input type="button" onClick="window.location.href=\''.$sURL.'\'" value="'.Dict::S('UI:Button:Cancel').'"/>');
  120. $oP->add('&nbsp;&nbsp;');
  121. $oP->add('<input type="submit" value="'.Dict::S('UI:Button:Apply').'"/></p>');
  122. $oP->add('</form>');
  123. $oP->add('</fieldset>');
  124. $aFavoriteOrgs = appUserPreferences::GetPref('favorite_orgs', null);
  125. if ($aFavoriteOrgs == null)
  126. {
  127. // All checked
  128. $oP->add_ready_script(
  129. <<<EOF
  130. if ($('#user_prefs table.pagination').length > 0)
  131. {
  132. // paginated display, restore the selection
  133. var pager = $('#user_prefs form .pager');
  134. $(':input[name=selectionMode]', pager).val('negative');
  135. $('#user_prefs table.listResults').trigger('load_selection');
  136. }
  137. else
  138. {
  139. $('#user_prefs table.listResults').trigger('check_all');
  140. }
  141. EOF
  142. );
  143. }
  144. else
  145. {
  146. $sChecked = implode('","', $aFavoriteOrgs);
  147. $oP->add_ready_script(
  148. <<<EOF
  149. var aChecked = ["$sChecked"];
  150. if ($('#user_prefs table.pagination').length > 0)
  151. {
  152. // paginated display, restore the selection
  153. var pager = $('#user_prefs form .pager');
  154. $(':input[name=selectionMode]', pager).val('positive');
  155. for (i=0; i<aChecked.length; i++)
  156. {
  157. pager.append('<input type="hidden" name="storedSelection[]" id="'+aChecked[i]+'" value="'+aChecked[i]+'"/>');
  158. }
  159. $('#user_prefs table.listResults').trigger('load_selection');
  160. }
  161. else
  162. {
  163. $('#user_prefs form :checkbox[name^=selectObject]').each( function()
  164. {
  165. if ($.inArray($(this).val(), aChecked) > -1)
  166. {
  167. $(this).attr('checked', true);
  168. $(this).trigger('change');
  169. }
  170. });
  171. }
  172. EOF
  173. );
  174. }
  175. //////////////////////////////////////////////////////////////////////////
  176. //
  177. // Shortcuts
  178. //
  179. //////////////////////////////////////////////////////////////////////////
  180. $oP->add('<fieldset><legend>'.Dict::S('Menu:MyShortcuts').'</legend>');
  181. //$oP->p(Dict::S('UI:Menu:MyShortcuts+'));
  182. $oBMSearch = new DBObjectSearch('Shortcut');
  183. $oBMSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
  184. //$aExtraParams = array('menu' => false, 'toolkit_menu' => false, 'display_limit' => false, 'localize_values' => $bLocalize, 'zlist' => 'details');
  185. $aExtraParams = array();
  186. $oBlock = new DisplayBlock($oBMSearch, 'list', false, $aExtraParams);
  187. $oBlock->Display($oP, 'shortcut_list', array('view_link' => false, 'menu' => false, 'toolkit_menu' => false, 'selection_mode' => true, 'selection_type' => 'multiple', 'cssCount'=> '#shortcut_selection_count', 'table_id' => 'user_prefs_shortcuts'));
  188. $oP->add('<p>');
  189. $oSet = new DBObjectSet($oBMSearch);
  190. if ($oSet->Count() > 0)
  191. {
  192. $sButtons = '<img src="../images/tv-item-last.gif">';
  193. $sButtons .= '&nbsp;';
  194. $sButtons .= '<button id="shortcut_btn_rename">'.Dict::S('UI:Button:Rename').'</button>';
  195. $sButtons .= '&nbsp;';
  196. $sButtons .= '<button id="shortcut_btn_delete">'.Dict::S('UI:Button:Delete').'</button>';
  197. // Selection count updated by the pager, and used to enable buttons
  198. $oP->add('<input type="hidden" id="shortcut_selection_count"/>');
  199. $oP->add('</fieldset>');
  200. $sConfirmDelete = addslashes(Dict::S('UI:ShortcutDelete:Confirm'));
  201. $oP->add_ready_script(
  202. <<<EOF
  203. function OnShortcutBtnRename()
  204. {
  205. var oParams = $('#datatable_shortcut_list').datatable('GetMultipleSelectionParams');
  206. oParams.operation = 'shortcut_rename_dlg';
  207. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', oParams, function(data){
  208. $('body').append(data);
  209. });
  210. return false;
  211. }
  212. function OnShortcutBtnDelete()
  213. {
  214. if (confirm('$sConfirmDelete'))
  215. {
  216. var oParams = $('#datatable_shortcut_list').datatable('GetMultipleSelectionParams');
  217. oParams.operation = 'shortcut_delete_go';
  218. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', oParams, function(data){
  219. $('body').append(data);
  220. });
  221. }
  222. return false;
  223. }
  224. function OnSelectionCountChange()
  225. {
  226. var iCountSelected = $("#shortcut_selection_count").val();
  227. if (iCountSelected == 0)
  228. {
  229. $('#shortcut_btn_rename').attr('disabled', 'disabled');
  230. $('#shortcut_btn_delete').attr('disabled', 'disabled');
  231. }
  232. else if (iCountSelected == 1)
  233. {
  234. $('#shortcut_btn_rename').removeAttr('disabled');
  235. $('#shortcut_btn_delete').removeAttr('disabled');
  236. }
  237. else
  238. {
  239. $('#shortcut_btn_rename').attr('disabled', 'disabled');
  240. $('#shortcut_btn_delete').removeAttr('disabled');
  241. }
  242. }
  243. var oUpperCheckBox = $('#datatable_shortcut_list .checkAll').first();
  244. oUpperCheckBox.parent().width(oUpperCheckBox.width() + 2);
  245. $('#datatable_shortcut_list').append('<tr><td colspan="2">&nbsp;&nbsp;&nbsp;$sButtons</td></tr>');
  246. $('#shortcut_selection_count').bind('change', OnSelectionCountChange);
  247. $('#shortcut_btn_rename').bind('click', OnShortcutBtnRename);
  248. $('#shortcut_btn_delete').bind('click', OnShortcutBtnDelete);
  249. OnSelectionCountChange();
  250. EOF
  251. );
  252. } // if count > 0
  253. //////////////////////////////////////////////////////////////////////////
  254. //
  255. // Footer
  256. //
  257. $oP->add('</div>');
  258. $oP->add_ready_script("$('#fav_page_length').bind('keyup change', function(){ ValidateOtherSettings(); })");
  259. }
  260. /////////////////////////////////////////////////////////////////////////////
  261. //
  262. // Main program
  263. //
  264. /////////////////////////////////////////////////////////////////////////////
  265. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  266. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  267. $iStep = utils::ReadParam('step', 1);
  268. $oPage = new iTopWebPage(Dict::S('UI:Preferences'));
  269. $sOperation = utils::ReadParam('operation', '');
  270. try
  271. {
  272. switch($sOperation)
  273. {
  274. case 'apply':
  275. $oFilter = DBObjectSearch::FromOQL('SELECT Organization');
  276. $sSelectionMode = utils::ReadParam('selectionMode', '');
  277. $aExceptions = utils::ReadParam('storedSelection', array());
  278. if (($sSelectionMode == 'negative') && (count($aExceptions) == 0))
  279. {
  280. // All Orgs selected
  281. appUserPreferences::SetPref('favorite_orgs', null);
  282. }
  283. else
  284. {
  285. // Some organizations selected... store them
  286. $aSelectOrgs = utils::ReadMultipleSelection($oFilter);
  287. appUserPreferences::SetPref('favorite_orgs', $aSelectOrgs);
  288. }
  289. DisplayPreferences($oPage);
  290. break;
  291. case 'apply_language':
  292. $sLangCode = utils::ReadParam('language', 'EN US');
  293. $oUser = UserRights::GetUserObject();
  294. $oUser->Set('language', $sLangCode);
  295. $oUser->DBUpdate();
  296. // Redirect to force a reload/display of the page with the new language
  297. $oAppContext = new ApplicationContext();
  298. $sURL = utils::GetAbsoluteUrlAppRoot().'pages/preferences.php?'.$oAppContext->GetForLink();
  299. $oPage->add_header('Location: '.$sURL);
  300. break;
  301. case 'apply_others':
  302. $iDefaultPageSize = (int)utils::ReadParam('default_page_size', -1);
  303. if ($iDefaultPageSize > 0)
  304. {
  305. appUserPreferences::SetPref('default_page_size', $iDefaultPageSize);
  306. }
  307. DisplayPreferences($oPage);
  308. break;
  309. case 'display':
  310. default:
  311. DisplayPreferences($oPage);
  312. }
  313. $oPage->output();
  314. }
  315. catch(CoreException $e)
  316. {
  317. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  318. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  319. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  320. $oP->error(Dict::Format('UI:Error_Details', $e->getHtmlDesc()));
  321. $oP->output();
  322. if (MetaModel::IsLogEnabledIssue())
  323. {
  324. if (MetaModel::IsValidClass('EventIssue'))
  325. {
  326. $oLog = new EventIssue();
  327. $oLog->Set('message', $e->getMessage());
  328. $oLog->Set('userinfo', '');
  329. $oLog->Set('issue', $e->GetIssue());
  330. $oLog->Set('impact', 'Page could not be displayed');
  331. $oLog->Set('callstack', $e->getTrace());
  332. $oLog->Set('data', $e->getContextData());
  333. $oLog->DBInsertNoReload();
  334. }
  335. IssueLog::Error($e->getMessage());
  336. }
  337. // For debugging only
  338. //throw $e;
  339. }
  340. catch(Exception $e)
  341. {
  342. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  343. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  344. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  345. $oP->error(Dict::Format('UI:Error_Details', $e->getMessage()));
  346. $oP->output();
  347. if (MetaModel::IsLogEnabledIssue())
  348. {
  349. if (MetaModel::IsValidClass('EventIssue'))
  350. {
  351. $oLog = new EventIssue();
  352. $oLog->Set('message', $e->getMessage());
  353. $oLog->Set('userinfo', '');
  354. $oLog->Set('issue', 'PHP Exception');
  355. $oLog->Set('impact', 'Page could not be displayed');
  356. $oLog->Set('callstack', $e->getTrace());
  357. $oLog->Set('data', array());
  358. $oLog->DBInsertNoReload();
  359. }
  360. IssueLog::Error($e->getMessage());
  361. }
  362. }
  363. ?>