gnu-social/actions/conversation.php

299 lines
6.9 KiB
PHP
Raw Normal View History

2009-02-28 23:12:31 +00:00
<?php
/**
* Display a conversation in the browser
*
* PHP version 5
*
* @category Action
* @package Laconica
* @author Evan Prodromou <evan@controlyourself.ca>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://laconi.ca/
*
* Laconica - a distributed open-source microblogging tool
* Copyright (C) 2009, Control Yourself, Inc.
2009-02-28 23:12:31 +00:00
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('LACONICA')) {
exit(1);
}
require_once INSTALLDIR.'/lib/noticelist.php';
2009-04-03 21:12:33 +01:00
2009-02-28 23:12:31 +00:00
/**
* Conversation tree in the browser
*
* @category Action
* @package Laconica
* @author Evan Prodromou <evan@controlyourself.ca>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://laconi.ca/
*/
2009-05-15 18:55:49 +01:00
2009-02-28 23:12:31 +00:00
class ConversationAction extends Action
{
var $id = null;
2009-02-28 23:12:31 +00:00
var $page = null;
/**
* Initialization.
*
* @param array $args Web and URL arguments
*
* @return boolean false if id not passed in
*/
function prepare($args)
{
parent::prepare($args);
$this->id = $this->trimmed('id');
2009-04-03 21:12:33 +01:00
if (empty($this->id)) {
2009-02-28 23:12:31 +00:00
return false;
}
$this->page = $this->trimmed('page');
if (empty($this->page)) {
$this->page = 1;
}
return true;
}
/**
* Handle the action
*
* @param array $args Web and URL arguments
*
* @return void
*/
2009-02-28 23:12:31 +00:00
function handle($args)
{
2009-04-03 21:12:33 +01:00
parent::handle($args);
2009-02-28 23:12:31 +00:00
$this->showPage();
}
/**
* Returns the page title
*
* @return string page title
*/
2009-02-28 23:12:31 +00:00
function title()
{
return _("Conversation");
}
/**
* Show content.
*
* Display a hierarchical unordered list in the content area.
* Uses ConversationTree to do most of the heavy lifting.
*
* @return void
*/
2009-02-28 23:12:31 +00:00
function showContent()
{
// FIXME this needs to be a tree, not a list
2009-04-03 21:12:33 +01:00
$qry = 'SELECT * FROM notice WHERE conversation = %s ';
$offset = ($this->page-1) * NOTICES_PER_PAGE;
2009-04-03 21:12:33 +01:00
$limit = NOTICES_PER_PAGE + 1;
$txt = sprintf($qry, $this->id);
$notices = Notice::getStream($txt,
'notice:conversation:'.$this->id,
$offset, $limit);
2009-05-15 18:55:49 +01:00
$ct = new ConversationTree($notices, $this);
2009-02-28 23:12:31 +00:00
2009-05-15 18:55:49 +01:00
$cnt = $ct->show();
2009-02-28 23:12:31 +00:00
$this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
$this->page, 'conversation', array('id' => $this->id));
}
}
/**
* Conversation tree
*
* The widget class for displaying a hierarchical list of notices.
*
* @category Widget
* @package Laconica
* @author Evan Prodromou <evan@controlyourself.ca>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://laconi.ca/
*/
2009-05-15 18:55:49 +01:00
class ConversationTree extends NoticeList
{
var $tree = null;
2009-05-15 18:55:49 +01:00
var $table = null;
/**
* Show the tree of notices
*
* @return void
*/
2009-05-15 18:55:49 +01:00
function show()
{
$cnt = 0;
$this->tree = array();
$this->table = array();
2009-05-15 18:55:49 +01:00
while ($this->notice->fetch()) {
2009-05-15 18:55:49 +01:00
$cnt++;
$id = $this->notice->id;
$notice = clone($this->notice);
$this->table[$id] = $notice;
2009-05-15 18:55:49 +01:00
if (is_null($notice->reply_to)) {
$this->tree['root'] = array($notice->id);
2009-05-15 18:55:49 +01:00
} else if (array_key_exists($notice->reply_to, $this->tree)) {
$this->tree[$notice->reply_to][] = $notice->id;
} else {
$this->tree[$notice->reply_to] = array($notice->id);
}
}
$this->out->elementStart('div', array('id' =>'notices_primary'));
$this->out->element('h2', null, _('Notices'));
$this->out->elementStart('ol', array('class' => 'notices xoxo'));
2009-05-15 18:55:49 +01:00
if (array_key_exists('root', $this->tree)) {
$rootid = $this->tree['root'][0];
$this->showNoticePlus($rootid);
2009-05-15 18:55:49 +01:00
}
$this->out->elementEnd('ol');
2009-05-15 18:55:49 +01:00
$this->out->elementEnd('div');
return $cnt;
}
/**
* Shows a notice plus its list of children.
*
* @param integer $id ID of the notice to show
*
* @return void
*/
2009-05-15 18:55:49 +01:00
function showNoticePlus($id)
{
$notice = $this->table[$id];
// We take responsibility for doing the li
$this->out->elementStart('li', array('class' => 'hentry notice',
'id' => 'notice-' . $this->notice->id));
$item = $this->newListItem($notice);
$item->show();
if (array_key_exists($id, $this->tree)) {
$children = $this->tree[$id];
$this->out->elementStart('ol', array('class' => 'notices'));
2009-05-15 18:55:49 +01:00
foreach ($children as $child) {
$this->showNoticePlus($child);
}
$this->out->elementEnd('ol');
2009-05-15 18:55:49 +01:00
}
$this->out->elementEnd('li');
}
/**
* Override parent class to return our preferred item.
*
* @param Notice $notice Notice to display
*
* @return NoticeListItem a list item to show
*/
2009-05-15 18:55:49 +01:00
function newListItem($notice)
{
return new ConversationTreeItem($notice, $this->out);
}
}
/**
* Conversation tree list item
*
* Special class of NoticeListItem for use inside conversation trees.
*
* @category Widget
* @package Laconica
* @author Evan Prodromou <evan@controlyourself.ca>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://laconi.ca/
*/
2009-05-15 18:55:49 +01:00
class ConversationTreeItem extends NoticeListItem
{
/**
* start a single notice.
*
* The default creates the <li>; we skip, since the ConversationTree
* takes care of that.
*
* @return void
*/
2009-05-15 18:55:49 +01:00
function showStart()
{
return;
2009-05-15 18:55:49 +01:00
}
/**
* finish the notice
*
* The default closes the <li>; we skip, since the ConversationTree
* takes care of that.
*
* @return void
*/
2009-05-15 18:55:49 +01:00
function showEnd()
{
return;
2009-05-15 18:55:49 +01:00
}
/**
* show link to notice conversation page
*
* Since we're only used on the conversation page, we skip this
*
* @return void
*/
2009-05-15 18:55:49 +01:00
function showContext()
{
return;
2009-05-15 18:55:49 +01:00
}
}