]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/addpeopletag.php
Merge remote-tracking branch 'upstream/master' into social-master
[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 class AddpeopletagAction extends Action
56 {
57     var $user;
58     var $tagged;
59     var $peopletag;
60
61     /**
62      * Check pre-requisites and instantiate attributes
63      *
64      * @param Array $args array of arguments (URL, GET, POST)
65      *
66      * @return boolean success flag
67      */
68     function prepare(array $args=array())
69     {
70         parent::prepare($args);
71
72         // CSRF protection
73
74         $token = $this->trimmed('token');
75
76         if (!$token || $token != common_session_token()) {
77             // TRANS: Client error displayed when the session token does not match or is not given.
78             $this->clientError(_('There was a problem with your session token.'.
79                                  ' Try again, please.'));
80         }
81
82         // Only for logged-in users
83
84         $this->user = common_current_user();
85
86         if (empty($this->user)) {
87             // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
88             $this->clientError(_('Not logged in.'));
89         }
90
91         // Profile to subscribe to
92
93         $tagged_id = $this->arg('tagged');
94
95         $this->tagged = Profile::getKV('id', $tagged_id);
96
97         if (empty($this->tagged)) {
98             // TRANS: Client error displayed trying to perform an action related to a non-existing profile.
99             $this->clientError(_('No such profile.'));
100         }
101
102         $id = $this->arg('peopletag_id');
103         $this->peopletag = Profile_list::getKV('id', $id);
104
105         if (empty($this->peopletag)) {
106             // TRANS: Client error displayed trying to reference a non-existing list.
107             $this->clientError(_('No such list.'));
108         }
109
110         return true;
111     }
112
113     /**
114      * Handle request
115      *
116      * Does the tagging and returns results.
117      *
118      * @param Array $args unused.
119      *
120      * @return void
121      */
122     function handle(array $args=array())
123     {
124         // Throws exception on error
125         $ptag = Profile_tag::setTag($this->user->id, $this->tagged->id,
126                                 $this->peopletag->tag);
127
128         if (!$ptag) {
129             $user = User::getKV('id', $id);
130
131             if ($user instanceof User) {
132                 $this->clientError(
133                         // TRANS: Client error displayed when an unknown error occurs when adding a user to a list.
134                         // TRANS: %s is a username.
135                         sprintf(_('There was an unexpected error while listing %s.'),
136                         $user->nickname));
137             } else {
138                 // TRANS: Client error displayed when an unknown error occurs when adding a user to a list.
139                 // TRANS: %s is a profile URL.
140                 $this->clientError(sprintf(_('There was a problem listing %s. ' .
141                                       'The remote server is probably not responding correctly. ' .
142                                       'Please try retrying later.'), $this->profile->profileurl));
143             }
144         }
145         if ($this->boolean('ajax')) {
146             $this->startHTML('text/xml;charset=utf-8');
147             $this->elementStart('head');
148             // TRANS: Title after adding a user to a list.
149             $this->element('title', null, _m('TITLE','Listed'));
150             $this->elementEnd('head');
151             $this->elementStart('body');
152             $unsubscribe = new UntagButton($this, $this->tagged, $this->peopletag);
153             $unsubscribe->show();
154             $this->elementEnd('body');
155             $this->endHTML();
156         } else {
157             $url = common_local_url('subscriptions',
158                                     array('nickname' => $this->user->nickname));
159             common_redirect($url, 303);
160         }
161     }
162 }