]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
let users set their own profile tags from profilesettings
authorEvan Prodromou <evan@prodromou.name>
Thu, 20 Nov 2008 21:48:21 +0000 (16:48 -0500)
committerEvan Prodromou <evan@prodromou.name>
Thu, 20 Nov 2008 21:48:21 +0000 (16:48 -0500)
darcs-hash:20081120214821-84dde-c8569ef645b389de545f78bf01a270f28b871f02.gz

actions/profilesettings.php
classes/Profile_tag.php
classes/User.php
classes/laconica.ini
lib/util.php

index 2ae736087635b16628f740a2fa3f4835f79143d3..7f12de9fef42975686f860c0cb2c7f3aeebfb6e6 100644 (file)
@@ -67,10 +67,9 @@ class ProfilesettingsAction extends SettingsAction {
                                                                                   'action' =>
                                                                                   common_local_url('profilesettings')));
                common_hidden('token', common_session_token());
-
-
-
+               
                # too much common patterns here... abstractable?
+               
                common_input('nickname', _('Nickname'),
                                         ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
                                         _('1-64 lowercase letters or numbers, no punctuation or spaces'));
@@ -85,6 +84,9 @@ class ProfilesettingsAction extends SettingsAction {
                common_input('location', _('Location'),
                                         ($this->arg('location')) ? $this->arg('location') : $profile->location,
                                         _('Where you are, like "City, State (or Region), Country"'));
+               common_input('tags', _('Tags'),
+                                        ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
+                                        _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated'));
 
                $language = common_language();
                common_dropdown('language', _('Language'), get_nice_language_list(), _('Preferred language'), TRUE, $language);
@@ -194,7 +196,8 @@ class ProfilesettingsAction extends SettingsAction {
                $autosubscribe = $this->boolean('autosubscribe');
                $language = $this->trimmed('language');
                $timezone = $this->trimmed('timezone');
-
+               $tagstring = $this->trimmed('tags');
+               
                # Some validation
 
                if (!Validate::string($nickname, array('min_length' => 1,
@@ -226,8 +229,18 @@ class ProfilesettingsAction extends SettingsAction {
                        return;
         } else if (!is_null($language) && strlen($language) > 50) {
                $this->show_form(_('Language is too long (max 50 chars).'));
+                       return;
                }
 
+               $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
+
+               foreach ($tags as $tag) {
+                       if (!common_valid_profile_tag($tag)) {
+                               $this->show_form(sprintf(_('Invalid tag: "%s"'), $tag));
+                               return;
+                       }
+               }
+               
                $user = common_current_user();
 
                $user->query('BEGIN');
@@ -300,6 +313,15 @@ class ProfilesettingsAction extends SettingsAction {
                        return;
                }
 
+               # Set the user tags
+               
+               $result = $user->setTags($tags);
+
+               if (!$result) {
+                       common_server_error(_('Couldn\'t save tags.'));
+                       return;
+               }
+               
                $user->query('COMMIT');
 
                common_broadcast_profile($profile);
index f2d6ec6cf8a24f3b552482996375f0a7d702ee30..464a817fc8878e275cb4fef5983f31c11321896d 100644 (file)
@@ -20,4 +20,67 @@ class Profile_tag extends Memcached_DataObject
 
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
+
+       static function getTags($tagger, $tagged) {
+               
+               $tags = array();
+
+               # XXX: store this in memcached
+               
+               $profile_tag = new Profile_tag();
+               $profile_tag->tagger = $tagger;
+               $profile_tag->tagged = $tagged;
+               
+               $profile_tag->find();
+               
+               while ($profile_tag->fetch()) {
+                       $tags[] = $profile_tag->tag;
+               }
+               
+               $profile_tag->free();
+               
+               return $tags;
+       }
+       
+       static function setTags($tagger, $tagged, $tags) {
+               
+               $oldtags = Profile_tag::getTags($tagger, $tagged);
+               
+               # Delete stuff that's old that not in new
+               
+               $to_delete = array_diff($oldtags, $newtags);
+               
+               # Insert stuff that's in new and not in old
+               
+               $to_insert = array_diff($newtags, $oldtags);
+               
+               $profile_tag = new Profile_tag();
+               
+               $profile_tag->tagger = $tagger;
+               $profile_tag->tagged = $tagged;
+               
+               $profile_tag->query('BEGIN');
+               
+               foreach ($to_delete as $deltag) {
+                       $profile_tag->tag = $deltag;
+                       $result = $profile_tag->delete();
+                       if (!$result) {
+                               common_log_db_error($profile_tag, 'DELETE', __FILE__);
+                               return false;
+                       }
+               }
+               
+               foreach ($to_insert as $instag) {
+                       $profile_tag->tag = $instag;
+                       $result = $profile_tag->insert();
+                       if (!$result) {
+                               common_log_db_error($profile_tag, 'INSERT', __FILE__);
+                               return false;
+                       }
+               }
+               
+               $profile_tag->query('COMMIT');
+               
+               return true;
+       }
 }
index 45b13321380ff8d356d686202bf6bd1d76178815..696c85e7373b45c4f4178403a28d8150c9c5173e 100644 (file)
@@ -401,4 +401,12 @@ class User extends Memcached_DataObject
                        $cache->delete(common_cache_key('user:faves:'.$this->id).';last');
                }
        }
+       
+       function getSelfTags() {
+               return Profile_tag::getTags($this->id, $this->id);
+       }
+       
+       function setSelfTags($newtags) {
+               return Profile_tag::setTags($this->id, $this->id, $newtags);
+       }
 }
index 470c0eaf1bee086b4017ec75026bc2494a5e25e9..ccf7e2f873de6aed491aa179be5406270eb33770 100644 (file)
@@ -155,7 +155,6 @@ id = N
 [notice_inbox]
 user_id = 129
 notice_id = 129
-source = 17
 created = 142
 source = 17
 
index efce283a91d25e47d455c6cc481ad574a4a97754..13fcfcc70ad56f4f241b650d823c71bb641f9575 100644 (file)
@@ -844,6 +844,10 @@ function common_canonical_tag($tag) {
        return strtolower(str_replace(array('-', '_', '.'), '', $tag));
 }
 
+function common_valid_profile_tag($str) {
+       return preg_match('/^[A-Za-z0-9_\-\.]{1,64}$/', $str);
+}
+
 function common_at_link($sender_id, $nickname) {
        $sender = Profile::staticGet($sender_id);
        $recipient = common_relative_profile($sender, common_canonical_nickname($nickname));