]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilecompletion.php
Merge remote-tracking branch 'mainline/1.0.x' into people_tags_rebase
[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
72     function prepare($args)
73     {
74         parent::prepare($args);
75
76         // CSRF protection
77
78         $token = $this->trimmed('token');
79
80         if (!$token || $token != common_session_token()) {
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             $this->clientError(_('Not logged in.'));
92             return false;
93         }
94
95         $id = $this->arg('peopletag_id');
96         $this->peopletag = Profile_list::staticGet('id', $id);
97
98         if (empty($this->peopletag)) {
99             $this->clientError(_('No such peopletag.'));
100             return false;
101         }
102
103         $field = $this->arg('field');
104         if (!in_array($field, array('fulltext', 'nickname', 'fullname', 'description', 'location', 'uri'))) {
105             $this->clientError(sprintf(_('Unidentified field %s'), htmlspecialchars($field)), 404);
106             return false;
107         }
108         $this->field = $field;
109
110         return true;
111     }
112
113     /**
114      * Handle request
115      *
116      * Does the subscription and returns results.
117      *
118      * @param Array $args unused.
119      *
120      * @return void
121      */
122
123     function handle($args)
124     {
125         $this->msg = null;
126
127         $this->startHTML('text/xml;charset=utf-8');
128         $this->elementStart('head');
129         $this->element('title', null, _('Search results'));
130         $this->elementEnd('head');
131         $this->elementStart('body');
132         $profiles = $this->getResults();
133
134         if ($this->msg !== null) {
135             $this->element('p', 'error', $this->msg);
136         } else {
137             if (count($profiles) > 0) {
138                 $this->elementStart('ul', array('id' => 'profile_search_results', 'class' => 'profile-lister'));
139                 foreach ($profiles as $profile) {
140                     $this->showProfileItem($profile);
141                 }
142                 $this->elementEnd('ul');
143             } else {
144                 $this->element('p', 'error', _('No results.'));
145             }
146         }
147         $this->elementEnd('body');
148         $this->elementEnd('html');
149     }
150
151     function getResults()
152     {
153         $profiles = array();
154         $q = $this->arg('q');
155         $q = strtolower($q);
156         if (strlen($q) < 3) {
157             $this->msg = _('The search string must be atleast 3 characters long');
158         }
159         $page = $this->arg('page');
160         $page = (int) (empty($page) ? 1 : $page);
161
162         $profile = new Profile();
163         $search_engine = $profile->getSearchEngine('profile');
164
165         if (Event::handle('StartProfileCompletionSearch', array($this, &$profile, $search_engine))) {
166             $search_engine->set_sort_mode('chron');
167             $search_engine->limit((($page-1)*PROFILES_PER_PAGE), PROFILES_PER_PAGE + 1);
168
169             if (false === $search_engine->query($q)) {
170                 $cnt = 0;
171             }
172             else {
173                 $cnt = $profile->find();
174             }
175             Event::handle('EndProfileCompletionSearch', $this, &$profile, $search_engine);
176         }
177
178         while ($profile->fetch()) {
179             $profiles[] = clone($profile);
180         }
181         return $this->filter($profiles);
182     }
183
184     function filter($profiles)
185     {
186         $current = $this->user->getProfile();
187         $filtered_profiles = array();
188         foreach ($profiles as $profile) {
189             if ($current->canTag($profile)) {
190                 $filtered_profiles[] = $profile;
191             }
192         }
193         return $filtered_profiles;
194     }
195
196     function showProfileItem($profile)
197     {
198         $this->elementStart('li', 'entity_removable_profile');
199         $item = new TaggedProfileItem($this, $profile);
200         $item->show();
201         $this->elementStart('span', 'entity_actions');
202
203         if ($profile->isTagged($this->peopletag)) {
204             $untag = new UntagButton($this, $profile, $this->peopletag);
205             $untag->show();
206         } else {
207             $tag = new TagButton($this, $profile, $this->peopletag);
208             $tag->show();
209         }
210
211         $this->elementEnd('span');
212         $this->elementEnd('li');
213     }
214 }