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