Blame view

common/behaviors/NotifyBehavior.php 2.57 KB
d8c1a2e0   Yarik   Big commit artbox
1
2
3
4
  <?php
      
      namespace common\behaviors;
      
f202ab2b   Yarik   Article table ref...
5
      use common\models\Article;
d8c1a2e0   Yarik   Big commit artbox
6
7
8
9
10
11
      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;
d8c1a2e0   Yarik   Big commit artbox
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
      
      /**
       * 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;
5c2eb7c8   Yarik   Big commit almost...
37
38
39
                  /**
                   * @var ActiveRecord $model
                   */
d8c1a2e0   Yarik   Big commit artbox
40
41
                  $model = $entity::findOne($owner->entity_id);
                  if($model != NULL) {
5c2eb7c8   Yarik   Big commit almost...
42
                      $email = '';
d8c1a2e0   Yarik   Big commit artbox
43
44
45
46
47
48
49
50
51
                      if(!empty( $owner->user )) {
                          $customer = $owner->user;
                          if(preg_match('/\S+@\S+\.\S+/', $customer->username)) {
                              $email = $customer->username;
                          } else {
                              return false;
                          }
                      }
                      $url = \Yii::$app->urlManager->getHostInfo();
5c2eb7c8   Yarik   Big commit almost...
52
53
54
                      /**
                       * @todo Change that statements
                       */
d8c1a2e0   Yarik   Big commit artbox
55
                      if($model::className() == Product::className()) {
f202ab2b   Yarik   Article table ref...
56
57
58
                          $url .= '/product/' . $model->alias . '#artbox-comment';
                      } elseif($model::className() == Article::className()) {
                          $url .= '/blog/' . $model->translit . '#artbox-comment';
d8c1a2e0   Yarik   Big commit artbox
59
60
61
62
                      }
                      $mailer = Mailer::widget([
                          'type'    => 'comment_notify',
                          'params'  => [
f202ab2b   Yarik   Article table ref...
63
64
                              'model'   => $model,
                              'url'     => $url,
d8c1a2e0   Yarik   Big commit artbox
65
66
67
68
69
70
71
72
73
74
75
                              'comment' => $owner,
                          ],
                          'subject' => 'Ваш комментарий опубликован',
                          'email'   => ( !empty( $customer ) ? $email : $owner->email ),
                      ]);
                      return $mailer;
                  }
              }
              return false;
          }
      }