From c3d46b81a8f6abec7e00b4f3714e67d2dbcad247 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Sun, 6 Oct 2013 13:40:53 +0200 Subject: [PATCH] Added Profile_prefs class for profile preferences Profile_prefs aims to consolidate all the profile preferences into a single table. Otherwise we end up with a bajillion *_prefs classes, like User_urlshortener_prefs, or new fields in existing User/Profile classes, like 'urlshorteningservice', 'homepage', 'phone_number', 'pet_name' etc. Eventually we should migrate as many user-settable preferences as we can into this system. The data in Profile_prefs is organized by: * profile_id Identify the current Profile. * namespace Which plugin/section the preference is for. * topic Preference name (like 'homepage') * data Preference data (like 'https://gnu.org/') The names 'topic' and 'data' are because 'key' and 'value' may be rather ambigous when dealing with our DB_DataObject classes etc. --- classes/Profile_prefs.php | 160 ++++++++++++++++++++++++++++++++++++++ db/core.php | 1 + 2 files changed, 161 insertions(+) create mode 100644 classes/Profile_prefs.php diff --git a/classes/Profile_prefs.php b/classes/Profile_prefs.php new file mode 100644 index 0000000000..8589485286 --- /dev/null +++ b/classes/Profile_prefs.php @@ -0,0 +1,160 @@ +. + * + * @category Data + * @package GNUSocial + * @author Mikael Nordfeldth + * @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 + public $namespace; // varchar(255) not_null + public $topic; // varchar(255) not_null + 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'), + 'namespace' => array('type' => 'varchar', 'length' => 255, 'not null' => true, 'description' => 'namespace, like pluginname or category'), + 'topic' => array('type' => 'varchar', 'length' => 255, 'not null' => true, 'description' => 'preference key, i.e. description, age...'), + '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(); + $prefs->profile_id = $profile->id; + $prefs->namespace = $namespace; + $prefs->find(); + } else { + $prefs = self::pivotGet('profile_id', $profile->id, array('namespace'=>$namespace, 'topic'=>$topic)); + } + + 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) + { + $prefs = self::listFind('profile_id', $profile->id); + + $list = array(); + while ($entry = $prefs->fetch()) { + if (!isset($list[$entry->namespace])) { + $list[$entry->namespace] = array(); + } + $list[$entry->namespace][$entry->topic] = $entry->data; + } + return $list; + } + + static function getTopic(Profile $profile, $namespace, $topic) { + $pref = self::pkeyGet(array('profile_id'=>$profile->id, 'namespace'=>$namespace, 'topic'=>$topic)); + if (is_null($pref)) { + throw new NoResultException($pref); + } + return $pref; + } + + static function getData(Profile $profile, $namespace, $topic) { + $pref = self::getTopic($profile, $namespace, $topic); + return $pref->data; + } + + static function getConfigData(Profile $profile, $namespace, $topic) { + try { + $data = self::getData($profile, $namespace, $topic); + } catch (Exception $e) { + $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(); + $pref->profile_id = $profile->id; + $pref->namespace = $namespace; + $pref->topic = $topic; + $pref->data = $data; + + if (!$pref->insert()) { + throw new ServerException('Could not save profile preference.'); + } + return true; + } +} diff --git a/db/core.php b/db/core.php index d132b56779..da2f5fde5f 100644 --- a/db/core.php +++ b/db/core.php @@ -59,6 +59,7 @@ $classes = array('Schema_version', 'Invitation', 'Message', // 'Notice_inbox', + 'Profile_prefs', 'Profile_tag', 'Profile_list', 'Profile_tag_subscription',