]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/tagother.php
Quick style update for profile blocks.
[quix0rs-gnu-social.git] / actions / tagother.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
24 class TagotherAction extends Action
25 {
26     var $profile = null;
27     var $error = null;
28
29     function prepare($args)
30     {
31         parent::prepare($args);
32         if (!common_logged_in()) {
33             $this->clientError(_('Not logged in.'), 403);
34             return false;
35         }
36
37         $id = $this->trimmed('id');
38         if (!$id) {
39             $this->clientError(_('No ID argument.'));
40             return false;
41         }
42
43         $this->profile = Profile::staticGet('id', $id);
44
45         if (!$this->profile) {
46             $this->clientError(_('No profile with that ID.'));
47             return false;
48         }
49
50         return true;
51     }
52
53     function handle($args)
54     {
55         parent::handle($args);
56         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
57             $this->saveTags();
58         } else {
59             $this->showForm($profile);
60         }
61     }
62
63     function title()
64     {
65         return sprintf(_('Tag %s'), $this->profile->nickname);
66     }
67
68     function showForm($error=null)
69     {
70         $this->error = $error;
71         $this->showPage();
72     }
73
74     function showContent()
75     {
76         $this->elementStart('div', 'entity_profile vcard author');
77         $this->element('h2', null, _('User profile'));
78
79         $avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
80         $this->element('img', array('src' => ($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_PROFILE_SIZE),
81                                     'class' => 'photo avatar entity_depiction',
82                                     'width' => AVATAR_PROFILE_SIZE,
83                                     'height' => AVATAR_PROFILE_SIZE,
84                                     'alt' =>
85                                     ($this->profile->fullname) ? $this->profile->fullname :
86                                     $this->profile->nickname));
87
88         $this->element('a', array('href' => $this->profile->profileurl,
89                                   'class' => 'entity_nickname nickname'),
90                        $this->profile->nickname);
91
92         if ($this->profile->fullname) {
93             $this->element('div', 'fn entity_fn', $this->profile->fullname);
94         }
95
96         if ($this->profile->location) {
97             $this->element('div', 'label entity_location', $this->profile->location);
98         }
99
100         if ($this->profile->homepage) {
101             $this->element('a', array('href' => $this->profile->homepage,
102                                       'rel' => 'me',
103                                       'class' => 'url entity_url'),
104                            $this->profile->homepage);
105         }
106
107         if ($this->profile->bio) {
108             $this->element('div', 'note entity_note', $this->profile->bio);
109         }
110
111         $this->elementEnd('div');
112
113         $this->elementStart('form', array('method' => 'post',
114                                            'id' => 'form_tag_user',
115                                            'class' => 'form_settings',
116                                            'name' => 'tagother',
117                                            'action' => common_local_url('tagother', array('id' => $this->profile->id))));
118
119         $this->elementStart('fieldset');
120         $this->element('legend', null, _('Tag user'));
121         $this->hidden('token', common_session_token());
122         $this->hidden('id', $this->profile->id);
123
124         $user = common_current_user();
125
126         $this->elementStart('ul', 'form_data');
127         $this->elementStart('li');
128         $this->input('tags', _('Tags'),
129                      ($this->arg('tags')) ? $this->arg('tags') : implode(' ', Profile_tag::getTags($user->id, $this->profile->id)),
130                      _('Tags for this user (letters, numbers, -, ., and _), comma- or space- separated'));
131         $this->elementEnd('li');
132         $this->elementEnd('ul');
133         $this->submit('save', _('Save'));
134         $this->elementEnd('fieldset');
135         $this->elementEnd('form');
136     }
137
138     function saveTags()
139     {
140         $id = $this->trimmed('id');
141         $tagstring = $this->trimmed('tags');
142         $token = $this->trimmed('token');
143
144         if (!$token || $token != common_session_token()) {
145             $this->showForm(_('There was a problem with your session token. '.
146                               'Try again, please.'));
147             return;
148         }
149
150         if (is_string($tagstring) && strlen($tagstring) > 0) {
151
152             $tags = array_map('common_canonical_tag',
153                               preg_split('/[\s,]+/', $tagstring));
154
155             foreach ($tags as $tag) {
156                 if (!common_valid_profile_tag($tag)) {
157                     $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
158                     return;
159                 }
160             }
161         } else {
162             $tags = array();
163         }
164
165         $user = common_current_user();
166
167         if (!Subscription::pkeyGet(array('subscriber' => $user->id,
168                                          'subscribed' => $this->profile->id)) &&
169             !Subscription::pkeyGet(array('subscriber' => $this->profile->id,
170                                          'subscribed' => $user->id)))
171         {
172             $this->clientError(_('You can only tag people you are subscribed to or who are subscribed to you.'));
173             return;
174         }
175
176         $result = Profile_tag::setTags($user->id, $this->profile->id, $tags);
177
178         if (!$result) {
179             $this->clientError(_('Could not save tags.'));
180             return;
181         }
182
183         $action = $user->isSubscribed($this->profile) ? 'subscriptions' : 'subscribers';
184
185         if ($this->boolean('ajax')) {
186             $this->startHTML('text/xml;charset=utf-8');
187             $this->elementStart('head');
188             $this->element('title', null, _('Tags'));
189             $this->elementEnd('head');
190             $this->elementStart('body');
191             $this->elementStart('p', 'subtags');
192             foreach ($tags as $tag) {
193                 $this->element('a', array('href' => common_local_url($action,
194                                                                      array('nickname' => $user->nickname,
195                                                                            'tag' => $tag))),
196                                $tag);
197             }
198             $this->elementEnd('p');
199             $this->elementEnd('body');
200             $this->elementEnd('html');
201         } else {
202             common_redirect(common_local_url($action, array('nickname' =>
203                                                             $user->nickname)),
204                             303);
205         }
206     }
207
208     function showPageNotice()
209     {
210         if ($this->error) {
211             $this->element('p', 'error', $this->error);
212         } else {
213             $this->elementStart('div', 'instructions');
214             $this->element('p', null,
215                            _('Use this form to add tags to your subscribers or subscriptions.'));
216             $this->elementEnd('div');
217         }
218     }
219 }
220