user.preferences.class.inc.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. * Store and retrieve user's preferences (i.e persistent per user settings)
  20. *
  21. * @copyright Copyright (C) 2010-2017 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 (array_key_exists($sCode, $aPrefs))
  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. if (array_key_exists($sCode, $aPrefs) && ($aPrefs[$sCode] === $sValue))
  72. {
  73. // Do not write it again
  74. }
  75. else
  76. {
  77. $aPrefs[$sCode] = $sValue;
  78. self::$oUserPrefs->Set('preferences', $aPrefs);
  79. self::Save();
  80. }
  81. }
  82. /**
  83. * Clears the value for a given preference (or list of preferences that matches a pattern), and updates the database
  84. * @param string $sPattern Code/Pattern of the properties/preferences to reset
  85. * @param boolean $bPattern Whether or not the supplied code is a PCRE pattern
  86. */
  87. static function UnsetPref($sCodeOrPattern, $bPattern = false)
  88. {
  89. if (self::$oUserPrefs == null)
  90. {
  91. self::Load();
  92. }
  93. $aPrefs = self::$oUserPrefs->Get('preferences');
  94. if ($bPattern)
  95. {
  96. // the supplied code is a pattern, clear all preferences that match
  97. foreach($aPrefs as $sKey => $void)
  98. {
  99. if (preg_match($sCodeOrPattern, $sKey))
  100. {
  101. unset($aPrefs[$sKey]);
  102. }
  103. }
  104. self::$oUserPrefs->Set('preferences', $aPrefs);
  105. }
  106. else
  107. {
  108. unset($aPrefs[$sCodeOrPattern]);
  109. self::$oUserPrefs->Set('preferences', $aPrefs);
  110. }
  111. // Save only if needed
  112. if (self::$oUserPrefs->IsModified())
  113. {
  114. self::Save();
  115. }
  116. }
  117. /**
  118. * Call this function to get all the preferences for the user, packed as a JSON object
  119. * @return string JSON representation of the preferences
  120. */
  121. static function GetAsJSON()
  122. {
  123. if (self::$oUserPrefs == null)
  124. {
  125. self::Load();
  126. }
  127. $aPrefs = self::$oUserPrefs->Get('preferences');
  128. return json_encode($aPrefs);
  129. }
  130. /**
  131. * Call this function if the user has changed (like when doing a logoff...)
  132. */
  133. static public function ResetPreferences()
  134. {
  135. self::$oUserPrefs = null;
  136. }
  137. /**
  138. * Call this function to ERASE all the preferences from the current user
  139. */
  140. static public function ClearPreferences()
  141. {
  142. self::$oUserPrefs = null;
  143. }
  144. static protected function Save()
  145. {
  146. if (self::$oUserPrefs != null)
  147. {
  148. if (self::$oUserPrefs->IsModified())
  149. {
  150. self::$oUserPrefs->DBUpdate();
  151. }
  152. }
  153. }
  154. /**
  155. * Loads the preferences for the current user, creating the record in the database
  156. * if needed
  157. */
  158. static protected function Load()
  159. {
  160. if (self::$oUserPrefs != null) return;
  161. $oSearch = new DBObjectSearch('appUserPreferences');
  162. $oSearch->AddCondition('userid', UserRights::GetUserId(), '=');
  163. $oSet = new DBObjectSet($oSearch);
  164. $oObj = $oSet->Fetch();
  165. if ($oObj == null)
  166. {
  167. // No prefs (yet) for this user, create the object
  168. $oObj = new appUserPreferences();
  169. $oObj->Set('userid', UserRights::GetUserId());
  170. $oObj->Set('preferences', array()); // Default preferences: an empty array
  171. try
  172. {
  173. $oObj->DBInsert();
  174. }
  175. catch(Exception $e)
  176. {
  177. // Ignore errors
  178. }
  179. }
  180. self::$oUserPrefs = $oObj;
  181. }
  182. public static function Init()
  183. {
  184. $aParams = array
  185. (
  186. "category" => "gui",
  187. "key_type" => "autoincrement",
  188. "name_attcode" => "userid",
  189. "state_attcode" => "",
  190. "reconc_keys" => array(),
  191. "db_table" => "priv_app_preferences",
  192. "db_key_field" => "id",
  193. "db_finalclass_field" => "",
  194. );
  195. MetaModel::Init_Params($aParams);
  196. 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())));
  197. MetaModel::Init_AddAttribute(new AttributePropertySet("preferences", array("allowed_values"=>null, "sql"=>"preferences", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  198. }
  199. /**
  200. * Overloading this function here to secure a fix done right before the release
  201. * The real fix should be to implement this verb in DBObject
  202. */
  203. public function DBDeleteTracked(CMDBChange $oChange, $bSkipStrongSecurity = null, &$oDeletionPlan = null)
  204. {
  205. $this->DBDelete($oDeletionPlan);
  206. }
  207. }
  208. ?>