- Изменен метод afterRegistration

parent 09d60be7
...@@ -4,7 +4,10 @@ namespace common\modules\users\models; ...@@ -4,7 +4,10 @@ namespace common\modules\users\models;
use common\components\UnisenderAPI; use common\components\UnisenderAPI;
use common\modules\messageTemplate\controllers\TemplateAdminController; use common\modules\messageTemplate\controllers\TemplateAdminController;
use common\modules\messageTemplate\models\MessageTemplate; use common\modules\messageTemplate\models\MessageTemplate;
use common\modules\triggers\components\conditions\conditions\CheckUserToRegistration;
use common\modules\triggers\models\TriggerCondition;
use common\modules\triggers\models\TriggerSchedule; use common\modules\triggers\models\TriggerSchedule;
use common\modules\triggers\models\TriggerTrigger;
use Yii; use Yii;
use yii\base\NotSupportedException; use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior; use yii\behaviors\TimestampBehavior;
...@@ -548,52 +551,59 @@ class User extends \common\components\ActiveRecordModel implements IdentityInter ...@@ -548,52 +551,59 @@ class User extends \common\components\ActiveRecordModel implements IdentityInter
/** /**
* @param $template_id * @param $template_id
*/ */
public function afterRegistration($template_id, $params=array()){ public function afterRegistration(){
/** @var \DateTime $time_now */ /** @var TriggerTrigger[] $actualTriggers */
$time_now=new \DateTime(); $actualTriggers = TriggerTrigger::getActualTriggers();
/** @var MessageTemplate $templateModel */ foreach($actualTriggers as $trigger) {
$templateModel = MessageTemplate::findOne($template_id); $exists = TriggerCondition::find()->where(['trigger_id'=>$trigger->id, 'condition_id'=>CheckUserToRegistration::CONDITION_ID])->exists();
/** @var Templates $template */ if ($exists===true) {
$template = new Templates($template_id, $params); /** @var \DateTime $time_now */
$email = $this->email; $time_now=new \DateTime();
$sender = new UnisenderAPI(); /** @var MessageTemplate $templateModel */
// Create the send list $templateModel = MessageTemplate::findOne($trigger->message_template_id);
$newList = $sender->createList(); /** @var Templates $template */
$newListObject=Json::decode($newList); $template = new Templates($trigger->message_template_id);
if (array_key_exists('result', $newListObject) && array_key_exists('id', $newListObject['result'])) { $email = $this->email;
$newListId=$newListObject['result']['id']; $sender = new UnisenderAPI();
// Subscribe user to new List // Create the send list
$sender->subscribe(['list_ids' => $newListId, 'fields[email]' => $email, 'double_optin' => 1]); $newList = $sender->createList();
// Create new message $newListObject=Json::decode($newList);
$newMessage=$sender->createEmailMessage($this->name, $email, $templateModel->subject, $template->getTemplate(), $newListId); if (array_key_exists('result', $newListObject) && array_key_exists('id', $newListObject['result'])) {
// Decode result $newListId=$newListObject['result']['id'];
$newMessageObject=Json::decode($newMessage); // Subscribe user to new List
if (array_key_exists('result', $newMessageObject) && array_key_exists('message_id', $newMessageObject['result'])) { $sender->subscribe(['list_ids' => $newListId, 'fields[email]' => $email, 'double_optin' => 1]);
// Get the message ID // Create new message
$newMessageId=$newMessageObject['result']['message_id']; $newMessage=$sender->createEmailMessage($this->name, $email, $templateModel->subject, $template->getTemplate(), $newListId);
// Create new campaign // Decode result
$newCampaign = $sender->createCampaign($newMessageId); $newMessageObject=Json::decode($newMessage);
// Parse the result if (array_key_exists('result', $newMessageObject) && array_key_exists('message_id', $newMessageObject['result'])) {
$newCampaignObject = Json::decode($newCampaign); // Get the message ID
if (array_key_exists('result', $newCampaignObject) && array_key_exists('campaign_id', $newCampaignObject['result'])) { $newMessageId=$newMessageObject['result']['message_id'];
$newCampaignId = $newCampaignObject['result']['campaign_id']; // Create new campaign
$newCampaign = $sender->createCampaign($newMessageId);
$schedule = new TriggerSchedule(); // Parse the result
$schedule->sended=1; $newCampaignObject = Json::decode($newCampaign);
$schedule->checked=0; if (array_key_exists('result', $newCampaignObject) && array_key_exists('campaign_id', $newCampaignObject['result'])) {
$schedule->message_id=$newMessageId; $newCampaignId = $newCampaignObject['result']['campaign_id'];
$schedule->message=$template->getTemplate();
$schedule->email=$email; $schedule = new TriggerSchedule();
$schedule->time=$time_now->format('Y-m-d H:i:s'); $schedule->sended=1;
$schedule->date_create=$time_now->format('Y-m-d H:i:s'); $schedule->checked=0;
$schedule->list_id=$newListId; $schedule->message_id=$newMessageId;
$schedule->campaign_id=$newCampaignId; $schedule->message=$template->getTemplate();
if (!$schedule->save()) { $schedule->email=$email;
echo 'Письмо не было отправлено'; $schedule->time=$time_now->format('Y-m-d H:i:s');
} $schedule->date_create=$time_now->format('Y-m-d H:i:s');
} $schedule->list_id=$newListId;
} $schedule->campaign_id=$newCampaignId;
} if (!$schedule->save()) {
echo 'Письмо не было отправлено';
}
}
}
}
}
}
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment