preferences.php 14 KB

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