Profile_prefs.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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() not_null default_0000-00-00%2000%3A00%3A00
  34. public $modified; // datetime() 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', 'not null' => true, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'),
  44. 'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', '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. 'indexes' => array(
  51. 'profile_prefs_profile_id_idx' => array('profile_id'),
  52. ),
  53. );
  54. }
  55. public static function getNamespacePrefs(Profile $profile, $namespace, array $topic = [])
  56. {
  57. if (empty($topic)) {
  58. $prefs = new Profile_prefs();
  59. $prefs->profile_id = $profile->getID();
  60. $prefs->namespace = $namespace;
  61. $prefs->find();
  62. } else {
  63. $prefs = self::pivotGet('profile_id', $profile->getID(), array('namespace'=>$namespace, 'topic'=>$topic));
  64. }
  65. if (empty($prefs->N)) {
  66. throw new NoResultException($prefs);
  67. }
  68. return $prefs;
  69. }
  70. public static function getNamespace(Profile $profile, $namespace, array $topic = [])
  71. {
  72. $prefs = self::getNamespacePrefs($profile, $namespace, $topic);
  73. return $prefs->fetchAll();
  74. }
  75. public static function getAll(Profile $profile)
  76. {
  77. try {
  78. $prefs = self::listFind('profile_id', array($profile->getID()));
  79. } catch (NoResultException $e) {
  80. return array();
  81. }
  82. $list = array();
  83. while ($prefs->fetch()) {
  84. if (!isset($list[$prefs->namespace])) {
  85. $list[$prefs->namespace] = array();
  86. }
  87. $list[$prefs->namespace][$prefs->topic] = $prefs->data;
  88. }
  89. return $list;
  90. }
  91. public static function getTopic(Profile $profile, $namespace, $topic)
  92. {
  93. return Profile_prefs::getByPK(array('profile_id' => $profile->getID(),
  94. 'namespace' => $namespace,
  95. 'topic' => $topic));
  96. }
  97. public static function getData(Profile $profile, $namespace, $topic, $def = null)
  98. {
  99. try {
  100. $pref = self::getTopic($profile, $namespace, $topic);
  101. } catch (NoResultException $e) {
  102. if ($def === null) {
  103. // If no default value was set, continue the exception.
  104. throw $e;
  105. }
  106. // If there was a default value, return that.
  107. return $def;
  108. }
  109. return $pref->data;
  110. }
  111. public static function getConfigData(Profile $profile, $namespace, $topic)
  112. {
  113. try {
  114. $data = self::getData($profile, $namespace, $topic);
  115. } catch (NoResultException $e) {
  116. $data = common_config($namespace, $topic);
  117. }
  118. return $data;
  119. }
  120. /*
  121. * Sets a profile preference based on Profile, namespace and topic
  122. *
  123. * @param Profile $profile Which profile this is for
  124. * @param string $namespace Under which namespace (pluginname etc.)
  125. * @param string $topic Preference name (think key in key-val store)
  126. * @param string $data Data to be put into preference storage, null means delete
  127. *
  128. * @return true if changes are made, false if no action taken
  129. * @throws ServerException if preference could not be saved
  130. */
  131. public static function setData(Profile $profile, $namespace, $topic, $data = null)
  132. {
  133. try {
  134. $pref = self::getTopic($profile, $namespace, $topic);
  135. if (is_null($data)) {
  136. $pref->delete();
  137. } else {
  138. $orig = clone($pref);
  139. $pref->data = DB_DataObject_Cast::blob($data);
  140. $pref->update($orig);
  141. }
  142. return true;
  143. } catch (NoResultException $e) {
  144. if (is_null($data)) {
  145. return false; // No action taken
  146. }
  147. }
  148. $pref = new Profile_prefs();
  149. $pref->profile_id = $profile->getID();
  150. $pref->namespace = $namespace;
  151. $pref->topic = $topic;
  152. $pref->data = DB_DataObject_Cast::blob($data);
  153. $pref->created = common_sql_now();
  154. if ($pref->insert() === false) {
  155. throw new ServerException('Could not save profile preference.');
  156. }
  157. return true;
  158. }
  159. }