]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/tagother.php
5daf612fb530f7a5881532a22b40940ce0956b20
[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         $avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
77
78         $this->element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_PROFILE_SIZE),
79                                     'class' => 'avatar stream',
80                                     'width' => AVATAR_PROFILE_SIZE,
81                                     'height' => AVATAR_PROFILE_SIZE,
82                                     'alt' =>
83                                     ($this->profile->fullname) ? $this->profile->fullname :
84                                     $this->profile->nickname));
85
86         $this->element('a', array('href' => $this->profile->profileurl,
87                                   'class' => 'external profile nickname'),
88                        $this->profile->nickname);
89
90         if ($this->profile->fullname) {
91             $this->elementStart('div', 'fullname');
92             if ($this->profile->homepage) {
93                 $this->element('a', array('href' => $this->profile->homepage),
94                                $this->profile->fullname);
95             } else {
96                 $this->text($this->profile->fullname);
97             }
98             $this->elementEnd('div');
99         }
100         if ($this->profile->location) {
101             $this->element('div', 'location', $this->profile->location);
102         }
103         if ($this->profile->bio) {
104             $this->element('div', 'bio', $this->profile->bio);
105         }
106
107         $this->elementStart('form', array('method' => 'post',
108                                            'id' => 'tag_user',
109                                            'name' => 'tagother',
110                                            'action' => $this->selfUrl()));
111         $this->hidden('token', common_session_token());
112         $this->hidden('id', $this->profile->id);
113         $this->input('tags', _('Tags'),
114                      ($this->arg('tags')) ? $this->arg('tags') : implode(' ', Profile_tag::getTags($user->id, $this->profile->id)),
115                      _('Tags for this user (letters, numbers, -, ., and _), comma- or space- separated'));
116
117         $this->submit('save', _('Save'));
118         $this->elementEnd('form');
119     }
120
121     function saveTags()
122     {
123         $id = $this->trimmed('id');
124         $tagstring = $this->trimmed('tags');
125         $token = $this->trimmed('token');
126
127         if (!$token || $token != common_session_token()) {
128             $this->showForm(_('There was a problem with your session token.'.
129                               ' Try again, please.'));
130             return;
131         }
132
133         if (is_string($tagstring) && strlen($tagstring) > 0) {
134
135             $tags = array_map('common_canonical_tag',
136                               preg_split('/[\s,]+/', $tagstring));
137
138             foreach ($tags as $tag) {
139                 if (!common_valid_profile_tag($tag)) {
140                     $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
141                     return;
142                 }
143             }
144         } else {
145             $tags = array();
146         }
147
148         $user = common_current_user();
149
150         if (!Subscription::pkeyGet(array('subscriber' => $user->id,
151                                          'subscribed' => $this->profile->id)) &&
152             !Subscription::pkeyGet(array('subscriber' => $this->profile->id,
153                                          'subscribed' => $user->id)))
154         {
155             $this->clientError(_('You can only tag people you are subscribed to or who are subscribed to you.'));
156             return;
157         }
158
159         $result = Profile_tag::setTags($user->id, $this->profile->id, $tags);
160
161         if (!$result) {
162             $this->clientError(_('Could not save tags.'));
163             return;
164         }
165
166         $action = $user->isSubscribed($this->profile) ? 'subscriptions' : 'subscribers';
167
168         if ($this->boolean('ajax')) {
169             common_start_html('text/xml');
170             $this->elementStart('head');
171             $this->element('title', null, _('Tags'));
172             $this->elementEnd('head');
173             $this->elementStart('body');
174             $this->elementStart('p', 'subtags');
175             foreach ($tags as $tag) {
176                 $this->element('a', array('href' => common_local_url($action,
177                                                                      array('nickname' => $user->nickname,
178                                                                            'tag' => $tag))),
179                                $tag);
180             }
181             $this->elementEnd('p');
182             $this->elementEnd('body');
183             $this->elementEnd('html');
184         } else {
185             common_redirect(common_local_url($action, array('nickname' =>
186                                                             $user->nickname)));
187         }
188     }
189
190     function showPageNotice()
191     {
192         if ($this->error) {
193             $this->element('p', 'error', $this->error);
194         } else {
195             $this->elementStart('div', 'instructions');
196             $this->element('p', null,
197                            _('Use this form to add tags to your subscribers or subscriptions.'));
198             $this->elementEnd('div');
199         }
200     }
201 }
202