5 * Data class for Notice preferences
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.
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.
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/>.
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/
30 class Notice_prefs extends Managed_DataObject
32 public $__table = 'notice_prefs'; // table name
33 public $notice_id; // int(4) primary_key not_null
34 public $namespace; // varchar(191) not_null
35 public $topic; // varchar(191) not_null
37 public $created; // datetime not_null default_0000-00-00%2000%3A00%3A00
38 public $modified; // timestamp not_null default_CURRENT_TIMESTAMP
40 public static function schemaDef()
44 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'user'),
45 'namespace' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'namespace, like pluginname or category'),
46 'topic' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'preference key, i.e. description, age...'),
47 'data' => array('type' => 'blob', 'description' => 'topic data, may be anything'),
48 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
49 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
51 'primary key' => array('notice_id', 'namespace', 'topic'),
52 'foreign keys' => array(
53 'notice_prefs_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
56 'notice_prefs_notice_id_idx' => array('notice_id'),
61 static function getNamespacePrefs(Notice $notice, $namespace, array $topic=array())
64 $prefs = new Notice_prefs();
65 $prefs->notice_id = $notice->getID();
66 $prefs->namespace = $namespace;
69 $prefs = self::pivotGet('notice_id', $notice->getID(), array('namespace'=>$namespace, 'topic'=>$topic));
72 if (empty($prefs->N)) {
73 throw new NoResultException($prefs);
79 static function getNamespace(Notice $notice, $namespace, array $topic=array())
81 $prefs = self::getNamespacePrefs($notice, $namespace, $topic);
82 return $prefs->fetchAll();
85 static function getAll(Notice $notice)
88 $prefs = self::listFind('notice_id', array($notice->getID()));
89 } catch (NoResultException $e) {
94 while ($prefs->fetch()) {
95 if (!isset($list[$prefs->namespace])) {
96 $list[$prefs->namespace] = array();
98 $list[$prefs->namespace][$prefs->topic] = $prefs->data;
103 static function getTopic(Notice $notice, $namespace, $topic) {
104 return self::getByPK(array('notice_id' => $notice->getID(),
105 'namespace' => $namespace,
109 static function getData(Notice $notice, $namespace, $topic, $def=null) {
111 $pref = self::getTopic($notice, $namespace, $topic);
112 } catch (NoResultException $e) {
114 // If no default value was set, continue the exception.
117 // If there was a default value, return that.
123 static function getConfigData(Notice $notice, $namespace, $topic) {
125 $data = self::getData($notice, $namespace, $topic);
126 } catch (NoResultException $e) {
127 $data = common_config($namespace, $topic);
133 * Sets a notice preference based on Notice, namespace and topic
135 * @param Notice $notice Which notice this is for
136 * @param string $namespace Under which namespace (pluginname etc.)
137 * @param string $topic Preference name (think key in key-val store)
138 * @param string $data Data to be put into preference storage, null means delete
140 * @return true if changes are made, false if no action taken
141 * @throws ServerException if preference could not be saved
143 static function setData(Notice $notice, $namespace, $topic, $data=null) {
145 $pref = self::getTopic($notice, $namespace, $topic);
146 if (is_null($data)) {
149 $orig = clone($pref);
151 $pref->update($orig);
154 } catch (NoResultException $e) {
155 if (is_null($data)) {
156 return false; // No action taken
160 $pref = new Notice_prefs();
161 $pref->notice_id = $notice->getID();
162 $pref->namespace = $namespace;
163 $pref->topic = $topic;
165 $pref->created = common_sql_now();
167 if ($pref->insert() === false) {
168 throw new ServerException('Could not save notice preference.');