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 $this->clientError(_('No profile with that ID.'));
52 $current = common_current_user()->getProfile();
53 if ($this->profile && !$current->canTag($this->profile)) {
54 $this->clientError(_('You cannot tag this user.'));
59 function handle($args)
61 parent::handle($args);
62 if (Event::handle('StartTagProfileAction', array($this, $this->profile))) {
63 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
68 Event::handle('EndTagProfileAction', array($this, $this->profile));
74 if (!$this->profile) {
75 return _('Tag a profile');
77 return sprintf(_('Tag %s'), $this->profile->nickname);
80 function showForm($error=null)
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');
97 function showContent()
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'));
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,
109 ($this->profile->fullname) ? $this->profile->fullname :
110 $this->profile->nickname));
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);
119 if ($this->profile->location) {
120 $this->element('div', 'label entity_location', $this->profile->location);
123 if ($this->profile->homepage) {
124 $this->element('a', array('href' => $this->profile->homepage,
126 'class' => 'url entity_url'),
127 $this->profile->homepage);
130 if ($this->profile->bio) {
131 $this->element('div', 'note entity_note', $this->profile->bio);
134 $this->elementEnd('div');
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))));
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);
147 $user = common_current_user();
149 $this->elementStart('ul', 'form_data');
150 $this->elementStart('li');
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');
162 Event::handle('EndShowTagProfileForm', array($this, $this->profile));
168 $id = $this->trimmed('id');
169 $tagstring = $this->trimmed('tags');
170 $token = $this->trimmed('token');
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.'));
182 if (is_string($tagstring) && strlen($tagstring) > 0) {
184 $tags = preg_split('/[\s,]+/', $tagstring);
186 foreach ($tags as &$tag) {
187 $private = @$tag[0] === '.';
189 $tag = common_canonical_tag($tag);
190 if (!common_valid_profile_tag($tag)) {
191 $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
195 $tag_priv[$tag] = $private;
199 $user = common_current_user();
202 $result = Profile_tag::setTags($user->id, $this->profile->id, $tags, $tag_priv);
204 throw new Exception('The tags could not be saved.');
206 } catch (Exception $e) {
207 $this->showForm($e->getMessage());
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');
218 if ($user->id == $this->profile->id) {
219 $widget = new SelftagsWidget($this, $user, $this->profile);
222 $widget = new PeopletagsWidget($this, $user, $this->profile);
226 $this->elementEnd('body');
227 $this->elementEnd('html');
229 $this->error = 'Tags saved.';
233 Event::handle('EndSavePeopletags', array($this, $tagstring));
237 function showPageNotice()
240 $this->element('p', 'error', $this->error);
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');