Location: PHPKode > projects > QuickTalk Forum > quicktalk/bin/qtf_class_topic.php
<?php

// QuickTalk 2.5 build:20100924

class cTopic
{

// --------

var $id = -1;
var $numid = -1;
var $section = -1;
var $type = 'T';
var $status = '0'; // 0=open 1=closed
var $statusdate = '0'; // last status change date
var $hottopic = '';  // if is hot topic, value '_h'
var $tags='';
var $firstpostid = -1;
var $lastpostid = -1; 
var $firstpostuser = -1;
var $lastpostuser = -1;
var $firstpostname;
var $lastpostname;
var $firstpostdate = '0';
var $lastpostdate = '0';
var $replies = 0;
var $views = 0;
var $youreply = '&nbsp;';
var $smile = '00';
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->statusdate    = $aTopic->statusdate;
      $this->tags          = $aTopic->tags;
      $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->replies       = $aTopic->replies; 
      $this->views         = $aTopic->views;
      $this->youreply      = $aTopic->youreply;
      $this->smile         = $aTopic->smile; 
      $this->title         = $aTopic->title; 
      $this->preview       = $aTopic->preview;
    }
    else
    {
      die('Invalid constructor parameter #1 in the class cTopic');
    }
    if ( QTF_HOTTOPICS )
    {
      if ( $this->replies>=QTF_HOTTOPICS ) $this->hottopic = '_h';
    }
  }
  
}

// --------

function MakeFromArray($aTopic,$intPreview=100)
{
  foreach($aTopic as $strKey=>$oValue) {
  switch ($strKey) {
    case 'textmsg':      $this->preview      = QTcompact(QTunbbc($oValue),$intPreview,' '); 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 'statusdate':   $this->statusdate   = $oValue; break;
    case 'tags':         $this->tags         = $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 '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;
  }}
}

// --------

function GetIcon($strSkin='skin/default',$strHref='',$strTitleFormat='%s')
{
  $str='';
  switch(strtoupper($this->type))
  {
  case 'T':
    global $oVIP;
    $arrStatuses = $oVIP->GetStatuses();
    if ( isset($arrStatuses [$this->status]['statusname']) ) { $str=$strSkin.'/'.$arrStatuses [$this->status]['icon']; } else { $str=$strSkin.'/ico_topic_t_1.gif'; }
    if ( !file_exists($str) ) $str='admin/ico_status.gif';
    return AsImg($str,'T',sprintf($strTitleFormat,$this->GetIconName()),'ico ico_t','',$strHref);
    break;
  case 'A':
    $str = $strSkin.'/ico_topic_a_'.$this->status.'.gif';
    if ( !file_exists($str) ) $str='admin/ico_status.gif';
    return AsImg($str,$this->type,sprintf($strTitleFormat,$this->GetIconName()),'ico ico_a','',$strHref);    
    break;
  }
  return $str;
}

function GetIconName()
{
  $str='';
  switch(strtoupper($this->type))
  {
  case 'T': global $oVIP; $arrStatuses = $oVIP->GetStatuses(); $str = (isset($arrStatuses[$this->status]['statusname']) ? $arrStatuses[$this->status]['statusname'] : 'unknown status'); break;
  case 'A': $str = 'Ico_topic_'.strtolower($this->type).$this->status; break;
  }
  return L($str);
}

// --------

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='0')
{
  // attention: status '1' means 'closed' (opened='0')
  if ( $this->status==$strStatus ) return false;
  
  global $oDB;
  
  $this->status=$strStatus;
  $this->statusdate=date('Ymd His');
  $oDB->Query('UPDATE '.TABTOPIC.' SET status="'.$this->status.'", statusdate="'.$this->statusdate.'",modifdate="'.date('Ymd His').'" WHERE id='.$this->id);
  $this->status=$strStatus;
}

// -------- can be used without object instance)

function SetType($intId=-1,$str='T')
{
  if ( $intId<0 ) die('Wrong id in topic->SetType');
  global $oDB;  
  $oDB->Query('UPDATE '.TABTOPIC.' SET type="'.$str.'" WHERE id='.$intId);
  if ( isset($this) ) $this->type=$str;
}

// --------

