preferences.php 15 KB

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