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