function TagsAdd($str,$oSEC)
{
  // Check
  
  if ( !is_string($str) ) die('cTopic->TagsAdd: wrong argument #1');
  $str = trim($str);
  if ( empty($str) ) return false;
  if ( substr($str,-1,1)==';' ) $str = substr($str,0,-1);
  if ( $str==';' ) return false;
    
  // Read tags to add
  
  $str = strtr(trim($str),'éèêëÉÈÊËáàâäÁÀÂÄÅåíìîïÍÌÎÏóòôöÓÒÔÖõÕúùûüÚÙÛÜ','eeeeeeeeaaaaaaaaaaiiiiiiiioooooooooouuuuuuuu');
  $arrAdd = explode(';',$str);
  foreach($arrAdd as $intKey=>$strValue) { $arrAdd[$intKey]=trim($strValue); }

  // Read current tags
  
  $arrTag = array();
  if ( !empty($this->tags) )
  {
  $arrTag = explode(';',strtolower($this->tags));
  foreach($arrTag as $intKey=>$strValue) { $arrTag[$intKey]=trim($strValue); }
  }
  
  // Add tags
  
  foreach($arrAdd as $strValue)
  {
  if ( !in_array(strtolower($strValue),$arrTag) ) $arrTag[]=$strValue;
  }
  $this->tags = implode(';',$arrTag);  
  
  // Save
  
  global $oDB; $oDB->Query('UPDATE '.TABTOPIC.' SET tags="'.$this->tags.'",modifdate="'.date('Ymd His').'" WHERE id='.$this->id);

}

// --------

function TagsDel($str,$oSEC)
{
  // Check
  
  if ( !is_string($str) ) die('cTopic->TagsDel: wrong argument #1');
  if ( empty($this->tags) ) return false;
  $str = trim($str);
  if ( empty($str) ) return false;
  if ( substr($str,-1,1)==';' ) $str = substr($str,0,-1);
  if ( $str==';' ) return false;

  global $oDB; 
  
  if ( $str=='*' )
  {
    // Delete [all]
    $this->tags='';
    $oDB->Query('UPDATE '.TABTOPIC.' SET tags="",modifdate="'.date('Ymd His').'" WHERE id='.$this->id);
  }
  else
  {  
    // Read tags to delete

    $str = strtr(trim($str),'éèêëÉÈÊËáàâäÁÀÂÄÅåíìîïÍÌÎÏóòôöÓÒÔÖõÕúùûüÚÙÛÜ','eeeeeeeeaaaaaaaaaaiiiiiiiioooooooooouuuuuuuu');
    $arrDel = explode(';',$str);
    foreach($arrDel as $intKey=>$strValue) { $arrDel[$intKey]=strtolower(trim($strValue)); }

    // Read current tags

    $arrTag = array();
    if ( !empty($this->tags) )
    {
    $arrTag = explode(';',strtolower($this->tags));
    foreach($arrTag as $intKey=>$strValue) { $arrTag[$intKey]=trim($strValue); }
    }

    // Delete tags

    $this->tags = '';
    foreach($arrTag as $strValue)
    {
      if ( !in_array(strtolower($strValue),$arrDel) ) $this->tags .= $strValue.';';
    }
    if ( !empty($this->tags) ) { if ( substr($this->tags,-1,1)==';' ) $this->tags = substr($this->tags,0,-1); } 

    // Save

    $oDB->Query('UPDATE '.TABTOPIC.' SET tags="'.$this->tags.'",modifdate="'.date('Ymd His').'" WHERE id='.$this->id);
  }
}

// --------

function UpdateTopicStats($id=-1,$intMax=100,$oLastPost=null)
{
  if ( $id<0 ) die('Topic->UpdateTopicStats: Wrong id');

  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 ( $intMax>0 ) {
  if ( $this->replies>=$intMax ) {
  $oDB->Query('UPDATE '.TABTOPIC.' SET status="1" WHERE id='.$id);
  }}
}

// --------

function InsertTopic($bUserStat=true)
{
  global $oDB;
  $oDB->Query(
  'INSERT INTO '.TABTOPIC." (
  id,forum,numid,type,status,statusdate,tags,firstpostid,lastpostid,firstpostuser,lastpostuser,firstpostname,lastpostname,firstpostdate,lastpostdate,replies)
  VALUES (
  $this->id,
  $this->section,
  $this->numid,
  \"$this->type\",
  \"$this->status\",
  \"$this->statusdate\",
  \"$this->tags\",
  $this->firstpostid,
  $this->lastpostid,
  $this->firstpostuser,
  $this->lastpostuser,
  \"$this->firstpostname\",
  \"$this->lastpostname\",
  \"".date('Ymd His')."\",
  \"".date('Ymd His')."\",$this->replies)"
  );
  
  if ( $bUserStat ) 
  {
  if ( isset($_SESSION['qtf_usr_posts']) ) $_SESSION['qtf_usr_posts']++;
  $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']++;
  }
}

// --------

}

?>
Return current item: QuickTalk Forum