]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Profile_prefs.php
Profile_prefs::getAll fix call to listFind
[quix0rs-gnu-social.git] / classes / Profile_prefs.php
index d7da8813e313dbdcf88228ca5879d2a5e772bdfc..996fc1b6b6ebbd26c11ad73d1b73a943f5a4396c 100644 (file)
@@ -20,7 +20,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  * @category  Data
- * @package   GNUSocial
+ * @package   GNUsocial
  * @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
@@ -31,8 +31,8 @@ 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 $namespace;                       // varchar(191)  not_null
+    public $topic;                           // varchar(191)  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
@@ -42,8 +42,8 @@ class Profile_prefs extends Managed_DataObject
         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...'),
+                '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', 'not null' => true, 'description' => 'date this record was created'),
                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
@@ -62,11 +62,11 @@ class Profile_prefs extends Managed_DataObject
     {
         if (empty($topic)) {
             $prefs = new Profile_prefs();
-            $prefs->profile_id = $profile->id;
+            $prefs->profile_id = $profile->getID();
             $prefs->namespace  = $namespace;
             $prefs->find();
         } else {
-            $prefs = self::pivotGet('profile_id', $profile->id, array('namespace'=>$namespace, 'topic'=>$topic));
+            $prefs = self::pivotGet('profile_id', $profile->getID(), array('namespace'=>$namespace, 'topic'=>$topic));
         }
 
         if (empty($prefs->N)) {
@@ -85,7 +85,7 @@ class Profile_prefs extends Managed_DataObject
     static function getAll(Profile $profile)
     {
         try {
-            $prefs = self::listFind('profile_id', $profile->id);
+            $prefs = self::listFind('profile_id', array($profile->getID()));
         } catch (NoResultException $e) {
             return array();
         }
@@ -101,22 +101,29 @@ class Profile_prefs extends Managed_DataObject
     }
 
     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;
+        return Profile_prefs::getByPK(array('profile_id' => $profile->getID(),
+                                            'namespace'  => $namespace,
+                                            'topic'      => $topic));
     }
 
-    static function getData(Profile $profile, $namespace, $topic) {
-        $pref = self::getTopic($profile, $namespace, $topic);
+    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;
+        }
         return $pref->data;
     }
 
     static function getConfigData(Profile $profile, $namespace, $topic) {
         try {
             $data = self::getData($profile, $namespace, $topic);
-        } catch (Exception $e) {
+        } catch (NoResultException $e) {
             $data = common_config($namespace, $topic);
         }
         return $data;
@@ -151,12 +158,13 @@ class Profile_prefs extends Managed_DataObject
         }
 
         $pref = new Profile_prefs();
-        $pref->profile_id = $profile->id;
+        $pref->profile_id = $profile->getID();
         $pref->namespace  = $namespace;
         $pref->topic      = $topic;
         $pref->data       = $data;
+        $pref->created    = common_sql_now();
         
-        if (!$pref->insert()) {
+        if ($pref->insert() === false) {
             throw new ServerException('Could not save profile preference.');
         }
         return true;