selftag.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Action for showing profiles self-tagged with a given tag
  18. *
  19. * @category Action
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @author Zach Copley <zach@status.net>
  23. * @copyright 2009 StatusNet, Inc.
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. /**
  28. * This class outputs a paginated list of profiles self-tagged with a given tag
  29. *
  30. * @category Output
  31. * @copyright 2009 StatusNet, Inc.
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. *
  34. * @see Action
  35. */
  36. class SelftagAction extends Action
  37. {
  38. public $tag = null;
  39. public $page = null;
  40. /**
  41. * For initializing members of the class.
  42. *
  43. * @param array $args misc. arguments
  44. *
  45. * @return boolean true
  46. * @throws ClientException
  47. */
  48. public function prepare(array $args = [])
  49. {
  50. parent::prepare($args);
  51. $this->tag = $this->trimmed('tag');
  52. if (!common_valid_profile_tag($this->tag)) {
  53. // TRANS: Client error displayed when trying to list a profile with an invalid list.
  54. // TRANS: %s is the invalid list name.
  55. $this->clientError(sprintf(
  56. _('Not a valid list: %s.'),
  57. $this->tag
  58. ));
  59. return null;
  60. }
  61. $this->page = ($this->arg('page')) ? $this->arg('page') : 1;
  62. common_set_returnto($this->selfUrl());
  63. return true;
  64. }
  65. /**
  66. * Handler method
  67. *
  68. * @return void is read only action?
  69. */
  70. public function handle()
  71. {
  72. parent::handle();
  73. $this->showPage();
  74. }
  75. /**
  76. * Whips up a query to get a list of profiles based on the provided
  77. * people tag and page, initalizes a ProfileList widget, and displays
  78. * it to the user.
  79. */
  80. public function showContent()
  81. {
  82. $profile = new Profile();
  83. $offset = ($this->page - 1) * PROFILES_PER_PAGE;
  84. $limit = PROFILES_PER_PAGE + 1;
  85. // XXX: memcached this
  86. $qry = 'SELECT profile.* ' .
  87. 'FROM profile JOIN ( profile_tag, profile_list ) ' .
  88. 'ON profile.id = profile_tag.tagger ' .
  89. 'AND profile_tag.tagger = profile_list.tagger ' .
  90. 'AND profile_list.tag = profile_tag.tag ' .
  91. 'WHERE profile_tag.tagger = profile_tag.tagged ' .
  92. "AND profile_tag.tag = '%s' ";
  93. $user = common_current_user();
  94. if (empty($user)) {
  95. $qry .= 'AND profile_list.private = false ';
  96. } else {
  97. $qry .= 'AND (profile_list.tagger = ' . $user->id .
  98. ' OR profile_list.private = false) ';
  99. }
  100. $qry .= 'ORDER BY profile_tag.modified DESC ' .
  101. 'LIMIT ' . $limit . ' OFFSET ' . $offset;
  102. $profile->query(sprintf($qry, $this->tag));
  103. $ptl = new SelfTagProfileList($profile, $this); // pass the ammunition
  104. $cnt = $ptl->show();
  105. $this->pagination(
  106. $this->page > 1,
  107. $cnt > PROFILES_PER_PAGE,
  108. $this->page,
  109. 'selftag',
  110. ['tag' => $this->tag]
  111. );
  112. }
  113. /**
  114. * Returns the page title
  115. *
  116. * @return string page title
  117. */
  118. public function title()
  119. {
  120. // TRANS: Page title for page showing self tags.
  121. // TRANS: %1$s is a tag, %2$d is a page number.
  122. return sprintf(
  123. _('Users self-tagged with %1$s, page %2$d'),
  124. $this->tag,
  125. $this->page
  126. );
  127. }
  128. }
  129. class SelfTagProfileList extends ProfileList
  130. {
  131. public function newListItem(Profile $target)
  132. {
  133. return new SelfTagProfileListItem($target, $this->action);
  134. }
  135. }
  136. class SelfTagProfileListItem extends ProfileListItem
  137. {
  138. public function linkAttributes()
  139. {
  140. $aAttrs = parent::linkAttributes();
  141. if (common_config('nofollow', 'selftag')) {
  142. $aAttrs['rel'] .= ' nofollow';
  143. }
  144. return $aAttrs;
  145. }
  146. public function homepageAttributes()
  147. {
  148. $aAttrs = parent::linkAttributes();
  149. if (common_config('nofollow', 'selftag')) {
  150. $aAttrs['rel'] = 'nofollow';
  151. }
  152. return $aAttrs;
  153. }
  154. public function showTags()
  155. {
  156. $selftags = new SelfTagsWidget($this->out, $this->profile, $this->profile);
  157. $selftags->show();
  158. $user = common_current_user();
  159. if (!empty($user) && $user->id != $this->profile->getID() &&
  160. $user->getProfile()->canTag($this->profile)) {
  161. $yourtags = new PeopleTagsWidget($this->out, $user, $this->profile);
  162. $yourtags->show();
  163. }
  164. }
  165. }