Blame view

common/behaviors/NotifyBehavior.php 2.65 KB
8f36efb7   Yarik   Added comments to...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  <?php
      
      namespace common\behaviors;
      
      use common\models\Articles;
      use common\modules\comment\models\CommentModel;
      use common\modules\product\models\Product;
      use common\widgets\Mailer;
      use yii\base\Behavior;
      use yii\base\Event;
      use yii\db\ActiveRecord;
      use yii\helpers\Url;
      
      /**
       * Class NotifyBehavior
       * @property CommentModel $owner
       * @package common\behaviors
       */
      class NotifyBehavior extends Behavior
      {
          
          public function events()
          {
              return [
                  ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate',
              ];
          }
          
          public function afterUpdate($event)
          {
              /**
               * @var Event        $event
               * @var CommentModel $owner
               */
              $owner = $this->owner;
              if($owner->status == $owner::STATUS_ACTIVE) {
                  $entity = $owner->entity;
                  $model = $entity::findOne($owner->entity_id);
                  if($model != NULL) {
                      if(!empty( $owner->user )) {
                          $customer = $owner->user;
8d864e8e   Yarik   Added comments to...
42
43
44
45
46
                          if(preg_match('/\S+@\S+\.\S+/', $customer->username)) {
                              $email = $customer->username;
                          } else {
                              return false;
                          }
8f36efb7   Yarik   Added comments to...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
                      }
                      $url = '';
                      if($model::className() == Product::className()) {
                          $url = Url::to([
                              'catalog/product',
                              'product' => $model,
                              '#'       => 'artbox-comment',
                          ], true);
                      } elseif($model::className() == Articles::className()) {
                          $url = Url::to([
                              'articles/show',
                              'translit' => $model->translit,
                              'id'       => $model->id,
                              '#'        => 'artbox-comment',
                          ], true);
                      }
                      $mailer = Mailer::widget([
                          'type'    => 'comment_notify',
                          'params'  => [
                              'model' => $model,
                              'url'   => $url,
                          ],
                          'subject' => 'Ваш комментарий опубликован',
8d864e8e   Yarik   Added comments to...
70
                          'email'   => ( !empty( $customer ) ? $email : $owner->email ),
8f36efb7   Yarik   Added comments to...
71
72
73
74
75
76
77
                      ]);
                      return $mailer;
                  }
              }
              return false;
          }
      }