]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/tagother.php
Merge remote branch 'gitorious/1.0.x' into 1.0.x
[quix0rs-gnu-social.git] / actions / tagother.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
23
24 // @todo FIXME: documentation missing.
25 class TagotherAction extends Action
26 {
27     var $profile = null;
28     var $error = null;
29
30     function prepare($args)
31     {
32         parent::prepare($args);
33         if (!common_logged_in()) {
34             $this->clientError(_('Not logged in.'), 403);
35             return false;
36         }
37
38         $id = $this->trimmed('id');
39         if (!$id) {
40             $this->clientError(_('No ID argument.'));
41             return false;
42         }
43
44         $this->profile = Profile::staticGet('id', $id);
45
46         if (!$this->profile) {
47             $this->clientError(_('No profile with that ID.'));
48             return false;
49         }
50
51         return true;
52     }
53
54     function handle($args)
55     {
56         parent::handle($args);
57         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
58             $this->saveTags();
59         } else {
60             $this->showForm($profile);
61         }
62     }
63
64     function title()
65     {
66         return sprintf(_('Tag %s'), $this->profile->nickname);
67     }
68
69     function showForm($error=null)
70     {
71         $this->error = $error;
72         $this->showPage();
73     }
74
75     function showContent()
76     {
77         $this->elementStart('div', 'entity_profile vcard author');
78         $this->element('h2', null, _('User profile'));
79
80         $avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
81         $this->element('img', array('src' => ($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_PROFILE_SIZE),
82                                     'class' => 'photo avatar entity_depiction',
83                                     'width' => AVATAR_PROFILE_SIZE,
84                                     'height' => AVATAR_PROFILE_SIZE,
85                                     'alt' =>
86                                     ($this->profile->fullname) ? $this->profile->fullname :
87                                     $this->profile->nickname));
88
89         $this->element('a', array('href' => $this->profile->profileurl,
90                                   'class' => 'entity_nickname nickname'),
91                        $this->profile->nickname);
92
93         if ($this->profile->fullname) {
94             $this->element('div', 'fn entity_fn', $this->profile->fullname);
95         }
96
97         if ($this->profile->location) {
98             $this->element('div', 'label entity_location', $this->profile->location);
99         }
100
101         if ($this->profile->homepage) {
102             $this->element('a', array('href' => $this->profile->homepage,
103                                       'rel' => 'me',
104                                       'class' => 'url entity_url'),
105                            $this->profile->homepage);
106         }
107
108         if ($this->profile->bio) {
109             $this->element('div', 'note entity_note', $this->profile->bio);
110         }
111
112         $this->elementEnd('div');
113
114         $this->elementStart('form', array('method' => 'post',
115                                            'id' => 'form_tag_user',
116                                            'class' => 'form_settings',
117                                            'name' => 'tagother',
118                                            'action' => common_local_url('tagother', array('id' => $this->profile->id))));
119
120         $this->elementStart('fieldset');
121         $this->element('legend', null, _('Tag user'));
122         $this->hidden('token', common_session_token());
123         $this->hidden('id', $this->profile->id);
124
125         $user = common_current_user();
126
127         $this->elementStart('ul', 'form_data');
128         $this->elementStart('li');
129         $this->input('tags', _('Tags'),
130                      ($this->arg('tags')) ? $this->arg('tags') : implode(' ', Profile_tag::getTags($user->id, $this->profile->id)),
131                      _('Tags for this user (letters, numbers, -, ., and _), separated by commas or spaces.'));
132         $this->elementEnd('li');
133         $this->elementEnd('ul');
134         $this->submit('save', _m('BUTTON','Save'));
135         $this->elementEnd('fieldset');
136         $this->elementEnd('form');
137     }
138
139     function saveTags()
140     {
141         $id = $this->trimmed('id');
142         $tagstring = $this->trimmed('tags');
143         $token = $this->trimmed('token');
144
145         if (!$token || $token != common_session_token()) {
146             $this->showForm(_('There was a problem with your session token. '.
147                               'Try again, please.'));
148             return;
149         }
150
151         if (is_string($tagstring) && strlen($tagstring) > 0) {
152
153             $tags = array_map('common_canonical_tag',
154                               preg_split('/[\s,]+/', $tagstring));
155
156             foreach ($tags as $tag) {
157                 if (!common_valid_profile_tag($tag)) {
158                     // TRANS: Form validation error when entering an invalid tag.
159                     // TRANS: %s is the invalid tag.
160                     $this->showForm(sprintf(_('Invalid tag: "%s".'), $tag));
161                     return;
162                 }
163             }
164         } else {
165             $tags = array();
166         }
167
168         $user = common_current_user();
169
170         if (!Subscription::pkeyGet(array('subscriber' => $user->id,
171                                          'subscribed' => $this->profile->id)) &&
172             !Subscription::pkeyGet(array('subscriber' => $this->profile->id,
173                                          'subscribed' => $user->id)))
174         {
175             $this->clientError(_('You can only tag people you are subscribed to or who are subscribed to you.'));
176             return;
177         }
178
179         $result = Profile_tag::setTags($user->id, $this->profile->id, $tags);
180
181         if (!$result) {
182             $this->clientError(_('Could not save tags.'));
183             return;
184         }
185
186         $action = $user->isSubscribed($this->profile) ? 'subscriptions' : 'subscribers';
187
188         if ($this->boolean('ajax')) {
189             $this->startHTML('text/xml;charset=utf-8');
190             $this->elementStart('head');
191             $this->element('title', null, _('Tags'));
192             $this->elementEnd('head');
193             $this->elementStart('body');
194             $this->elementStart('p', 'subtags');
195             foreach ($tags as $tag) {
196                 $this->element('a', array('href' => common_local_url($action,
197                                                                      array('nickname' => $user->nickname,
198                                                                            'tag' => $tag))),
199                                $tag);
200             }
201             $this->elementEnd('p');
202             $this->elementEnd('body');
203             $this->elementEnd('html');
204         } else {
205             common_redirect(common_local_url($action, array('nickname' =>
206                                                             $user->nickname)),
207                             303);
208         }
209     }
210
211     function showPageNotice()
212     {
213         if ($this->error) {
214             $this->element('p', 'error', $this->error);
215         } else {
216             $this->elementStart('div', 'instructions');
217             $this->element('p', null,
218                            _('Use this form to add tags to your subscribers or subscriptions.'));
219             $this->elementEnd('div');
220         }
221     }
222 }