]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/conversation.php
f3beade6c77b6772541a54534d4b724a8890e8c5
[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) 2008, Controlez-Vous, 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 require_once(INSTALLDIR.'/lib/noticelist.php');
35
36 /**
37  * Conversation tree in the browser
38  *
39  * @category Action
40  * @package  Laconica
41  * @author   Evan Prodromou <evan@controlyourself.ca>
42  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
43  * @link     http://laconi.ca/
44  */
45
46 class ConversationAction extends Action
47 {
48     var $id = null;
49     var $page = null;
50
51     /**
52      * Initialization.
53      *
54      * @param array $args Web and URL arguments
55      *
56      * @return boolean false if id not passed in
57      */
58
59     function prepare($args)
60     {
61         parent::prepare($args);
62         $this->id = $this->trimmed('id');
63         if (empty($this->id)) {
64             return false;
65         }
66         $this->page = $this->trimmed('page');
67         if (empty($this->page)) {
68             $this->page = 1;
69         }
70         return true;
71     }
72
73     function handle($args)
74     {
75         parent::handle($args);
76         $this->showPage();
77     }
78
79     function title()
80     {
81         return _("Conversation");
82     }
83
84     function showContent()
85     {
86         // FIXME this needs to be a tree, not a list
87
88         $qry = 'SELECT * FROM notice WHERE conversation = %s ';
89
90         $offset = ($this->page-1)*NOTICES_PER_PAGE;
91         $limit  = NOTICES_PER_PAGE + 1;
92
93         $txt = sprintf($qry, $this->id);
94
95         $notices = Notice::getStream($txt,
96                                      'notice:conversation:'.$this->id,
97                                      $offset, $limit);
98
99         $ct = new ConversationTree($notices, $this);
100
101         $cnt = $ct->show();
102
103         $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
104                           $this->page, 'conversation', array('id' => $this->id));
105     }
106
107 }
108
109 class ConversationTree extends NoticeList
110 {
111     var $tree = null;
112     var $table = null;
113
114     function show()
115     {
116         $cnt = 0;
117
118         $this->tree = array();
119         $table = array();
120
121         while ($this->notice->fetch()) {
122             $cnt++;
123             $this->table[$this->notice->id] = clone($this->notice);
124             if (is_null($notice->reply_to)) {
125                 // We assume no notice has -1 ID
126                 $this->tree[-1] = array($notice->id);
127             } else if (array_key_exists($notice->reply_to, $this->tree)) {
128                 $this->tree[$notice->reply_to][] = $notice->id;
129             } else {
130                 $this->tree[$notice->reply_to] = array($notice->id);
131             }
132         }
133
134         $this->out->elementStart('div', array('id' =>'notices_primary'));
135         $this->out->element('h2', null, _('Notices'));
136         $this->out->elementStart('ul', array('class' => 'notices'));
137
138         if (array_key_exists(-1, $this->tree)) {
139             $this->showNoticePlus($this->tree[-1][0]);
140         }
141
142         $this->out->elementEnd('ul');
143         $this->out->elementEnd('div');
144
145         return $cnt;
146     }
147
148     function showNoticePlus($id)
149     {
150         $notice = $this->table[$id];
151
152         print_r($notice);
153
154         // We take responsibility for doing the li
155
156         $this->out->elementStart('li', array('class' => 'hentry notice',
157                                              'id' => 'notice-' . $this->notice->id));
158
159         $item = $this->newListItem($notice);
160         $item->show();
161
162         if (array_key_exists($id, $this->tree)) {
163             $children = $this->tree[$id];
164
165             $this->out->elementStart('ul', array('class' => 'notices'));
166
167             foreach ($children as $child) {
168                 $this->showNoticePlus($child);
169             }
170
171             $this->out->elementEnd('ul');
172         }
173
174         $this->out->elementEnd('li');
175     }
176
177     function newListItem($notice)
178     {
179         return new ConversationTreeItem($notice, $this->out);
180     }
181 }
182
183 class ConversationTreeItem extends NoticeListItem
184 {
185     function showStart()
186     {
187         // skip; ConversationTree draws the list
188     }
189
190     function showEnd()
191     {
192         // skip; ConversationTree draws the list
193     }
194
195     function showContext()
196     {
197         // skip; this _is_ the context!
198     }
199 }