]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
add more URL-shortening options to othersettings
authorEvan Prodromou <evan@status.net>
Mon, 26 Apr 2010 06:01:11 +0000 (02:01 -0400)
committerEvan Prodromou <evan@status.net>
Mon, 26 Apr 2010 06:01:11 +0000 (02:01 -0400)
actions/othersettings.php
classes/User_urlshortener_prefs.php

index 10e9873b390b16f6cbc7e0d849a0c6d770a9814a..ba3bdd88720e6e416b10332fbd622b8b03064d70 100644 (file)
@@ -98,8 +98,10 @@ class OthersettingsAction extends AccountSettingsAction
         $this->hidden('token', common_session_token());
         $this->elementStart('ul', 'form_data');
 
-        $shorteners = array();
+        $shorteners = array(_('[none]') => array('freeService' => false));
+
         Event::handle('GetUrlShorteners', array(&$shorteners));
+
         $services = array();
         foreach($shorteners as $name=>$value)
         {
@@ -119,8 +121,22 @@ class OthersettingsAction extends AccountSettingsAction
             $this->elementEnd('li');
         }
         $this->elementStart('li');
+        $this->input('maxurllength',
+                     _('URL longer than'),
+                     ($this->arg('maxurllength')) ?
+                     $this->arg('maxurllength') : User_urlshortener_prefs::maxUrlLength($user),
+                     _('URLs longer than this will be shortened.'));
+        $this->elementEnd('li');
+        $this->elementStart('li');
+        $this->input('maxnoticelength',
+                     _('Text longer than'),
+                     ($this->arg('maxnoticelength')) ?
+                     $this->arg('maxnoticelength') : User_urlshortener_prefs::maxNoticeLength($user),
+                     _('URLs in notices longer than this will be shortened.'));
+        $this->elementEnd('li');
+        $this->elementStart('li');
         $this->checkbox('viewdesigns', _('View profile designs'),
-                        $user->viewdesigns, _('Show or hide profile designs.'));
+                         -                        $user->viewdesigns, _('Show or hide profile designs.'));
         $this->elementEnd('li');
         $this->elementEnd('ul');
         $this->submit('save', _('Save'));
@@ -156,6 +172,18 @@ class OthersettingsAction extends AccountSettingsAction
 
         $viewdesigns = $this->boolean('viewdesigns');
 
+        $maxurllength = $this->trimmed('maxurllength');
+
+        if (!Validate::number($maxurllength, array('min' => 0))) {
+            throw new ClientException(_('Invalid number for max url length.'));
+        }
+
+        $maxnoticelength = $this->trimmed('maxnoticelength');
+
+        if (!Validate::number($maxnoticelength, array('min' => 0))) {
+            throw new ClientException(_('Invalid number for max notice length.'));
+        }
+
         $user = common_current_user();
 
         assert(!is_null($user)); // should already be checked
@@ -175,6 +203,32 @@ class OthersettingsAction extends AccountSettingsAction
             return;
         }
 
+        $prefs = User_urlshortener_prefs::getPrefs($user);
+        $orig  = null;
+
+        if (empty($prefs)) {
+            $prefs = new User_urlshortener_prefs();
+
+            $prefs->user_id = $user->id;
+            $prefs->created = common_sql_now();
+        } else {
+            $orig = clone($prefs);
+        }
+
+        $prefs->urlshorteningservice = $urlshorteningservice;
+        $prefs->maxurllength         = $maxurllength;
+        $prefs->maxnoticelength      = $maxnoticelength;
+
+        if (!empty($orig)) {
+            $result = $prefs->update($orig);
+        } else {
+            $result = $prefs->insert();
+        }
+
+        if (!$result) {
+            throw new ServerException(_('Error saving user URL shortening preferences.'));
+        }
+
         $user->query('COMMIT');
 
         $this->showForm(_('Preferences saved.'), true);
index aef39e3719e1c3aed3acdf3a9e22917c3f56b51f..3eb008a672b70b53863b1e1b990ec928b503e3c3 100755 (executable)
@@ -44,4 +44,45 @@ class User_urlshortener_prefs extends Memcached_DataObject
     {
         return array(false, false, false);
     }
+
+    static function maxUrlLength($user)
+    {
+        $def = common_config('url', 'maxlength');
+
+        $prefs = self::getPrefs($user);
+
+        if (empty($prefs)) {
+            return $def;
+        } else {
+            return $prefs->maxurllength;
+        }
+    }
+
+    static function maxNoticeLength($user)
+    {
+        $def = common_config('url', 'maxnoticelength');
+
+        if ($def == -1) {
+            $def = Notice::maxContent();
+        }
+
+        $prefs = self::getPrefs($user);
+
+        if (empty($prefs)) {
+            return $def;
+        } else {
+            return $prefs->maxnoticelength;
+        }
+    }
+
+    static function getPrefs($user)
+    {
+        if (empty($user)) {
+            return null;
+        }
+
+        $prefs = User_urlshortener_prefs::staticGet('user_id', $user->id);
+
+        return $prefs;
+    }
 }