'doList',
'cancel' => 'doBack',
'new' => 'doNew',
'insert' => 'doInsert',
'edit' => 'doEdit',
'update' => 'doUpdate',
'del' => 'doDelete',
'setDefaultTemplate' => 'doSetDefaultTemplate',
'chooseRecipients' => 'doChooseRecipients',
'saveRecipients' => 'doSaveRecipients',
'preview' => 'doPreview',
'send' => 'doSend',
'history' => 'doHistory',
);
function exec($Doc)
{
$this->DBObj = SiteMap::getObj('Mail/Obj.php', isset($_REQUEST['id']) ? $_REQUEST['id'] : null);
$this->Doc = $Doc;
parent::exec();
}
function _doListBind(&$DB_List)
{
$DB_List->insertColLast(
'send_date',
array(
'title' => 'Posted Date',
'order_by' => 'send_date',
'tpl' => DB_LIST_CELL_DATE,
'params' => array('send_date')
)
);
$DB_List->insertColLast(
'title',
array(
'title' => 'Title',
'order_by' => 'title',
'tpl' => DB_LIST_CELL_TEXT,
'params' => array('title')
)
);
$DB_List->insertColLast(
'options',
array(
'title' => 'Options',
'tpl' => 'Mail/cells/options.tpl',
'edit_link' => BASE_URL . '/' . CURR_PAGE . '?action=edit&id=',
'del_link' => BASE_URL . '/' . CURR_PAGE . '?action=del&id=',
'params' => array('id')
)
);
return true;
}
function _bindFormFields($form)
{
$form->addElement('text', 'subject', 'Subject');
$form->addElement('text', 'mail_from', 'From');
$form->addElement('text', 'author', 'Author');
$send_date = &$form->createElement('calendar', 'send_date', 'Posting Date');
$form->addElement($send_date);
$form->addElement('html_editor', 'html', 'Content');
$form->addRuleRequired(array('subject', 'mail_from', 'author'));
$form->addRuleRequired(array('html'), 'server');
return $form;
}
function _getNewForm()
{
$form = $this->_getBaseForm();
$form = $this->_bindFormFields($form);
$data = array(
'send_date' => date('Y-m-d'),
'html' => $this->DBObj->getDefaultTemplate(),
);
$form->setDefaults($data);
$this->setFormTitle($form, 'Add '.$this->DBObj->itemName);
$form->addElement('hidden', 'action', 'insert') ;
return $form;
}
function doSetDefault()
{
$this->DBObj->setDefault();
$this->doBack();
}
function addLink()
{
$item = array(
'tpl' => 'Mail/template-toolbar.tpl',
'options' => $this->DBObj->getMailTemplate4Select('title'),
'selected' => $this->DBObj->getDefaultTemplateId(),
);
$this->Doc->addContent($item);
parent::addLink();
}
function doSetDefaultTemplate()
{
$this->DBObj->setDefaultTemplate($_REQUEST['id_template']);
$this->doBack();
}
function getRecipientsFrom()
{
$form = $this->_getBaseForm();
$form->addElement(
'extended_textarea',
'recipients_list',
'Email addresses',
array('cols' => 80, 'rows' => 4),
'',
'
Note: You may enter one or more email addresses above separated by commas
'
);
$form->addElement('file_db', 'recipients_csv', 'CSV file with email addresses');
$form->addElement(
'advmultiselect',
'groups',
array('Contact Groups', 'Available', 'Selected'),
$this->DBObj->getContactGroup4Select('title'),
array('style' => 'width:185px;')
);
$form->addElement('hidden', 'action', 'saveRecipients');
$form->addElement('hidden', 'id');
$form->addFormRule(array($this, 'validateForm'));
$form->setSubmitTitle('Save & Preview');
return $form;
}
function doChooseRecipients()
{
$form = $this->getRecipientsFrom();
$this->DBObj->initFromDB();
$data = $this->DBObj->getData();
$form->setDefaults($data);
$this->renderRecipientsForm($form);
}
function parseEmails($text)
{
$emails = preg_replace('/[\r\n]+/',' ', $text);
$emails = split('[, ;]', $emails);
$emails = array_filter($emails, 'trim');
return $emails;
}
function getRecipientsFromData($data)
{
$emails = array();
$recipients_list = $this->parseEmails($data['recipients_list']);
if (!empty($recipients_list)) {
$emails = array_merge($emails, $recipients_list);
}
if (!empty($data['groups'])) {
$contactEmails = $this->DBObj->getContactEmailsByGroup($data['groups']);
$emails = array_merge($emails, $contactEmails);
}
if (!empty($data['recipients_csv'])) {
$file = WWW_PATH . '/userfiles/' . $data['recipients_csv'];
$recipients_csv = file_get_contents($file);
$recipients_csv = $this->parseEmails($recipients_csv);
$emails = array_merge($emails, $recipients_csv);
}
$emails = array_unique($emails);
return $emails;
}
function validateForm($data, $files)
{
$errors = array();
$emails = array();
// textarea
$recipients_list = $this->parseEmails($data['recipients_list']);
if (!empty($recipients_list)) {
if (true !== ($invalidEmails = $this->validateEmails($recipients_list))) {
$errors['recipients_list'] = 'Wrong email address (' . implode(', ', $invalidEmails) . ')';
} else {
$emails = array_merge($emails, $recipients_list);
}
}
// file csv
if (isset($data['recipients_csv']['file_name'])) {
$file = WWW_PATH . '/userfiles/' . $data['recipients_csv']['file_name'];
} else {
$file = $files['recipients_csv']['tmp_name']['file'];
$contentType = $files['recipients_csv']['type']['file'];
if (!in_array($contentType, array('text/csv', 'text/comma-separated-values', 'application/octet-stream'))) {
$errors['recipients_csv'] = 'File is in wrong format';
$file = null;
}
}
if ($file && file_exists($file)) {
$recipients_csv = file_get_contents($file);
$recipients_csv = $this->parseEmails($recipients_csv);
if (true !== ($invalidEmails = $this->validateEmails($recipients_csv))) {
$errors['recipients_csv'] = 'Wrong email address (' . implode(', ', $invalidEmails) . ')';
} else {
$emails = array_merge($emails, $recipients_csv);
}
}
if (!empty($data['groups'])) {
$contactEmails = $this->DBObj->getContactEmailsByGroup($data['groups']);
$emails = array_merge($emails, $contactEmails);
}
if (empty($emails)) {
$errors['form_error'] = 'Emails are required';
}
return empty($errors) ? true : $errors;
}
function validateEmails($emails)
{
$err = array();
$regex = '/^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/';
$emails = array_unique($emails);
$invalidEmails = array();
foreach ($emails as $email) {
if (!preg_match($regex, $email)) {
$invalidEmails[] = $email;
}
}
if (count($invalidEmails)) {
return $invalidEmails;
}
return true;
}
function doSaveRecipients()
{
$form = $this->getRecipientsFrom();
if ($form->validate()) {
$this->DBObj->initFromForm($form);
$this->DBObj->update($data);
require_once('class/HTTP.php');
skHTTP::redirect(BASE_URL . '/' . CURR_PAGE . '?action=preview&id=' . $this->DBObj->id);
}
$this->renderRecipientsForm($form);
}
function renderRecipientsForm($form)
{
if (isset($form->_errors['form_error'])) {
$this->Doc->addContent(array('tpl' => 'msg_error.tpl', 'text' => $form->_errors['form_error']));
}
$form->exec();
}
function doPreview()
{
$this->DBObj->initFromDB();
$item = $this->DBObj->getData();
$recipients = $this->getRecipientsFromData($item);
$item['recipients'] = $recipients;
$item['recipientsCount'] = count($recipients);
$item['tpl'] = BASE_PATH . '/tpl/Mail/preview.tpl';
$this->Doc->addContent($item);
}
function doSend()
{
$this->DBObj->initFromDB();
$data = $this->DBObj->getData();
$data['html'] = str_replace(BASE_URL . '/fckfiles/', '', $data['html']);
$recipients = $this->getRecipientsFromData($data);
require_once('lib/htmlMimeMail/htmlMimeMail.php');
$mail = new htmlMimeMail();
$mail->setHtml($data['html'], null, realpath(WWW_PATH . '/fckfiles') . '/');
$mail->setFrom($data['mail_from']);
$mail->setSubject($data['subject']);
$mail->setHeader('X-Mailer', 'HTML Mime mail class');
foreach ($recipients as $email) {
$result = $mail->send(array($email));
$this->DBObj->writeReport($data['id'], $email, $result);
}
Session::setData($this->_getPage4SaveMessage(), 'msg', $this->DBObj->itemName . ' has been sent');
$this->doBack();
}
function doHistory()
{
//$this->addLink();
$item = $this->DBObj->getReport();
$item['tpl'] = BASE_PATH . '/tpl/Mail/history.tpl';
$this->Doc->addContent($item);
}
}