Location: PHPKode > projects > QuickTicket > quickticket/quickticket/bin/qti_class_section.php
<?php

// QuickTicket 1.9.0.3 build:20081001

/*
 * CLASS cSection
 *
 */

class cSection
{

// -- Properties --

var $domid;     // Domain id
var $domname;   // Domain name
var $id;        // Section id
var $idtitle;   // Section title
var $name;      // Section name (translation)
var $descr;     // Section descr (translation)
var $type;      // Type:   0=visible, 1=hidden, 2=hidden by user
var $status;    // Status: 0=actif,   1=closed
var $notify;    // Notify: 0=disable, 1=enabled
var $modid;     // Moderator id
var $modname;   // Moderaotr name
var $topics;    // Total topics (including news)
var $replies;   // Total replies (type R and F), not D
var $numfield;  // Format of the ref number: 'N' means no ref number
var $titlefield;// Topic title: 0=None, 1=Optional, 2=Mandatory
var $infofield; // Last column in the topic list: 'N' means no last column
var $wisheddate;// Topic wisheddate: 0=None, 1=Optional, 2=Mandatory
var $notifycc;  // Topic alternate notify: 0=None, 1=Optional, 2=Mandatory
var $prefix;    // Prefix icon from the serie 'a'
/* computed values */
var $lastpostid;
var $lastposttopic;
var $lastpostdate;
var $lastpostuser;
var $lastpostname;

// -- 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 * FROM '.TABSECTION.' WHERE id='.$aSection);
      $row = $oDB->Getrow();
      if ( $row===False )
      {
        unset($_SESSION['qtiGoto']);
        unset($_SESSION['qtiTstatus']);
        unset($_SESSION['qtiTtypes']);
        unset($_SESSION['qtiDomains']);
        die('No section '.$aSection);
      }
      $this->MakeFromArray($row);
    }
    elseif ( is_array($aSection) )
    {
      $this->MakeFromArray($aSection);
    }
    elseif ( strtolower(get_class($aSection))=='csection' )
    {
      $this->domid        = $aSection->domid;
      $this->domname      = $aSection->domname;
      $this->id           = $aSection->id;
      $this->idtitle      = $aSection->idtitle;
      $this->name         = $aSection->name;
      $this->descr        = $aSection->descr;
      $this->type         = $aSection->type;
      $this->status       = $aSection->status;
      $this->notify       = $aSection->notify;
      $this->modid        = $aSection->modid;
      $this->modname      = $aSection->modname;
      $this->topics       = $aSection->topics;
      $this->replies      = $aSection->replies;
      $this->numfield     = $aSection->numfield;
      $this->titlefield   = $aSection->titlefield;
      $this->infofield    = $aSection->infofield;
      $this->wisheddate   = $aSection->wisheddate;
      $this->notifycc     = $aSection->notifycc;
      $this->prefix       = $aSection->prefix;
      $this->lastpostid   = $aSection->lastpostid;
      $this->lastposttopic= $aSection->lastposttopic;
      $this->lastpostdate = $aSection->lastpostdate;
      $this->lastpostuser = $aSection->lastpostuser;
      $this->lastpostname = $aSection->lastpostname;
    }
    else
    {
      die('Invalid constructor parameter #1 for the class cSection');
    }
  }
  if ( $bLast ) $this->GetSectionLastPost();
}

// --------

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 'notify':       $this->notify    = intval($oValue); break;
    case 'moderator':    $this->modid     = $oValue; break;
    case 'moderatorname':$this->modname   = $oValue; break;
    case 'topics':       $this->topics    = intval($oValue); break;
    case 'replies':      $this->replies   = intval($oValue); break;
    case 'numfield':     $this->numfield  = $oValue; break;
    case 'titlefield':   $this->titlefield= intval($oValue); break;
    case 'infofield':    $this->infofield = $oValue; break;
    case 'wisheddate':   $this->wisheddate= intval($oValue); break;
    case 'alternate':    $this->notifycc  = intval($oValue); break;
    case 'prefix':       $this->prefix    = $oValue; break;
  }}
  $this->name = ObjectName('sec','s'.$this->id,$this->idtitle);
  $this->descr = ObjectName('secdesc','s'.$this->id,false);
  $this->domname= ObjectName('domain','d'.$this->domid,'(domain '.$this->domid.')');
}

// --------

