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