abstract class BaseMessage extends MyObject
{
public $read = false;
public $inboxTable;
public $outboxTable;
public function __construct($data = null, $table = 'messages', $inboxTable = 'inbox', $outboxTable = 'outbox')
{
parent::__construct($data, $table);
//save our tables.
$this->inboxTable = $inboxTable;
$this->outboxTable = $outboxTable;
$this->recipients = $this->getRecipients();
$this->needsJs('lib/js/message.js');
if (is_array($data))
$this->read = $data['read'];
$this->fullTextFields[] = 'subject';
$this->fullTextFields[] = 'body';
}
public function delete()
{
dbExecute("
DELETE FROM $this->inboxTable
WHERE message_id = '$this->id'
");
dbExecute("
DELETE FROM $this->outboxTable
WHERE message_id = '$this->id'
");
$kids = $this->getChildren();
if (count($kids))
{
foreach ($kids AS $kid)
$kid->delete();
}
parent::delete();
}
protected function getRecipients()
{
if (!is_array($this->recipients))
$ret = explode(",", $this->recipients);
else
$ret = $this->recipients;
return $ret;
}
protected function getEditPageParamXml()
{
return <<
XML;
}
public function initEditPage()
{
global $me;
parent::initEditPage();
$this->pageTitle = 'Send Private Message';
$parentId = $this->params('parent_id');
if ($parentId)
{
$class = get_class($this);
$parent = new $class($parentId);
if (!$parent->canView())
throw new PageError('You cannot reply to this message.');
}
}
public function canEdit()
{
global $me;
if ($this->id && $me->isAdmin())
return true;
if (!$this->id)
return true;
return false;
}
public function editFormAddFields($form)
{
global $me;
//set our layout
$form->layout = new OneColumnTableLayout();
$recipients = array();
//create our recipient list
//did we get a user id? (sending it directly to someone)
$userId = $this->params('user_id');
$user = new User($userId);
if ($userId)
$recipients[$user->id] = $user->getName();
//otherwise call our hook function
$recipients = $this->createRecipientsList($recipients);
//add our recipients field!
$form->add('MultiSelectField', 'recipients', array(
'multiple' => true,
'required' => true,
'size' => 10,
'noneTitle' => false,
'options' => $recipients,
'explanation' => '
Tip: By holding the \'Ctrl\' key and clicking on names, you
can select multiple people.
Additonally, you can select large chunks of the list by
holding \'Shift\' and clicking names
'
));
$form->add('TextField', 'subject', array(
"required" => true,
"width" => '95%',
));
$form->add('TextAreaField', 'body', array(
"required" => true,
"title" => 'Message',
"width" => '95%',
"height" => '150px'
));
$form->add('HiddenField', 'parent_id', array());
$form->addSubmit('Send Message');
}
//stub function for you to ad recipients in.
protected function createRecipientsList($recipients)
{
return $recipients;
}
public function createReplyForm($recipients)
{
$form = new Form();
$form->action = $this->getUrl(".edit");
//if we have recipients... its a reply all
if (count($recipients) > 1)
$form->name = 'replyAllForm_' . $this->id;
else
$form->name = 'replyForm_' . $this->id;
//we're the parent..
$form->add('HiddenField', 'parent_id', array(
'value' => $this->id
));
//here are our recipients..
$form->add('HiddenField', 'recipients', array(
'value' => array_unique($recipients)
));
//set our subject too.
$form->add('HiddenField', 'subject', array(
'value' => $this->subject
));
//let them enter text for the body.
$form->add('TextAreaField', 'body', array(
'height' => '75px',
'width' => '95%',
'title' => ''
));
if (count($recipients) == 1)
{
$user = new User($recipients[0]);
$form->addSubmit('Send to ' . $user->getName());
}
else
$form->addSubmit('Send to All');
return $form;
}
function editFormLoad($form)
{
$userId = $this->params('user_id');
$parentId = $this->params('parent_id');
if (!$this->id)
{
$data = array();
//are we replying to a parent?
if ($parentId)
{
$class = get_class($this);
$parent = new $class($parentId);
//our subject
if (preg_match('/^Re:/', $parent->subject))
$subject = $parent->subject;
else
$subject = 'Re: ' . $parent->subject;
//create our data...
$data['parent_id'] = $parentId;
$data['recipients'] = implode(',', $parent->recipients);
$data['subject'] = $subject;
}
//are we sending to a specific user?
else if ($userId)
$data['recipients'] = array($userId);
//finally set our data.
$form->setData($data);
}
else
parent::editFormLoad($form);
}
function editPagePostSaveSync($form)
{
global $me;
parent::editPagePostSaveSync($form);
//add our recipients
$recipients = explode(',', $form->getData('recipients'));
foreach ($recipients AS $id)
$this->addRecipient($id);
//set it as ours
$this->addSender($me->id);
}
protected function addSender($id)
{
dbExecute("
INSERT INTO $this->outboxTable
(user_id, message_id)
VALUES
('$id', '$this->id')
");
}
protected function addRecipient($id)
{
//put it in our table.
dbExecute("
INSERT INTO $this->inboxTable
(user_id, message_id)
VALUES
('$id', '$this->id')
");
//email our person.
$this->emailRecipient($id);
}
protected function emailRecipient($id)
{
global $me;
$user = new User($id);
$subject = $me->getName() . ' sent you a message on ' . SITE_NAME;
//plain text
$body = "You can either log in and view your inbox, or go straight to the message with the link below.\n\n";
$body .= $user->getTicket(strtolower(get_class($this)) . ".view?id=$this->id") . "\n\n";
$body .= "Thank you,\nThe Happy " . SITE_NAME . " Message Notification Robot";
$body .= "\n\nPS. You can turn these emails off at:\n" . $user->getUrl(".emailprefs", null, true);
//html
$html = $user->getTicket(strtolower(get_class($this)) . ".view?id=$this->id", 'Click here to read the message.') . " ";
$html .= "Thank you, The Happy " . SITE_NAME . " Message Notification Robot";
$html .= " PS. You can turn these emails off " . $user->getLink('.emailprefs', 'here.', null, true);
$user->mail($subject, $body, $html);
}
public function initViewPage()
{
//get our id..
$this->id = $this->params('id');
$this->recipients = $this->getRecipients();
//init our page... with subject as the title
parent::initViewPage();
$this->pageTitle = "View Message - $this->subject";
}
public function canView()
{
global $me;
if ($this->user_id == $me->id)
return true;
if (in_array($me->id, $this->recipients))
return true;
return false;
}
public function drawViewPage()
{
global $me;
//if we have a parent... draw that one first to give context.
if ($this->parent_id)
{
$class = get_class($this);
$parent = new $class($this->parent_id);
if ($parent->parent_id)
{
echo "\n";
echo "
\n";
$this->drawUrl(".view?id=$parent->parent_id", 'Previous Message');
echo "
";
}
echo "\n";
$parent->drawMessage(true);
echo " \n";
}
//otherwise just draw our message.
else
{
echo "\n";
$this->drawMessage(true);
echo " \n";
}
}
public function drawRecipients()
{
$recips = array();
foreach ($this->recipients AS $key)
{
$recipient = new User($key);
$recips[] = $recipient->getName(true, 'tiny');
}
echo implode(", ", $recips);
}
protected function getRecipientIds($ignoreMe = false)
{
global $me;
$recips = array();
foreach ($this->recipients AS $key)
{
if (!$ignoreMe || ($ignoreMe && $me->id != $key))
$recips[] = $key;
}
return $recips;
}
public function drawMessage($kids = false)
{
global $me;
if ($this->canView())
{
//it's been read.
$this->markRead();
echo "\n";
//our title bar stuff.
$this->drawTitleBar();
$this->drawMessageDetails();
//draw our container
echo "\n";
//our actual content
$this->drawMessageBody();
//only reply if we sent it.
if (!$this->creator->isMe())
$this->drawMessageReplyForm();
//draw our children
$this->drawChildMessages($kids, $this->id);
echo "
";
//end comment
echo " ";
}
}
protected function drawTitleBar()
{
//heres our title bar
echo "";
//our title icon
echo '
";
//end title bar
echo "
\n";
}
protected function drawMessageDetails()
{
//our hidden detail div.
echo "id\" style=\"display: none;\">";
echo "
";
echo "Subject: $this->subject ";
echo "Sent by: " . $this->creator->getName(true) . " \n";
echo "Sent: " . Time::formatRelative($this->add_date, false) . " \n";
echo "Recipients: ";
$this->drawRecipients();
echo "
";
echo "
\n";
}
protected function drawMessageBody()
{
echo "";
echo Linkify::bbcode($this->body);
$this->drawMessageSignature();
echo "
";
}
protected function drawMessageSignature()
{
}
protected function drawMessageReplyForm()
{
//and our actual reply form
echo "id\" style=\"display: none;\">\n";
$form = $this->createReplyForm(array($this->creator->id));
$form->drawAll();
echo "
\n";
//and our actual reply to all form
if (count($this->recipients) > 1)
{
echo "id\" style=\"display: none;\">\n";
$recips = $this->getRecipientIds(true);
$recips[] = $this->user_id;
$form = $this->createReplyForm($recips);
$form->drawAll();
echo "
\n";
}
}
function drawChildMessages()
{
//draw our kids if we can.
$kidRs = dbQuery("
SELECT id
FROM $this->tableName
WHERE parent_id = '$this->id'
ORDER BY add_date DESC
");
if (dbGetNumRows($kidRs))
{
echo "";
echo "
\n";
while ($kidAr = dbFetchAssoc($kidRs))
{
$class = get_class($this);
$kid = new $class($kidAr['id']);
$kid->drawMessage(true);
}
echo " \n";
echo "
";
}
}
function drawHeaderRow($type)
{
echo "\n";
}
function drawRow($type, $on)
{
global $me;
$class = "messageRow";
if (!$this->read && $me->id != $this->user_id)
$class .= " unread";
if ($on)
$class .= " on";
echo "\n";
echo "\t\n";
echo "\t\t id\"/>\n";
echo "\t \n";
if ($type == 'inbox')
{
echo "\t\n";
echo "\t\t" . $this->creator->getName(true, 'tiny') . "\n";
echo "\t \n";
}
else
{
echo "\t\n";
echo "\t\t";
echo $this->drawRecipients();
echo "\n\t \n";
}
echo "\t\n\t\t";
$this->drawUrl(array('page' => 'view', 'id' => $this->id), $this->subject);
echo "\n\t \n";
echo "\t\n";
echo "\t\t" . Time::formatRelative($this->add_date, false) . "\n";
echo "\t \n";
echo " \n";
}
function markRead($force = false)
{
global $me;
if (in_array($me->id, $this->recipients) || $force)
{
dbExecute("
UPDATE $this->inboxTable
SET `read` = '1'
WHERE user_id = '$me->id'
AND message_id = '$this->id'
");
}
}
function getChildren()
{
$kids = array();
if ($this->id)
{
$rs = dbQuery("
SELECT *
FROM $this->tableName
WHERE parent_id = '$this->id'
");
while ($ar = dbFetchAssoc($rs))
$kids[] = new Message($ar);
}
return $kids;
}
function getRssItem()
{
$rss = parent::getRssItem();
$rss->title = $this->subject;
$rss->description = $this->body;
return $rss;
}
function getPublicData()
{
$data = parent::getPublicData();
$data['name'] = $this->subject;
$data['message'] = $this->body;
$data['read'] = $this->read;
return $data;
}
public function getName($link = false)
{
return parent::getName($link, $this->subject);
}
public function getCreateFieldsArray()
{
$fields = parent::getCreateFieldsArray();
$fields['parent_id'] = "parent_id int(11) default 0 not null";
$fields['subject'] = "subject varchar(255) default '' not null";
$fields['body'] = "body text default '' not null";
$fields['recipients'] = "recipients text default '' not null";
return $fields;
}
public function getCreateIndexesArray()
{
$fields = parent::getCreateIndexesArray();
$fields['parent_id'] = "key(parent_id)";
return $fields;
}
}
?>