<?php
/** * yvComment - A User Comments Component, developed for Joomla 1.5
* @version 1.1.1
* @package yvComment
* @(c) 2007 yvolk (Yuri Volkov), http://yurivolkov.com. All rights reserved.
* @license GPL
**/
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();
jimport('joomla.filter.output');
jimport('joomla.application.component.model');
// TODO: Do not allow guest links if they are turned off.
/**
* yvComments Component "comment" Model
*
*/
class yvcommentModelcomment extends JModel
{
/**
* yvCommentID
*
* @var int
*/
var $_yvCommentID = 0;
var $_ArticleID = 0;
var $_task = '';
var $_url = '';
// Article, commented by this comment
var $_ArticleRow = null;
// Parent article of Article, commented by this comment
var $_parentArticleRow = null;
// If we can add comments to this article (based on its SectionID)
var $_ArticleSectionAddEnabled = null;
/**
* Comments for selected ArticleID
*
* @var array
*/
var $_data = null;
var $_null = null;
// Current Comment in $_data
var $_indCurrent = -1;
// Total number of rows (without pagination)
var $_dataRowsTotal = 0;
var $_pagination = null; //JPagination
var $_Message = array();
// Configuration settings
var $_sectionidConfig = 0;
var $_hide_title = false;
function __construct()
{
parent::__construct();
global $yvComment;
//echo 'Constructor of yvcommentModelComment <br />';
$this->_sectionidConfig = $yvComment->getConfigValue('sectionid', '0');
$this->_hide_title = (boolean) ($yvComment->getConfigValue('hide_title', '0'));
}
/**
* Method to set identifiers...
*
* @access public
*/
function setParms($ArticleID, $yvCommentID = 0, $task = 'add', $url = '', $filter_state = 'P')
{
// Set yvComment ID and wipe data
$this->_ArticleID = $ArticleID;
$this->_yvCommentID = $yvCommentID;
$this->_url = $url;
$this->_data = null;
$this->setTask($task);
//echo 'setParms id=' . $this->_yvCommentID . '; ArticleID=' . $this->_ArticleID . '; task=' . $task .'<br/>';
}
function getArticleSectionAddEnabled() {
global $yvComment;
if ($this->_ArticleSectionAddEnabled == null) {
$this->_ArticleSectionAddEnabled = false;
if ($this->_ArticleID != 0) {
// Check if we can add comments to this article
global $yvComment;
$articlesectionids = trim($yvComment->getConfigValue('articlesectionids', ''));
if (strlen($articlesectionids) == 0) {
$this->_ArticleSectionAddEnabled = true;
} else {
$ArticleSectionID = $yvComment->DLookup('sectionid','#__content','id=' . $this->_ArticleID);
$blnExclude = (bool) $yvComment->getConfigValue('articlesectionids_exclude', '0');
$blnFound = false;
// Find $ArticleSectionID in this list
$array1 = explode(",", $articlesectionids);
foreach ($array1 as $sectionid1) {
if ((int)$sectionid1 == $ArticleSectionID) {
$blnFound = true;
break;
}
}
if ($blnFound) {
$this->_ArticleSectionAddEnabled = !$blnExclude;
} else {
$this->_ArticleSectionAddEnabled = $blnExclude;
}
}
}
}
return $this->_ArticleSectionAddEnabled;
}
/**
* Rows of Comments...
*
*/
function &getData()
{
if ($this->_loadData('data'))
{
// Initialize some variables
// ...
}
//else $this->_initData();
return $this->_data;
}
// Current Comment
function &getCurrentItem()
{
$Ok = false;
if ($this->_indCurrent >= 0) {
$Ok = true;
} else if ( $this->_yvCommentID != 0) {
$this->_loadData('current');
if ($this->_indCurrent >= 0) {
$Ok = true;
}
}
if ($Ok) {
return $this->_data[$this->_indCurrent];
} else {
return $this->_null;
}
}
function getPagination() {
$this->_loadData('pagination');
return $this->_pagination;
}
// $mode = 'pagination', 'data', 'current'
function _loadData($mode = 'pagination')
{
global $yvComment;
$user =& JFactory::getUser();
$Ok = false;
// Lets load the content if it doesn't already exist
if ($this->_sectionidConfig == 0) {
// Nothing to load
} elseif (empty($this->_pagination) ||
( (($mode == 'data') || ($mode == 'current'))
&& empty($this->_data))) {
$Select = 'c.*, u.name AS AuthorName';
$From = $yvComment->getTableName() . ' AS c' .
' LEFT JOIN #__users AS u ON u.id=c.created_by';
$Where = '';
$OrderBy = '';
if ($this->_ArticleID != 0) {
$Where = '(c.parentid=' . $this->_ArticleID . ')';
} else {
$Where = '(c.sectionid=' . $this->_sectionidConfig . ')';
}
$From = '(' . $From . ') LEFT JOIN #__content AS ar ON c.parentid=ar.id' ;
$Select .= ', ar.title AS ArticleTitle, ar.created_by AS ArticleAuthorID, ar.created_by_alias AS ArticleAuthorAlias';
$From = '(' . $From . ') LEFT JOIN #__users AS aru ON aru.id=ar.created_by' ;
$Select .= ', aru.name AS ArticleAuthorName';
$author_linkable = $yvComment->getConfigValue('author_linkable', '0');
if ($author_linkable) {
$Select .= ', cd.webpage';
$From = '(' . $From . ') LEFT JOIN #__contact_details AS cd ON cd.user_id = c.created_by';
}
if ($this->_ArticleID != 0) {
$Where .= ' AND (c.parentid=' . $this->_ArticleID . ')';
if ($yvComment->UserCanEdit() != 'all') {
$Where .= ' AND c.state=1';
}
} else if ( $this->_yvCommentID != 0) {
$Where .= ' AND c.id=' . $this->_yvCommentID;
//echo ' $this->_yvCommentID=' . $this->_yvCommentID . ' count=' . count($this->_data);
}
switch ($yvComment->getPageValue('orderby_pri', 'default')) {
case 'date' :
$OrderBy = 'c.created ASC';
break;
default :
$OrderBy = 'c.created DESC';
}
if (empty($this->_pagination)) {
jimport('joomla.html.pagination');
$this->_dataRowsTotal = $yvComment->DLookup_db($this->_db,'Count(*)', $From, $Where);
if ($this->_db->getErrorNum() != 0) {
$this->appendMessage($yvComment->printDbErrors($this->_db));
} else {
$Ok = true;
}
$limitstart = $yvComment->getPageValue('yvcomment_limitstart', '0'); // offset from first row returned
$limit = $yvComment->getPageValue('yvcomment_limit', '0'); // number of rows returned
$this->_pagination = new yvCommentJPagination($this->_dataRowsTotal, $limitstart, $limit);
//$this->appendMessage('limit=' . $limit . '; limitstart=' . $limitstart);
}
if ( (($mode == 'data') || ($mode == 'current'))
&& empty($this->_data)) {
if (($mode == 'current') && ($this->_ArticleID != 0)) {
$Where .= ' AND c.id=' . $this->_yvCommentID;
}
$query = 'SELECT ' . $Select
. ' FROM ' . $From
. ' WHERE ' . $Where
. ' ORDER BY ' . $OrderBy;
$this->_data = $this->_getList($query, $this->_pagination->limitstart, $this->_pagination->limit);
if ($this->_db->getErrorNum() != 0) {
$this->appendMessage($yvComment->printDbErrors($this->_db));
} else {
$Ok = (boolean) $this->_data;
}
//echo ' this->_yvCommentID=' . $this->_yvCommentID . ' count=' . count($this->_data);
}
} else {
$Ok = true;
}
if (!empty($this->_data) and ($this->_ArticleID == 0)) {
if (count($this->_data) > 0) {
$this->_ArticleID = $this->_data[0]->parentid;
}
}
if (!empty($this->_data) and ($mode == 'current')) {
// Find current Comment
$indCurrent = -1;
$nComments = count($this->_data);
for ($i=0; $i < $nComments; $i++) {
//echo 'i=' . $i . ' - ' . $this->_data[$i]->id . '; ';
if ($this->_yvCommentID == $this->_data[$i]->id ) {
$indCurrent = $i;
break;
}
}
$this->_indCurrent = $indCurrent;
}
return $Ok;
}
function &getArticleID()
{
//echo 'getArticleID ' . $this->_ArticleID;
return $this->_ArticleID;
}
// Article, commented by this comment
function &getArticle()
{
gl