tablename = GetTablePrefix().'PrivateMessages';
$this->type = TYPE_MESSAGE;
$this->NoResourceId = 1;
//$this->BasePermission = 'PM';
$this->id_field = 'PmId';
$this->TagPrefix = 'pm';
$prefix = GetTablePrefix();
$this->SelectSQL = 'SELECT pm.*, pmb.Subject, pmb.Body, pmb.Options, pu.Login
FROM %s AS pm
LEFT JOIN '.$prefix.'PrivateMessageBody AS pmb ON pm.PMBodyId = pmb.PMBodyId
LEFT JOIN '.$prefix.'PortalUser AS pu ON pu.PortalUserId = pm.FromId
WHERE %s';
}
function ParseObject($element)
{
global $objSession, $objUsers, $var_list_update, $bb_var_list_update;
$extra_attribs = ExtraAttributes($element->attributes);
if(strtolower($element->name) == $this->TagPrefix)
{
$field = strtolower( $element->attributes['_field'] );
switch($field)
{
case 'id':
$ret = $this->UniqueId();
break;
case 'body':
global $objCensorList, $objSmileys;
$ret = $this->Get('Body');
$ret = str_replace( "[/code]\r\n",'[/code]', $ret);
$ret = str_replace('"', '"', $ret);
$ret = $objCensorList->CensorText($ret);
if(!$this->GetOption("disable_smileys"))
{
$ret = $objSmileys->ParseText($ret);
}
if(!$this->GetOption("disable_bbcode"))
{
$ret = ReplaceBBCode($ret);
}
$ret = nl2br($ret);
if( $this->GetOption("show_sig") && $element->GetAttributeByName('_signature') > 0 )
{
$Sender =& $objUsers->GetItem($this->Get('FromId'));
$showsig = $this->GetOption("show_sig"); // show post creator signature in this post
$UserSig = (int)$objSession->GetPersistantVariable("bb_signatures"); // viewing user wishes to see other signatures
if($showsig == 1 && $UserSig == 1)
{
$sig = $Sender->GetPersistantVariable("my_signature");
if( !$this->GetOption("disable_smileys") ) $sig = $objSmileys->ParseText($sig);
if( !$this->GetOption("disable_bbcode") ) $sig = ReplaceBBCode($sig);
$ret .= "
".nl2br($sig);
}
}
break;
case 'reply_link':
$var_list_update['t'] = $element->GetAttributeByName('_PMTemplate');
$bb_var_list_update['pm_id'] = $this->UniqueId();
$ToUser =& $objUsers->GetItem($this->Get('FromId'));
if( is_object($ToUser) )
{
$ret = HREF_Wrapper('', Array('ToUser' => $ToUser->Get('Login'), 'IsReply' => 1) );
}
else
{
$ret = '';
}
break;
case 'delete_link':
$var_list_update['t'] = 'inbulletin/pm_list';
$bb_var_list_update['pm_id'] = $this->UniqueId();
$ret = HREF_Wrapper('', Array('Action' => 'bb_delete_pm', 'FolderId' => (int)GetVar('FolderId') ) );
break;
case 'fromlogin':
$Me = $objUsers->GetItem( $objSession->Get('PortalUserId') );
if( is_object($Me) )
{
$ret = $Me->Get('Login');
}
break;
case 'fromuser':
$ret = $this->Get('SenderUser');
break;
case 'senderuser':
$UserID = $this->Get('FromId') != $objSession->Get('PortalUserId') ? $this->Get('FromId') : $this->Get('ToId');
$FromUser = $objUsers->GetItem($UserID);
if( is_object($FromUser) )
{
$ret = $FromUser->Get('Login');
}
else
{
$ret = '';
}
break;
case 'link':
$var_list_update['t'] = $element->GetAttributeByName('_PMTemplate');
$bb_var_list_update['pm_id'] = $this->UniqueId();
$ret = HREF_Wrapper('', Array('FolderId' => (int)GetVar('FolderId') ) );
break;
case 'datecreated':
$ret = adodb_date($element->GetAttributeByName('_Format'), $this->Get('CreatedOn') );
break;
case 'subject':
// PM Statuses = {0 - Unread, 1 - Viewed, 2 - Read, 3 - Replyed, 4 - Sent}
$subject = $this->Get('Subject') ? $this->Get('Subject') : '['.language('no_subject').']';
$ret = $this->Get('Status') < 2 ? ''.$subject.'' : $subject;
$ret = get_magic_quotes_gpc() ? $ret : stripslashes($ret);
break;
default:
$ret = parent::ParseObject($element);
break;
}
}
return $ret;
}
function GetOption($name)
{
$opt = decbin($this->Get('Options'));
$opt = str_pad($opt, 8, '0');
if($opt == 0) return false;
switch($name)
{
case "show_sig":
$option = $opt[0];
break;
case "disable_bbcode":
$option = $opt[1];
break;
case "disable_smileys":
$option = $opt[2];
break;
}
return $option;
}
function Delete()
{
// if all references to pm body were deleted -> also delete message body
$db =& GetADODBConnection();
$BodyID = $this->Get('PMBodyId');
$BodyTable = GetTablePrefix().'PrivateMessageBody';
$ret = parent::Delete();
if($ret)
{
$db->Execute('UPDATE '.$BodyTable.' SET ReferenceCount = ReferenceCount - 1 WHERE PMBodyId = '.$BodyID);
}
$db->Execute('DELETE FROM '.$BodyTable.' WHERE PMBodyId = '.$BodyID.' AND ReferenceCount = 0');
}
function SetOption($name,$value)
{
$opt = str_pad(decbin($this->Get('Options')),8,"0");
switch($name)
{
case "show_sig":
$option = 0;
break;
case "disable_bbcode":
$option = 1;
break;
case "disable_smileys":
$option = 2;
break;
}
if(is_numeric($option))
{
if($value)
{
$opt[$option] =1;
}
else
$opt[$option]=0;
}
$this->Set('Options', bindec($opt) );
return $option;
}
function UpdateOptions()
{
$db =& GetADODBConnection();
$sql = 'UPDATE '.GetTablePrefix().'PrivateMessageBody SET Options = '.$this->get('Options').' WHERE PMBodyId = '.$this->Get('PMBodyId');
$db->Execute($sql);
}
}
class clsPrivateMessageList extends clsItemList
{
var $FolderId = 0; // folder id to get private messages from
function clsPrivateMessageList()
{
global $bb_var_list;
parent::clsItemList();
$this->classname = 'clsPrivateMessage';
$this->SetTable('live', GetTablePrefix().'PrivateMessages');
$this->Page = $bb_var_list['pmp'];
$this->PerPageVar = 'Perpage_PrivateMessages';
$this->PageEnvar = 'bb_var_list_update';
$this->PageEnvarIndex = 'pmp';
$this->EnablePaging = true;
//$this->AddSortField('PMs_Sortfield','PMs_SortOrder');
$this->ItemType = TYPE_MESSAGE;
}
/**
* @return void
* @param int $ToId
* @param string $Subject
* @param string $Text
* @desc Create new private mesage
*/
function &Add_PrivateMessage($ToId, $Subject, $Text)
{
global $objSession, $objUsers;
// PM Statuses = {0 - Unread, 1 - Viewed, 2 - Read, 3 - Replyed, 4 - Sent}
// PM Folders = {0 - Inbox, -1 - Sent}
$db =& GetADODBConnection();
$sql = 'INSERT INTO '.GetTablePrefix().'PrivateMessageBody(Subject,Body,ReferenceCount) VALUES (%s,%s,2)';
$db->Execute( sprintf($sql, $db->qstr($Subject, get_magic_quotes_gpc() ), $db->qstr($Text, get_magic_quotes_gpc() ) ) );
if( $db->ErrorNo() == 0 )
{
// pm body was inserted
$pm =& $this->GetDummy();
$FromId = $objSession->Get('PortalUserId');
$BodyId = $db->Insert_ID();
$CreatedOn = adodb_date("U");
// create message in sender "Sent" folder
$pm->Set( Array('FromId', 'ToId', 'CreatedOn','FolderId','Status','PMBodyId','PmId'),
Array($FromId, $ToId, $CreatedOn, -1, 2, $BodyId,0) );
$pm->Create();
// create message in recipient "Inbox" folder
$pm->Set( Array('FromId', 'ToId', 'CreatedOn','FolderId','Status','PMBodyId','PmId'),
Array($FromId, $ToId, $CreatedOn, 0, 0, $BodyId,0) );
$pm->Create();
$Recipient =& $objUsers->GetItem($ToId);
if( is_object($Recipient) )
{
// send email to recipient that pm were created (if he allowed this)
if($Recipient->GetPersistantVariable('bb_pm_notify') == 1)
{
$pm->Set('SenderUser', $Recipient->Get('Login') );
$objSession->SetVariable('ToUser', $pm->Get('SenderUser') );
$pm->SendUserEventMail('PM.ADD', $ToId);
}
}
}
return $pm;
}
function LoadUserPMs($attribs=array())
{
global $objSession, $objConfig;
$this->SetFolderId( GetVar('FolderId') );
$OrderBy ='';
//$this->Clear();
$limit = $this->GetPageLimitSQL();
// apply sorting
$sort_field = trim($objSession->GetPersistantVariable('PMs_Sortfield'));
if( !strlen($sort_field) )
{
$sort_field = trim( $objConfig->Get('PMs_Sortfield') );
}
if($sort_field)
{
$OrderBy = 'ORDER BY '.$sort_field.' '.$objSession->GetPersistantVariable('PMs_SortOrder');
}
else
{
$OrderBy = 'ORDER BY CreatedOn DESC';
}
$where = 'pm.FolderId = '.$this->FolderId;
$ThisUserId = $objSession->Get('PortalUserId');
switch($this->FolderId)
{
case 0: // inbox
$where .= ' AND pm.ToId = '.$ThisUserId;
break;
case -1: // sent
$where .= ' AND pm.FromId = '.$ThisUserId;
break;
}
$prefix = GetTablePrefix();
if($this->FolderId == -1)
{
$field_list = 'pm.ToId AS FromId, pm.FromId AS ToId';
$join_clause = 'pu.PortalUserId = pm.ToId';
}
else
{
$field_list = 'pm.ToId AS ToId, pm.FromId AS FromId';
$join_clause = 'pu.PortalUserId = pm.FromId';
}
$sql = 'SELECT pm.PmId, pm.FolderId, pm.Status, pm.PMBodyId, pm.CreatedOn, pmb.Subject, pmb.Body, pu.Login AS SenderUser, '.$field_list.'
FROM '.$this->SourceTable.' AS pm LEFT JOIN '.$prefix.'PrivateMessageBody AS pmb ON pm.PMBodyId = pmb.PMBodyId LEFT JOIN '.$prefix.'PortalUser AS pu ON '.$join_clause.'
WHERE '.$where;
$this->QueryItemCount = QueryCount($sql);
$sql .= " ".$OrderBy;
return $this->Query_Item($sql, $limit);
}
function Query_Item($sql,$limit = null, $fix_method = 'set_first')
{
if($sql !== $this->LastQuerySQL && $limit !== $this->LastLimitClause)
{
$ret = parent::Query_Item($sql,$limit,$fix_method);
$this->LastQuerySQL = $sql;
$this->LastLimitClause = $limit;
return $ret;
}
else
{
return $this->Items;
}
}
function SetFolderId($NewId)
{
$this->FolderId = (int)$NewId;
}
}
?>