gnu-social/plugins/ConversationTree/lib/conversationtree.php

133 lines
3.7 KiB
PHP
Raw Normal View History

2011-09-24 22:29:12 +09:00
<?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
defined('GNUSOCIAL') || die();
2011-09-24 22:29:12 +09:00
/**
* Conversation tree
*
* The widget class for displaying a hierarchical list of notices.
*
* @category Widget
* @package ConversationTreePlugin
* @author Evan Prodromou <evan@status.net>
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
2011-09-24 22:29:12 +09:00
*/
class ConversationTree extends NoticeList
{
public $tree = null;
public $table = null;
2011-09-24 22:29:12 +09:00
/**
* Show the tree of notices
*
* @return int
2011-09-24 22:29:12 +09:00
*/
public function show(): int
2011-09-24 22:29:12 +09:00
{
$cnt = $this->_buildTree();
$this->out->elementStart('div', ['id' => 'notices_primary']);
2011-09-24 22:29:12 +09:00
// TRANS: Header on conversation page. Hidden by default (h2).
$this->out->element('h2', null, _('Notices'));
$this->out->elementStart('ol', ['class' => 'notices xoxo old-school']);
2011-09-24 22:29:12 +09:00
if (array_key_exists('root', $this->tree)) {
$rootid = $this->tree['root'][0];
$this->showNoticePlus($rootid);
}
$this->out->elementEnd('ol');
$this->out->elementEnd('div');
return $cnt;
}
public function _buildTree(): int
2011-09-24 22:29:12 +09:00
{
$cnt = 0;
$this->tree = [];
$this->table = [];
2011-09-24 22:29:12 +09:00
while ($this->notice->fetch()) {
$cnt++;
$id = $this->notice->id;
2011-09-24 22:29:12 +09:00
$notice = clone($this->notice);
$this->table[$id] = $notice;
if (is_null($notice->reply_to)) {
$this->tree['root'] = [$notice->id];
} elseif (array_key_exists($notice->reply_to, $this->tree)) {
2011-09-24 22:29:12 +09:00
$this->tree[$notice->reply_to][] = $notice->id;
} else {
$this->tree[$notice->reply_to] = [$notice->id];
2011-09-24 22:29:12 +09:00
}
}
return $cnt;
}
/**
* Shows a notice plus its list of children.
*
* @param integer $id ID of the notice to show
*
* @return void
*/
public function showNoticePlus($id): void
2011-09-24 22:29:12 +09:00
{
$notice = $this->table[$id];
$this->out->elementStart('li', ['class' => 'h-entry notice',
'id' => 'notice-' . $id]);
2011-09-24 22:29:12 +09:00
$item = $this->newListItem($notice);
$item->show();
if (array_key_exists($id, $this->tree)) {
$children = $this->tree[$id];
$this->out->elementStart('ol', ['class' => 'notices threaded-replies xoxo']);
2011-09-24 22:29:12 +09:00
sort($children);
foreach ($children as $child) {
$this->showNoticePlus($child);
}
$this->out->elementEnd('ol');
}
$this->out->elementEnd('li');
}
/**
* Override parent class to return our preferred item.
*
* @param Notice $notice Notice to display
*
* @return ConversationTreeItem a list item to show
2011-09-24 22:29:12 +09:00
*/
public function newListItem(Notice $notice): ConversationTreeItem
2011-09-24 22:29:12 +09:00
{
return new ConversationTreeItem($notice, $this->out);
}
}