Profile_prefs.php 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * Data class for Profile preferences
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  Data
mmn's avatar
mmn committed
23
 * @package   GNUsocial
24 25 26 27 28 29 30 31 32 33
 * @author    Mikael Nordfeldth <mmn@hethane.se>
 * @copyright 2013 Free Software Foundation, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://www.gnu.org/software/social/
 */

class Profile_prefs extends Managed_DataObject
{
    public $__table = 'profile_prefs';       // table name
    public $profile_id;                      // int(4)  primary_key not_null
34 35
    public $namespace;                       // varchar(191)  not_null
    public $topic;                           // varchar(191)  not_null
36 37 38 39 40 41 42 43 44
    public $data;                            // text
    public $created;                         // datetime   not_null default_0000-00-00%2000%3A00%3A00
    public $modified;                        // timestamp   not_null default_CURRENT_TIMESTAMP

    public static function schemaDef()
    {
        return array(
            'fields' => array(
                'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'user'),
45 46
                '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...'),
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
                'data' => array('type' => 'blob', 'description' => 'topic data, may be anything'),
                'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
                'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
            ),
            'primary key' => array('profile_id', 'namespace', 'topic'),
            'foreign keys' => array(
                'profile_prefs_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
            ),
            'indexes' => array(
                'profile_prefs_profile_id_idx' => array('profile_id'),
            ),
        );
    }

    static function getNamespacePrefs(Profile $profile, $namespace, array $topic=array())
    {
        if (empty($topic)) {
            $prefs = new Profile_prefs();
65
            $prefs->profile_id = $profile->getID();
66 67 68
            $prefs->namespace  = $namespace;
            $prefs->find();
        } else {
69
            $prefs = self::pivotGet('profile_id', $profile->getID(), array('namespace'=>$namespace, 'topic'=>$topic));
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        }

        if (empty($prefs->N)) {
            throw new NoResultException($prefs);
        }

        return $prefs;
    }

    static function getNamespace(Profile $profile, $namespace, array $topic=array())
    {
        $prefs = self::getNamespacePrefs($profile, $namespace, $topic);
        return $prefs->fetchAll();
    }

    static function getAll(Profile $profile)
    {
87
        try {
88
            $prefs = self::listFind('profile_id', array($profile->getID()));
89 90 91
        } catch (NoResultException $e) {
            return array();
        }
92 93

        $list = array();
chimo's avatar
chimo committed
94 95 96
        while ($prefs->fetch()) {
            if (!isset($list[$prefs->namespace])) {
                $list[$prefs->namespace] = array();
97
            }
chimo's avatar
chimo committed
98
            $list[$prefs->namespace][$prefs->topic] = $prefs->data;
99 100 101 102 103
        }
        return $list;
    }

    static function getTopic(Profile $profile, $namespace, $topic) {
104 105 106
        return Profile_prefs::getByPK(array('profile_id' => $profile->getID(),
                                            'namespace'  => $namespace,
                                            'topic'      => $topic));
107 108
    }

109 110 111 112 113 114 115 116 117 118 119
    static function getData(Profile $profile, $namespace, $topic, $def=null) {
        try {
            $pref = self::getTopic($profile, $namespace, $topic);
        } catch (NoResultException $e) {
            if ($def === null) {
                // If no default value was set, continue the exception.
                throw $e;
            }
            // If there was a default value, return that.
            return $def;
        }
120 121 122 123 124 125
        return $pref->data;
    }

    static function getConfigData(Profile $profile, $namespace, $topic) {
        try {
            $data = self::getData($profile, $namespace, $topic);
126
        } catch (NoResultException $e) {
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
            $data = common_config($namespace, $topic);
        }
        return $data;
    }

    /*
     * Sets a profile preference based on Profile, namespace and topic
     *
     * @param  Profile $profile   Which profile this is for
     * @param  string  $namespace Under which namespace (pluginname etc.)
     * @param  string  $topic     Preference name (think key in key-val store)
     * @param  string  $data      Data to be put into preference storage, null means delete
     *
     * @return true if changes are made, false if no action taken
     * @throws ServerException if preference could not be saved
     */
    static function setData(Profile $profile, $namespace, $topic, $data=null) {
        try {
            $pref = self::getTopic($profile, $namespace, $topic);
            if (is_null($data)) {
                $pref->delete();
            } else {
                $orig = clone($pref);
                $pref->data = $data;
                $pref->update($orig);
            }
            return true;
        } catch (NoResultException $e) {
            if (is_null($data)) {
                return false; // No action taken
            }
        }

        $pref = new Profile_prefs();
161
        $pref->profile_id = $profile->getID();
162 163 164
        $pref->namespace  = $namespace;
        $pref->topic      = $topic;
        $pref->data       = $data;
165
        $pref->created    = common_sql_now();
166
        
167
        if ($pref->insert() === false) {
168 169 170 171 172
            throw new ServerException('Could not save profile preference.');
        }
        return true;
    }
}