]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/peopletagautocomplete.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / actions / peopletagautocomplete.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008-2010, StatusNet, Inc.
5  *
6  * Peopletag autocomple 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 class PeopletagautocompleteAction extends Action
35 {
36     var $user;
37     var $tags;
38     var $last_mod;
39
40     /**
41      * Check pre-requisites and instantiate attributes
42      *
43      * @param Array $args array of arguments (URL, GET, POST)
44      *
45      * @return boolean success flag
46      */
47     function prepare(array $args=array())
48     {
49         parent::prepare($args);
50
51         // Only for logged-in users
52
53         $this->user = common_current_user();
54
55         if (empty($this->user)) {
56             // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
57             $this->clientError(_('Not logged in.'));
58         }
59
60         // CSRF protection
61
62         $token = $this->trimmed('token');
63
64         if (!$token || $token != common_session_token()) {
65             // TRANS: Client error displayed when the session token does not match or is not given.
66             $this->clientError(_('There was a problem with your session token.'.
67                                  ' Try again, please.'));
68         }
69
70         $profile = $this->user->getProfile();
71         $tags = $profile->getLists(common_current_user());
72
73         $this->tags = array();
74         while ($tags->fetch()) {
75
76             if (empty($this->last_mod)) {
77                 $this->last_mod = $tags->modified;
78             }
79
80             $arr = array();
81             $arr['tag'] = $tags->tag;
82             $arr['mode'] = $tags->private ? 'private' : 'public';
83             // $arr['url'] = $tags->homeUrl();
84             $arr['freq'] = $tags->taggedCount();
85
86             $this->tags[] = $arr;
87         }
88
89         $tags = NULL;
90
91         return true;
92     }
93
94     /**
95      * Last modified time
96      *
97      * Helps in browser-caching
98      *
99      * @return String time
100      */
101     function lastModified()
102     {
103         return strtotime($this->last_mod);
104     }
105
106     /**
107      * Handle request
108      *
109      * Print the JSON autocomplete data
110      *
111      * @param Array $args unused.
112      *
113      * @return void
114      */
115     function handle(array $args=array())
116     {
117         //common_debug('Autocomplete data: ' . json_encode($this->tags));
118         if ($this->tags) {
119             print(json_encode($this->tags));
120             exit(0);
121         }
122         return false;
123     }
124 }