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