Module.php
4.33 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
147
<?php
namespace artbox\webcomment;
use artbox\webcomment\models\CommentModel;
use artbox\webcomment\models\RatingModel;
use Yii;
use yii\base\InvalidConfigException;
use yii\console\Application;
/**
* Class Module
*
* @package artweb\artbox\comment
*/
class Module extends \yii\base\Module
{
/**
* @var string module name
*/
public static $name = 'artbox-comment';
/**
* User identity class, default to artbox\order\models\Customer
*
* @var string|null
*/
public $userIdentityClass = null;
/**
* Comment model class, default to artbox\webcomment\modules\models\CommentModel
*
* @var string comment model class
*/
public $commentModelClass = null;
public $ratingModelClass = null;
/**
* This namespace will be used to load controller classes by prepending it to the controller
* class name.
*
* @var string the namespace that controller classes are in.
*/
public $controllerNamespace = 'artbox\webcomment\controllers';
/**
* @var \yii\db\Connection DB connection, default to \Yii::$app->db
*/
public $db = null;
/**
* Key, used to encrypt and decrypt comment service data.
*
* @var string Encryption key
*/
public static $encryptionKey = 'artbox-comment';
/**
* Whether to enable comment rating or not.
*
* @var bool
*/
public $enableRating = true;
/**
* Whether to enable comment premoderate or not.
*
* @var bool
*/
public $enablePremoderate = true;
/**
* @var string[] $cacheModels
*/
public $cacheModels = [];
/**
* 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()
{
if ($this->commentModelClass === null) {
$this->commentModelClass = CommentModel::className();
}
if ($this->enableRating && $this->ratingModelClass === null) {
$this->ratingModelClass = RatingModel::className();
}
if (\Yii::$app instanceof Application) {
$this->controllerNamespace = 'artbox\webcomment\commands';
} elseif ($this->userIdentityClass === null) {
$this->userIdentityClass = Yii::$app->getUser()->identityClass;
}
if ($this->db === null) {
$this->db = \Yii::$app->db;
}
if (!Yii::getAlias('@artbox/webcomment', false)) {
Yii::setAlias('@artbox/webcomment', __DIR__);
}
parent::init();
}
/**
* Prompt to register current module
*
* @throws \yii\base\InvalidConfigException
*/
public static function registerMe()
{
throw new InvalidConfigException(\Yii::t('artbox-comment', 'Register artbox-comment module'));
}
/**
* 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;
}
}