3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 require_once INSTALLDIR . '/lib/settingsaction.php';
23 require_once INSTALLDIR . '/lib/peopletags.php';
25 class TagprofileAction extends Action
30 function prepare($args)
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);
40 $id = $this->trimmed('id');
42 $this->profile = false;
44 $this->profile = Profile::staticGet('id', $id);
46 if (!$this->profile) {
47 // TRANS: Client error displayed when referring to non-existing profile ID.
48 $this->clientError(_('No profile with that ID.'));
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.'));
61 function handle($args)
63 parent::handle($args);
64 if (Event::handle('StartTagProfileAction', array($this, $this->profile))) {
65 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
70 Event::handle('EndTagProfileAction', array($this, $this->profile));
76 if (!$this->profile) {
77 // TRANS: Title for people tag form when not on a profile page.
78 return _('Tag a profile');
80 // TRANS: Title for people tag form when on a profile page.
81 // TRANS: %s is a profile nickname.
82 return sprintf(_('Tag %s'), $this->profile->nickname);
85 function showForm($error=null)
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 people tag 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->elementEnd('html');
103 function showContent()
105 if (Event::handle('StartShowTagProfileForm', array($this, $this->profile)) && $this->profile) {
106 $this->elementStart('div', 'entity_profile vcard author');
107 // TRANS: Header in people tag form.
108 $this->element('h2', null, _('User profile'));
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,
116 ($this->profile->fullname) ? $this->profile->fullname :
117 $this->profile->nickname));
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);
126 if ($this->profile->location) {
127 $this->element('div', 'label entity_location', $this->profile->location);
130 if ($this->profile->homepage) {
131 $this->element('a', array('href' => $this->profile->homepage,
133 'class' => 'url entity_url'),
134 $this->profile->homepage);
137 if ($this->profile->bio) {
138 $this->element('div', 'note entity_note', $this->profile->bio);
141 $this->elementEnd('div');
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))));
149 $this->elementStart('fieldset');
150 // TRANS: Fieldset legend for people tag form.
151 $this->element('legend', null, _('Tag user'));
152 $this->hidden('token', common_session_token());
153 $this->hidden('id', $this->profile->id);
155 $user = common_current_user();
157 $this->elementStart('ul', 'form_data');
158 $this->elementStart('li');
160 $tags = Profile_tag::getTagsArray($user->id, $this->profile->id, $user->id);
161 // TRANS: Field label on people tag form.
162 $this->input('tags', _m('LABEL','Tags'),
163 ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $tags),
164 // TRANS: Field title on people tag form.
165 _('Tags for this user (letters, numbers, -, ., and _), comma- or space- separated.'));
166 $this->elementEnd('li');
167 $this->elementEnd('ul');
168 // TRANS: Button text to save people tags.
169 $this->submit('save', _m('BUTTON','Save'));
170 $this->elementEnd('fieldset');
171 $this->elementEnd('form');
173 Event::handle('EndShowTagProfileForm', array($this, $this->profile));
179 $id = $this->trimmed('id');
180 $tagstring = $this->trimmed('tags');
181 $token = $this->trimmed('token');
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.'));
194 if (is_string($tagstring) && strlen($tagstring) > 0) {
196 $tags = preg_split('/[\s,]+/', $tagstring);
198 foreach ($tags as &$tag) {
199 $private = @$tag[0] === '.';
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));
209 $tag_priv[$tag] = $private;
213 $user = common_current_user();
216 $result = Profile_tag::setTags($user->id, $this->profile->id, $tags, $tag_priv);
218 throw new Exception('The tags could not be saved.');
220 } catch (Exception $e) {
221 $this->showForm($e->getMessage());
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');
232 if ($user->id == $this->profile->id) {
233 $widget = new SelftagsWidget($this, $user, $this->profile);
236 $widget = new PeopletagsWidget($this, $user, $this->profile);
240 $this->elementEnd('body');
241 $this->elementEnd('html');
243 // TRANS: Success message if people tags are saved.
244 $this->error = _('Tags saved.');
248 Event::handle('EndSavePeopletags', array($this, $tagstring));
252 function showPageNotice()
255 $this->element('p', 'error', $this->error);
257 $this->elementStart('div', 'instructions');
258 $this->element('p', null,
259 // TRANS: Page notice.
260 _('Use this form to add tags to your subscribers or subscriptions.'));
261 $this->elementEnd('div');