sortablesubscriptionlist.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Widget to show a sortable list of profiles
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Public
  23. * @package StatusNet
  24. * @author Zach Copley <zach@status.net>
  25. * @copyright 2011 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. require_once INSTALLDIR . '/lib/subscriptionlist.php';
  33. /**
  34. * Widget to show a sortable list of subscriptions
  35. *
  36. * @category Public
  37. * @package StatusNet
  38. * @author Zach Copley <zach@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  40. * @link http://status.net/
  41. */
  42. class SortableSubscriptionList extends SubscriptionList
  43. {
  44. /** Owner of this list */
  45. var $owner = null;
  46. function __construct($profile, $owner=null, $action=null)
  47. {
  48. parent::__construct($profile, $owner, $action);
  49. $this->owner = $owner;
  50. }
  51. function startList()
  52. {
  53. $this->out->elementStart('table', array('class' => 'profile_list xoxo'));
  54. $this->out->elementStart('thead');
  55. $this->out->elementStart('tr');
  56. $tableHeaders = array(
  57. // TRANS: Column header in table for user nickname.
  58. 'nickname' => _m('Nickname'),
  59. // TRANS: Column header in table for timestamp when user was created.
  60. 'created' => _m('Created')
  61. );
  62. foreach ($tableHeaders as $id => $label) {
  63. $attrs = array('id' => $id);
  64. $current = (!empty($this->action->sort) && $this->action->sort == $id);
  65. if ($current || empty($this->action->sort) && $id == 'nickname') {
  66. $attrs['class'] = 'current';
  67. }
  68. if ($current && $this->action->reverse) {
  69. $attrs['class'] .= ' reverse';
  70. $attrs['class'] = trim($attrs['class']);
  71. }
  72. $this->out->elementStart('th', $attrs);
  73. $linkAttrs = array();
  74. $params = array('sort' => $id);
  75. if (!empty($this->action->q)) {
  76. $params['q'] = $this->action->q;
  77. }
  78. if ($current && !$this->action->reverse) {
  79. $params['reverse'] = 'true';
  80. }
  81. $args = array();
  82. $filter = $this->action->arg('filter');
  83. if (!empty($filter)) {
  84. $args['filter'] = $filter;
  85. }
  86. $linkAttrs['href'] = common_local_url(
  87. $this->action->arg('action'), $args, $params
  88. );
  89. $this->out->element('a', $linkAttrs, $label);
  90. $this->out->elementEnd('th');
  91. }
  92. // TRANS: Column header for number of subscriptions.
  93. $this->out->element('th', array('id' => 'subscriptions'), _m('Subscriptions'));
  94. // TRANS: Column header for number of notices.
  95. $this->out->element('th', array('id' => 'notices'), _m('Notices'));
  96. $this->out->element('th', array('id' => 'controls'), null);
  97. $this->out->elementEnd('tr');
  98. $this->out->elementEnd('thead');
  99. $this->out->elementStart('tbody');
  100. }
  101. function endList()
  102. {
  103. $this->out->elementEnd('tbody');
  104. $this->out->elementEnd('table');
  105. }
  106. function showProfiles()
  107. {
  108. // Note: we don't use fetchAll() because it's borked with query()
  109. $profiles = array();
  110. while ($this->profile->fetch()) {
  111. $profiles[] = clone($this->profile);
  112. }
  113. $cnt = count($profiles);
  114. $max = min($cnt, $this->maxProfiles());
  115. for ($i = 0; $i < $max; $i++) {
  116. $odd = ($i % 2 == 0); // for zebra striping
  117. $pli = $this->newListItem($profiles[$i], $odd);
  118. $pli->show();
  119. }
  120. return $cnt;
  121. }
  122. function newListItem($profile, $odd)
  123. {
  124. return new SortableSubscriptionListItem($profile, $this->owner, $this->action, $odd);
  125. }
  126. }
  127. class SortableSubscriptionListItem extends SubscriptionListItem
  128. {
  129. /** Owner of this list */
  130. var $owner = null;
  131. function __construct($profile, $owner, $action, $alt)
  132. {
  133. parent::__construct($profile, $owner, $action);
  134. $this->alt = $alt; // is this row alternate?
  135. $this->owner = $owner;
  136. }
  137. function startItem()
  138. {
  139. $attr = array(
  140. 'class' => 'profile',
  141. 'id' => 'profile-' . $this->profile->id
  142. );
  143. if ($this->alt) {
  144. $attr['class'] .= ' alt';
  145. }
  146. $this->out->elementStart('tr', $attr);
  147. }
  148. function endItem()
  149. {
  150. $this->out->elementEnd('tr');
  151. }
  152. function startProfile()
  153. {
  154. $this->out->elementStart('td', 'entity_profile h-card');
  155. }
  156. function endProfile()
  157. {
  158. $this->out->elementEnd('td');
  159. }
  160. function startActions()
  161. {
  162. $this->out->elementStart('td', 'entity_actions');
  163. $this->out->elementStart('ul');
  164. }
  165. function endActions()
  166. {
  167. // delete button
  168. $cur = common_current_user();
  169. list($action, $r2args) = $this->out->returnToArgs();
  170. $r2args['action'] = $action;
  171. if ($cur instanceof User && $cur->hasRight(Right::DELETEUSER)) {
  172. $this->out->elementStart('li', 'entity_delete');
  173. $df = new DeleteUserForm($this->out, $this->profile, $r2args);
  174. $df->show();
  175. $this->out->elementEnd('li');
  176. }
  177. $this->out->elementEnd('ul');
  178. $this->out->elementEnd('td');
  179. }
  180. function show()
  181. {
  182. if (Event::handle('StartProfileListItem', array($this))) {
  183. $this->startItem();
  184. if (Event::handle('StartProfileListItemProfile', array($this))) {
  185. $this->showProfile();
  186. Event::handle('EndProfileListItemProfile', array($this));
  187. }
  188. // XXX Add events?
  189. $this->showCreatedDate();
  190. $this->showSubscriberCount();
  191. $this->showNoticeCount();
  192. if (Event::handle('StartProfileListItemActions', array($this))) {
  193. $this->showActions();
  194. Event::handle('EndProfileListItemActions', array($this));
  195. }
  196. $this->endItem();
  197. Event::handle('EndProfileListItem', array($this));
  198. }
  199. }
  200. function showSubscriberCount()
  201. {
  202. $this->out->elementStart('td', 'entry_subscriber_count');
  203. $this->out->raw($this->profile->subscriberCount());
  204. $this->out->elementEnd('td');
  205. }
  206. function showCreatedDate()
  207. {
  208. $this->out->elementStart('td', 'entry_created');
  209. $this->out->raw(date('j M Y', strtotime($this->profile->created)));
  210. $this->out->elementEnd('td');
  211. }
  212. function showNoticeCount()
  213. {
  214. $this->out->elementStart('td', 'entry_notice_count');
  215. $this->out->raw($this->profile->noticeCount());
  216. $this->out->elementEnd('td');
  217. }
  218. /**
  219. * Overrided to truncate the bio if it's real long, because it
  220. * looks better that way in the SortableSubscriptionList's table
  221. */
  222. function showBio()
  223. {
  224. if (!empty($this->profile->bio)) {
  225. $cutoff = 140; // XXX Should this be configurable?
  226. $bio = htmlspecialchars($this->profile->bio);
  227. if (mb_strlen($bio) > $cutoff) {
  228. $bio = mb_substr($bio, 0, $cutoff - 1)
  229. .'<a href="' . $this->profile->profileurl .'">…</a>';
  230. }
  231. $this->out->elementStart('p', 'note');
  232. $this->out->raw($bio);
  233. $this->out->elementEnd('p');
  234. }
  235. }
  236. /**
  237. * Only show the tags if we're logged in
  238. */
  239. function showTags()
  240. {
  241. if (common_logged_in()) {
  242. parent::showTags();
  243. }
  244. }
  245. }