]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Directory/lib/sortablesubscriptionlist.php
Added more checked type-hints.
[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         // Note: we don't use fetchAll() because it's borked with query()
134
135         $profiles = array();
136
137         while ($this->profile->fetch()) {
138             $profiles[] = clone($this->profile);
139         }
140
141         $cnt = count($profiles);
142
143         $max = min($cnt, $this->maxProfiles());
144
145         for ($i = 0; $i < $max; $i++) {
146             $odd = ($i % 2 == 0); // for zebra striping
147             $pli = $this->newListItem($profiles[$i], $odd);
148             $pli->show();
149         }
150
151         return $cnt;
152     }
153
154     function newListItem(Profile $profile, $odd)
155     {
156         return new SortableSubscriptionListItem($profile, $this->owner, $this->action, $odd);
157     }
158 }
159
160 class SortableSubscriptionListItem extends SubscriptionListItem
161 {
162     /** Owner of this list */
163     var $owner = null;
164
165     function __construct($profile, $owner, $action, $alt)
166     {
167         parent::__construct($profile, $owner, $action);
168
169         $this->alt   = $alt; // is this row alternate?
170         $this->owner = $owner;
171     }
172
173     function startItem()
174     {
175         $attr = array(
176             'class' => 'profile',
177             'id'    => 'profile-' . $this->profile->id
178         );
179
180         if ($this->alt) {
181             $attr['class'] .= ' alt';
182         }
183
184         $this->out->elementStart('tr', $attr);
185     }
186
187     function endItem()
188     {
189         $this->out->elementEnd('tr');
190     }
191
192     function startProfile()
193     {
194         $this->out->elementStart('td', 'entity_profile h-card');
195     }
196
197     function endProfile()
198     {
199         $this->out->elementEnd('td');
200     }
201
202     function startActions()
203     {
204         $this->out->elementStart('td', 'entity_actions');
205         $this->out->elementStart('ul');
206     }
207
208     function endActions()
209     {
210                 
211                 // delete button
212                 $cur = common_current_user();
213         list($action, $r2args) = $this->out->returnToArgs();
214         $r2args['action'] = $action;        
215                 if ($cur instanceof User && $cur->hasRight(Right::DELETEUSER)) {
216                         $this->out->elementStart('li', 'entity_delete');
217                         $df = new DeleteUserForm($this->out, $this->profile, $r2args);
218                         $df->show();            
219                         $this->out->elementEnd('li');
220                 }
221                         
222         $this->out->elementEnd('ul');
223         $this->out->elementEnd('td');
224     }
225
226     function show()
227     {
228         if (Event::handle('StartProfileListItem', array($this))) {
229             $this->startItem();
230             if (Event::handle('StartProfileListItemProfile', array($this))) {
231                 $this->showProfile();
232                 Event::handle('EndProfileListItemProfile', array($this));
233             }
234
235             // XXX Add events?
236             $this->showCreatedDate();
237             $this->showSubscriberCount();
238             $this->showNoticeCount();
239
240             if (Event::handle('StartProfileListItemActions', array($this))) {
241                 $this->showActions();
242                 Event::handle('EndProfileListItemActions', array($this));
243             }
244             $this->endItem();
245             Event::handle('EndProfileListItem', array($this));
246         }
247     }
248
249     function showSubscriberCount()
250     {
251         $this->out->elementStart('td', 'entry_subscriber_count');
252         $this->out->raw($this->profile->subscriberCount());
253         $this->out->elementEnd('td');
254     }
255
256     function showCreatedDate()
257     {
258         $this->out->elementStart('td', 'entry_created');
259         $this->out->raw(date('j M Y', strtotime($this->profile->created)));
260         $this->out->elementEnd('td');
261     }
262
263     function showNoticeCount()
264     {
265         $this->out->elementStart('td', 'entry_notice_count');
266         $this->out->raw($this->profile->noticeCount());
267         $this->out->elementEnd('td');
268     }
269
270     /**
271      * Overrided to truncate the bio if it's real long, because it
272      * looks better that way in the SortableSubscriptionList's table
273      */
274     function showBio()
275     {
276         if (!empty($this->profile->bio)) {
277             $cutoff = 140; // XXX Should this be configurable?
278             $bio    = htmlspecialchars($this->profile->bio);
279
280             if (mb_strlen($bio) > $cutoff) {
281                 $bio = mb_substr($bio, 0, $cutoff - 1)
282                     .'<a href="' . $this->profile->profileurl .'">…</a>';
283             }
284
285             $this->out->elementStart('p', 'note');
286             $this->out->raw($bio);
287             $this->out->elementEnd('p');
288         }
289     }
290
291     /**
292      * Only show the tags if we're logged in
293      */
294     function showTags()
295     {
296          if (common_logged_in()) {
297             parent::showTags();
298         }
299
300     }
301 }