]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Directory/lib/sortablesubscriptionlist.php
use an array of profiles rather than a looping cursor for profile lists
[quix0rs-gnu-social.git] / plugins / Directory / lib / sortablesubscriptionlist.php
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
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/lib/subscriptionlist.php';
35
36 /**
37  * Widget to show a sortable list of subscriptions
38  *
39  * @category Public
40  * @package  StatusNet
41  * @author   Zach Copley <zach@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45 class SortableSubscriptionList extends SubscriptionList
46 {
47     /** Owner of this list */
48     var $owner = null;
49
50     function __construct($profile, $owner=null, $action=null)
51     {
52         parent::__construct($profile, $owner, $action);
53
54         $this->owner = $owner;
55     }
56
57     function startList()
58     {
59         $this->out->elementStart('table', array('class' => 'profile_list xoxo'));
60         $this->out->elementStart('thead');
61         $this->out->elementStart('tr');
62
63         $tableHeaders = array(
64             // TRANS: Column header in table for user nickname.
65             'nickname'    => _m('Nickname'),
66             // TRANS: Column header in table for timestamp when user was created.
67             'created'     => _m('Created')
68         );
69
70         foreach ($tableHeaders as $id => $label) {
71
72             $attrs   = array('id' => $id);
73             $current = (!empty($this->action->sort) && $this->action->sort == $id);
74
75             if ($current || empty($this->action->sort) && $id == 'nickname') {
76                 $attrs['class'] = 'current';
77             }
78
79             if ($current && $this->action->reverse) {
80                 $attrs['class'] .= ' reverse';
81                 $attrs['class'] = trim($attrs['class']);
82             }
83
84             $this->out->elementStart('th', $attrs);
85
86             $linkAttrs = array();
87             $params    = array('sort' => $id);
88
89             if (!empty($this->action->q)) {
90                 $params['q'] = $this->action->q;
91             }
92
93             if ($current && !$this->action->reverse) {
94                 $params['reverse'] = 'true';
95             }
96
97             $args = array();
98
99             $filter = $this->action->arg('filter');
100
101             if (!empty($filter)) {
102                 $args['filter'] = $filter;
103             }
104
105             $linkAttrs['href'] = common_local_url(
106                 $this->action->arg('action'), $args, $params
107             );
108
109             $this->out->element('a', $linkAttrs, $label);
110             $this->out->elementEnd('th');
111         }
112
113         // TRANS: Column header for number of subscriptions.
114         $this->out->element('th', array('id' => 'subscriptions'), _m('Subscriptions'));
115         // TRANS: Column header for number of notices.
116         $this->out->element('th', array('id' => 'notices'), _m('Notices'));
117         $this->out->element('th', array('id' => 'controls'), null);
118
119         $this->out->elementEnd('tr');
120         $this->out->elementEnd('thead');
121
122         $this->out->elementStart('tbody');
123     }
124
125     function endList()
126     {
127         $this->out->elementEnd('tbody');
128         $this->out->elementEnd('table');
129     }
130
131     function showProfiles()
132     {
133         $profiles = $this->profile->fetchAll();
134
135         $cnt = count($profiles);
136
137         $max = min($cnt, $this->maxProfiles());
138
139         for ($i = 0; $i < $max; $i++) {
140             $odd = ($i % 2 == 0); // for zebra striping
141             $pli = $this->newListItem($profiles[$i], $odd);
142             $pli->show();
143         }
144
145         return $cnt;
146     }
147
148     function newListItem($profile, $odd)
149     {
150         return new SortableSubscriptionListItem($profile, $this->owner, $this->action, $odd);
151     }
152 }
153
154 class SortableSubscriptionListItem extends SubscriptionListItem
155 {
156     /** Owner of this list */
157     var $owner = null;
158
159     function __construct($profile, $owner, $action, $alt)
160     {
161         parent::__construct($profile, $owner, $action);
162
163         $this->alt   = $alt; // is this row alternate?
164         $this->owner = $owner;
165     }
166
167     function startItem()
168     {
169         $attr = array(
170             'class' => 'profile',
171             'id'    => 'profile-' . $this->profile->id
172         );
173
174         if ($this->alt) {
175             $attr['class'] .= ' alt';
176         }
177
178         $this->out->elementStart('tr', $attr);
179     }
180
181     function endItem()
182     {
183         $this->out->elementEnd('tr');
184     }
185
186     function startProfile()
187     {
188         $this->out->elementStart('td', 'entity_profile vcard entry-content');
189     }
190
191     function endProfile()
192     {
193         $this->out->elementEnd('td');
194     }
195
196     function startActions()
197     {
198         $this->out->elementStart('td', 'entity_actions');
199         $this->out->elementStart('ul');
200     }
201
202     function endActions()
203     {
204         $this->out->elementEnd('ul');
205         $this->out->elementEnd('td');
206     }
207
208     function show()
209     {
210         if (Event::handle('StartProfileListItem', array($this))) {
211             $this->startItem();
212             if (Event::handle('StartProfileListItemProfile', array($this))) {
213                 $this->showProfile();
214                 Event::handle('EndProfileListItemProfile', array($this));
215             }
216
217             // XXX Add events?
218             $this->showCreatedDate();
219             $this->showSubscriberCount();
220             $this->showNoticeCount();
221
222             if (Event::handle('StartProfileListItemActions', array($this))) {
223                 $this->showActions();
224                 Event::handle('EndProfileListItemActions', array($this));
225             }
226             $this->endItem();
227             Event::handle('EndProfileListItem', array($this));
228         }
229     }
230
231     function showSubscriberCount()
232     {
233         $this->out->elementStart('td', 'entry_subscriber_count');
234         $this->out->raw($this->profile->subscriberCount());
235         $this->out->elementEnd('td');
236     }
237
238     function showCreatedDate()
239     {
240         $this->out->elementStart('td', 'entry_created');
241         $this->out->raw(date('j M Y', strtotime($this->profile->created)));
242         $this->out->elementEnd('td');
243     }
244
245     function showNoticeCount()
246     {
247         $this->out->elementStart('td', 'entry_notice_count');
248         $this->out->raw($this->profile->noticeCount());
249         $this->out->elementEnd('td');
250     }
251
252     /**
253      * Overrided to truncate the bio if it's real long, because it
254      * looks better that way in the SortableSubscriptionList's table
255      */
256     function showBio()
257     {
258         if (!empty($this->profile->bio)) {
259             $cutoff = 140; // XXX Should this be configurable?
260             $bio    = htmlspecialchars($this->profile->bio);
261
262             if (mb_strlen($bio) > $cutoff) {
263                 $bio = mb_substr($bio, 0, $cutoff - 1)
264                     .'<a href="' . $this->profile->profileurl .'">…</a>';
265             }
266
267             $this->out->elementStart('p', 'note');
268             $this->out->raw($bio);
269             $this->out->elementEnd('p');
270         }
271     }
272
273     /**
274      * Only show the tags if we're logged in
275      */
276     function showTags()
277     {
278          if (common_logged_in()) {
279             parent::showTags();
280         }
281
282     }
283 }