Blame view

Module.php 4.33 KB
a2cde075   Yarik   first commit
1
2
  <?php
      
faff2c48   Yarik   Artbox comment cr...
3
      namespace artbox\webcomment;
a2cde075   Yarik   first commit
4
      
faff2c48   Yarik   Artbox comment cr...
5
6
      use artbox\webcomment\models\CommentModel;
      use artbox\webcomment\models\RatingModel;
a2cde075   Yarik   first commit
7
      use Yii;
faff2c48   Yarik   Artbox comment cr...
8
      use yii\base\InvalidConfigException;
a2cde075   Yarik   first commit
9
      use yii\console\Application;
faff2c48   Yarik   Artbox comment cr...
10
      
a2cde075   Yarik   first commit
11
12
      /**
       * Class Module
faff2c48   Yarik   Artbox comment cr...
13
       *
a2cde075   Yarik   first commit
14
15
16
17
18
       * @package artweb\artbox\comment
       */
      class Module extends \yii\base\Module
      {
          
a2cde075   Yarik   first commit
19
20
21
22
23
24
          /**
           * @var string module name
           */
          public static $name = 'artbox-comment';
          
          /**
faff2c48   Yarik   Artbox comment cr...
25
26
           * User identity class, default to artbox\order\models\Customer
           *
a2cde075   Yarik   first commit
27
28
           * @var string|null
           */
faff2c48   Yarik   Artbox comment cr...
29
          public $userIdentityClass = null;
a2cde075   Yarik   first commit
30
31
          
          /**
faff2c48   Yarik   Artbox comment cr...
32
33
           * Comment model class, default to artbox\webcomment\modules\models\CommentModel
           *
a2cde075   Yarik   first commit
34
35
           * @var string comment model class
           */
faff2c48   Yarik   Artbox comment cr...
36
          public $commentModelClass = null;
b409d909   Yarik   Rating caching
37
          
faff2c48   Yarik   Artbox comment cr...
38
          public $ratingModelClass = null;
a2cde075   Yarik   first commit
39
40
41
42
          
          /**
           * This namespace will be used to load controller classes by prepending it to the controller
           * class name.
faff2c48   Yarik   Artbox comment cr...
43
           *
a2cde075   Yarik   first commit
44
45
           * @var string the namespace that controller classes are in.
           */
faff2c48   Yarik   Artbox comment cr...
46
          public $controllerNamespace = 'artbox\webcomment\controllers';
a2cde075   Yarik   first commit
47
48
49
50
          
          /**
           * @var \yii\db\Connection DB connection, default to \Yii::$app->db
           */
faff2c48   Yarik   Artbox comment cr...
51
          public $db = null;
a2cde075   Yarik   first commit
52
53
54
          
          /**
           * Key, used to encrypt and decrypt comment service data.
faff2c48   Yarik   Artbox comment cr...
55
           *
a2cde075   Yarik   first commit
56
57
58
59
60
61
           * @var string Encryption key
           */
          public static $encryptionKey = 'artbox-comment';
          
          /**
           * Whether to enable comment rating or not.
faff2c48   Yarik   Artbox comment cr...
62
           *
a2cde075   Yarik   first commit
63
64
           * @var bool
           */
fd5e93c2   Yarik   Artbox comment pr...
65
          public $enableRating = true;
b409d909   Yarik   Rating caching
66
          
fd5e93c2   Yarik   Artbox comment pr...
67
68
69
70
71
72
          /**
           * Whether to enable comment premoderate or not.
           *
           * @var bool
           */
          public $enablePremoderate = true;
a2cde075   Yarik   first commit
73
74
          
          /**
b409d909   Yarik   Rating caching
75
76
77
78
79
           * @var string[] $cacheModels
           */
          public $cacheModels = [];
          
          /**
a2cde075   Yarik   first commit
80
81
82
83
84
85
86
87
           * Initializes the module.
           * This method is called after the module is created and initialized with property values
           * given in configuration. The default implementation will initialize
           * [[controllerNamespace]] if it is not set. If you override this method, please make sure
           * you call the parent implementation.
           */
          public function init()
          {
faff2c48   Yarik   Artbox comment cr...
88
              if ($this->commentModelClass === null) {
a2cde075   Yarik   first commit
89
90
                  $this->commentModelClass = CommentModel::className();
              }
fd5e93c2   Yarik   Artbox comment pr...
91
              if ($this->enableRating && $this->ratingModelClass === null) {
a2cde075   Yarik   first commit
92
93
                  $this->ratingModelClass = RatingModel::className();
              }
faff2c48   Yarik   Artbox comment cr...
94
95
96
              if (\Yii::$app instanceof Application) {
                  $this->controllerNamespace = 'artbox\webcomment\commands';
              } elseif ($this->userIdentityClass === null) {
395a7b37   Yarik   Module fix
97
                  $this->userIdentityClass = Yii::$app->getUser()->identityClass;
a2cde075   Yarik   first commit
98
              }
faff2c48   Yarik   Artbox comment cr...
99
              if ($this->db === null) {
a2cde075   Yarik   first commit
100
101
                  $this->db = \Yii::$app->db;
              }
faff2c48   Yarik   Artbox comment cr...
102
103
104
              if (!Yii::getAlias('@artbox/webcomment', false)) {
                  Yii::setAlias('@artbox/webcomment', __DIR__);
              }
a2cde075   Yarik   first commit
105
106
              parent::init();
          }
b409d909   Yarik   Rating caching
107
          
faff2c48   Yarik   Artbox comment cr...
108
109
110
111
112
113
114
115
116
          /**
           * Prompt to register current module
           *
           * @throws \yii\base\InvalidConfigException
           */
          public static function registerMe()
          {
              throw new InvalidConfigException(\Yii::t('artbox-comment', 'Register artbox-comment module'));
          }
a2cde075   Yarik   first commit
117
          
b409d909   Yarik   Rating caching
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
          /**
           * Rating model factory
           *
           * @param $config
           *
           * @return \yii\db\ActiveRecord
           */
          public function createRating($config)
          {
              /**
               * @var \yii\db\ActiveRecord $model
               */
              $model = \Yii::createObject(
                  array_merge(
                      [
                          'class' => $this->ratingModelClass,
                      ],
                      $config
                  )
              );
              $behaviors = [];
              foreach ($this->cacheModels as $cacheModel) {
                  $behaviors[] = \Yii::createObject($cacheModel);
              }
              if (!empty($behaviors)) {
                  $model->attachBehaviors($behaviors);
              }
              return $model;
          }
a2cde075   Yarik   first commit
147
      }