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::getKV('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.'));
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.'));
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 // TRANS: Title for list form when not on a profile page.
76 return _('List a profile');
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);
83 function showForm($error=null)
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');
101 function showContent()
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'));
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,
114 ($this->profile->fullname) ? $this->profile->fullname :
115 $this->profile->nickname));
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);
124 if ($this->profile->location) {
125 $this->element('div', 'label entity_location', $this->profile->location);
128 if ($this->profile->homepage) {
129 $this->element('a', array('href' => $this->profile->homepage,
131 'class' => 'url entity_url'),
132 $this->profile->homepage);
135 if ($this->profile->bio) {
136 $this->element('div', 'note entity_note', $this->profile->bio);
139 $this->elementEnd('div');
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))));
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);
153 $user = common_current_user();
155 $this->elementStart('ul', 'form_data');
156 $this->elementStart('li');
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');
171 Event::handle('EndShowTagProfileForm', array($this, $this->profile));
177 $id = $this->trimmed('id');
178 $tagstring = $this->trimmed('tags');
179 $token = $this->trimmed('token');
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.'));
192 if (is_string($tagstring) && strlen($tagstring) > 0) {
194 $tags = preg_split('/[\s,]+/', $tagstring);
196 foreach ($tags as &$tag) {
197 $private = @$tag[0] === '.';
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));
207 $tag_priv[$tag] = $private;
211 $user = common_current_user();
214 $result = Profile_tag::setTags($user->id, $this->profile->id, $tags, $tag_priv);
216 throw new Exception('The tags could not be saved.');
218 } catch (Exception $e) {
219 $this->showForm($e->getMessage());
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');
230 if ($user->id == $this->profile->id) {
231 $widget = new SelftagsWidget($this, $user, $this->profile);
234 $widget = new PeopletagsWidget($this, $user, $this->profile);
238 $this->elementEnd('body');
241 // TRANS: Success message if lists are saved.
242 $this->error = _('Lists saved.');
246 Event::handle('EndSavePeopletags', array($this, $tagstring));
250 function showPageNotice()
253 $this->element('p', 'error', $this->error);
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');