peopletagautocomplete.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. class PeopletagautocompleteAction extends Action
  33. {
  34. var $user;
  35. var $tags;
  36. var $last_mod;
  37. /**
  38. * Check pre-requisites and instantiate attributes
  39. *
  40. * @param Array $args array of arguments (URL, GET, POST)
  41. *
  42. * @return boolean success flag
  43. */
  44. function prepare($args)
  45. {
  46. parent::prepare($args);
  47. // Only for logged-in users
  48. $this->user = common_current_user();
  49. if (empty($this->user)) {
  50. // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
  51. $this->clientError(_('Not logged in.'));
  52. }
  53. // CSRF protection
  54. $token = $this->trimmed('token');
  55. if (!$token || $token != common_session_token()) {
  56. // TRANS: Client error displayed when the session token does not match or is not given.
  57. $this->clientError(_('There was a problem with your session token.'.
  58. ' Try again, please.'));
  59. }
  60. $profile = $this->user->getProfile();
  61. $tags = $profile->getLists($this->scoped);
  62. $this->tags = array();
  63. while ($tags->fetch()) {
  64. if (empty($this->last_mod)) {
  65. $this->last_mod = $tags->modified;
  66. }
  67. $arr = array();
  68. $arr['tag'] = $tags->tag;
  69. $arr['mode'] = $tags->private ? 'private' : 'public';
  70. // $arr['url'] = $tags->homeUrl();
  71. $arr['freq'] = $tags->taggedCount();
  72. $this->tags[] = $arr;
  73. }
  74. $tags = NULL;
  75. return true;
  76. }
  77. /**
  78. * Last modified time
  79. *
  80. * Helps in browser-caching
  81. *
  82. * @return String time
  83. */
  84. function lastModified()
  85. {
  86. return strtotime($this->last_mod);
  87. }
  88. /**
  89. * Handle request
  90. *
  91. * Print the JSON autocomplete data
  92. *
  93. * @param Array $args unused.
  94. *
  95. * @return void
  96. */
  97. function handle($args)
  98. {
  99. //common_log(LOG_DEBUG, 'Autocomplete data: ' . json_encode($this->tags));
  100. if ($this->tags) {
  101. print(json_encode($this->tags));
  102. exit(0);
  103. }
  104. return false;
  105. }
  106. }