function GetSectionLastPost()
{  
  // Initialize
  
  $this->lastpostid = null;
  $this->lastposttopic = null;
  $this->lastpostdate = null;
  $this->lastpostuser = null;
  $this->lastpostname = null;
  
  // Check
  
  if ( $this->id<0 ) return null;
  if ( $this->topics==0 ) return null;

  // Query - attention: this subquery requires mysql 4.1 (use here two queries)
  
  global $oDB;
  
  $oDB->Query( 'SELECT MAX(id) as maxid FROM '.TABPOST.' WHERE forum='.$this->id );
  $row = $oDB->Getrow();
  $intId = intval($row['maxid']);
  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 MoveTopics($intS=null,$intD=0,$intRenum=1,$intTopic=-1,$bClosedOnly=false)
{
  // Allow instanciation
  
  if ( !isset($intS) ) $intS = $this->id;

  // Check

  if ( !is_int($intS) ) die('cSection->MoveTopics: Argument #1 must be integer');
  if ( !is_int($intD) ) die('cSection->MoveTopics: Argument #2 must be integer');
  if ( $intS<0 ) die('cSection->MoveTopics: Wrong argument #1 (id<0)');
  if ( $intD<0 ) die('cSection->MoveTopics: Wrong argument #2 (d<1)');
  if ( $intS==$intD ) die('cSection->Count: Wrong argument, source=destination');

  // Process

  global $oDB;

  $strTopic = '';
  $strPost = '';
  $strField = ''; if ( $intRenum==0 ) $strField = ', numid = 0';

  // if move only one topic
  if ( $intTopic>=0 )
  {
    $strTopic=" AND id=$intTopic";
    $strPost=" AND topic=$intTopic";
    if ( $intRenum==2 )
    {
    $nextnumid = Nextid(TABTOPIC,'numid','WHERE forum='.$intD);
    $strField = ', numid='.intval($nextnumid);
    }
    $bClosedOnly=false;
  }
  else
  {
    if ( $intRenum==2 )
    {
    $nextnumid = Nextid(TABTOPIC,'numid','WHERE forum='.$intD);
    $oDB->Query('SELECT MIN(numid) as minnumid FROM '.TABTOPIC.' WHERE forum='.$intS);
    $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='.$intS.' AND status="Z" AND type="T"');
    while($row=$oDB->Getrow())
    {
      $arr[] = $row['id'];
    }
    foreach($arr as $intKey => $intS)
    {
    $oDB->Query('UPDATE '.TABTOPIC." SET forum=$intD $strField WHERE id=$intS");
    $oDB->Query('UPDATE '.TABPOST." SET forum=$intD $strSetPrefix WHERE topic=$intS");
    }
  }
  else
  {
    $oDB->Query( 'UPDATE '.TABTOPIC." SET forum=$intD $strField WHERE forum=$intS $strTopic" );
    $oDB->Query( 'UPDATE '.TABPOST." SET forum=$intD $strSetPrefix WHERE forum=$intS $strPost" );
  }

  // stats
  UpdateSectionStats($intS);
  UpdateSectionStats($intD);
}

// --------

function DeleteTopics($intS=null,$bClosedOnly=false)
{
  // Allow instanciation
  
  if ( !isset($intS) ) $intS = $this->id;

  // Check

  if ( !is_int($intS) ) die('cSection->DeleteTopics: Argument #1 must be an integer');
  if ( $intS<0 ) die('cSection->DeleteTopics: Wrong argument #1 (id<0)');

  // Process - delete topics and posts
  
  global $oDB;

  if ( $bClosedOnly )
  {
    $arr = array();
    $oDB->Query('SELECT id FROM '.TABTOPIC.' WHERE forum='.$intS.' AND status="Z" AND type="T"');
    while($row=$oDB->Getrow())
    {
      $arr[] = $row['id'];
    }
    foreach($arr as $intKey => $id)
    {
      $oDB->Query('DELETE FROM '.TABPOST.' WHERE topic='.$id);
      $oDB->Query('DELETE FROM '.TABTOPIC.' WHERE id='.$id);
    }
    UpdateSectionStats($intS);
  }
  else
  {
    $oDB->Query('DELETE FROM '.TABPOST.' WHERE forum='.$intS);
    $oDB->Query('DELETE FROM '.TABTOPIC.' WHERE forum='.$intS);
    $oDB->Query('UPDATE '.TABSECTION.' SET topics=0, replies=0 WHERE id='.$intS);
  }
}

// --- [i] ---

function Count($strObject='topics',$intS=null,$intD=10)
{
  // Allow instanciation

  if ( !isset($intS) ) $intS = $this->id;

  // Check

  if ( !is_int($intS) ) die('cSection->Count: Argument #2 must be an integer');
  if ( !is_int($intD) ) die('cSection->Count: Argument #3 must be an integer');
  if ( $intS<0 ) die('cSection->Count: Wrong argument #2 (id<0)');
  if ( $intD<1 ) die('cSection->Count: Wrong argument #3 (d<1)');

  // Process

  global $oDB;
  switch($strObject)
  {
  case 'topics': $oDB->Query('SELECT count(id) as countid FROM '.TABTOPIC.' WHERE forum='.$intS); break;
  case 'news': $oDB->Query('SELECT count(id) as countid FROM '.TABTOPIC.' WHERE forum='.$intS.' AND type="A"'); break;
  case 'closed': $oDB->Query('SELECT count(id) as countid FROM '.TABTOPIC.' WHERE status="Z" AND forum='.$intS.' AND type="T"'); break;
  case 'unreplied': $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE forum='.$intS.' AND replies=0 AND firstpostdate<"'.DateAdd(date('Ymd His'),-$intD,'day').'"' ); break;
  case 'unrepliednews': $oDB->Query( 'SELECT count(id) as countid FROM '.TABTOPIC.' WHERE forum='.$intS.' AND type="A" AND replies=0 AND firstpostdate<"'.DateAdd(date('Ymd His'),-$intD,'day').'"' ); break;
  default: die('cSection->Count: Wrong argument #1');
  }
  $row = $oDB->Getrow();
  return intval($row['countid']);
}

// --------

}

?>
Return current item: QuickTicket