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

// QuickTicket 1.9.0.3 build:20081001

class cTopic
{

// -- Properties --

var $id = -1;
var $numid = -1;
var $section = -1;
var $type = 'T'; // A=News P=Post. Attention, alphabetic order can be used as display order (i.e. "News on top")
var $status = 'A'; // A=submitted Z=closed (News 0=closed). Attention user can sort according to the status index.
var $wisheddate = '0';
var $firstpostid = -1;
var $lastpostid = -1;
var $firstpostuser = -1;
var $lastpostuser = -1;
var $firstpostname;
var $lastpostname;
var $firstpostdate = '0';
var $lastpostdate = '0';
var $x;
var $y;
var $z;
var $actorid = -1;
var $actorname = '';
var $notifiedid = -1;
var $notifiedname = '';
var $replies = 0;
var $views = 0;
var $youreply = '&nbsp;';
var $preview;
var $smile;
var $title;

// -- Constructor method (accept an array or a cTopic as parameter #1) --

function cTopic($aTopic=null,$intUser=-1,$intPreviewsize=250)
{
  if ( isset($aTopic) )
  {
    if ( is_int($aTopic) )
    {
      if ( $aTopic<0 ) die('cTopic: Wrong id');
      global $oDB;
      $oDB->Query('SELECT * FROM '.TABTOPIC.' WHERE id='.$aTopic);
      $row = $oDB->Getrow();
      if ( $row===False ) die('No topic '.$aTopic);
      $this->MakeFromArray($row,$intPreviewsize);
      if ( $intUser>=0 )
      {
        // +1 when user is not the topic creator himself
        if ( $intUser != $this->firstpostuser )
        {
        $oDB->Query('UPDATE '.TABTOPIC.' SET views = views+1 WHERE id='.$this->id);
        }
      }
    }
    elseif ( is_array($aTopic) )
    {
      $this->MakeFromArray($aTopic,$intPreviewsize);
    }
    elseif ( strtolower(get_class($aTopic))=='ctopic' )
    {
      $this->id            = $aTopic->id;
      $this->numid         = $aTopic->numid;
      $this->section       = $aTopic->section;
      $this->type          = $aTopic->type;
      $this->status        = $aTopic->status;
      $this->wisheddate    = $aTopic->wisheddate;
      $this->firstpostid   = $aTopic->firstpostid;
      $this->lastpostid    = $aTopic->lastpostid;
      $this->firstpostuser = $aTopic->firstpostuser;
      $this->lastpostuser  = $aTopic->lastpostuser;
      $this->firstpostname = $aTopic->firstpostname;
      $this->lastpostname  = $aTopic->lastpostname;
      $this->firstpostdate = $aTopic->firstpostdate;
      $this->lastpostdate  = $aTopic->lastpostdate;
      $this->x             = $aTopic->x;
      $this->y             = $aTopic->y;
      $this->z             = $aTopic->z;
      $this->actorid       = $aTopic->actorid;
      $this->actorname     = $aTopic->actorname;
      $this->notifiedid    = $aTopic->notifiedid;
      $this->notifiedname  = $aTopic->notifiedname;
      $this->replies       = $aTopic->replies;
      $this->views         = $aTopic->views;
      $this->youreply      = $aTopic->youreply;      
      $this->preview       = $aTopic->preview;
      $this->smile         = $aTopic->smile;
      $this->title         = $aTopic->title;
    }
    else
    {
      die('Invalid constructor parameter #1 in the class cTopic');
    }
  }

}

// --------

function MakeFromArray($aTopic,$intPreviewsize=100)
{
  foreach ($aTopic as $strKey => $oValue) {
  switch ($strKey) {
    case 'textmsg':
      $this->preview = $oValue;
      $this->preview = QTbbc($this->preview,'deepdrop',' ',$intPreviewsize,null,null,'');
      break;
    case 'id':           $this->id           = intval($oValue); break;
    case 'numid':        $this->numid        = intval($oValue); break;
    case 'forum':        $this->section      = intval($oValue); break;
    case 'type':         $this->type         = $oValue; break;
    case 'status':       $this->status       = $oValue; break;
    case 'wisheddate':   $this->wisheddate   = $oValue; break;
    case 'firstpostid':  $this->firstpostid  = intval($oValue); break;
    case 'lastpostid':   $this->lastpostid   = intval($oValue); break;
    case 'firstpostuser':$this->firstpostuser= intval($oValue); break;
    case 'lastpostuser': $this->lastpostuser = intval($oValue); break;
    case 'firstpostname':$this->firstpostname= $oValue; break;
    case 'lastpostname': $this->lastpostname = $oValue; break;
    case 'firstpostdate':$this->firstpostdate= $oValue; break;
    case 'lastpostdate': $this->lastpostdate = $oValue; break;
    case 'actorid':      $this->actorid      = intval($oValue); if ($this->actorid==0) $this->actorid=null; break;
    case 'actorname':    $this->actorname    = $oValue; break;
    case 'notifiedid':   $this->notifiedid   = intval($oValue); if ($this->notifiedid==0) $this->notifiedid=null; break;
    case 'notifiedname': $this->notifiedname = $oValue; break;
    case 'replies':      $this->replies      = intval($oValue); break;
    case 'views':        $this->views        = intval($oValue); break;
    case 'icon':         $this->smile        = $oValue; break;
    case 'title':        $this->title        = $oValue; break;
    case 'x': if ( is_numeric($oValue) ) $this->x = floatval($oValue); break; // must be FLOAT (or NULL)
    case 'y': if ( is_numeric($oValue) ) $this->y = floatval($oValue); break; // must be FLOAT (or NULL)
    case 'z': if ( is_numeric($oValue) ) $this->z = floatval($oValue); break; // must be FLOAT (or NULL)
  }}
}

// --------

function GetTopicTitle()
{
  global $oDB;
  $oDB->Query('SELECT title FROM '.TABPOST.' WHERE id='.$this->firstpostid);
  $row = $oDB->Getrow();
  $this->title = $row['title'];
  return $this->title;
}

// --------

function SetStatus($strStatus='A',$bCanNotify=true,$aPost=null)
{
  global $oDB;

  $oDB->Query('UPDATE '.TABTOPIC.' SET status="'.$strStatus.'" WHERE id='.$this->id);
  $this->status=$strStatus;

  // For NEWS or OTHER types, Z status is converted to 0 (to make it closed but visible)
  if ( $strStatus=='Z' )
  {
    $oDB->Query('UPDATE '.TABTOPIC.' SET status="0" WHERE id='.$this->id.' AND type="A"');
  }

  // NOTIFY
  if ( $bCanNotify ) $this->NotifyStatus(-1,$aPost);
}

// --------
// this change the type.

function SetType($intId=-1,$str='T')
{
  // Check
  
  if ( $intId<0 ) die('Topic->SetType: Wrong id');

  // Process
  
  global $oDB;

  $oDB->Query( 'UPDATE '.TABTOPIC.' SET type="'.$str.'" WHERE id='.$intId );
  if ( isset($this) ) $this->type=$str;
}

// --------

function SetActor($intActor=-1,$bCanNotify=TRUE,$aPost=null)
{
  $intOldactorid = $this->actorid;
  if ( $intActor<0 ) die('Topic->SetActor: Wrong actor id');

  global $oDB,$L;

  // change actor
  $this->actorid = intval($intActor);
  $this->actorname = GetUserInfo($this->actorid,'name');
  $oDB->Query('UPDATE '.TABTOPIC.' SET actorid='.$this->actorid.', actorname="'.$this->actorname.'" WHERE id='.$this->id);

  // posting a forward messsage
  $oPost = new cPost;
  $oPost->id = Nextid(TABPOST);
  $oPost->section = $this->section;
  $oPost->topic = $this->id;
  $oPost->type = 'F';
  $oPost->title = $L['Topic_handled'].' '.strtolower($L['By']).' '.$this->actorname;
  $oPost->text = sprintf($L['Topic_forwarded'],$this->actorname);
  $oPost->userid = $this->actorid;
  $oPost->username = $this->actorname;
  $oPost->issuedate = date('Ymd His');
  $oPost->modifdate = '';
  $oPost->modifuser = '';
  $oPost->InsertPost(true);
  $this->UpdateTopicStats($this->id,9999,$oPost);

  if ( $bCanNotify ) $this->NotifyActor($intOldactorid);
}

// --------

function UpdateTopicStats($id=-1,$intMax=100,$oLastPost=null)
{
  // Check

  if ( $id<0 ) die('Topic->UpdateTopicStats: Wrong id');

  // Process

  global $oDB;

  // query topics
  $oDB->Query( 'SELECT count(id) as countid FROM '.TABPOST.' WHERE type="R" AND topic='.$id );
  $row = $oDB->Getrow();
  $this->replies = intval($row['countid']);

  $strQ = '';
  if ( isset($oLastPost) )
  {
  $this->lastpostid = $oLastPost->id;
  $this->lastpostuser = $oLastPost->userid;
  $this->lastpostname = $oLastPost->username;
  $this->lastpostdate = $oLastPost->issuedate;
  $strQ = ', lastpostid='.$this->lastpostid.', lastpostuser='.$this->lastpostuser.',lastpostname="'.$this->lastpostname.'", lastpostdate="'.$this->lastpostdate.'"';
  }

  // save stats
  $oDB->Query( 'UPDATE '.TABTOPIC.' SET replies='.$this->replies.$strQ.' WHERE id='.$id );

  // close topic if full
  if ( $this->replies>$intMax )
  {
  $oDB->Query('UPDATE '.TABTOPIC.' SET status="1" WHERE id='.$id);
  }
}

// --------

function InsertTopic($bUserStat=true,$bCanNotify=true,$oPost=null,$oSEC=null)
{
  global $oDB,$L;

  // insert
  $oDB->Query(
  'INSERT INTO '.TABTOPIC." (id,forum,numid,type,status,firstpostid,lastpostid,firstpostuser,lastpostuser,firstpostname,lastpostname,firstpostdate,lastpostdate,replies,actorid,actorname,wisheddate,notifiedid,notifiedname,x,y,z)
  VALUES (
  $this->id,$this->section,$this->numid,\"$this->type\",\"$this->status\",
  $this->firstpostid,$this->lastpostid,$this->firstpostuser,$this->lastpostuser,
  \"$this->firstpostname\",\"$this->lastpostname\",
  \"".Date('Ymd His')."\",\"".Date('Ymd His')."\",
  $this->replies,$this->actorid,\"".addslashes($this->actorname)."\",".
  ( empty($this->wisheddate) ? '"0"' : '"'.$this->wisheddate.'"').','.
  ( $this->notifiedid>=0 ? $this->notifiedid.',"'.addslashes($this->notifiedname).'"' : 'NULL,NULL').','.
  ( isset($this->x) ? $this->x : 'NULL').','.
  ( isset($this->y) ? $this->y : 'NULL').','.
  ( isset($this->z) ? $this->z : 'NULL').')'
  );

  // Status notification
  if ( $bCanNotify && $this->type=='T' ) $this->NotifyStatus(-1,$oPost,$oSEC);

  Unset($_SESSION[QT]['sys_topics']);

  if ( $bUserStat )
  {
  $oDB->Query('SELECT count(*) as countid FROM '.TABPOST.' WHERE userid='.$this->firstpostuser);
  $row = $oDB->Getrow();
  $oDB->Query('UPDATE '.TABUSER.' SET lastdate="'.Date('Ymd His').'", numpost='.$row['countid'].', ip="'.$oDB->ip.'" WHERE id='.$this->firstpostuser);
  $_SESSION[QT.'_usr_posts']++;
  }
}

// --------

function NotifyActor($intOldactorid=-1,$oSEC=null)
{
  if ( QTI_NOTIFY_NEWACTOR || QTI_NOTIFY_OLDACTOR ) {
  if ( $intOldactorid!=$this->actorid ) {
  if ( in_array($this->section,$_SESSION[QT]['sys_notifysections']) ) {

    global $oDB,$L;

    // get section info (for the number format)
    if ( !isset($oSEC) ) $oSEC = new cSection($this->section);
    // prepare mail
    $strTopic = ''; if ( $oSEC->numfield!='N' ) $strTopic = sprintf($oSEC->numfield,$this->numid).' ';
    $strMails = '';
    if ( QTI_NOTIFY_NEWACTOR && $this->actorid>=0 ) $strMails .= GetUserInfo($this->actorid,'mail').',';
    if ( QTI_NOTIFY_OLDACTOR && $intOldactorid>=0 ) $strMails .= GetUserInfo($intOldactorid,'mail');
    $strMessage = sprintf("{$L['Topic']} %s ",$strTopic);
    $strMessage .= sprintf($L['Topic_forwarded'],$this->actorname);
    $strSubject = "{$_SESSION[QT]['site_name']}: {$L['Notification']} $strTopic";
    // send mail
    include('bin/qt_lib_smtp.php');
    if ( !empty($strMails) ) QTmail($strMails,QTconv($strSubject,'-4'),QTconv($strMessage,'-4'),QTI_HTML_CHAR);
  }}}
}

function NotifyStatus($intOldactorid=-1,$aPost=null,$oSEC=null)
{

  if ( in_array($this->section,$_SESSION[QT]['sys_notifysections']) ) {
  if ( !empty($_SESSION['qtiTstatus'][$this->status]['mailto']) ) {

    global $oDB,$L;

    // get section info (for the number format)
    if ( !isset($oSEC) ) $oSEC = new cSection($this->section);

    // read message (and get it if not yet defined)
    if ( !isset($this->title) && isset($aPost) )
    {
      $oPost = new cPost($aPost);
      $this->title = $oPost->title;
      $this->preview = QTbbc($oPost->text,'deepdrop',' ',100,null,null,'');
    }
    if ( empty($this->title) ) $this->title = '';
    if ( empty($this->preview) ) $this->preview = '';
    $strTopic = ($oSEC->numfield!='N' ? sprintf($oSEC->numfield,$this->numid).' ' : '').$this->title."\r\n".$this->preview;

    $strFile = $_SESSION[QT]['language'].'/mail_status.inc';

    // notify list

    $lstMails = explode(',',$_SESSION['qtiTstatus'][$this->status]['mailto']);
    $lstMails = array_unique($lstMails);

    // notify mails
    $arrMails = array();
    foreach ($lstMails as $intUser)
    {
      switch ($intUser)
      {
      case 'MF': $arrMails[] = GetUserInfo("$this->section",'mail',$oSEC); break;
      case 'MA': if ( $this->actorid>=0 ) $arrMails[] = GetUserInfo($this->actorid,'mail'); break;
      case 'U':
        $arrMails[] = GetUserInfo(intval($this->firstpostuser),'mail');
        if ( $this->notifiedid>=0 ) $arrMails[] = GetUserInfo(intval($this->notifiedid),'mail');
        break;
      case 'A': $arrMails = $arrMails + GetUserInfo('A','mail'); break;
      case 'S': $arrMails = $arrMails + GetUserInfo('S','mail'); break;
      default:  if ( $intUser>=0 ) $arrMails[] = GetUserInfo(intval($intUser),'mail'); break;
      }
    }
    $arrMails = array_unique($arrMails);
    $strMails = implode(', ',$arrMails);

    // message containing 2 parameters (the status, the topic preview)
    $strMessage = "{$L['Status']}: %s \r\n%s";
    if ( file_exists($strFile) ) include($strFile);
    $strMessage = sprintf($strMessage,$_SESSION['qtiTstatus'][$this->status]['name'],$strTopic);

    $strSubject = $_SESSION[QT]['site_name'].': '.$L['Notification'].' '.$this->title;

    // send mail
    include('bin/qt_lib_smtp.php');
    QTmail($strMails,QTconv($strSubject,'-4'),QTconv($strMessage,'-4'),QTI_HTML_CHAR);

    // show send mails
    $strMails = '<br/><br/>'.$L['Notification'].': '.$strMails;

  }}
}

// --------

}

?>
Return current item: QuickTicket