]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/peopletagautocomplete.php
Merge remote-tracking branch 'mainline/1.0.x' into people_tags_rebase
[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
48     function prepare($args)
49     {
50         parent::prepare($args);
51
52         // Only for logged-in users
53
54         $this->user = common_current_user();
55
56         if (empty($this->user)) {
57             $this->clientError(_('Not logged in.'));
58             return false;
59         }
60
61         // CSRF protection
62
63         $token = $this->trimmed('token');
64
65         if (!$token || $token != common_session_token()) {
66             $this->clientError(_('There was a problem with your session token.'.
67                                  ' Try again, please.'));
68             return false;
69         }
70
71         $profile = $this->user->getProfile();
72         $tags = $profile->getOwnedTags(common_current_user());
73
74         $this->tags = array();
75         while ($tags->fetch()) {
76
77             if (empty($this->last_mod)) {
78                 $this->last_mod = $tags->modified;
79             }
80
81             $arr = array();
82             $arr['tag'] = $tags->tag;
83             $arr['mode'] = $tags->private ? 'private' : 'public';
84             // $arr['url'] = $tags->homeUrl();
85             $arr['freq'] = $tags->taggedCount();
86
87             $this->tags[] = $arr;
88         }
89
90         $tags->free();
91
92         return true;
93     }
94
95     /**
96      * Last modified time
97      *
98      * Helps in browser-caching
99      *
100      * @return String time
101      */
102
103     function lastModified()
104     {
105         return strtotime($this->last_mod);
106     }
107
108     /**
109      * Handle request
110      *
111      * Print the JSON autocomplete data
112      *
113      * @param Array $args unused.
114      *
115      * @return void
116      */
117
118     function handle($args)
119     {
120         //common_log(LOG_DEBUG, 'Autocomplete data: ' . json_encode($this->tags));
121         if ($this->tags) {
122             print(json_encode($this->tags));
123             exit(0);
124         }
125         return false;
126     }
127 }