2013-10-06 12:40:53 +01:00
|
|
|
<?php
|
2019-09-11 12:14:40 +01:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social 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.
|
|
|
|
//
|
|
|
|
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2013-10-06 12:40:53 +01:00
|
|
|
/**
|
|
|
|
* Data class for Profile preferences
|
|
|
|
*
|
|
|
|
* @category Data
|
2013-10-14 23:20:36 +01:00
|
|
|
* @package GNUsocial
|
2013-10-06 12:40:53 +01:00
|
|
|
* @author Mikael Nordfeldth <mmn@hethane.se>
|
2019-09-11 12:14:40 +01:00
|
|
|
* @copyright 2013 Free Software Foundation, Inc http://www.fsf.org
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
2013-10-06 12:40:53 +01:00
|
|
|
*/
|
|
|
|
|
2019-09-11 12:14:40 +01:00
|
|
|
defined('GNUSOCIAL') || die();
|
|
|
|
|
2013-10-06 12:40:53 +01:00
|
|
|
class Profile_prefs extends Managed_DataObject
|
|
|
|
{
|
|
|
|
public $__table = 'profile_prefs'; // table name
|
|
|
|
public $profile_id; // int(4) primary_key not_null
|
2015-02-12 17:18:55 +00:00
|
|
|
public $namespace; // varchar(191) not_null
|
|
|
|
public $topic; // varchar(191) not_null
|
2013-10-06 12:40:53 +01:00
|
|
|
public $data; // text
|
2019-07-25 00:43:25 +01:00
|
|
|
public $created; // datetime() not_null default_0000-00-00%2000%3A00%3A00
|
|
|
|
public $modified; // datetime() not_null default_CURRENT_TIMESTAMP
|
2013-10-06 12:40:53 +01:00
|
|
|
|
|
|
|
public static function schemaDef()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'fields' => array(
|
|
|
|
'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'user'),
|
2015-02-12 17:18:55 +00:00
|
|
|
'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...'),
|
2013-10-06 12:40:53 +01:00
|
|
|
'data' => array('type' => 'blob', 'description' => 'topic data, may be anything'),
|
2019-07-25 00:43:25 +01:00
|
|
|
'created' => array('type' => 'datetime', 'not null' => true, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'),
|
|
|
|
'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'),
|
2013-10-06 12:40:53 +01:00
|
|
|
),
|
|
|
|
'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'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-11 12:14:40 +01:00
|
|
|
public static function getNamespacePrefs(Profile $profile, $namespace, array $topic = [])
|
2013-10-06 12:40:53 +01:00
|
|
|
{
|
|
|
|
if (empty($topic)) {
|
|
|
|
$prefs = new Profile_prefs();
|
2015-06-04 21:26:31 +01:00
|
|
|
$prefs->profile_id = $profile->getID();
|
2013-10-06 12:40:53 +01:00
|
|
|
$prefs->namespace = $namespace;
|
|
|
|
$prefs->find();
|
|
|
|
} else {
|
2015-06-04 21:26:31 +01:00
|
|
|
$prefs = self::pivotGet('profile_id', $profile->getID(), array('namespace'=>$namespace, 'topic'=>$topic));
|
2013-10-06 12:40:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($prefs->N)) {
|
|
|
|
throw new NoResultException($prefs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $prefs;
|
|
|
|
}
|
|
|
|
|
2019-09-11 12:14:40 +01:00
|
|
|
public static function getNamespace(Profile $profile, $namespace, array $topic = [])
|
2013-10-06 12:40:53 +01:00
|
|
|
{
|
|
|
|
$prefs = self::getNamespacePrefs($profile, $namespace, $topic);
|
|
|
|
return $prefs->fetchAll();
|
|
|
|
}
|
|
|
|
|
2019-09-11 12:14:40 +01:00
|
|
|
public static function getAll(Profile $profile)
|
2013-10-06 12:40:53 +01:00
|
|
|
{
|
2013-10-06 15:37:51 +01:00
|
|
|
try {
|
2015-12-17 14:55:39 +00:00
|
|
|
$prefs = self::listFind('profile_id', array($profile->getID()));
|
2013-10-06 15:37:51 +01:00
|
|
|
} catch (NoResultException $e) {
|
|
|
|
return array();
|
|
|
|
}
|
2013-10-06 12:40:53 +01:00
|
|
|
|
|
|
|
$list = array();
|
2015-12-17 14:58:06 +00:00
|
|
|
while ($prefs->fetch()) {
|
|
|
|
if (!isset($list[$prefs->namespace])) {
|
|
|
|
$list[$prefs->namespace] = array();
|
2013-10-06 12:40:53 +01:00
|
|
|
}
|
2015-12-17 14:58:06 +00:00
|
|
|
$list[$prefs->namespace][$prefs->topic] = $prefs->data;
|
2013-10-06 12:40:53 +01:00
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2019-09-11 12:14:40 +01:00
|
|
|
public static function getTopic(Profile $profile, $namespace, $topic)
|
|
|
|
{
|
2015-06-04 21:26:31 +01:00
|
|
|
return Profile_prefs::getByPK(array('profile_id' => $profile->getID(),
|
|
|
|
'namespace' => $namespace,
|
|
|
|
'topic' => $topic));
|
2013-10-06 12:40:53 +01:00
|
|
|
}
|
|
|
|
|
2019-09-11 12:14:40 +01:00
|
|
|
public static function getData(Profile $profile, $namespace, $topic, $def = null)
|
|
|
|
{
|
2014-07-13 15:26:08 +01:00
|
|
|
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;
|
|
|
|
}
|
2013-10-06 12:40:53 +01:00
|
|
|
return $pref->data;
|
|
|
|
}
|
|
|
|
|
2019-09-11 12:14:40 +01:00
|
|
|
public static function getConfigData(Profile $profile, $namespace, $topic)
|
|
|
|
{
|
2013-10-06 12:40:53 +01:00
|
|
|
try {
|
|
|
|
$data = self::getData($profile, $namespace, $topic);
|
2013-11-02 12:05:08 +00:00
|
|
|
} catch (NoResultException $e) {
|
2013-10-06 12:40:53 +01:00
|
|
|
$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
|
|
|
|
*/
|
2019-09-11 12:14:40 +01:00
|
|
|
public static function setData(Profile $profile, $namespace, $topic, $data = null)
|
|
|
|
{
|
2013-10-06 12:40:53 +01:00
|
|
|
try {
|
|
|
|
$pref = self::getTopic($profile, $namespace, $topic);
|
|
|
|
if (is_null($data)) {
|
|
|
|
$pref->delete();
|
|
|
|
} else {
|
|
|
|
$orig = clone($pref);
|
2019-09-11 12:14:40 +01:00
|
|
|
$pref->data = DB_DataObject_Cast::blob($data);
|
2013-10-06 12:40:53 +01:00
|
|
|
$pref->update($orig);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch (NoResultException $e) {
|
|
|
|
if (is_null($data)) {
|
|
|
|
return false; // No action taken
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$pref = new Profile_prefs();
|
2015-06-04 21:26:31 +01:00
|
|
|
$pref->profile_id = $profile->getID();
|
2013-10-06 12:40:53 +01:00
|
|
|
$pref->namespace = $namespace;
|
|
|
|
$pref->topic = $topic;
|
2019-09-11 12:14:40 +01:00
|
|
|
$pref->data = DB_DataObject_Cast::blob($data);
|
2014-07-13 16:09:18 +01:00
|
|
|
$pref->created = common_sql_now();
|
2013-10-06 12:40:53 +01:00
|
|
|
|
2013-11-02 12:05:08 +00:00
|
|
|
if ($pref->insert() === false) {
|
2013-10-06 12:40:53 +01:00
|
|
|
throw new ServerException('Could not save profile preference.');
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|