Notice_prefs.php 6.4 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 Notice preferences
  18. *
  19. * @category Data
  20. * @package GNUsocial
  21. * @author Mikael Nordfeldth <mmn@hethane.se>
  22. * @author Diogo Cordeiro <diogo@fc.up.pt>
  23. * @copyright 2013 Free Software Foundation, Inc http://www.fsf.org
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. class Notice_prefs extends Managed_DataObject
  28. {
  29. public $__table = 'notice_prefs'; // table name
  30. public $notice_id; // int(4) primary_key not_null
  31. public $namespace; // varchar(191) not_null
  32. public $topic; // varchar(191) not_null
  33. public $data; // text
  34. public $created; // datetime()
  35. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  36. public static function schemaDef()
  37. {
  38. return array(
  39. 'fields' => array(
  40. 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'user'),
  41. 'namespace' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'namespace, like pluginname or category'),
  42. 'topic' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'preference key, i.e. description, age...'),
  43. 'data' => array('type' => 'blob', 'description' => 'topic data, may be anything'),
  44. 'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
  45. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  46. ),
  47. 'primary key' => array('notice_id', 'namespace', 'topic'),
  48. 'foreign keys' => array(
  49. 'notice_prefs_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
  50. ),
  51. );
  52. }
  53. public static function getNamespacePrefs(Notice $notice, $namespace, array $topic = [])
  54. {
  55. if (empty($topic)) {
  56. $prefs = new Notice_prefs();
  57. $prefs->notice_id = $notice->getID();
  58. $prefs->namespace = $namespace;
  59. $prefs->find();
  60. } else {
  61. $prefs = self::pivotGet('notice_id', $notice->getID(), array('namespace'=>$namespace, 'topic'=>$topic));
  62. }
  63. if (empty($prefs->N)) {
  64. throw new NoResultException($prefs);
  65. }
  66. return $prefs;
  67. }
  68. public static function getNamespace(Notice $notice, $namespace, array $topic = [])
  69. {
  70. $prefs = self::getNamespacePrefs($notice, $namespace, $topic);
  71. return $prefs->fetchAll();
  72. }
  73. public static function getAll(Notice $notice)
  74. {
  75. try {
  76. $prefs = self::listFind('notice_id', array($notice->getID()));
  77. } catch (NoResultException $e) {
  78. return array();
  79. }
  80. $list = array();
  81. while ($prefs->fetch()) {
  82. if (!isset($list[$prefs->namespace])) {
  83. $list[$prefs->namespace] = array();
  84. }
  85. $list[$prefs->namespace][$prefs->topic] = $prefs->data;
  86. }
  87. return $list;
  88. }
  89. public static function getTopic(Notice $notice, $namespace, $topic)
  90. {
  91. return self::getByPK([
  92. 'notice_id' => $notice->getID(),
  93. 'namespace' => $namespace,
  94. 'topic' => $topic,
  95. ]);
  96. }
  97. public static function getData(Notice $notice, $namespace, $topic, $def = null)
  98. {
  99. try {
  100. $pref = self::getTopic($notice, $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(Notice $notice, $namespace, $topic)
  112. {
  113. try {
  114. $data = self::getData($notice, $namespace, $topic);
  115. } catch (NoResultException $e) {
  116. $data = common_config($namespace, $topic);
  117. }
  118. return $data;
  119. }
  120. /*
  121. * Sets a notice preference based on Notice, namespace and topic
  122. *
  123. * @param Notice $notice Which notice 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(Notice $notice, $namespace, $topic, $data = null)
  132. {
  133. try {
  134. $pref = self::getTopic($notice, $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 Notice_prefs();
  149. $pref->notice_id = $notice->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 notice preference.');
  156. }
  157. return true;
  158. }
  159. }