コード例 #1
0
ファイル: Plugin.php プロジェクト: aydancoskun/octobercms
 /**
  * Attach event listeners on boot.
  * @return void
  */
 public function boot()
 {
     // Before send: attach blank image that will track mail opens
     Event::listen('mailer.prepareSend', function ($self, $view, $message) {
         $swift = $message->getSwiftMessage();
         $mail = Email::create(['code' => $view, 'to' => $swift->getTo(), 'cc' => $swift->getCc(), 'bcc' => $swift->getBcc(), 'subject' => $swift->getSubject(), 'body' => $swift->getBody(), 'sender' => $swift->getSender(), 'reply_to' => $swift->getReplyTo(), 'date' => $swift->getDate()]);
         $url = Backend::url('mja/mail/image/image', ['id' => $mail->id, 'hash' => $mail->hash . '.png']);
         $swift->setBody($swift->getBody() . '<img src="' . $url . '" style="display:none;width:0;height:0;" />');
     });
     // After send: log the result
     Event::listen('mailer.send', function ($self, $view, $message, $response) {
         $swift = $message->getSwiftMessage();
         $mail = Email::where('code', $view)->get()->last();
         if ($mail === null) {
             return;
         }
         $mail->response = $response;
         $mail->sent = true;
         $mail->save();
     });
     // Use for the mails sent list filter
     Event::listen('backend.filter.extendScopesBefore', function ($filter) {
         if (!$filter->getController() instanceof MailController) {
             return;
         }
         $filter->scopes['views']['options'] = [];
         $templates = MailTemplate::get();
         foreach ($templates as $template) {
             $filter->scopes['views']['options'][$template->code] = $template->code;
         }
     });
     // Extend the mail template so that we could have the number of sent emails
     // in the template list of this plugin.
     MailTemplate::extend(function ($model) {
         // Email relation
         $model->addDynamicMethod('emails', function () use($model) {
             return $model->hasMany('Mja\\Mail\\Models\\Email', 'code', 'code');
         });
         // Emails sent
         $model->addDynamicMethod('getTimesSentAttribute', function () use($model) {
             return (int) $model->emails()->count();
         });
         // Email opens
         $model->addDynamicMethod('getTimesOpenedAttribute', function () use($model) {
             return (int) $model->opens()->count();
         });
         // Last time sent
         $model->addDynamicMethod('getLastSentAttribute', function () use($model) {
             $email = $model->emails()->orderBy('id', 'desc')->first();
             return $email ? $email->created_at : null;
         });
         // Last time opened
         $model->addDynamicMethod('getLastOpenAttribute', function () use($model) {
             $emails = $model->emails()->get();
             $last_open = null;
             foreach ($emails as $email) {
                 $lo = $email->opens()->orderBy('id', 'desc')->first();
                 if ($lo && ($lo->created_at < $last_open || $last_open === null)) {
                     $last_open = $lo->created_at;
                 }
             }
             return $last_open;
         });
         // Get the email opens by template
         $model->addDynamicMethod('opens', function () use($model) {
             $model->setKeyName('code');
             $data = $model->hasManyThrough('Mja\\Mail\\Models\\EmailOpens', 'Mja\\Mail\\Models\\Email', 'code', 'email_id');
             $model->setKeyName('id');
             return $data;
         });
     });
 }