]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - _darcs/pristine/actions/tagother.php
replace all tabs with four spaces
[quix0rs-gnu-social.git] / _darcs / pristine / 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     function handle($args) {
27
28         parent::handle($args);
29
30         if (!common_logged_in()) {
31             $this->client_error(_('Not logged in'), 403);
32             return;
33         }
34
35         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
36             $this->save_tags();
37         } else {
38             $id = $this->trimmed('id');
39             if (!$id) {
40                 $this->client_error(_('No id argument.'));
41                 return;
42             }
43             $profile = Profile::staticGet('id', $id);
44             if (!$profile) {
45                 $this->client_error(_('No profile with that ID.'));
46                 return;
47             }
48             $this->show_form($profile);
49         }
50     }
51
52     function show_form($profile, $error=NULL) {
53
54         $user = common_current_user();
55
56         common_show_header(_('Tag a person'),
57                            NULL, array($profile, $error), array($this, 'show_top'));
58
59         $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
60
61         common_element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_PROFILE_SIZE),
62                                     'class' => 'avatar stream',
63                                     'width' => AVATAR_PROFILE_SIZE,
64                                     'height' => AVATAR_PROFILE_SIZE,
65                                     'alt' =>
66                                     ($profile->fullname) ? $profile->fullname :
67                                     $profile->nickname));
68
69         common_element('a', array('href' => $profile->profileurl,
70                                   'class' => 'external profile nickname'),
71                        $profile->nickname);
72
73         if ($profile->fullname) {
74             common_element_start('div', 'fullname');
75             if ($profile->homepage) {
76                 common_element('a', array('href' => $profile->homepage),
77                                $profile->fullname);
78             } else {
79                 common_text($profile->fullname);
80             }
81             common_element_end('div');
82         }
83         if ($profile->location) {
84             common_element('div', 'location', $profile->location);
85         }
86         if ($profile->bio) {
87             common_element('div', 'bio', $profile->bio);
88         }
89
90         common_element_start('form', array('method' => 'post',
91                                            'id' => 'tag_user',
92                                            'name' => 'tagother',
93                                            'action' => $this->self_url()));
94         common_hidden('token', common_session_token());
95         common_hidden('id', $profile->id);
96         common_input('tags', _('Tags'),
97                      ($this->arg('tags')) ? $this->arg('tags') : implode(' ', Profile_tag::getTags($user->id, $profile->id)),
98                      _('Tags for this user (letters, numbers, -, ., and _), comma- or space- separated'));
99
100         common_submit('save', _('Save'));
101         common_element_end('form');
102         common_show_footer();
103
104     }
105
106     function save_tags() {
107
108         $id = $this->trimmed('id');
109         $tagstring = $this->trimmed('tags');
110         $token = $this->trimmed('token');
111
112         if (!$token || $token != common_session_token()) {
113             $this->show_form(_('There was a problem with your session token. Try again, please.'));
114             return;
115         }
116
117         $profile = Profile::staticGet('id', $id);
118
119         if (!$profile) {
120             $this->client_error(_('No such profile.'));
121             return;
122         }
123
124         if (is_string($tagstring) && strlen($tagstring) > 0) {
125
126             $tags = array_map('common_canonical_tag',
127                               preg_split('/[\s,]+/', $tagstring));
128
129             foreach ($tags as $tag) {
130                 if (!common_valid_profile_tag($tag)) {
131                     $this->show_form($profile, sprintf(_('Invalid tag: "%s"'), $tag));
132                     return;
133                 }
134             }
135         } else {
136             $tags = array();
137         }
138
139         $user = common_current_user();
140
141         if (!Subscription::pkeyGet(array('subscriber' => $user->id,
142                                          'subscribed' => $profile->id)) &&
143             !Subscription::pkeyGet(array('subscriber' => $profile->id,
144                                          'subscribed' => $user->id)))
145         {
146             $this->client_error(_('You can only tag people you are subscribed to or who are subscribed to you.'));
147             return;
148         }
149
150         $result = Profile_tag::setTags($user->id, $profile->id, $tags);
151
152         if (!$result) {
153             $this->client_error(_('Could not save tags.'));
154             return;
155         }
156
157         $action = $user->isSubscribed($profile) ? 'subscriptions' : 'subscribers';
158
159         if ($this->boolean('ajax')) {
160             common_start_html('text/xml');
161             common_element_start('head');
162             common_element('title', null, _('Tags'));
163             common_element_end('head');
164             common_element_start('body');
165             common_element_start('p', 'subtags');
166             foreach ($tags as $tag) {
167                 common_element('a', array('href' => common_local_url($action,
168                                                                      array('nickname' => $user->nickname,
169                                                                            'tag' => $tag))),
170                                $tag);
171             }
172             common_element_end('p');
173             common_element_end('body');
174             common_element_end('html');
175         } else {
176             common_redirect(common_local_url($action, array('nickname' =>
177                                                             $user->nickname)));
178         }
179     }
180
181     function show_top($arr = NULL) {
182         list($profile, $error) = $arr;
183         if ($error) {
184             common_element('p', 'error', $error);
185         } else {
186             common_element_start('div', 'instructions');
187             common_element('p', NULL,
188                            _('Use this form to add tags to your subscribers or subscriptions.'));
189             common_element_end('div');
190         }
191     }
192 }
193