We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

peopletagautocomplete.php 3.24 KB
Newer Older
Shashi Gowda's avatar
Shashi Gowda committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
<?php
/**
 * StatusNet - the distributed open-source microblogging tool
 * Copyright (C) 2008-2010, StatusNet, Inc.
 *
 * Peopletag autocomple action.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * PHP version 5
 *
 * @category  Action
 * @package   StatusNet
 * @author    Shashi Gowda <connect2shashi@gmail.com>
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
 * @link      http://status.net/
 */

if (!defined('STATUSNET')) {
    exit(1);
}

class PeopletagautocompleteAction extends Action
{
    var $user;
37 38
    var $tags;
    var $last_mod;
Shashi Gowda's avatar
Shashi Gowda committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

    /**
     * Check pre-requisites and instantiate attributes
     *
     * @param Array $args array of arguments (URL, GET, POST)
     *
     * @return boolean success flag
     */
    function prepare($args)
    {
        parent::prepare($args);

        // Only for logged-in users

        $this->user = common_current_user();

        if (empty($this->user)) {
56
            // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
Shashi Gowda's avatar
Shashi Gowda committed
57 58 59 60 61 62 63 64
            $this->clientError(_('Not logged in.'));
        }

        // CSRF protection

        $token = $this->trimmed('token');

        if (!$token || $token != common_session_token()) {
65
            // TRANS: Client error displayed when the session token does not match or is not given.
Shashi Gowda's avatar
Shashi Gowda committed
66 67 68 69
            $this->clientError(_('There was a problem with your session token.'.
                                 ' Try again, please.'));
        }

70
        $profile = $this->user->getProfile();
mattl's avatar
mattl committed
71
        $tags = $profile->getLists($this->scoped);
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

        $this->tags = array();
        while ($tags->fetch()) {

            if (empty($this->last_mod)) {
                $this->last_mod = $tags->modified;
            }

            $arr = array();
            $arr['tag'] = $tags->tag;
            $arr['mode'] = $tags->private ? 'private' : 'public';
            // $arr['url'] = $tags->homeUrl();
            $arr['freq'] = $tags->taggedCount();

            $this->tags[] = $arr;
        }

89
        $tags = NULL;
90

Shashi Gowda's avatar
Shashi Gowda committed
91 92 93
        return true;
    }

94 95 96 97 98 99 100 101 102 103 104 105
    /**
     * Last modified time
     *
     * Helps in browser-caching
     *
     * @return String time
     */
    function lastModified()
    {
        return strtotime($this->last_mod);
    }

Shashi Gowda's avatar
Shashi Gowda committed
106 107 108
    /**
     * Handle request
     *
109
     * Print the JSON autocomplete data
Shashi Gowda's avatar
Shashi Gowda committed
110 111 112 113 114 115 116
     *
     * @param Array $args unused.
     *
     * @return void
     */
    function handle($args)
    {
117 118 119 120
        //common_log(LOG_DEBUG, 'Autocomplete data: ' . json_encode($this->tags));
        if ($this->tags) {
            print(json_encode($this->tags));
            exit(0);
Shashi Gowda's avatar
Shashi Gowda committed
121
        }
122
        return false;
Shashi Gowda's avatar
Shashi Gowda committed
123 124
    }
}