]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/conversation.php
Merge branch 'master' into 0.9.x
[quix0rs-gnu-social.git] / actions / conversation.php
1 <?php
2 /**
3  * Display a conversation in the browser
4  *
5  * PHP version 5
6  *
7  * @category Action
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2009, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 // XXX: not sure how to do paging yet,
35 // so set a 60-notice limit
36
37 require_once INSTALLDIR.'/lib/noticelist.php';
38
39 /**
40  * Conversation tree in the browser
41  *
42  * @category Action
43  * @package  StatusNet
44  * @author   Evan Prodromou <evan@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
46  * @link     http://status.net/
47  */
48 class ConversationAction extends Action
49 {
50     var $id   = null;
51     var $page = null;
52
53     /**
54      * Initialization.
55      *
56      * @param array $args Web and URL arguments
57      *
58      * @return boolean false if id not passed in
59      */
60     function prepare($args)
61     {
62         parent::prepare($args);
63         $this->id = $this->trimmed('id');
64         if (empty($this->id)) {
65             return false;
66         }
67         $this->id = $this->id+0;
68         $this->page = $this->trimmed('page');
69         if (empty($this->page)) {
70             $this->page = 1;
71         }
72         return true;
73     }
74
75     /**
76      * Handle the action
77      *
78      * @param array $args Web and URL arguments
79      *
80      * @return void
81      */
82     function handle($args)
83     {
84         parent::handle($args);
85         $this->showPage();
86     }
87
88     /**
89      * Returns the page title
90      *
91      * @return string page title
92      */
93     function title()
94     {
95         // TRANS: Title for page with a conversion (multiple notices in context).
96         return _('Conversation');
97     }
98
99     /**
100      * Show content.
101      *
102      * Display a hierarchical unordered list in the content area.
103      * Uses ConversationTree to do most of the heavy lifting.
104      *
105      * @return void
106      */
107     function showContent()
108     {
109         $notices = Notice::conversationStream($this->id, null, null);
110
111         $ct = new ConversationTree($notices, $this);
112
113         $cnt = $ct->show();
114     }
115
116     function isReadOnly()
117     {
118         return true;
119     }
120 }
121
122 /**
123  * Conversation tree
124  *
125  * The widget class for displaying a hierarchical list of notices.
126  *
127  * @category Widget
128  * @package  StatusNet
129  * @author   Evan Prodromou <evan@status.net>
130  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
131  * @link     http://status.net/
132  */
133 class ConversationTree extends NoticeList
134 {
135     var $tree  = null;
136     var $table = null;
137
138     /**
139      * Show the tree of notices
140      *
141      * @return void
142      */
143     function show()
144     {
145         $cnt = $this->_buildTree();
146
147         $this->out->elementStart('div', array('id' =>'notices_primary'));
148         // TRANS: Header on conversation page. Hidden by default (h2).
149         $this->out->element('h2', null, _('Notices'));
150         $this->out->elementStart('ol', array('class' => 'notices xoxo'));
151
152         if (array_key_exists('root', $this->tree)) {
153             $rootid = $this->tree['root'][0];
154             $this->showNoticePlus($rootid);
155         }
156
157         $this->out->elementEnd('ol');
158         $this->out->elementEnd('div');
159
160         return $cnt;
161     }
162
163     function _buildTree()
164     {
165         $cnt = 0;
166
167         $this->tree  = array();
168         $this->table = array();
169
170         while ($this->notice->fetch()) {
171
172             $cnt++;
173
174             $id     = $this->notice->id;
175             $notice = clone($this->notice);
176
177             $this->table[$id] = $notice;
178
179             if (is_null($notice->reply_to)) {
180                 $this->tree['root'] = array($notice->id);
181             } else if (array_key_exists($notice->reply_to, $this->tree)) {
182                 $this->tree[$notice->reply_to][] = $notice->id;
183             } else {
184                 $this->tree[$notice->reply_to] = array($notice->id);
185             }
186         }
187
188         return $cnt;
189     }
190
191     /**
192      * Shows a notice plus its list of children.
193      *
194      * @param integer $id ID of the notice to show
195      *
196      * @return void
197      */
198     function showNoticePlus($id)
199     {
200         $notice = $this->table[$id];
201
202         // We take responsibility for doing the li
203
204         $this->out->elementStart('li', array('class' => 'hentry notice',
205                                              'id' => 'notice-' . $id));
206
207         $item = $this->newListItem($notice);
208         $item->show();
209
210         if (array_key_exists($id, $this->tree)) {
211             $children = $this->tree[$id];
212
213             $this->out->elementStart('ol', array('class' => 'notices'));
214
215             sort($children);
216
217             foreach ($children as $child) {
218                 $this->showNoticePlus($child);
219             }
220
221             $this->out->elementEnd('ol');
222         }
223
224         $this->out->elementEnd('li');
225     }
226
227     /**
228      * Override parent class to return our preferred item.
229      *
230      * @param Notice $notice Notice to display
231      *
232      * @return NoticeListItem a list item to show
233      */
234     function newListItem($notice)
235     {
236         return new ConversationTreeItem($notice, $this->out);
237     }
238 }
239
240 /**
241  * Conversation tree list item
242  *
243  * Special class of NoticeListItem for use inside conversation trees.
244  *
245  * @category Widget
246  * @package  StatusNet
247  * @author   Evan Prodromou <evan@status.net>
248  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
249  * @link     http://status.net/
250  */
251 class ConversationTreeItem extends NoticeListItem
252 {
253     /**
254      * start a single notice.
255      *
256      * The default creates the <li>; we skip, since the ConversationTree
257      * takes care of that.
258      *
259      * @return void
260      */
261     function showStart()
262     {
263         return;
264     }
265
266     /**
267      * finish the notice
268      *
269      * The default closes the <li>; we skip, since the ConversationTree
270      * takes care of that.
271      *
272      * @return void
273      */
274     function showEnd()
275     {
276         return;
277     }
278
279     /**
280      * show link to notice conversation page
281      *
282      * Since we're only used on the conversation page, we skip this
283      *
284      * @return void
285      */
286     function showContext()
287     {
288         return;
289     }
290 }