user.preferences.class.inc.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. * Store and retrieve user's preferences (i.e persistent per user settings)
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once('../core/dbobject.class.php');
  25. require_once('../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. * Call this function to get all the preferences for the user, packed as a JSON object
  77. * @return string JSON representation of the preferences
  78. */
  79. static function GetAsJSON()
  80. {
  81. if (self::$oUserPrefs == null)
  82. {
  83. self::Load();
  84. }
  85. $aPrefs = self::$oUserPrefs->Get('preferences');
  86. return json_encode($aPrefs);
  87. }
  88. /**
  89. * Call this function if the user has changed (like when doing a logoff...)
  90. */
  91. static public function Reset()
  92. {
  93. self::$oUserPrefs = null;
  94. }
  95. /**
  96. * Call this function to ERASE all the preferences from the current user
  97. */
  98. static public function ClearPreferences()
  99. {
  100. self::$oUserPrefs = null;
  101. }
  102. static protected function Save()
  103. {
  104. if (self::$oUserPrefs != null)
  105. {
  106. if (self::$oUserPrefs->IsModified())
  107. {
  108. self::$oUserPrefs->DBUpdate();
  109. }
  110. }
  111. }
  112. /**
  113. * Loads the preferences for the current user, creating the record in the database
  114. * if needed
  115. */
  116. static protected function Load()
  117. {
  118. if (self::$oUserPrefs != null) return;
  119. $oSearch = new DBObjectSearch('appUserPreferences');
  120. $oSearch->AddCondition('userid', UserRights::GetUserId(), '=');
  121. $oSet = new DBObjectSet($oSearch);
  122. $oObj = $oSet->Fetch();
  123. if ($oObj == null)
  124. {
  125. // No prefs (yet) for this user, create the object
  126. $oObj = new appUserPreferences();
  127. $oObj->Set('userid', UserRights::GetUserId());
  128. $oObj->Set('preferences', array()); // Default preferences: an empty array
  129. $oObj->DBInsert();
  130. }
  131. self::$oUserPrefs = $oObj;
  132. }
  133. public static function Init()
  134. {
  135. $aParams = array
  136. (
  137. "category" => "gui,alwaysreadable",
  138. "key_type" => "autoincrement",
  139. "name_attcode" => "userid",
  140. "state_attcode" => "",
  141. "reconc_keys" => array(),
  142. "db_table" => "priv_app_preferences",
  143. "db_key_field" => "id",
  144. "db_finalclass_field" => "",
  145. );
  146. MetaModel::Init_Params($aParams);
  147. MetaModel::Init_AddAttribute(new AttributeExternalKey("userid", array("targetclass"=>"User", "allowed_values"=>null, "sql"=>"userid", "is_null_allowed"=>true, "on_target_delete"=>DEL_MANUAL, "depends_on"=>array())));
  148. MetaModel::Init_AddAttribute(new AttributePropertySet("preferences", array("allowed_values"=>null, "sql"=>"preferences", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  149. }
  150. }
  151. ?>