<?php
// QuickTalk 2.5 build:20100924
class cSection
{
// --------
var $domid=0; // Domain id
var $domname=''; // Domain name
var $id=-1; // Section id
var $idtitle='untitled'; // Section idtitle
var $name='untitled'; // Section name (translation)
var $descr=''; // Section descr (translation)
var $type=0; // Type: 0=visible, 1=hidden, 2=hidden by user
var $status=0; // Status: 0=actif, 1=closed
var $notify=1; // Notify: 0=disable, 1=enabled
var $modid=0; // Moderator id
var $modname='Admin'; // Moderaotr name
var $numfield='N'; // Format of the ref number: 'N' means no ref number
var $titlefield=1; // Topic title: 0=None, 1=Optional, 2=Mandatory
//var $eventdate=0; // Topic eventdate: 0=None, 1=Optional, 2=Mandatory, 3=Date+time (optional), 4=Date+time (mandatory)
//var $wisheddate=0; // Topic wisheddate: 0=None, 1=Optional, 2=Mandatory
//var $notifycc=0; // Topic alternate notify: 0=None, 1=Optional, 2=Mandatory
var $prefix='a'; // Prefix icon from the serie 'a'
var $options=''; // Several options
// computed values
var $lastpostid=-1;
var $lastposttopic=-1;
var $lastpostdate='0';
var $lastpostuser=0;
var $lastpostname='Visitor';
// options
var $d_order=''; // Ticket default sort order
var $d_last=''; // Last column in the topic list: 'none' means no last column
var $d_logo=''; // Ovewrite icon with an image-logo named s_x.gif/.jpeg/.jpg/.png (where x is the section id)
// -- Constructor method (accept an id, an array or a cSection as parameter) --
function cSection($aSection=null,$bLast=false)
{
if ( isset($aSection) )
{
if ( is_int($aSection) )
{
if ( $aSection<0 ) die('No section '.$aSection);
global $oDB;
$oDB->Query('SELECT f.*,d.title as domtitle FROM '.TABSECTION.' f INNER JOIN '.TABDOMAIN.' d ON d.id=f.domainid WHERE f.id='.$aSection);
$row = $oDB->Getrow();
if ( $row===False ) die('No section '.$aSection);
$this->MakeFromArray($row);
}
elseif ( is_array($aSection) )
{
$this->MakeFromArray($aSection);
}
else
{
die('Invalid constructor parameter #1 for the class cSection');
}
}
// Read stats and options
$this->ReadOptions();
// Find last post
if ( $bLast ) $this->GetSectionLastPost($bLast);
// Default sort order
if ( $this->d_last=='no' || empty($this->d_last) ) $this->d_last = 'none';
if ( empty($this->d_order) ) $this->d_order='lastpostdate';
}
// --------
function MakeFromArray($aSection)
{
foreach($aSection as $strKey=>$oValue) {
switch ($strKey) {
case 'domainid': $this->domid = intval($oValue); break;
case 'id': $this->id = intval($oValue); break;
case 'title': $this->idtitle = $oValue; break;
case 'type': $this->type = intval($oValue); break;
case 'status': $this->status = intval($oValue); break;
case 'options': $this->options = $oValue; break;
case 'notify': $this->notify = intval($oValue); break;
case 'moderator': $this->modid = intval($oValue); break;
case 'moderatorname':$this->modname = $oValue; break;
case 'numfield': $this->numfield = $oValue; break;
case 'titlefield': $this->titlefield= intval($oValue); break;
//case 'eventdate': $this->eventdate = intval($oValue); break;
//case 'wisheddate': $this->wisheddate= intval($oValue); break;
//case 'alternate': $this->notifycc = intval($oValue); break;
case 'prefix': $this->prefix = $oValue; break;
}}
$this->name = ObjTrans('sec','s'.$this->id,$this->idtitle);
$this->descr = ObjTrans('secdesc','s'.$this->id,false);
$this->domname= ObjTrans('domain','d'.$this->domid,'(domain '.$this->domid.')');
}
// --- [i] ---
function Add($intDom=0,$str='untitled')
{
QTargs( 'cSection->Add',array($intDom,$str),array('int','str') );
if ( empty($str) ) die('cSection->Add: Argument #1 must be a string');
// Inserts a new section
global $oDB;
$oDB->Query( 'INSERT INTO '.TABSECTION.' (domainid,id,type,status,notify,titleorder,moderator,titlefield,title,stats,options,moderatorname,numfield,prefix) VALUES ('.$intDom.','.$oDB->Nextid(TABSECTION).',0,0,1,0,0,1,"'.addslashes($str).'","","coord=0;order=0;last=0;logo=0","Admin","N","a")' );
// Impact on globals
if ( isset($_SESSION[QT]['sys_sections']) ) Unset($_SESSION[QT]['sys_sections']);
}
// ----------
function Delete()
{
if ( $this->id<1 ) die('cSection->Delete: Argument #1 must be an integer'); // section 0 cannot be deleted
// Process
global $oDB;
$this->DeleteTopics();
$oDB->Query('DELETE FROM '.TABSECTION.' WHERE id='.$this->id);
cVIP::LangDel(array('sec','secdesc'),'s'.$this->id);
// Impact on globals
if ( isset($_SESSION[QT]['sys_sections']) ) Unset($_SESSION[QT]['sys_sections']);
}
// --------
function GetLogo()
{
if ( !empty($this->d_logo) )
{
if ( file_exists('upload/section/'.$this->d_logo) ) return 'upload/section/'.$this->d_logo;
}
return $_SESSION[QT]['skin_dir'].'/ico_section_'.$this->type.'_'.$this->status.'.gif';
}
// --------
function GetSectionLastPost($intLast)
{
// Initialize
$this->lastpostid = null;
$this->lastposttopic = null;
$this->lastpostdate = null;
$this->lastpostuser = null;
$this->lastpostname = null;
// Check
if ( $this->id<0 ) return null;
// Query - attention: Subqueries requires mysql 4.1. We use two queries
// Search query can be bypassed by providing a $intLast
global $oDB;
// Search (maxid)
if ( is_integer($intLast) )
{
$intId = $intLast;
}
else
{
$oDB->Query( 'SELECT MAX(id) as maxid FROM '.TABPOST.' WHERE forum='.$this->id );
$row = $oDB->Getrow();
$intId = intval($row['maxid']);
}
// Get lastpost informations
if ( $intId>0 )
{
$oDB->Query( 'SELECT id,topic,issuedate,userid,username FROM '.TABPOST.' WHERE id='.$intId );
if ( $row=$oDB->Getrow() )
{
$this->lastpostid = intval($row['id']);
$this->lastposttopic = intval($row['topic']);
$this->lastpostdate = $row['issuedate'];
$this->lastpostuser = intval($row['userid']);
$this->lastpostname = $row['username'];
}
}
}
// --- [i] ---
function GetTagsUsed($intS=null,$intMax=20)
{
// -1 to compute on all sections
// maximum returned is $intMax distinct tags (from most recents active messages)
// Allow instanciation
if ( !isset($intS) ) $intS = $this->id;
// Check
if ( !is_int($intS) ) die('cSection->GetTagsUsed: Argument #1 must be integer');
// Process
$arrTags = array();
global $oDB;
$oDB->Query( 'SELECT DISTINCT tags, lastpostdate FROM '.TABTOPIC.($intS==-1 ? '' : ' WHERE forum='.$intS).' ORDER BY lastpostdate DESC' );
$i=0;
while($row=$oDB->Getrow()) {
if ( !empty($row['tags']) ) {
$arr = explode(';',$row['tags']);
foreach($arr as $str)
{
if ( !empty($str) ) {
if ( !in_array($str,$arrTags) ) {
$arrTags[$str] = $str;
$i++;
if ( $i>$intMax ) break;
}}
}
if ( $i>$intMax ) break;
}}
if ( count($arrTags)>2 ) asort($arrTags);
return $arrTags;
}
// --------
function MoveTopics($intD=0,$intRenum=1,$intTopic=-1,$bClosedOnly=false,$strYear='')
{
if ( !is_int($intD) ) die('cSection->MoveTopics: Argument #2 must be integer');
if ( $this->id<0 ) die('cSection->MoveTopics: Wrong argument #1 (id<0)');
if ( $intD<0 ) die('cSection->MoveTopics: Wrong argument #2 (d<1)');
if ( $this->id==$intD ) die('cSection->MoveTopics: Wrong argument, source=destination');
if ( !is_string($strYear) ) $strYear = intval($strYear); // $strYear can be a integer or "old"
if ( strlen($strYear)>4 ) die('cSection->MoveTopics: Argument #2 must be a string');
global $oDB;
$strWhere = (empty($strYear) ? '' : ' AND '.SqlDateCondition($strYear));
$strTopic = '';
$strPost = '';
$strField = ''; if ( $intRenum==0 ) $strField = ', numid = 0';
// Move only one topic
if ( $intTopic>=0 )
{
$strTopic=" AND id=$intTopic";
$strPost=" AND topic=$intTopic";
if ( $intRenum==2 )
{
$nextnumid = $oDB->Nextid(TABTOPIC,'numid','WHERE forum='.$intD);
$strField = ', numid='.intval($nextnumid);
}
$bClosedOnly=false;
}
else
{
if ( $intRenum==2 )
{
$nextnumid = $oDB->Nextid(TABTOPIC,'numid','WHERE forum='.$intD);
$oDB->Query('SELECT MIN(numid) as minnumid FROM '.TABTOPIC.' WHERE forum='.$this->id);
$row = $oDB->Getrow();
$minnumid = $row['minnumid'];
$strField = ", numid = $nextnumid + (numid - $minnumid)";
}
}
// Check if prefix system are the same. If not, remove the prefix.
$strSetPrefix = '';
if ( !empty($this->prefix) )
{
$oDB->Query('SELECT prefix FROM '.TABSECTION.' WHERE id='.$intD);
$row = $oDB->Getrow();
if ( $row['prefix']!=$this->prefix ) $strSetPrefix = ',icon="00"';
}
// Update topics and posts
if ( $bClosedOnly )
{
$arr = array();
$oDB->Query('SELECT id FROM '.TABTOPIC.' WHERE forum='.$this->id.' AND status="1" AND type="T"'.$strWhere);
while($row=$oDB->Getrow()) $arr[] = intval($row['id']);
foreach($arr as $intVal)
{
$oDB->Query( 'UPDATE '.TABTOPIC.' SET forum='.$intD.$strField.',modifdate="'.date('Ymd His').'" WHERE id='.$intVal );
$oDB->Query( 'UPDATE '.TABPOST.' SET forum='.$intD.$strSetPrefix.' WHERE topic='.$intVal );
}
}
else
{
$oDB->Query( 'UPDATE '.TABTOPIC.' SET forum='.$intD.$strField.',modifdate="'.date('Ymd His').'" WHERE forum='.$this->id.$strTopic.$strWhere );
$oDB->Query( 'UPDATE '.TABPOST.' SET forum='.$intD.$strSetPrefix.' WHERE forum='.$this->id.$strPost.str_replace('firstpostdate','issuedate',$strWhere) );
}
}
function DeleteTopics($bClosedOnly=false,$strYear='')
{
if ( $this->id<0 ) die('cSection->DeleteTopics: Wrong argument #1 (id<0)');
if ( !is_string($strYear) ) $strYear = intval($strYear); // $strYear can be a integer or the string 'old'
if ( strlen($strYear)>4 ) die('cSection->DeleteTopics: Argument #2 must be a string');
// Process - delete topics and posts
global $oDB;
$strWhere = (empty($strYear) ? '' : ' AND '.SqlDateCondition($strYear));
if ( $bClosedOnly )
{
$arr = array();
$oDB->Query( 'SELECT id FROM '.TABTOPIC.' WHERE forum='.$this->id.' AND status="1" AND type="T"'.$strWhere );
while($row=$oDB->Getrow()) $arr[]=$row['id'];
foreach($arr as $id)
{
$oDB->Query( 'DELETE FROM '.TABPOST.' WHERE topic='.$id );
$oDB->Query( 'DELETE FROM '.TABTOPIC.' WHERE id='.$id );
}
}
else
{
$oDB->Query( 'DELETE FROM '.TABPOST.' WHERE forum='.$this->id.str_replace('firstpostdate','issuedate',$strWhere) );
$oDB->Query( 'DELETE FROM '.TABTOPIC.' WHERE forum='.$this->id.$strWhere );
}
}
// --------
function Count($strObject='topics',$intD=10,$strYear='')
{
if ( !is_int($intD) ) die('cSection->Count: Argument #2 must be an int');
if ( $this->id<0 ) die('cSection->Count: Wrong argument (id<0)');
if ( $intD<1 ) die('cSection->Count: Wrong argument #2 (d<1)');
if ( !is_string($strYear) ) $strYear = intval($strYear); // $strYear can be a integer or "old"
if ( strlen($strYear)>4 ) die('cSection->Count: Argument #3 must be a string');
global $oDB;
$strWhere = (empty($strYear) ? '' : ' AND '.SqlDateCondition($strYear));
// Process
switch($strObject)
{
case 'topics': $oDB->Query( 'SELECT count(*) as countid FROM '.TABTOPIC.' WHERE forum='.$this->id.$strWhere ); break;
case 'topicsZ': $oDB->Query( 'SELECT count(*) as countid FROM '.TABTOPIC.' WHERE status="1" AND forum='.$this->id.$strWhere ); break;
case 'news': $oDB->Query( 'SELECT count(*) as countid FROM '.TABTOPIC.' WHERE forum='.$this->id.' AND type="A"'.$strWhere ); break;
case 'replies': $oDB->Query( 'SELECT count(*) as countid FROM '.TABPOST.' WHERE forum='.$this->id.' AND (type="R" OR type="F")'.str_replace('firstpostdate','issuedate',$strWhere) ); break;
case 'repliesZ': $oDB->Query( 'SELECT count(*) as countid FROM '.TABPOST.' p INNER JOIN '.TABTOPIC.' t ON p.topic=t.id WHERE p.forum='.$this->id.' AND p.type<>"P" AND t.status="1"'.$strWhere ); break;
case 'unreplied': $oDB->Query( 'SELECT count(*) as countid FROM '.TABTOPIC.' WHERE forum='.$this->id.' AND replies=0 AND firstpostdate<"'.DateAdd(date('Ymd His'),-$intD,'day').'"'.$strWhere ); break;
case 'unrepliedT': $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE forum='.$this->id.' AND type="T" AND replies=0 AND firstpostdate<"'.DateAdd(date('Ymd His'),-$intD,'day').'"'.$strWhere ); break;
case 'unrepliedA': $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE forum='.$this->id.' AND type="A" AND replies=0 AND firstpostdate<"'.DateAdd(date('Ymd His'),-$intD,'day').'"'.$strWhere ); break;
case 'tags': $oDB->Query( 'SELECT count(*) as countid FROM '.TABTOPIC.' WHERE tags<>"" AND forum='.$this->id.$strWhere ); break;
case 'messages': $oDB->Query( 'SELECT count(*) as countid FROM '.TABPOST.' WHERE forum='.$this->id.$strWhere ); break;
case 'unrepliednews': $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE forum='.$this->id.' AND type="A" AND replies=0 AND firstpostdate<"'.DateAdd(date('Ymd His'),-$intD,'day').'"'.$strWhere ); break; // (unrepliedA) stays for backbward compatibility
default: die('cSection->Count: Wrong argument #1 '.$strObject);
}
$row = $oDB->Getrow();
return intval($row['countid']);
}
// --------
function ReadOptions()
{
$arr = QTexplode($this->options);
if ( isset($arr['order']) ) $this->d_order = $arr['order'];
if ( isset($arr['last']) ) $this->d_last = $arr['last'];
if ( isset($arr['logo']) ) $this->d_logo = $arr['logo'];
}
function SaveOptions($arr)
{
if ( !is_array($arr) ) die('SaveOptions: argument #1 must be an array');
$this->options = QTimplode($arr);
global $oDB;
$oDB->Query('UPDATE '.TABSECTION.' SET options="'.$this->options.'" WHERE id='.$this->id);
$this->ReadOptions(); // Refresh
}
function UpdateLastPostDate()
{
global $oDB;
if ( in_array($oDB->type,array('sqlite','mssql','pg','db2')) )
{
$oDB->Query( 'UPDATE '.TABTOPIC.' SET lastpostdate=(SELECT MAX(issuedate) FROM '.TABPOST.' p, '.TABTOPIC.' t WHERE t.id=p.topic) WHERE forum='.$this->id );
}
else
{
$oDB->Query( 'UPDATE '.TABTOPIC.' t SET t.lastpostdate=(SELECT MAX(p.issuedate) FROM '.TABPOST.' p WHERE t.id=p.topic) WHERE t.forum='.$this->id );
}
}
function UpdateReplies()
{
global $oDB;
if ( in_array($oDB->type,array('sqlite','mssql','pg','db2')) )
{
$oDB->Query( 'UPDATE '.TABTOPIC.' SET replies=(SELECT COUNT(*) FROM '.TABPOST.' p, '.TABTOPIC.' t WHERE t.id=p.topic) WHERE forum='.$this->id );
}
else
{
$oDB->Query( 'UPDATE '.TABTOPIC.' t SET t.replies=(SELECT COUNT(p.id) FROM '.TABPOST.' p WHERE t.id=p.topic AND p.type<>"P") WHERE t.forum='.$this->id );
}
}
// --------
}
?>