]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/tagother.php
Merge branch 'master' of /var/www/trunk
[quix0rs-gnu-social.git] / actions / tagother.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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('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->elementStart('dl', 'entity_depiction');
81         $this->element('dt', null, _('Photo'));
82         $this->elementStart('dd');
83         $this->element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_PROFILE_SIZE),
84                                     'class' => 'photo avatar',
85                                     'width' => AVATAR_PROFILE_SIZE,
86                                     'height' => AVATAR_PROFILE_SIZE,
87                                     'alt' =>
88                                     ($this->profile->fullname) ? $this->profile->fullname :
89                                     $this->profile->nickname));
90         $this->elementEnd('dd');
91         $this->elementEnd('dl');
92
93
94         $this->elementStart('dl', 'entity_nickname');
95         $this->element('dt', null, _('Nickname'));
96         $this->elementStart('dd');
97         $this->element('a', array('href' => $this->profile->profileurl,
98                                   'class' => 'nickname'),
99                        $this->profile->nickname);
100         $this->elementEnd('dd');
101         $this->elementEnd('dl');
102
103         if ($this->profile->fullname) {
104             $this->elementStart('dl', 'entity_fn');
105             $this->element('dt', null, _('Full name'));
106             $this->elementStart('dd');
107             $this->element('span', 'fn', $this->profile->fullname);
108             $this->elementEnd('dd');
109             $this->elementEnd('dl');
110         }
111         if ($this->profile->location) {
112             $this->elementStart('dl', 'entity_location');
113             $this->element('dt', null, _('Location'));
114             $this->element('dd', 'location', $this->profile->location);
115             $this->elementEnd('dl');
116         }
117         if ($this->profile->homepage) {
118             $this->elementStart('dl', 'entity_url');
119             $this->element('dt', null, _('URL'));
120             $this->elementStart('dd');
121             $this->element('a', array('href' => $this->profile->homepage,
122                                       'rel' => 'me', 'class' => 'url'),
123                            $this->profile->homepage);
124             $this->elementEnd('dd');
125             $this->elementEnd('dl');
126         }
127         if ($this->profile->bio) {
128             $this->elementStart('dl', 'entity_note');
129             $this->element('dt', null, _('Note'));
130             $this->element('dd', 'note', $this->profile->bio);
131             $this->elementEnd('dl');
132         }
133         $this->elementEnd('div');
134
135         $this->elementStart('form', array('method' => 'post',
136                                            'id' => 'form_tag_user',
137                                            'class' => 'form_settings',
138                                            'name' => 'tagother',
139                                            'action' => $this->selfUrl()));
140         $this->elementStart('fieldset');
141         $this->element('legend', null, _('Tag user'));
142         $this->hidden('token', common_session_token());
143         $this->hidden('id', $this->profile->id);
144
145         $this->elementStart('ul', 'form_data');
146         $this->elementStart('li');
147         $this->input('tags', _('Tags'),
148                      ($this->arg('tags')) ? $this->arg('tags') : implode(' ', Profile_tag::getTags($user->id, $this->profile->id)),
149                      _('Tags for this user (letters, numbers, -, ., and _), comma- or space- separated'));
150         $this->elementEnd('li');
151         $this->elementEnd('ul');
152         $this->submit('save', _('Save'));
153         $this->elementEnd('fieldset');
154         $this->elementEnd('form');
155     }
156
157     function saveTags()
158     {
159         $id = $this->trimmed('id');
160         $tagstring = $this->trimmed('tags');
161         $token = $this->trimmed('token');
162
163         if (!$token || $token != common_session_token()) {
164             $this->showForm(_('There was a problem with your session token.'.
165                               ' Try again, please.'));
166             return;
167         }
168
169         if (is_string($tagstring) && strlen($tagstring) > 0) {
170
171             $tags = array_map('common_canonical_tag',
172                               preg_split('/[\s,]+/', $tagstring));
173
174             foreach ($tags as $tag) {
175                 if (!common_valid_profile_tag($tag)) {
176                     $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
177                     return;
178                 }
179             }
180         } else {
181             $tags = array();
182         }
183
184         $user = common_current_user();
185
186         if (!Subscription::pkeyGet(array('subscriber' => $user->id,
187                                          'subscribed' => $this->profile->id)) &&
188             !Subscription::pkeyGet(array('subscriber' => $this->profile->id,
189                                          'subscribed' => $user->id)))
190         {
191             $this->clientError(_('You can only tag people you are subscribed to or who are subscribed to you.'));
192             return;
193         }
194
195         $result = Profile_tag::setTags($user->id, $this->profile->id, $tags);
196
197         if (!$result) {
198             $this->clientError(_('Could not save tags.'));
199             return;
200         }
201
202         $action = $user->isSubscribed($this->profile) ? 'subscriptions' : 'subscribers';
203
204         if ($this->boolean('ajax')) {
205             $this->startHTML('text/xml');
206             $this->elementStart('head');
207             $this->element('title', null, _('Tags'));
208             $this->elementEnd('head');
209             $this->elementStart('body');
210             $this->elementStart('p', 'subtags');
211             foreach ($tags as $tag) {
212                 $this->element('a', array('href' => common_local_url($action,
213                                                                      array('nickname' => $user->nickname,
214                                                                            'tag' => $tag))),
215                                $tag);
216             }
217             $this->elementEnd('p');
218             $this->elementEnd('body');
219             $this->elementEnd('html');
220         } else {
221             common_redirect(common_local_url($action, array('nickname' =>
222                                                             $user->nickname)));
223         }
224     }
225
226     function showPageNotice()
227     {
228         if ($this->error) {
229             $this->element('p', 'error', $this->error);
230         } else {
231             $this->elementStart('div', 'instructions');
232             $this->element('p', null,
233                            _('Use this form to add tags to your subscribers or subscriptions.'));
234             $this->elementEnd('div');
235         }
236     }
237 }
238