]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/tagprofile.php
Cosmetic changes to common_redirect, clientError, serverError
[quix0rs-gnu-social.git] / actions / tagprofile.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 require_once INSTALLDIR . '/lib/peopletags.php';
24
25 class TagprofileAction 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             common_set_returnto($_SERVER['REQUEST_URI']);
35             if (Event::handle('RedirectToLogin', array($this, null))) {
36                 common_redirect(common_local_url('login'), 303);
37             }
38         }
39
40         $id = $this->trimmed('id');
41         if (!$id) {
42             $this->profile = false;
43         } else {
44             $this->profile = Profile::getKV('id', $id);
45
46             if (!$this->profile) {
47                 // TRANS: Client error displayed when referring to non-existing profile ID.
48                 $this->clientError(_('No profile with that ID.'));
49             }
50         }
51
52         $current = common_current_user()->getProfile();
53         if ($this->profile && !$current->canTag($this->profile)) {
54             // TRANS: Client error displayed when trying to tag a user that cannot be tagged.
55             $this->clientError(_('You cannot tag this user.'));
56         }
57     }
58
59     function handle($args)
60     {
61         parent::handle($args);
62         if (Event::handle('StartTagProfileAction', array($this, $this->profile))) {
63             if ($_SERVER['REQUEST_METHOD'] == 'POST') {
64                 $this->saveTags();
65             } else {
66                 $this->showForm();
67             }
68             Event::handle('EndTagProfileAction', array($this, $this->profile));
69         }
70     }
71
72     function title()
73     {
74         if (!$this->profile) {
75             // TRANS: Title for list form when not on a profile page.
76             return _('List a profile');
77         }
78         // TRANS: Title for list form when on a profile page.
79         // TRANS: %s is a profile nickname.
80         return sprintf(_m('ADDTOLIST','List %s'), $this->profile->nickname);
81     }
82
83     function showForm($error=null)
84     {
85         $this->error = $error;
86         if ($this->boolean('ajax')) {
87             $this->startHTML('text/xml;charset=utf-8');
88             $this->elementStart('head');
89             // TRANS: Title for list form when an error has occurred.
90             $this->element('title', null, _m('TITLE','Error'));
91             $this->elementEnd('head');
92             $this->elementStart('body');
93             $this->element('p', 'error', $error);
94             $this->elementEnd('body');
95             $this->endHTML();
96         } else {
97             $this->showPage();
98         }
99     }
100
101     function showContent()
102     {
103         if (Event::handle('StartShowTagProfileForm', array($this, $this->profile)) && $this->profile) {
104             $this->elementStart('div', 'entity_profile vcard author');
105             // TRANS: Header in list form.
106             $this->element('h2', null, _('User profile'));
107
108             $avatarUrl = $this->profile->avatarUrl(AVATAR_PROFILE_SIZE);
109             $this->element('img', array('src' => $avatarUrl,
110                                         'class' => 'photo avatar entity_depiction',
111                                         'width' => AVATAR_PROFILE_SIZE,
112                                         'height' => AVATAR_PROFILE_SIZE,
113                                         'alt' =>
114                                         ($this->profile->fullname) ? $this->profile->fullname :
115                                         $this->profile->nickname));
116
117             $this->element('a', array('href' => $this->profile->profileurl,
118                                       'class' => 'entity_nickname nickname'),
119                            $this->profile->nickname);
120             if ($this->profile->fullname) {
121                 $this->element('div', 'fn entity_fn', $this->profile->fullname);
122             }
123
124             if ($this->profile->location) {
125                 $this->element('div', 'label entity_location', $this->profile->location);
126             }
127
128             if ($this->profile->homepage) {
129                 $this->element('a', array('href' => $this->profile->homepage,
130                                           'rel' => 'me',
131                                           'class' => 'url entity_url'),
132                                $this->profile->homepage);
133             }
134
135             if ($this->profile->bio) {
136                 $this->element('div', 'note entity_note', $this->profile->bio);
137             }
138
139             $this->elementEnd('div');
140
141             $this->elementStart('form', array('method' => 'post',
142                                                'id' => 'form_tag_user',
143                                                'class' => 'form_settings',
144                                                'name' => 'tagprofile',
145                                                'action' => common_local_url('tagprofile', array('id' => $this->profile->id))));
146
147             $this->elementStart('fieldset');
148             // TRANS: Fieldset legend for list form.
149             $this->element('legend', null, _('List user'));
150             $this->hidden('token', common_session_token());
151             $this->hidden('id', $this->profile->id);
152
153             $user = common_current_user();
154
155             $this->elementStart('ul', 'form_data');
156             $this->elementStart('li');
157
158             $tags = Profile_tag::getTagsArray($user->id, $this->profile->id, $user->id);
159             // TRANS: Field label on list form.
160             $this->input('tags', _m('LABEL','Lists'),
161                          ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $tags),
162                          // TRANS: Field title on list form.
163                          _('Lists for this user (letters, numbers, -, ., and _), comma- or space- separated.'));
164             $this->elementEnd('li');
165             $this->elementEnd('ul');
166             // TRANS: Button text to save lists.
167             $this->submit('save', _m('BUTTON','Save'));
168             $this->elementEnd('fieldset');
169             $this->elementEnd('form');
170
171             Event::handle('EndShowTagProfileForm', array($this, $this->profile));
172         }
173     }
174
175     function saveTags()
176     {
177         $id = $this->trimmed('id');
178         $tagstring = $this->trimmed('tags');
179         $token = $this->trimmed('token');
180
181         if (Event::handle('StartSavePeopletags', array($this, $tagstring))) {
182             if (!$token || $token != common_session_token()) {
183                 // TRANS: Client error displayed when the session token does not match or is not given.
184                 $this->showForm(_('There was a problem with your session token. '.
185                                   'Try again, please.'));
186                 return;
187             }
188
189             $tags = array();
190             $tag_priv = array();
191
192             if (is_string($tagstring) && strlen($tagstring) > 0) {
193
194                 $tags = preg_split('/[\s,]+/', $tagstring);
195
196                 foreach ($tags as &$tag) {
197                     $private = @$tag[0] === '.';
198
199                     $tag = common_canonical_tag($tag);
200                     if (!common_valid_profile_tag($tag)) {
201                         // TRANS: Form validation error displayed if a given tag is invalid.
202                         // TRANS: %s is the invalid tag.
203                         $this->showForm(sprintf(_('Invalid tag: "%s".'), $tag));
204                         return;
205                     }
206
207                     $tag_priv[$tag] = $private;
208                 }
209             }
210
211             $user = common_current_user();
212
213             try {
214                 $result = Profile_tag::setTags($user->id, $this->profile->id, $tags, $tag_priv);
215                 if (!$result) {
216                     throw new Exception('The tags could not be saved.');
217                 }
218             } catch (Exception $e) {
219                 $this->showForm($e->getMessage());
220                 return false;
221             }
222
223             if ($this->boolean('ajax')) {
224                 $this->startHTML('text/xml;charset=utf-8');
225                 $this->elementStart('head');
226                 $this->element('title', null, _m('TITLE','Tags'));
227                 $this->elementEnd('head');
228                 $this->elementStart('body');
229
230                 if ($user->id == $this->profile->id) {
231                     $widget = new SelftagsWidget($this, $user, $this->profile);
232                     $widget->show();
233                 } else {
234                     $widget = new PeopletagsWidget($this, $user, $this->profile);
235                     $widget->show();
236                 }
237
238                 $this->elementEnd('body');
239                 $this->endHTML();
240             } else {
241                 // TRANS: Success message if lists are saved.
242                 $this->error = _('Lists saved.');
243                 $this->showForm();
244             }
245
246             Event::handle('EndSavePeopletags', array($this, $tagstring));
247         }
248     }
249
250     function showPageNotice()
251     {
252         if ($this->error) {
253             $this->element('p', 'error', $this->error);
254         } else {
255             $this->elementStart('div', 'instructions');
256             $this->element('p', null,
257                            // TRANS: Page notice.
258                            _('Use this form to add your subscribers or subscriptions to lists.'));
259             $this->elementEnd('div');
260         }
261     }
262 }