user.preferences.class.inc.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. * Store and retrieve user's preferences (i.e persistent per user settings)
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. require_once(APPROOT.'/core/dbobject.class.php');
  25. require_once(APPROOT.'/core/userrights.class.inc.php');
  26. /**
  27. * This class is used to store, in a persistent manner, user related settings (preferences)
  28. * For each user, one record in the database will be created, making their preferences permanent and storing a list
  29. * of properties (pairs of name/value strings)
  30. * This overcomes some limitations of cookies: limited number of cookies, maximum size, depends on the browser, etc..
  31. * This class is used in conjunction with the GetUserPreferences/SetUserPreferences javascript functions (utils.js)
  32. */
  33. class appUserPreferences extends DBObject
  34. {
  35. static $oUserPrefs = null; // Local cache
  36. /**
  37. * Get the value of the given property/preference
  38. * If not set, the default value will be returned
  39. * @param string $sCode Code/Name of the property to set
  40. * @param string $sDefaultValue The default value
  41. * @return string The value of the property for the current user
  42. */
  43. static function GetPref($sCode, $sDefaultValue)
  44. {
  45. if (self::$oUserPrefs == null)
  46. {
  47. self::Load();
  48. }
  49. $aPrefs = self::$oUserPrefs->Get('preferences');
  50. if (isset($aPrefs[$sCode]))
  51. {
  52. return $aPrefs[$sCode];
  53. }
  54. else
  55. {
  56. return $sDefaultValue;
  57. }
  58. }
  59. /**
  60. * Set the value for a given preference, and stores it into the database
  61. * @param string $sCode Code/Name of the property/preference to set
  62. * @param string $sValue Value to set
  63. */
  64. static function SetPref($sCode, $sValue)
  65. {
  66. if (self::$oUserPrefs == null)
  67. {
  68. self::Load();
  69. }
  70. $aPrefs = self::$oUserPrefs->Get('preferences');
  71. $aPrefs[$sCode] = $sValue;
  72. self::$oUserPrefs->Set('preferences', $aPrefs);
  73. self::Save();
  74. }
  75. /**
  76. * Clears the value for a given preference (or list of preferences that matches a pattern), and updates the database
  77. * @param string $sPattern Code/Pattern of the properties/preferences to reset
  78. * @param boolean $bPattern Whether or not the supplied code is a PCRE pattern
  79. */
  80. static function UnsetPref($sCodeOrPattern, $bPattern = false)
  81. {
  82. if (self::$oUserPrefs == null)
  83. {
  84. self::Load();
  85. }
  86. $aPrefs = self::$oUserPrefs->Get('preferences');
  87. if ($bPattern)
  88. {
  89. // the supplied code is a pattern, clear all preferences that match
  90. foreach($aPrefs as $sKey => $void)
  91. {
  92. if (preg_match($sCodeOrPattern, $sKey))
  93. {
  94. unset($aPrefs[$sKey]);
  95. }
  96. }
  97. self::$oUserPrefs->Set('preferences', $aPrefs);
  98. }
  99. else
  100. {
  101. unset($aPrefs[$sCode]);
  102. self::$oUserPrefs->Set('preferences', $aPrefs);
  103. }
  104. // Save only if needed
  105. if (self::$oUserPrefs->IsModified())
  106. {
  107. self::Save();
  108. }
  109. }
  110. /**
  111. * Call this function to get all the preferences for the user, packed as a JSON object
  112. * @return string JSON representation of the preferences
  113. */
  114. static function GetAsJSON()
  115. {
  116. if (self::$oUserPrefs == null)
  117. {
  118. self::Load();
  119. }
  120. $aPrefs = self::$oUserPrefs->Get('preferences');
  121. return json_encode($aPrefs);
  122. }
  123. /**
  124. * Call this function if the user has changed (like when doing a logoff...)
  125. */
  126. static public function Reset()
  127. {
  128. self::$oUserPrefs = null;
  129. }
  130. /**
  131. * Call this function to ERASE all the preferences from the current user
  132. */
  133. static public function ClearPreferences()
  134. {
  135. self::$oUserPrefs = null;
  136. }
  137. static protected function Save()
  138. {
  139. if (self::$oUserPrefs != null)
  140. {
  141. if (self::$oUserPrefs->IsModified())
  142. {
  143. self::$oUserPrefs->DBUpdate();
  144. }
  145. }
  146. }
  147. /**
  148. * Loads the preferences for the current user, creating the record in the database
  149. * if needed
  150. */
  151. static protected function Load()
  152. {
  153. if (self::$oUserPrefs != null) return;
  154. $oSearch = new DBObjectSearch('appUserPreferences');
  155. $oSearch->AddCondition('userid', UserRights::GetUserId(), '=');
  156. $oSet = new DBObjectSet($oSearch);
  157. $oObj = $oSet->Fetch();
  158. if ($oObj == null)
  159. {
  160. // No prefs (yet) for this user, create the object
  161. $oObj = new appUserPreferences();
  162. $oObj->Set('userid', UserRights::GetUserId());
  163. $oObj->Set('preferences', array()); // Default preferences: an empty array
  164. try
  165. {
  166. $oObj->DBInsert();
  167. }
  168. catch(Exception $e)
  169. {
  170. // Ignore errors
  171. }
  172. }
  173. self::$oUserPrefs = $oObj;
  174. }
  175. public static function Init()
  176. {
  177. $aParams = array
  178. (
  179. "category" => "gui",
  180. "key_type" => "autoincrement",
  181. "name_attcode" => "userid",
  182. "state_attcode" => "",
  183. "reconc_keys" => array(),
  184. "db_table" => "priv_app_preferences",
  185. "db_key_field" => "id",
  186. "db_finalclass_field" => "",
  187. );
  188. MetaModel::Init_Params($aParams);
  189. MetaModel::Init_AddAttribute(new AttributeExternalKey("userid", array("targetclass"=>"User", "allowed_values"=>null, "sql"=>"userid", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));
  190. MetaModel::Init_AddAttribute(new AttributePropertySet("preferences", array("allowed_values"=>null, "sql"=>"preferences", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  191. }
  192. /**
  193. * Overloading this function here to secure a fix done right before the release
  194. * The real fix should be to implement this verb in DBObject
  195. */
  196. public function DBDeleteTracked(CMDBChange $oChange, $bSkipStrongSecurity = null, &$oDeletionPlan = null)
  197. {
  198. $this->DBDelete($oDeletionPlan);
  199. }
  200. }
  201. ?>