]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilecompletion.php
The overloaded DB_DataObject function staticGet is now called getKV
[quix0rs-gnu-social.git] / actions / profilecompletion.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008-2010, StatusNet, Inc.
5  *
6  * Subscription action.
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/peopletageditform.php';
35
36 /**
37  * Subscription action
38  *
39  * Subscribing to a profile. Does not work for OMB 0.1 remote subscriptions,
40  * but may work for other remote subscription protocols, like OStatus.
41  *
42  * Takes parameters:
43  *
44  *    - subscribeto: a profile ID
45  *    - token: session token to prevent CSRF attacks
46  *    - ajax: boolean; whether to return Ajax or full-browser results
47  *
48  * Only works if the current user is logged in.
49  *
50  * @category  Action
51  * @package   StatusNet
52  * @author    Shashi Gowda <connect2shashi@gmail.com>
53  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
54  * @link      http://status.net/
55  */
56
57 class ProfilecompletionAction extends Action
58 {
59     var $user;
60     var $peopletag;
61     var $field;
62     var $msg;
63
64     /**
65      * Check pre-requisites and instantiate attributes
66      *
67      * @param Array $args array of arguments (URL, GET, POST)
68      *
69      * @return boolean success flag
70      */
71     function prepare($args)
72     {
73         parent::prepare($args);
74
75         // CSRF protection
76
77         $token = $this->trimmed('token');
78
79         if (!$token || $token != common_session_token()) {
80             // TRANS: Client error displayed when the session token does not match or is not given.
81             $this->clientError(_('There was a problem with your session token.'.
82                                  ' Try again, please.'));
83             return false;
84         }
85
86         // Only for logged-in users
87
88         $this->user = common_current_user();
89
90         if (empty($this->user)) {
91             // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
92             $this->clientError(_('Not logged in.'));
93             return false;
94         }
95
96         $id = $this->arg('peopletag_id');
97         $this->peopletag = Profile_list::getKV('id', $id);
98
99         if (empty($this->peopletag)) {
100             // TRANS: Client error displayed trying to reference a non-existing list.
101             $this->clientError(_('No such list.'));
102             return false;
103         }
104
105         $field = $this->arg('field');
106         if (!in_array($field, array('fulltext', 'nickname', 'fullname', 'description', 'location', 'uri'))) {
107             // TRANS: Client error displayed when trying to add an unindentified field to profile.
108             // TRANS: %s is a field name.
109             $this->clientError(sprintf(_('Unidentified field %s.'), htmlspecialchars($field)), 404);
110             return false;
111         }
112         $this->field = $field;
113
114         return true;
115     }
116
117     /**
118      * Handle request
119      *
120      * Does the subscription and returns results.
121      *
122      * @param Array $args unused.
123      *
124      * @return void
125      */
126
127     function handle($args)
128     {
129         $this->msg = null;
130
131         $this->startHTML('text/xml;charset=utf-8');
132         $this->elementStart('head');
133         // TRANS: Page title.
134         $this->element('title', null, _m('TITLE','Search results'));
135         $this->elementEnd('head');
136         $this->elementStart('body');
137         $profiles = $this->getResults();
138
139         if ($this->msg !== null) {
140             $this->element('p', 'error', $this->msg);
141         } else {
142             if (count($profiles) > 0) {
143                 $this->elementStart('ul', array('id' => 'profile_search_results', 'class' => 'profile-lister'));
144                 foreach ($profiles as $profile) {
145                     $this->showProfileItem($profile);
146                 }
147                 $this->elementEnd('ul');
148             } else {
149                 // TRANS: Output when there are no results for a search.
150                 $this->element('p', 'error', _('No results.'));
151             }
152         }
153         $this->elementEnd('body');
154         $this->elementEnd('html');
155     }
156
157     function getResults()
158     {
159         $profiles = array();
160         $q = $this->arg('q');
161         $q = strtolower($q);
162         if (strlen($q) < 3) {
163             // TRANS: Error message in case a search is shorter than three characters.
164             $this->msg = _('The search string must be at least 3 characters long.');
165         }
166         $page = $this->arg('page');
167         $page = (int) (empty($page) ? 1 : $page);
168
169         $profile = new Profile();
170         $search_engine = $profile->getSearchEngine('profile');
171
172         if (Event::handle('StartProfileCompletionSearch', array($this, &$profile, $search_engine))) {
173             $search_engine->set_sort_mode('chron');
174             $search_engine->limit((($page-1)*PROFILES_PER_PAGE), PROFILES_PER_PAGE + 1);
175
176             if (false === $search_engine->query($q)) {
177                 $cnt = 0;
178             }
179             else {
180                 $cnt = $profile->find();
181             }
182             Event::handle('EndProfileCompletionSearch', $this, $profile, $search_engine);
183         }
184
185         while ($profile->fetch()) {
186             $profiles[] = clone($profile);
187         }
188         return $this->filter($profiles);
189     }
190
191     function filter($profiles)
192     {
193         $current = $this->user->getProfile();
194         $filtered_profiles = array();
195         foreach ($profiles as $profile) {
196             if ($current->canTag($profile)) {
197                 $filtered_profiles[] = $profile;
198             }
199         }
200         return $filtered_profiles;
201     }
202
203     function showProfileItem($profile)
204     {
205         $this->elementStart('li', 'entity_removable_profile');
206         $item = new TaggedProfileItem($this, $profile);
207         $item->show();
208         $this->elementStart('span', 'entity_actions');
209
210         if ($profile->isTagged($this->peopletag)) {
211             $untag = new UntagButton($this, $profile, $this->peopletag);
212             $untag->show();
213         } else {
214             $tag = new TagButton($this, $profile, $this->peopletag);
215             $tag->show();
216         }
217
218         $this->elementEnd('span');
219         $this->elementEnd('li');
220     }
221 }