]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Profile_prefs.php
pkeyGet unfortunately returns null (should throw NoResultException) on empty result
[quix0rs-gnu-social.git] / classes / Profile_prefs.php
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
30 class Profile_prefs extends Managed_DataObject
31 {
32     public $__table = 'profile_prefs';       // table name
33     public $profile_id;                      // int(4)  primary_key not_null
34     public $namespace;                       // varchar(255)  not_null
35     public $topic;                           // varchar(255)  not_null
36     public $data;                            // text
37     public $created;                         // datetime   not_null default_0000-00-00%2000%3A00%3A00
38     public $modified;                        // timestamp   not_null default_CURRENT_TIMESTAMP
39
40     public static function schemaDef()
41     {
42         return array(
43             'fields' => array(
44                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'user'),
45                 'namespace' => array('type' => 'varchar', 'length' => 255, 'not null' => true, 'description' => 'namespace, like pluginname or category'),
46                 'topic' => array('type' => 'varchar', 'length' => 255, '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'),
50             ),
51             'primary key' => array('profile_id', 'namespace', 'topic'),
52             'foreign keys' => array(
53                 'profile_prefs_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
54             ),
55             'indexes' => array(
56                 'profile_prefs_profile_id_idx' => array('profile_id'),
57             ),
58         );
59     }
60
61     static function getNamespacePrefs(Profile $profile, $namespace, array $topic=array())
62     {
63         if (empty($topic)) {
64             $prefs = new Profile_prefs();
65             $prefs->profile_id = $profile->id;
66             $prefs->namespace  = $namespace;
67             $prefs->find();
68         } else {
69             $prefs = self::pivotGet('profile_id', $profile->id, array('namespace'=>$namespace, 'topic'=>$topic));
70         }
71
72         if (empty($prefs->N)) {
73             throw new NoResultException($prefs);
74         }
75
76         return $prefs;
77     }
78
79     static function getNamespace(Profile $profile, $namespace, array $topic=array())
80     {
81         $prefs = self::getNamespacePrefs($profile, $namespace, $topic);
82         return $prefs->fetchAll();
83     }
84
85     static function getAll(Profile $profile)
86     {
87         try {
88             $prefs = self::listFind('profile_id', $profile->id);
89         } catch (NoResultException $e) {
90             return array();
91         }
92
93         $list = array();
94         while ($entry = $prefs->fetch()) {
95             if (!isset($list[$entry->namespace])) {
96                 $list[$entry->namespace] = array();
97             }
98             $list[$entry->namespace][$entry->topic] = $entry->data;
99         }
100         return $list;
101     }
102
103     static function getTopic(Profile $profile, $namespace, $topic) {
104         $pref = new Profile_prefs;
105         $pref->profile_id = $profile->id;
106         $pref->namespace  = $namespace;
107         $pref->topic      = $topic;
108
109         if (!$pref->find(true)) {
110             throw new NoResultException($pref);
111         }
112         return $pref;
113     }
114
115     static function getData(Profile $profile, $namespace, $topic) {
116         $pref = self::getTopic($profile, $namespace, $topic);
117         return $pref->data;
118     }
119
120     static function getConfigData(Profile $profile, $namespace, $topic) {
121         try {
122             $data = self::getData($profile, $namespace, $topic);
123         } catch (Exception $e) {
124             $data = common_config($namespace, $topic);
125         }
126         return $data;
127     }
128
129     /*
130      * Sets a profile preference based on Profile, namespace and topic
131      *
132      * @param  Profile $profile   Which profile this is for
133      * @param  string  $namespace Under which namespace (pluginname etc.)
134      * @param  string  $topic     Preference name (think key in key-val store)
135      * @param  string  $data      Data to be put into preference storage, null means delete
136      *
137      * @return true if changes are made, false if no action taken
138      * @throws ServerException if preference could not be saved
139      */
140     static function setData(Profile $profile, $namespace, $topic, $data=null) {
141         try {
142             $pref = self::getTopic($profile, $namespace, $topic);
143             if (is_null($data)) {
144                 $pref->delete();
145             } else {
146                 $orig = clone($pref);
147                 $pref->data = $data;
148                 $pref->update($orig);
149             }
150             return true;
151         } catch (NoResultException $e) {
152             if (is_null($data)) {
153                 return false; // No action taken
154             }
155         }
156
157         $pref = new Profile_prefs();
158         $pref->profile_id = $profile->id;
159         $pref->namespace  = $namespace;
160         $pref->topic      = $topic;
161         $pref->data       = $data;
162         
163         if (!$pref->insert()) {
164             throw new ServerException('Could not save profile preference.');
165         }
166         return true;
167     }
168 }