457 lines
13 KiB
PHP
457 lines
13 KiB
PHP
<?php
|
|
|
|
namespace BRSystems\Core\Cms\cmsMailEngine\inc;
|
|
|
|
use BRSystems\Core\Library\cDb\cDb;
|
|
use BRSystems\Core\Library\cDbLog\cDbLog;
|
|
|
|
/**
|
|
* cmsMailEngine Module
|
|
*
|
|
* @category cmsMailEngine Module
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @since Version 2.0
|
|
*/
|
|
class cmsMailEngineModule
|
|
{
|
|
public $db;
|
|
public $table;
|
|
|
|
const CRON_TIMER_INTERVAL = 2;
|
|
|
|
/**
|
|
* Class constructor
|
|
*/
|
|
public function __construct() {
|
|
|
|
$this->db = cDb::getInstance();
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to get module records
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category cmsMailEngine Module
|
|
* @param string $where - sql query
|
|
* @param string $sort - sql query
|
|
* @param int $offset - db table offset
|
|
* @param int $limitPerPage - limit per page
|
|
* @return array of data
|
|
* @version 1.1
|
|
*/
|
|
public function getModuleRecords($where, $sort, $offset = 0, $limitPerPage = 20){
|
|
|
|
$sSql = "
|
|
SELECT SQL_CALC_FOUND_ROWS
|
|
mm.*,
|
|
mc.cl_name
|
|
FROM
|
|
{$this->table} mm
|
|
LEFT JOIN mod_mail_constructor mcc ON
|
|
mcc.id = mm.mail_template_id
|
|
LEFT JOIN mod_classificators mc ON
|
|
mc.id = mcc.mail_type
|
|
WHERE
|
|
1 {$where}
|
|
ORDER BY
|
|
{$sort}
|
|
LIMIT
|
|
{$offset}, {$limitPerPage}
|
|
";
|
|
|
|
$this->db->query($sSql);
|
|
return $this->db->getArray();
|
|
}
|
|
|
|
|
|
/**
|
|
* Function to get module records for plugin
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category cmsMailEngine Module
|
|
* @return array of data
|
|
* @version 1.1
|
|
*/
|
|
public function getPluginRecords(){
|
|
|
|
$sSql = "
|
|
SELECT
|
|
mm.id,
|
|
mm.mail_owner,
|
|
mm.mail_title
|
|
FROM
|
|
mod_mail_outgoing mm
|
|
LEFT JOIN mod_mail_constructor mcc ON
|
|
mcc.id = mm.mail_template_id
|
|
/*LEFT JOIN mod_classificators mc ON
|
|
mc.id = mcc.mail_type*/
|
|
WHERE
|
|
1
|
|
ORDER BY
|
|
mm.mail_inserted DESC
|
|
LIMIT
|
|
0, 150
|
|
";
|
|
|
|
$this->db->query($sSql);
|
|
return $this->db->getArray();
|
|
}
|
|
|
|
/**
|
|
* Function to delete table records form db
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category cmsMailEngine Module
|
|
* @param array $ids - record ids to delete
|
|
* @return bool true / false
|
|
* @version 1.1
|
|
*/
|
|
public function deleteRecords($ids){
|
|
|
|
$ids = implode(',', $ids);
|
|
if(!$this->db->execute("DELETE FROM {$this->table} WHERE id IN ({$ids})")){
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Function to change record status in db table
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category cmsMailEngine Module
|
|
* @param array $ids - record ids to affect
|
|
* @param string $field - field to update for status ENUM
|
|
* @return bool true / false
|
|
* @version 1.1
|
|
*/
|
|
public function changeRecordsStatus($ids, $field){
|
|
if(count($ids) && $field){
|
|
$time = time();
|
|
$ids = implode(',', $ids);
|
|
$status = $this->db->query("UPDATE {$this->table} mm SET {$field} = '1', mail_modified = '{$time}' WHERE {$field} = '0' AND mail_locked = '0' AND id IN ({$ids})");
|
|
if(!$status)
|
|
return $status;
|
|
$status = $this->db->query("UPDATE {$this->table} mm SET {$field} = '0', mail_modified = '{$time}' WHERE {$field} = '1' AND mail_locked = '0' AND id IN ({$ids}) AND mail_modified <> '{$time}'");
|
|
return $status;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Function to get record by id
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category cmsMailEngine Module
|
|
* @param int $id - record id
|
|
* @return array of data
|
|
* @version 1.1
|
|
*/
|
|
public function getRecordById($id){
|
|
|
|
return $this->db->_querySelectRow($this->table, false, array('id' => $id));
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to record data by id
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category cmsMailEngine Module
|
|
* @param int $id - record id
|
|
* @param array $data - data to update
|
|
* @return boot true or false
|
|
* @version 1.1
|
|
*/
|
|
public function saveRecordById($id, $data){
|
|
|
|
if(count($data)){
|
|
if($id){
|
|
if(!$this->db->_queryUpdate($this->table, $data, array('id' => $id), true)){
|
|
return false;
|
|
}
|
|
}else{
|
|
if(!$id = $this->db->_queryInsert($this->table, $data, false, true)){
|
|
return false;
|
|
}
|
|
}
|
|
return $id;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Function to get mails for cron to send
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category cmsMailEngine Module
|
|
* @param int $limit - limit to get mails in one row
|
|
* @internal param array $rows - record id rows array
|
|
* @return array|bool true / false
|
|
* @version 1.1
|
|
*/
|
|
public function getCronMAILS($limit = 20){
|
|
|
|
// Select 20 mail with atachments and 20 without
|
|
$sSql = "
|
|
SELECT DISTINCT * FROM (
|
|
SELECT * FROM (
|
|
SELECT
|
|
*
|
|
FROM
|
|
mod_mail_outgoing
|
|
WHERE
|
|
mail_locked = '0'
|
|
AND mail_disabled = '0'
|
|
AND mail_send_atempt <= 4
|
|
AND LENGTH(mail_owner) > 0
|
|
ORDER BY
|
|
mail_priority, mail_send_atempt, id ASC
|
|
LIMIT
|
|
0, {$limit}
|
|
) att
|
|
UNION ALL
|
|
SELECT * FROM (
|
|
SELECT
|
|
*
|
|
FROM
|
|
mod_mail_outgoing
|
|
WHERE
|
|
mail_locked = '0'
|
|
AND mail_disabled = '0'
|
|
AND mail_attachment = ''
|
|
AND mail_send_atempt <= 4
|
|
AND LENGTH(mail_owner) > 0
|
|
ORDER BY
|
|
mail_priority, mail_send_atempt, id ASC
|
|
LIMIT
|
|
0, {$limit}
|
|
) natt
|
|
) mtt
|
|
" . "";
|
|
|
|
$this->db->query($sSql);
|
|
return $this->db->getArray();
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to disable all new messages in table
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category cmsMailEngine Module
|
|
* @return bool true / false
|
|
* @version 1.1
|
|
*/
|
|
public function stopNewOutgoing(){
|
|
|
|
$sSql = "UPDATE mod_mail_outgoing SET mail_disabled = '1' WHERE mail_locked = '0'";
|
|
return $this->db->query($sSql);
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to enable all new messages in table
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category cmsMailEngine Module
|
|
* @return bool true / false
|
|
* @version 1.1
|
|
*/
|
|
public function resumeNewOutgoing(){
|
|
|
|
$sSql = "UPDATE mod_mail_outgoing SET mail_disabled = '0' WHERE mail_disabled = '1' AND mail_locked = '0'";
|
|
return $this->db->query($sSql);
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to insert mails for all newsletter users
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category cmsMailEngine Module
|
|
* @param array $data - insert query data fields with values
|
|
* @return bool true / false
|
|
* @version 1.1
|
|
*/
|
|
public function createMailsForAllUserMails($data){
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to get time before next 5min cron call
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category cmsMailEngine Module
|
|
* @param int $time - by ref time in seconds
|
|
* @return bool true / false
|
|
* @version 1.1
|
|
*/
|
|
public function getNext5MinCronCall(&$time)
|
|
{
|
|
$min = self::CRON_TIMER_INTERVAL;
|
|
$sSql = "
|
|
SELECT
|
|
`time`
|
|
FROM
|
|
" . cDbLog::C_BASE_ACTION_TABLE . "
|
|
WHERE
|
|
`action` like '%chk_cron%'
|
|
ORDER BY
|
|
id DESC
|
|
LIMIT
|
|
1
|
|
";
|
|
|
|
$t = 0;
|
|
$time = 'Not active!';
|
|
|
|
$this->db->query($sSql);
|
|
if ($tt = $this->db->getOne()) {
|
|
if ($tt + 60 * $min + 5 >= time()) {
|
|
$t = $tt + 60 * $min - time();
|
|
$time = '00:00';
|
|
if ($t > 0) {
|
|
$time = date("i:s", $t);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $t;
|
|
}
|
|
|
|
/**
|
|
* Function to get newsletter sent count today
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category cmsMailEngine Module
|
|
* @param array $between - search between
|
|
* @return mixed
|
|
* @version 1.1
|
|
*/
|
|
public function getNewsletterSentCount($between = array()){
|
|
|
|
$count = false;
|
|
|
|
if(!!$between){
|
|
$sSql = "
|
|
SELECT
|
|
COUNT(ms.id)
|
|
FROM
|
|
mod_mail_sent ms
|
|
WHERE
|
|
ms.mail_marker = 'newsletter'
|
|
AND ms.mail_sent BETWEEN {$between['from']} AND {$between['till']}
|
|
";
|
|
|
|
$this->db->query($sSql);
|
|
$count = $this->db->getOne();
|
|
}
|
|
|
|
return $count;
|
|
}
|
|
|
|
/**
|
|
* Function to enable newsletter outgoing mail count
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category cmsMailEngine Module
|
|
* @param int $enable - item count to enable
|
|
* @return mixed
|
|
* @version 1.1
|
|
*/
|
|
public function enableOutgoingNewsletterCount($enable = 0){
|
|
|
|
if($enable > 0){
|
|
|
|
$sSql = "
|
|
CREATE TEMPORARY TABLE _temp_outgoing_mail_upd
|
|
SELECT
|
|
mm.id
|
|
FROM
|
|
mod_mail_outgoing mm
|
|
WHERE
|
|
mm.mail_disabled = '1'
|
|
AND mm.mail_locked = '0'
|
|
AND mm.mail_marker = 'newsletter'
|
|
ORDER BY
|
|
mm.mail_priority, mm.mail_send_atempt, mm.mail_disabled, mm.id ASC
|
|
LIMIT
|
|
0, {$enable};
|
|
";
|
|
|
|
$this->db->query("DROP TABLE IF EXISTS _temp_outgoing_mail_upd");
|
|
$this->db->query($sSql);
|
|
$this->db->query("UPDATE mod_mail_outgoing SET mail_disabled = '0' WHERE id IN (SELECT id FROM _temp_outgoing_mail_upd)");
|
|
$this->db->query("DROP TABLE IF EXISTS _temp_outgoing_mail_upd");
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Function to enable newsletter outgoing mail count
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category cmsMailEngine Module
|
|
* @return mixed
|
|
* @version 1.1
|
|
*/
|
|
public function getMailTemplateDataByLanguage(){
|
|
|
|
|
|
$sSql = "
|
|
SELECT
|
|
mcc.id,
|
|
mcc.mail_type,
|
|
mccd.mail_title,
|
|
mccd.mail_data,
|
|
mccd.mail_lang_id as lang_id
|
|
FROM
|
|
mod_mail_constructor mcc
|
|
LEFT JOIN mod_mail_constructor_data mccd ON
|
|
mcc.id = mccd.mail_id
|
|
WHERE
|
|
mcc.mail_disabled = '0'
|
|
";
|
|
|
|
$this->db->query($sSql);
|
|
return $this->db->getFieldBasedArrayFieldsAdv(array('mail_type', 'lang_id'), array('mail_title', 'mail_data', 'lang'));
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to gather all newsletter subscription emails
|
|
*
|
|
* @author Aivars Žukovskis <info@brsystems.lv>
|
|
* @category cmsMailEngine Module
|
|
* @return mixed
|
|
* @version 1.1
|
|
*/
|
|
public function gatherAllNewsletterSubscriptionEmails(){
|
|
|
|
$this->db->_querySelect('mod_users_subscription_mails', array('email'), array('email_sub' => 1), false, false, false);
|
|
return $this->db->getKeyValBasedArray('email', 'email');
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to gather all system users emails + additional data
|
|
*
|
|
* @author Aivars Žukovskis <info@brsystems.lv>
|
|
* @category cmsMailEngine Module
|
|
* @return mixed
|
|
* @version 1.1
|
|
*/
|
|
public function gatherAllSystemUsersEmails(){
|
|
|
|
return $this->db->_querySelect('mod_users', array('user_name', 'user_surname', 'user_email', 'user_lang_id'), array('user_blocked' => 0, 'user_deleted' => 0));
|
|
|
|
}
|
|
}
|