]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/addpeopletag.php
people tag actions
[quix0rs-gnu-social.git] / actions / addpeopletag.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008-2010, StatusNet, Inc.
5  *
6  * Action to add a people tag to a user.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  * PHP version 5
22  *
23  * @category  Action
24  * @package   StatusNet
25  * @author    Shashi Gowda <connect2shashi@gmail.com>
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/lib/togglepeopletag.php';
35
36 /**
37  *  
38  * Action to tag a profile with a single tag.
39  * 
40  * Takes parameters:
41  *
42  *    - tagged: the ID of the profile being tagged
43  *    - token: session token to prevent CSRF attacks
44  *    - ajax: boolean; whether to return Ajax or full-browser results
45  *    - peopletag_id: the ID of the tag being used
46  *
47  * Only works if the current user is logged in.
48  *
49  * @category  Action
50  * @package   StatusNet
51  * @author    Shashi Gowda <connect2shashi@gmail.com>
52  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
53  * @link      http://status.net/
54  */
55
56 class AddpeopletagAction extends Action
57 {
58     var $user;
59     var $tagged;
60     var $peopletag;
61
62     /**
63      * Check pre-requisites and instantiate attributes
64      *
65      * @param Array $args array of arguments (URL, GET, POST)
66      *
67      * @return boolean success flag
68      */
69
70     function prepare($args)
71     {
72         parent::prepare($args);
73
74         // CSRF protection
75
76         $token = $this->trimmed('token');
77
78         if (!$token || $token != common_session_token()) {
79             $this->clientError(_('There was a problem with your session token.'.
80                                  ' Try again, please.'));
81             return false;
82         }
83
84         // Only for logged-in users
85
86         $this->user = common_current_user();
87
88         if (empty($this->user)) {
89             $this->clientError(_('Not logged in.'));
90             return false;
91         }
92
93         // Profile to subscribe to
94
95         $tagged_id = $this->arg('tagged');
96
97         $this->tagged = Profile::staticGet('id', $tagged_id);
98
99         if (empty($this->tagged)) {
100             $this->clientError(_('No such profile.'));
101             return false;
102         }
103
104         $id = $this->arg('peopletag_id');
105         $this->peopletag = Profile_list::staticGet('id', $id);
106
107         if (empty($this->peopletag)) {
108             $this->clientError(_('No such peopletag.'));
109             return false;
110         }
111
112         // OMB 0.1 doesn't have a mechanism for local-server-
113         // originated tag.
114
115         $omb01 = Remote_profile::staticGet('id', $tagged_id);
116
117         if (!empty($omb01)) {
118             $this->clientError(_('You cannot tag an OMB 0.1'.
119                                  ' remote profile with this action.'));
120             return false;
121         }
122
123         return true;
124     }
125
126     /**
127      * Handle request
128      *
129      * Does the tagging and returns results.
130      *
131      * @param Array $args unused.
132      *
133      * @return void
134      */
135
136     function handle($args)
137     {
138
139         // Throws exception on error
140         $ptag = Profile_tag::setTag($this->user->id, $this->tagged->id,
141                                 $this->peopletag->tag);
142
143         if (!$ptag) {
144             $user = User::staticGet('id', $id);
145             if ($user) {
146                 $this->clientError(
147                         sprintf(_('There was an unexpected error while tagging %s'),
148                         $user->nickname));
149             } else {
150                 $this->clientError(sprintf(_('There was a problem tagging %s.' .
151                                       'The remote server is probably not responding correctly, ' .
152                                       'please try retrying later.'), $this->profile->profileurl));
153             }
154             return false;
155         }
156         if ($this->boolean('ajax')) {
157             $this->startHTML('text/xml;charset=utf-8');
158             $this->elementStart('head');
159             $this->element('title', null, _('Subscribed'));
160             $this->elementEnd('head');
161             $this->elementStart('body');
162             $unsubscribe = new UntagButton($this, $this->tagged, $this->peopletag);
163             $unsubscribe->show();
164             $this->elementEnd('body');
165             $this->elementEnd('html');
166         } else {
167             $url = common_local_url('subscriptions',
168                                     array('nickname' => $this->user->nickname));
169             common_redirect($url, 303);
170         }
171     }
172 }