preferences.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * User preferences page
  18. * Displays / edit some user preferences
  19. *
  20. * @author Erwan Taloc <erwan.taloc@combodo.com>
  21. * @author Romain Quetiez <romain.quetiez@combodo.com>
  22. * @author Denis Flaven <denis.flaven@combodo.com>
  23. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  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. // Favorite organizations: the organizations listed in the drop-down menu
  37. $sOQL = 'SELECT Organization';
  38. $oFilter = DBObjectSearch::FromOQL($sOQL);
  39. $oBlock = new DisplayBlock($oFilter, 'list', false);
  40. $oP->add('<div class="page_header"><h1><img style="vertical-align:middle" src="../images/preferences.png"/>&nbsp;'.Dict::S('UI:Preferences')."</h1></div>\n");
  41. $oP->add('<div id="user_prefs" style="max-width:800px; min-width:400px;">');
  42. $oP->add('<fieldset><legend>'.Dict::S('UI:FavoriteOrganizations').'</legend>');
  43. $oP->p(Dict::S('UI:FavoriteOrganizations+'));
  44. $oP->add('<form method="post">');
  45. $oBlock->Display($oP, 1, array('menu' => false, 'selection_mode' => true, 'selection_type' => 'multiple', 'cssCount'=> '.selectedCount'));
  46. $oP->add($oAppContext->GetForForm());
  47. $oP->add('<input type="hidden" name="operation" value="apply"/>');
  48. $oP->add('</fieldset>');
  49. $sURL = utils::GetAbsoluteUrlAppRoot().'pages/UI.php';
  50. $oP->add('<p><input type="button" onClick="window.location.href=\''.$sURL.'\'" value="'.Dict::S('UI:Button:Cancel').'"/>');
  51. $oP->add('&nbsp;&nbsp;');
  52. $oP->add('<input type="submit" value="'.Dict::S('UI:Button:Apply').'"/></p>');
  53. $oP->add('</form>');
  54. $oP->add('</div>');
  55. $aFavoriteOrgs = appUserPreferences::GetPref('favorite_orgs', null);
  56. if ($aFavoriteOrgs == null)
  57. {
  58. // All checked
  59. $oP->add_ready_script(
  60. <<<EOF
  61. var pager = $('#user_prefs form .pager');
  62. if (pager.length > 0)
  63. {
  64. console.log("PAGINATED display");
  65. // paginated display, restore the selection
  66. $(':input[name=selectionMode]', pager).val('negative');
  67. $('#user_prefs table.listResults').trigger('load_selection');
  68. }
  69. else
  70. {
  71. console.log("Non-paginated display");
  72. CheckAll('#user_prefs .listResults :checkbox:not(:disabled)', true);
  73. }
  74. EOF
  75. );
  76. }
  77. else
  78. {
  79. $sChecked = implode('","', $aFavoriteOrgs);
  80. $oP->add_ready_script(
  81. <<<EOF
  82. var aChecked = ["$sChecked"];
  83. var pager = $('#user_prefs form .pager');
  84. if (pager.length > 0)
  85. {
  86. console.log("PAGINATED display");
  87. // paginated display, restore the selection
  88. $(':input[name=selectionMode]', pager).val('positive');
  89. for (i=0; i<aChecked.length; i++)
  90. {
  91. pager.append('<input type="hidden" name="storedSelection[]" id="'+aChecked[i]+'" value="'+aChecked[i]+'"/>');
  92. }
  93. $('#user_prefs table.listResults').trigger('load_selection');
  94. }
  95. else
  96. {
  97. console.log("Non-paginated display");
  98. $('#user_prefs form :checkbox[name^=selectObject]').each( function()
  99. {
  100. if ($.inArray($(this).val(), aChecked) > -1)
  101. {
  102. $(this).attr('checked', true);
  103. $(this).trigger('change');
  104. }
  105. });
  106. }
  107. EOF
  108. );
  109. }}
  110. /////////////////////////////////////////////////////////////////////////////
  111. //
  112. // Main program
  113. //
  114. /////////////////////////////////////////////////////////////////////////////
  115. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  116. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  117. $iStep = utils::ReadParam('step', 1);
  118. $oPage = new iTopWebPage(Dict::S('UI:Preferences'));
  119. $sOperation = utils::ReadParam('operation', '');
  120. try
  121. {
  122. switch($sOperation)
  123. {
  124. case 'apply':
  125. $oFilter = DBObjectSearch::FromOQL('SELECT Organization');
  126. $sSelectionMode = utils::ReadParam('selectionMode', '');
  127. $aExceptions = utils::ReadParam('storedSelection', array());
  128. if (($sSelectionMode == 'negative') && (count($aExceptions) == 0))
  129. {
  130. // All Orgs selected
  131. appUserPreferences::SetPref('favorite_orgs', null);
  132. }
  133. else
  134. {
  135. // Some organizations selected... store them
  136. $aSelectOrgs = utils::ReadMultipleSelection($oFilter);
  137. appUserPreferences::SetPref('favorite_orgs', $aSelectOrgs);
  138. }
  139. DisplayPreferences($oPage);
  140. break;
  141. case 'display':
  142. default:
  143. DisplayPreferences($oPage);
  144. }
  145. $oPage->output();
  146. }
  147. catch(CoreException $e)
  148. {
  149. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  150. $oP = new SetupWebPage(Dict::S('UI:PageTitle:FatalError'));
  151. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  152. $oP->error(Dict::Format('UI:Error_Details', $e->getHtmlDesc()));
  153. $oP->output();
  154. if (MetaModel::IsLogEnabledIssue())
  155. {
  156. if (MetaModel::IsValidClass('EventIssue'))
  157. {
  158. $oLog = new EventIssue();
  159. $oLog->Set('message', $e->getMessage());
  160. $oLog->Set('userinfo', '');
  161. $oLog->Set('issue', $e->GetIssue());
  162. $oLog->Set('impact', 'Page could not be displayed');
  163. $oLog->Set('callstack', $e->getTrace());
  164. $oLog->Set('data', $e->getContextData());
  165. $oLog->DBInsertNoReload();
  166. }
  167. IssueLog::Error($e->getMessage());
  168. }
  169. // For debugging only
  170. //throw $e;
  171. }
  172. catch(Exception $e)
  173. {
  174. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  175. $oP = new SetupWebPage(Dict::S('UI:PageTitle:FatalError'));
  176. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  177. $oP->error(Dict::Format('UI:Error_Details', $e->getMessage()));
  178. $oP->output();
  179. if (MetaModel::IsLogEnabledIssue())
  180. {
  181. if (MetaModel::IsValidClass('EventIssue'))
  182. {
  183. $oLog = new EventIssue();
  184. $oLog->Set('message', $e->getMessage());
  185. $oLog->Set('userinfo', '');
  186. $oLog->Set('issue', 'PHP Exception');
  187. $oLog->Set('impact', 'Page could not be displayed');
  188. $oLog->Set('callstack', $e->getTrace());
  189. $oLog->Set('data', array());
  190. $oLog->DBInsertNoReload();
  191. }
  192. IssueLog::Error($e->getMessage());
  193. }
  194. }
  195. ?>