123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- defined('GNUSOCIAL') || die();
- class Notice_prefs extends Managed_DataObject
- {
- public $__table = 'notice_prefs';
- public $notice_id;
- public $namespace;
- public $topic;
- public $data;
- public $created;
- public $modified;
- public static function schemaDef()
- {
- return array(
- 'fields' => array(
- 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'user'),
- 'namespace' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'namespace, like pluginname or category'),
- 'topic' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'preference key, i.e. description, age...'),
- 'data' => array('type' => 'blob', 'description' => 'topic data, may be anything'),
- 'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
- 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
- ),
- 'primary key' => array('notice_id', 'namespace', 'topic'),
- 'foreign keys' => array(
- 'notice_prefs_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
- ),
- );
- }
- public static function getNamespacePrefs(Notice $notice, $namespace, array $topic = [])
- {
- if (empty($topic)) {
- $prefs = new Notice_prefs();
- $prefs->notice_id = $notice->getID();
- $prefs->namespace = $namespace;
- $prefs->find();
- } else {
- $prefs = self::pivotGet('notice_id', $notice->getID(), array('namespace'=>$namespace, 'topic'=>$topic));
- }
- if (empty($prefs->N)) {
- throw new NoResultException($prefs);
- }
- return $prefs;
- }
- public static function getNamespace(Notice $notice, $namespace, array $topic = [])
- {
- $prefs = self::getNamespacePrefs($notice, $namespace, $topic);
- return $prefs->fetchAll();
- }
- public static function getAll(Notice $notice)
- {
- try {
- $prefs = self::listFind('notice_id', array($notice->getID()));
- } catch (NoResultException $e) {
- return array();
- }
- $list = array();
- while ($prefs->fetch()) {
- if (!isset($list[$prefs->namespace])) {
- $list[$prefs->namespace] = array();
- }
- $list[$prefs->namespace][$prefs->topic] = $prefs->data;
- }
- return $list;
- }
- public static function getTopic(Notice $notice, $namespace, $topic)
- {
- return self::getByPK([
- 'notice_id' => $notice->getID(),
- 'namespace' => $namespace,
- 'topic' => $topic,
- ]);
- }
- public static function getData(Notice $notice, $namespace, $topic, $def = null)
- {
- try {
- $pref = self::getTopic($notice, $namespace, $topic);
- } catch (NoResultException $e) {
- if ($def === null) {
-
- throw $e;
- }
-
- return $def;
- }
- return $pref->data;
- }
- public static function getConfigData(Notice $notice, $namespace, $topic)
- {
- try {
- $data = self::getData($notice, $namespace, $topic);
- } catch (NoResultException $e) {
- $data = common_config($namespace, $topic);
- }
- return $data;
- }
-
- public static function setData(Notice $notice, $namespace, $topic, $data = null)
- {
- try {
- $pref = self::getTopic($notice, $namespace, $topic);
- if (is_null($data)) {
- $pref->delete();
- } else {
- $orig = clone($pref);
- $pref->data = DB_DataObject_Cast::blob($data);
- $pref->update($orig);
- }
- return true;
- } catch (NoResultException $e) {
- if (is_null($data)) {
- return false;
- }
- }
- $pref = new Notice_prefs();
- $pref->notice_id = $notice->getID();
- $pref->namespace = $namespace;
- $pref->topic = $topic;
- $pref->data = DB_DataObject_Cast::blob($data);
- $pref->created = common_sql_now();
-
- if ($pref->insert() === false) {
- throw new ServerException('Could not save notice preference.');
- }
- return true;
- }
- }
|