Notice_prefs.php 6.7 KB

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