Profile_prefs.php 6.6 KB

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