Module.php 3.41 KB
<?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;
        
        /**
         * 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'));
        }
        
    }