<?php
/*
* Copyright (c) 2003 gencon Ltd, all rights reserved.
*
* This file is part of v-creator.
*
* v-creator is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* v-creator 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
require_once(VC_ROOT."/classes/VCMaintenance.php");
/**
* @ingroup VCmodules
* @brief Class for comment processing for post boards.
*
* $Revision: 1.2 $ $Date: 2005-09-26 10:22:34 $
*
* @author Andrew 'Diddymus' Rolfe
*
* This module is used to maintain and access the comments
* table and provide commenting functionality. The main use
* intended for this module is to attach comments to the
* postBoard module.
*/
class postComments extends VCMaintenance {
/**
* @brief Function to setup defines.
*
* When called this function sets up the following
* defines for use with the postBoard module:
*
* <DL>
* <DT>PostComments_BY_ID
* <DD>Allows comments to be retrieved in posting ID order.
* <DT>PostComments_BY_THREAD
* <DD>Allows comments to be retrieved in thread order.
* <DT>PostComments_SUMMARY
* <DD>Indicates that the summary dataSet format should be retrieved.
* </DL>
*
* @static
*/
function defines() {
define("PostComments_BY_ID", "0");
define("PostComments_BY_THREAD", "1");
define("PostComments_SUMMARY", "S");
define("PostComments_THREAD", "T");
}
function process() {
VCMaintenance::process(new postComments);
}
/**
* @brief Function to retrieve postboard comment data.
*
* This function is used to retrieve data on comments for postboard
* postings. There are two possible dataSet formats that can be retrieved
* based on PostCommentsFormat. If PostCommentsFormat is not set the
* dataSet values returned are:
*
* <DL>
* <DT>id
* <DD>A unique id generated for the comment.
* <DT>postboard_id
* <DD>The unique ID of the original PostBoard article.
* <DT>reply_id
* <DD>The unique ID of the comment this comment is in response to.
* <DT>title
* <DD>The title for this comment.
* <DT>text
* <DD>The actual text of the comment.
* <DT>author_id
* <DD>The unique id of the posting user.
* <DT>post_timestamp
* <DD>Timestamp value recording when the comment was posted.
* </DL>
*
* If PostCommentsFormat is set to PostComments_SUMMARY the dataSet
* values returned are:
*
* <DL>
* <DT>postboard_id
* <DD>The unique ID of the original PostBoard article.
* <DT>count
* <DD>The number of comments available
* </DL>
*
* The following values from VC_data will be used if available:
*
* <DL>
* <DT>PostCommentsFormat
* <DD>The format of the dataset to return. It should be left unset
* or set to PostComments_SUMMARY.
* <DD>This should be set to either PostComments_BY_ID or
* <DT>PostCommentsOrder
* <DD>This should be set to either PostComments_BY_ID or
* PostComments_BY_THREAD.
* <DT>PostCommentId
* <DD>This should be set to select a single specific comment by unique id.
* <DT>PostCommentsBoardId
* <DD>This should be set to to retrieve comments for a single specific
* postboard article.
* </DL>
*/
function getDataSet() {
global $VC_data;
if (isset($VC_data['PostCommentsFormat'])) {
if ($VC_data['PostCommentsFormat'] == 'S') {
$dataSet = PostComments::_i_getSummaryDataSet();
} elseif ($VC_data['PostCommentsFormat'] == 'T') {
$dataSet = PostComments::_i_getThreadDataSet();
}
} else {
$dataSet = PostComments::_i_getFullDataSet();
}
return $dataSet;
}
function _i_getSummaryDataSet() {
global $VC_data;
$dataSet = array(array('postboard_id','count','last_post_date'));
$where = "";
if ($VC_data['PostCommentsBoardId'] != null) {
$where .= " postboard_id = " .$VC_data['PostCommentsBoardId']. " ";
}
if ($where) $where = ' WHERE '.$where;
$sql = 'SELECT postboard_id, count(*) as count, '.
'UNIX_TIMESTAMP(max(post_timestamp)) as last_post_date '.
'FROM postComments'.
$where.
' GROUP BY postboard_id';
$result = $VC_data['VC_link']->execute($sql);
while ($row = $VC_data['VC_link']->getNextRow($result)) {
$dataSet[] = array($row['postboard_id'],$row['count'],
$row['last_post_date']);
}
return $dataSet;
}
function _i_getFullDataSet() {
global $VC_data;
$dataSet = array(array('id','postboard_id','reply_id','title',
'text','author_id','post_timestamp','indent'));
$where = "";
$order = "";
if (isset($VC_data['PostCommentsId'])) {
$where = " id = '" .$VC_data['PostCommentsId']. "' ";
}
if (isset($VC_data['PostCommentsBoardId'])) {
if ($where) $where .= ' AND ';
$where .= " postboard_id = '" .$VC_data['PostCommentsBoardId']. "' ";
}
if ($where) $where = ' WHERE '.$where;
if (isset($VC_data['PostCommentsOrder'])) {
switch ($VC_data['PostCommentsOrder']) {
case PostComments_BY_ID:
$order = ' id';
break;
case PostComments_BY_THREAD:
$order .= ' reply_id';
break;
}
}
if ($order) $order = ' ORDER BY '.$order;
$sql = 'SELECT id, postboard_id, reply_id, title, text, '.
'author_id, UNIX_TIMESTAMP(post_timestamp) as post_timestamp FROM postComments'.
$where.$order;
$result = $VC_data['VC_link']->execute($sql);
while ($row = $VC_data['VC_link']->getNextRow($result)) {
$dataSet[] = array($row['id'],$row['postboard_id'],$row['reply_id'],
$row['title'],$row['text'],
$row['author_id'],$row['post_timestamp'],160);
}
return $dataSet;
}
/**
*
*/
function _i_getThreadDataSet() {
global $VC_data;
$dataSet = array(array('id','postboard_id','reply_id','title',
'text','author_id','post_timestamp', 'indent'));
$commentOrder = array();
$commentIndent = array();
$threadStack = array();
$threadStack[] = array(0);
$where = "";
$order = "";
$boardid = $VC_data['SelectedId'];
if ($VC_data['PostCommentsBoardId']) {
if ($where) $where .= ' AND ';
$where .= " postboard_id = '" .$VC_data[PostCommentsBoardId]. "' ";
}
// Calculate the thread ordering for the comments
while (count($threadStack)) {
$currentid = $threadStack[count($threadStack)-1][0];
$sql = 'SELECT id FROM postComments WHERE postboard_id='.
$boardid.' and reply_id='.$currentid;
$result = $VC_data['VC_link']->execute($sql);
$newrow = array();
while ($row = $VC_data['VC_link']->getNextRow($result)) {
$newrow[] = $row[0];
}
$commentOrder[] = $threadStack[count($threadStack)-1][0];
$commentIndent[] = count($threadStack)-1;
array_splice($threadStack[count($threadStack)-1],0,1);
if ($newrow) $threadStack[] = $newrow;
if (!count($threadStack[count($threadStack)-1])) {
array_splice($threadStack,count($threadStack)-1,1);
}
}
// Now extract the comments one at a time based on the calculated
// threading order.
foreach ($commentOrder as $key=>$id) {
$indent = (($commentIndent["$key"]-1) * 50)+160;
$sql = 'SELECT id, postboard_id, reply_id, title, text, '.
'author_id, UNIX_TIMESTAMP(post_timestamp) FROM postComments'.
' where id='.$id;
$result = $VC_data['VC_link']->execute($sql);
while ($row = $VC_data['VC_link']->getNextRow($result)) {
$dataSet[] = array($row['id'],$row['postboard_id'],$row['reply_id'],
$row['title'],$row['text'],
$row['author_id'],$row['post_timestamp'],$indent);
}
}
return $dataSet;
}
/**
*
*/
function update() {
global $VC_data;
$id = VCPage::getVC_data('id');
$postboard_id = VCPage::getVC_data('postboard_id');
$reply_id = VCPage::getVC_data('reply_id');
$title = VCPage::getVC_data('title');
$text = VCPage::getVC_data('text');
$author_id = VCPage::getVC_data('author_id');
$post_timestamp = VCPage::getVC_data('post_timestamp');
$sql = "UPDATE postComments SET postboard_id='".$postboard_id."', reply_id='".$reply_id."', title='".$title."', text='".$text."', author_id='".$author_id."', post_timestamp='".$post_timestamp."' WHERE id=".$id;
$VC_data['VC_link']->run($sql);
}
function delete() {
global $VC_data;
$id = VCPage::getVC_data('id');
$sql = "DELETE FROM postComments where id=".$id;
$VC_data['VC_link']->run($sql);
}
function add() {
global $VC_data;
$postboard_id = VCPage::getVC_data('postboard_id');
$reply_id = VCPage::getVC_data('reply_id');
$title = VCPage::getVC_data('title');
$text = VCPage::getVC_data('text');
$author_id = VCPage::getVC_data('author_id');
$sql = "INSERT INTO postComments (postboard_id, reply_id, title, text, author_id, post_timestamp) VALUES($postboard_id, $reply_id, '$title', '$text', '$author_id', NOW())";
$VC_data['VC_link']->run($sql);
}
}
?>