Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a new method miniShop2::shootEmail #933

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 66 additions & 67 deletions core/components/minishop2/model/minishop2/minishop2.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -905,27 +905,6 @@ public function changeOrderStatus($order_id, $status_id)
}
}

$useScheduler = $this->modx->getOption('ms2_use_scheduler', null, false);
$task = null;
if ($useScheduler) {
/** @var Scheduler $scheduler */
$path = $this->modx->getOption(
'scheduler.core_path',
null,
$this->modx->getOption('core_path') . 'components/scheduler/'
);
$scheduler = $this->modx->getService('scheduler', 'Scheduler', $path . 'model/scheduler/');
if ($scheduler) {
$task = $scheduler->getTask('minishop2', 'ms2_send_email');
if (!$task) {
$task = $this->createEmailTask();
}
} else {
$useScheduler = false;
$this->modx->log(1, 'not found Scheduler extra');
}
}

if ($status->get('email_manager')) {
$subject = $this->pdoTools->getChunk('@INLINE ' . $status->get('subject_manager'), $pls);
$tpl = '';
Expand All @@ -940,21 +919,7 @@ public function changeOrderStatus($order_id, $status_id)
$this->modx->getOption('ms2_email_manager', null, $this->modx->getOption('emailsender'))
)
);
if (!empty($subject)) {
foreach ($emails as $email) {
if (preg_match('#.*?@#', $email)) {
if ($useScheduler && $task instanceof sTask) {
$task->schedule('+1 second', [
'email' => $email,
'subject' => $subject,
'body' => $body
]);
} else {
$this->sendEmail($email, $subject, $body);
}
}
}
}
$this->shootEmail($emails, $subject, $body);
}

if ($status->get('email_user')) {
Expand All @@ -966,24 +931,78 @@ public function changeOrderStatus($order_id, $status_id)
}
$body = $this->modx->runSnippet('msGetOrder', array_merge($pls, ['tpl' => $tpl]));
$email = $profile->get('email');
if (!empty($subject) && preg_match('#.*?@#', $email)) {
if ($useScheduler && $task instanceof sTask) {
$task->schedule('+1 second', [
'email' => $email,
'subject' => $subject,
'body' => $body
]);
} else {
$this->sendEmail($email, $subject, $body);
}
}
$this->shootEmail($email, $subject, $body);
}
}
}

return true;
}

/**
* Method of sending email depends on the settings
*
* @param array|string $emails
* @param string $subject
* @param string $body
* @param bool $force
*
* @return bool
*/
public function shootEmail($emails, $subject, $body = '', $force = false)
{
if (empty($subject) || empty($emails)) {
return false;
}
if (is_string($emails)) {
$emails = [$emails];
}

$sTask = null;
if ($force === false && $this->modx->getOption('ms2_use_scheduler', null, false)) {
/** @var Scheduler $scheduler */
$scheduler = $this->modx->getService('scheduler', 'Scheduler',
$this->modx->getOption('scheduler.core_path', null, MODX_CORE_PATH . 'components/scheduler/') . 'model/scheduler/'
);
if ($scheduler) {
$sTask = $scheduler->getTask('minishop2', 'ms2_send_email');
if (empty($sTask)) {
$sTask = $this->modx->newObject('sFileTask');
$sTask->fromArray([
'class_key' => 'sFileTask',
'content' => '/tasks/sendEmail.php',
'namespace' => 'minishop2',
'reference' => 'ms2_send_email',
'description' => 'MiniShop2 Email'
]);
if (!$sTask->save()) {
$sTask = null;
}
}
} else {
$this->modx->log(1, 'Not found Scheduler extra');
}
}

$result = false;
foreach ($emails as $email) {
if (preg_match('#.*?@#', $email)) {
if ($sTask instanceof sTask) {
$sTask->schedule('+1 second', [
'email' => $email,
'subject' => $subject,
'body' => $body
]);
$result = true;
} else {
$result = $this->sendEmail($email, $subject, $body);
}
}
}

return $result;
}

/**
* Function for sending email
*
Expand Down Expand Up @@ -1307,24 +1326,4 @@ protected function updateSetting($key, $value)
$setting->set('value', json_encode($value));
$setting->save();
}

/**
* Creating Sheduler's task for sending email
* @return false|object|null
*/
private function createEmailTask()
{
$task = $this->modx->newObject('sFileTask');
$task->fromArray([
'class_key' => 'sFileTask',
'content' => '/tasks/sendEmail.php',
'namespace' => 'minishop2',
'reference' => 'ms2_send_email',
'description' => 'MiniShop2 Email'
]);
if (!$task->save()) {
return false;
}
return $task;
}
}
Loading