preferences.php 14 KB

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