profilecompletion.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. require_once INSTALLDIR . '/lib/peopletageditform.php';
  33. /**
  34. * Subscription action
  35. *
  36. * Subscribing to a profile. Does not work for OMB 0.1 remote subscriptions,
  37. * but may work for other remote subscription protocols, like OStatus.
  38. *
  39. * Takes parameters:
  40. *
  41. * - subscribeto: a profile ID
  42. * - token: session token to prevent CSRF attacks
  43. * - ajax: boolean; whether to return Ajax or full-browser results
  44. *
  45. * Only works if the current user is logged in.
  46. *
  47. * @category Action
  48. * @package StatusNet
  49. * @author Shashi Gowda <connect2shashi@gmail.com>
  50. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
  51. * @link http://status.net/
  52. */
  53. class ProfilecompletionAction extends Action
  54. {
  55. var $user;
  56. var $peopletag;
  57. var $field;
  58. var $msg;
  59. /**
  60. * Check pre-requisites and instantiate attributes
  61. *
  62. * @param Array $args array of arguments (URL, GET, POST)
  63. *
  64. * @return boolean success flag
  65. */
  66. function prepare($args)
  67. {
  68. parent::prepare($args);
  69. // CSRF protection
  70. $token = $this->trimmed('token');
  71. if (!$token || $token != common_session_token()) {
  72. // TRANS: Client error displayed when the session token does not match or is not given.
  73. $this->clientError(_('There was a problem with your session token.'.
  74. ' Try again, please.'));
  75. }
  76. // Only for logged-in users
  77. $this->user = common_current_user();
  78. if (empty($this->user)) {
  79. // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
  80. $this->clientError(_('Not logged in.'));
  81. }
  82. $id = $this->arg('peopletag_id');
  83. $this->peopletag = Profile_list::getKV('id', $id);
  84. if (empty($this->peopletag)) {
  85. // TRANS: Client error displayed trying to reference a non-existing list.
  86. $this->clientError(_('No such list.'));
  87. }
  88. $field = $this->arg('field');
  89. if (!in_array($field, array('fulltext', 'nickname', 'fullname', 'description', 'location', 'uri'))) {
  90. // TRANS: Client error displayed when trying to add an unindentified field to profile.
  91. // TRANS: %s is a field name.
  92. $this->clientError(sprintf(_('Unidentified field %s.'), htmlspecialchars($field)), 404);
  93. }
  94. $this->field = $field;
  95. return true;
  96. }
  97. /**
  98. * Handle request
  99. *
  100. * Does the subscription and returns results.
  101. *
  102. * @param Array $args unused.
  103. *
  104. * @return void
  105. */
  106. function handle($args)
  107. {
  108. $this->msg = null;
  109. $this->startHTML('text/xml;charset=utf-8');
  110. $this->elementStart('head');
  111. // TRANS: Page title.
  112. $this->element('title', null, _m('TITLE','Search results'));
  113. $this->elementEnd('head');
  114. $this->elementStart('body');
  115. $profiles = $this->getResults();
  116. if ($this->msg !== null) {
  117. $this->element('p', 'error', $this->msg);
  118. } else {
  119. if (count($profiles) > 0) {
  120. $this->elementStart('ul', array('id' => 'profile_search_results', 'class' => 'profile-lister'));
  121. foreach ($profiles as $profile) {
  122. $this->showProfileItem($profile);
  123. }
  124. $this->elementEnd('ul');
  125. } else {
  126. // TRANS: Output when there are no results for a search.
  127. $this->element('p', 'error', _('No results.'));
  128. }
  129. }
  130. $this->elementEnd('body');
  131. $this->endHTML();
  132. }
  133. function getResults()
  134. {
  135. $profiles = array();
  136. $q = $this->arg('q');
  137. $q = strtolower($q);
  138. if (strlen($q) < 3) {
  139. // TRANS: Error message in case a search is shorter than three characters.
  140. $this->msg = _('The search string must be at least 3 characters long.');
  141. }
  142. $page = $this->arg('page');
  143. $page = (int) (empty($page) ? 1 : $page);
  144. $profile = new Profile();
  145. $search_engine = $profile->getSearchEngine('profile');
  146. if (Event::handle('StartProfileCompletionSearch', array($this, &$profile, $search_engine))) {
  147. $search_engine->set_sort_mode('chron');
  148. $search_engine->limit((($page-1)*PROFILES_PER_PAGE), PROFILES_PER_PAGE + 1);
  149. if (false === $search_engine->query($q)) {
  150. $cnt = 0;
  151. }
  152. else {
  153. $cnt = $profile->find();
  154. }
  155. Event::handle('EndProfileCompletionSearch', array($this, &$profile, $search_engine));
  156. }
  157. while ($profile->fetch()) {
  158. $profiles[] = clone($profile);
  159. }
  160. return $this->filter($profiles);
  161. }
  162. function filter($profiles)
  163. {
  164. $current = $this->user->getProfile();
  165. $filtered_profiles = array();
  166. foreach ($profiles as $profile) {
  167. if ($current->canTag($profile)) {
  168. $filtered_profiles[] = $profile;
  169. }
  170. }
  171. return $filtered_profiles;
  172. }
  173. function showProfileItem($profile)
  174. {
  175. $this->elementStart('li', 'entity_removable_profile');
  176. $item = new TaggedProfileItem($this, $profile);
  177. $item->show();
  178. $this->elementStart('span', 'entity_actions');
  179. if ($profile->isTagged($this->peopletag)) {
  180. $untag = new UntagButton($this, $profile, $this->peopletag);
  181. $untag->show();
  182. } else {
  183. $tag = new TagButton($this, $profile, $this->peopletag);
  184. $tag->show();
  185. }
  186. $this->elementEnd('span');
  187. $this->elementEnd('li');
  188. }
  189. }