]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/tagprofile.php
Merge remote-tracking branch 'mainline/1.0.x' into people_tags_rebase
[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::staticGet('id', $id);
45
46             if (!$this->profile) {
47                 $this->clientError(_('No profile with that ID.'));
48                 return false;
49             }
50         }
51
52         $current = common_current_user()->getProfile();
53         if ($this->profile && !$current->canTag($this->profile)) {
54             $this->clientError(_('You cannot tag this user.'));
55         }
56         return true;
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             return _('Tag a profile');
76         }
77         return sprintf(_('Tag %s'), $this->profile->nickname);
78     }
79
80     function showForm($error=null)
81     {
82         $this->error = $error;
83         if ($this->boolean('ajax')) {
84             $this->startHTML('text/xml;charset=utf-8');
85             $this->elementStart('head');
86             $this->element('title', null, _('Error'));
87             $this->elementEnd('head');
88             $this->elementStart('body');
89             $this->element('p', 'error', $error);
90             $this->elementEnd('body');
91             $this->elementEnd('html');
92         } else {
93             $this->showPage();
94         }
95     }
96
97     function showContent()
98     {
99         if (Event::handle('StartShowTagProfileForm', array($this, $this->profile)) && $this->profile) {
100             $this->elementStart('div', 'entity_profile vcard author');
101             $this->element('h2', null, _('User profile'));
102
103             $avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
104             $this->element('img', array('src' => ($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_PROFILE_SIZE),
105                                         'class' => 'photo avatar entity_depiction',
106                                         'width' => AVATAR_PROFILE_SIZE,
107                                         'height' => AVATAR_PROFILE_SIZE,
108                                         'alt' =>
109                                         ($this->profile->fullname) ? $this->profile->fullname :
110                                         $this->profile->nickname));
111
112             $this->element('a', array('href' => $this->profile->profileurl,
113                                       'class' => 'entity_nickname nickname'),
114                            $this->profile->nickname);
115             if ($this->profile->fullname) {
116                 $this->element('div', 'fn entity_fn', $this->profile->fullname);
117             }
118
119             if ($this->profile->location) {
120                 $this->element('div', 'label entity_location', $this->profile->location);
121             }
122
123             if ($this->profile->homepage) {
124                 $this->element('a', array('href' => $this->profile->homepage,
125                                           'rel' => 'me',
126                                           'class' => 'url entity_url'),
127                                $this->profile->homepage);
128             }
129
130             if ($this->profile->bio) {
131                 $this->element('div', 'note entity_note', $this->profile->bio);
132             }
133
134             $this->elementEnd('div');
135
136             $this->elementStart('form', array('method' => 'post',
137                                                'id' => 'form_tag_user',
138                                                'class' => 'form_settings',
139                                                'name' => 'tagprofile',
140                                                'action' => common_local_url('tagprofile', array('id' => $this->profile->id))));
141
142             $this->elementStart('fieldset');
143             $this->element('legend', null, _('Tag user'));
144             $this->hidden('token', common_session_token());
145             $this->hidden('id', $this->profile->id);
146
147             $user = common_current_user();
148
149             $this->elementStart('ul', 'form_data');
150             $this->elementStart('li');
151
152             $tags = Profile_tag::getTagsArray($user->id, $this->profile->id, $user->id);
153             $this->input('tags', _('Tags'),
154                          ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $tags),
155                          _('Tags for this user (letters, numbers, -, ., and _), comma- or space- separated'));
156             $this->elementEnd('li');
157             $this->elementEnd('ul');
158             $this->submit('save', _('Save'));
159             $this->elementEnd('fieldset');
160             $this->elementEnd('form');
161
162             Event::handle('EndShowTagProfileForm', array($this, $this->profile));
163         }
164     }
165
166     function saveTags()
167     {
168         $id = $this->trimmed('id');
169         $tagstring = $this->trimmed('tags');
170         $token = $this->trimmed('token');
171
172         if (Event::handle('StartSavePeopletags', array($this, $tagstring))) {
173             if (!$token || $token != common_session_token()) {
174                 $this->showForm(_('There was a problem with your session token. '.
175                                   'Try again, please.'));
176                 return;
177             }
178
179             $tags = array();
180             $tag_priv = array();
181
182             if (is_string($tagstring) && strlen($tagstring) > 0) {
183
184                 $tags = preg_split('/[\s,]+/', $tagstring);
185
186                 foreach ($tags as &$tag) {
187                     $private = @$tag[0] === '.';
188
189                     $tag = common_canonical_tag($tag);
190                     if (!common_valid_profile_tag($tag)) {
191                         $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
192                         return;
193                     }
194
195                     $tag_priv[$tag] = $private;
196                 }
197             }
198
199             $user = common_current_user();
200
201             try {
202                 $result = Profile_tag::setTags($user->id, $this->profile->id, $tags, $tag_priv);
203                 if (!$result) {
204                     throw new Exception('The tags could not be saved.');
205                 }
206             } catch (Exception $e) {
207                 $this->showForm($e->getMessage());
208                 return false;
209             }
210
211             if ($this->boolean('ajax')) {
212                 $this->startHTML('text/xml;charset=utf-8');
213                 $this->elementStart('head');
214                 $this->element('title', null, _('Tags'));
215                 $this->elementEnd('head');
216                 $this->elementStart('body');
217
218                 if ($user->id == $this->profile->id) {
219                     $widget = new SelftagsWidget($this, $user, $this->profile);
220                     $widget->show();
221                 } else {
222                     $widget = new PeopletagsWidget($this, $user, $this->profile);
223                     $widget->show();
224                 }
225
226                 $this->elementEnd('body');
227                 $this->elementEnd('html');
228             } else {
229                 $this->error = 'Tags saved.';
230                 $this->showForm();
231             }
232
233             Event::handle('EndSavePeopletags', array($this, $tagstring));
234         }
235     }
236
237     function showPageNotice()
238     {
239         if ($this->error) {
240             $this->element('p', 'error', $this->error);
241         } else {
242             $this->elementStart('div', 'instructions');
243             $this->element('p', null,
244                            _('Use this form to add tags to your subscribers or subscriptions.'));
245             $this->elementEnd('div');
246         }
247     }
248 }
249