Profile_prefs.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Data class for Profile preferences
  18. *
  19. * @category Data
  20. * @package GNUsocial
  21. * @author Mikael Nordfeldth <mmn@hethane.se>
  22. * @copyright 2013 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. class Profile_prefs extends Managed_DataObject
  27. {
  28. public $__table = 'profile_prefs'; // table name
  29. public $profile_id; // int(4) primary_key not_null
  30. public $namespace; // varchar(191) not_null
  31. public $topic; // varchar(191) not_null
  32. public $data; // text
  33. public $created; // datetime()
  34. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  35. public static function schemaDef()
  36. {
  37. return array(
  38. 'fields' => array(
  39. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'user'),
  40. 'namespace' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'namespace, like pluginname or category'),
  41. 'topic' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'preference key, i.e. description, age...'),
  42. 'data' => array('type' => 'blob', 'description' => 'topic data, may be anything'),
  43. 'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
  44. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  45. ),
  46. 'primary key' => array('profile_id', 'namespace', 'topic'),
  47. 'foreign keys' => array(
  48. 'profile_prefs_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  49. ),
  50. );
  51. }
  52. public static function getNamespacePrefs(Profile $profile, $namespace, array $topic = [])
  53. {
  54. if (empty($topic)) {
  55. $prefs = new Profile_prefs();
  56. $prefs->profile_id = $profile->getID();
  57. $prefs->namespace = $namespace;
  58. $prefs->find();
  59. } else {
  60. $prefs = self::pivotGet('profile_id', $profile->getID(), array('namespace'=>$namespace, 'topic'=>$topic));
  61. }
  62. if (empty($prefs->N)) {
  63. throw new NoResultException($prefs);
  64. }
  65. return $prefs;
  66. }
  67. public static function getNamespace(Profile $profile, $namespace, array $topic = [])
  68. {
  69. $prefs = self::getNamespacePrefs($profile, $namespace, $topic);
  70. return $prefs->fetchAll();
  71. }
  72. public static function getAll(Profile $profile)
  73. {
  74. try {
  75. $prefs = self::listFind('profile_id', array($profile->getID()));
  76. } catch (NoResultException $e) {
  77. return array();
  78. }
  79. $list = array();
  80. while ($prefs->fetch()) {
  81. if (!isset($list[$prefs->namespace])) {
  82. $list[$prefs->namespace] = array();
  83. }
  84. $list[$prefs->namespace][$prefs->topic] = $prefs->data;
  85. }
  86. return $list;
  87. }
  88. public static function getTopic(Profile $profile, $namespace, $topic)
  89. {
  90. return Profile_prefs::getByPK(array('profile_id' => $profile->getID(),
  91. 'namespace' => $namespace,
  92. 'topic' => $topic));
  93. }
  94. public static function getData(Profile $profile, $namespace, $topic, $def = null)
  95. {
  96. try {
  97. $pref = self::getTopic($profile, $namespace, $topic);
  98. } catch (NoResultException $e) {
  99. if ($def === null) {
  100. // If no default value was set, continue the exception.
  101. throw $e;
  102. }
  103. // If there was a default value, return that.
  104. return $def;
  105. }
  106. return $pref->data;
  107. }
  108. public static function getConfigData(Profile $profile, $namespace, $topic)
  109. {
  110. try {
  111. $data = self::getData($profile, $namespace, $topic);
  112. } catch (NoResultException $e) {
  113. $data = common_config($namespace, $topic);
  114. }
  115. return $data;
  116. }
  117. /*
  118. * Sets a profile preference based on Profile, namespace and topic
  119. *
  120. * @param Profile $profile Which profile this is for
  121. * @param string $namespace Under which namespace (pluginname etc.)
  122. * @param string $topic Preference name (think key in key-val store)
  123. * @param string $data Data to be put into preference storage, null means delete
  124. *
  125. * @return true if changes are made, false if no action taken
  126. * @throws ServerException if preference could not be saved
  127. */
  128. public static function setData(Profile $profile, $namespace, $topic, $data = null)
  129. {
  130. try {
  131. $pref = self::getTopic($profile, $namespace, $topic);
  132. if (is_null($data)) {
  133. $pref->delete();
  134. } else {
  135. $orig = clone($pref);
  136. $pref->data = DB_DataObject_Cast::blob($data);
  137. $pref->update($orig);
  138. }
  139. return true;
  140. } catch (NoResultException $e) {
  141. if (is_null($data)) {
  142. return false; // No action taken
  143. }
  144. }
  145. $pref = new Profile_prefs();
  146. $pref->profile_id = $profile->getID();
  147. $pref->namespace = $namespace;
  148. $pref->topic = $topic;
  149. $pref->data = DB_DataObject_Cast::blob($data);
  150. $pref->created = common_sql_now();
  151. if ($pref->insert() === false) {
  152. throw new ServerException('Could not save profile preference.');
  153. }
  154. return true;
  155. }
  156. }