]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ConversationTree/lib/conversationtree.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / ConversationTree / lib / conversationtree.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Conversation tree widget for oooooold school playas
7  * 
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Widget
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('GNUSOCIAL')) { exit(1); }
32
33 /**
34  * Conversation tree
35  *
36  * The widget class for displaying a hierarchical list of notices.
37  *
38  * @category Widget
39  * @package  StatusNet
40  * @author   Evan Prodromou <evan@status.net>
41  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
42  * @link     http://status.net/
43  */
44 class ConversationTree extends NoticeList
45 {
46     var $tree  = null;
47     var $table = null;
48
49     /**
50      * Show the tree of notices
51      *
52      * @return void
53      */
54     function show()
55     {
56         $cnt = $this->_buildTree();
57
58         $this->out->elementStart('div', array('id' =>'notices_primary'));
59         // TRANS: Header on conversation page. Hidden by default (h2).
60         $this->out->element('h2', null, _('Notices'));
61         $this->out->elementStart('ol', array('class' => 'notices xoxo old-school'));
62
63         if (array_key_exists('root', $this->tree)) {
64             $rootid = $this->tree['root'][0];
65             $this->showNoticePlus($rootid);
66         }
67
68         $this->out->elementEnd('ol');
69         $this->out->elementEnd('div');
70
71         return $cnt;
72     }
73
74     function _buildTree()
75     {
76         $cnt = 0;
77
78         $this->tree  = array();
79         $this->table = array();
80
81         while ($this->notice->fetch()) {
82
83             $cnt++;
84
85             $id     = $this->notice->id;
86             $notice = clone($this->notice);
87
88             $this->table[$id] = $notice;
89
90             if (is_null($notice->reply_to)) {
91                 $this->tree['root'] = array($notice->id);
92             } else if (array_key_exists($notice->reply_to, $this->tree)) {
93                 $this->tree[$notice->reply_to][] = $notice->id;
94             } else {
95                 $this->tree[$notice->reply_to] = array($notice->id);
96             }
97         }
98
99         return $cnt;
100     }
101
102     /**
103      * Shows a notice plus its list of children.
104      *
105      * @param integer $id ID of the notice to show
106      *
107      * @return void
108      */
109     function showNoticePlus($id)
110     {
111         $notice = $this->table[$id];
112
113         $this->out->elementStart('li', array('class' => 'h-entry notice',
114                                              'id' => 'notice-' . $id));
115
116         $item = $this->newListItem($notice);
117         $item->show();
118
119         if (array_key_exists($id, $this->tree)) {
120             $children = $this->tree[$id];
121
122             $this->out->elementStart('ol', array('class' => 'notices threaded-replies xoxo'));
123
124             sort($children);
125
126             foreach ($children as $child) {
127                 $this->showNoticePlus($child);
128             }
129
130             $this->out->elementEnd('ol');
131         }
132
133         $this->out->elementEnd('li');
134     }
135
136     /**
137      * Override parent class to return our preferred item.
138      *
139      * @param Notice $notice Notice to display
140      *
141      * @return NoticeListItem a list item to show
142      */
143     function newListItem(Notice $notice)
144     {
145         return new ConversationTreeItem($notice, $this->out);
146     }
147 }