]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/conversation.php
Merge commit 'br3nda/0.8.x-pginstaller' into 0.8.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  Laconica
9  * @author   Evan Prodromou <evan@controlyourself.ca>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://laconi.ca/
12  *
13  * Laconica - a distributed open-source microblogging tool
14  * Copyright (C) 2009, Control Yourself, 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('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  Laconica
44  * @author   Evan Prodromou <evan@controlyourself.ca>
45  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
46  * @link     http://laconi.ca/
47  */
48
49 class ConversationAction extends Action
50 {
51     var $id   = null;
52     var $page = null;
53
54     /**
55      * Initialization.
56      *
57      * @param array $args Web and URL arguments
58      *
59      * @return boolean false if id not passed in
60      */
61
62     function prepare($args)
63     {
64         parent::prepare($args);
65         $this->id = $this->trimmed('id');
66         if (empty($this->id)) {
67             return false;
68         }
69         $this->id = $this->id+0;
70         $this->page = $this->trimmed('page');
71         if (empty($this->page)) {
72             $this->page = 1;
73         }
74         return true;
75     }
76
77     /**
78      * Handle the action
79      *
80      * @param array $args Web and URL arguments
81      *
82      * @return void
83      */
84
85     function handle($args)
86     {
87         parent::handle($args);
88         $this->showPage();
89     }
90
91     /**
92      * Returns the page title
93      *
94      * @return string page title
95      */
96
97     function title()
98     {
99         return _("Conversation");
100     }
101
102     /**
103      * Show content.
104      *
105      * Display a hierarchical unordered list in the content area.
106      * Uses ConversationTree to do most of the heavy lifting.
107      *
108      * @return void
109      */
110
111     function showContent()
112     {
113         $notices = Notice::conversationStream($this->id, null, null);
114
115         $ct = new ConversationTree($notices, $this);
116
117         $cnt = $ct->show();
118     }
119
120     function isReadOnly()
121     {
122         return true;
123     }
124 }
125
126 /**
127  * Conversation tree
128  *
129  * The widget class for displaying a hierarchical list of notices.
130  *
131  * @category Widget
132  * @package  Laconica
133  * @author   Evan Prodromou <evan@controlyourself.ca>
134  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
135  * @link     http://laconi.ca/
136  */
137
138 class ConversationTree extends NoticeList
139 {
140     var $tree  = null;
141     var $table = null;
142
143     /**
144      * Show the tree of notices
145      *
146      * @return void
147      */
148
149     function show()
150     {
151         $cnt = $this->_buildTree();
152
153         $this->out->elementStart('div', array('id' =>'notices_primary'));
154         $this->out->element('h2', null, _('Notices'));
155         $this->out->elementStart('ol', array('class' => 'notices xoxo'));
156
157         if (array_key_exists('root', $this->tree)) {
158             $rootid = $this->tree['root'][0];
159             $this->showNoticePlus($rootid);
160         }
161
162         $this->out->elementEnd('ol');
163         $this->out->elementEnd('div');
164
165         return $cnt;
166     }
167
168     function _buildTree()
169     {
170         $this->tree  = array();
171         $this->table = array();
172
173         while ($this->notice->fetch()) {
174
175             $cnt++;
176
177             $id     = $this->notice->id;
178             $notice = clone($this->notice);
179
180             $this->table[$id] = $notice;
181
182             if (is_null($notice->reply_to)) {
183                 $this->tree['root'] = array($notice->id);
184             } else if (array_key_exists($notice->reply_to, $this->tree)) {
185                 $this->tree[$notice->reply_to][] = $notice->id;
186             } else {
187                 $this->tree[$notice->reply_to] = array($notice->id);
188             }
189         }
190
191         return $cnt;
192     }
193
194     /**
195      * Shows a notice plus its list of children.
196      *
197      * @param integer $id ID of the notice to show
198      *
199      * @return void
200      */
201
202     function showNoticePlus($id)
203     {
204         $notice = $this->table[$id];
205
206         // We take responsibility for doing the li
207
208         $this->out->elementStart('li', array('class' => 'hentry notice',
209                                              'id' => 'notice-' . $id));
210
211         $item = $this->newListItem($notice);
212         $item->show();
213
214         if (array_key_exists($id, $this->tree)) {
215             $children = $this->tree[$id];
216
217             $this->out->elementStart('ol', array('class' => 'notices'));
218
219             sort($children);
220
221             foreach ($children as $child) {
222                 $this->showNoticePlus($child);
223             }
224
225             $this->out->elementEnd('ol');
226         }
227
228         $this->out->elementEnd('li');
229     }
230
231     /**
232      * Override parent class to return our preferred item.
233      *
234      * @param Notice $notice Notice to display
235      *
236      * @return NoticeListItem a list item to show
237      */
238
239     function newListItem($notice)
240     {
241         return new ConversationTreeItem($notice, $this->out);
242     }
243 }
244
245 /**
246  * Conversation tree list item
247  *
248  * Special class of NoticeListItem for use inside conversation trees.
249  *
250  * @category Widget
251  * @package  Laconica
252  * @author   Evan Prodromou <evan@controlyourself.ca>
253  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
254  * @link     http://laconi.ca/
255  */
256
257 class ConversationTreeItem extends NoticeListItem
258 {
259     /**
260      * start a single notice.
261      *
262      * The default creates the <li>; we skip, since the ConversationTree
263      * takes care of that.
264      *
265      * @return void
266      */
267
268     function showStart()
269     {
270         return;
271     }
272
273     /**
274      * finish the notice
275      *
276      * The default closes the <li>; we skip, since the ConversationTree
277      * takes care of that.
278      *
279      * @return void
280      */
281
282     function showEnd()
283     {
284         return;
285     }
286
287     /**
288      * show link to notice conversation page
289      *
290      * Since we're only used on the conversation page, we skip this
291      *
292      * @return void
293      */
294
295     function showContext()
296     {
297         return;
298     }
299 }