diff --git a/components/ActiveQuery.php b/components/ActiveQuery.php index 5ac869d..063c80a 100644 --- a/components/ActiveQuery.php +++ b/components/ActiveQuery.php @@ -3,35 +3,56 @@ class ActiveQuery extends \yii\db\ActiveQuery { + /** + * Apply condition by status + * @param $status + * @return $this + */ public function status($status) { $this->andWhere(['status' => (int)$status]); return $this; } + /** + * Order by primary key DESC + * @return $this + */ public function desc() { $model = $this->modelClass; - $this->orderBy($model::primaryKey()[0].' DESC'); + $this->orderBy([$model::primaryKey()[0] => SORT_DESC]); return $this; } + /** + * Order by primary key ASC + * @return $this + */ public function asc() { $model = $this->modelClass; - $this->orderBy($model::primaryKey()[0].' ASC'); + $this->orderBy([$model::primaryKey()[0] => SORT_ASC]); return $this; } + /** + * Order by order_num + * @return $this + */ public function sort() { - $this->orderBy('order_num DESC'); + $this->orderBy(['order_num' => SORT_DESC]); return $this; } + /** + * Order by date + * @return $this + */ public function sortDate() { - $this->orderBy('time DESC'); + $this->orderBy(['time' => SORT_DESC]); return $this; } } \ No newline at end of file diff --git a/components/ActiveRecord.php b/components/ActiveRecord.php index 76d9e40..073317e 100644 --- a/components/ActiveRecord.php +++ b/components/ActiveRecord.php @@ -10,6 +10,10 @@ public static function find() return new ActiveQuery(get_called_class()); } + /** + * Formats all model errors into a single string + * @return string + */ public function formatErrors() { $result = ''; diff --git a/components/CategoryModel.php b/components/CategoryModel.php index 457a141..bd24d58 100644 --- a/components/CategoryModel.php +++ b/components/CategoryModel.php @@ -83,6 +83,10 @@ public static function find() return new ActiveQueryNS(get_called_class()); } + /** + * Get cached tree structure of category objects + * @return array + */ public static function tree() { return Data::cache(static::tableName().'_tree', 3600, function(){ @@ -90,6 +94,10 @@ public static function tree() }); } + /** + * Get cached flat array of category objects + * @return array + */ public static function cats() { return Data::cache(static::tableName().'_flat', 3600, function(){ @@ -97,6 +105,10 @@ public static function cats() }); } + /** + * Generates tree from categories + * @return array + */ public static function generateTree() { $collection = self::find()->with('seo')->sort()->asArray()->all(); @@ -141,6 +153,10 @@ public static function generateTree() return $trees; } + /** + * Generates flat array of categories + * @return array + */ public static function generateFlat() { $collection = self::find()->with('seo')->sort()->asArray()->all(); diff --git a/components/Controller.php b/components/Controller.php index dd389b1..102c049 100644 --- a/components/Controller.php +++ b/components/Controller.php @@ -12,6 +12,13 @@ class Controller extends \yii\web\Controller public $rootActions = []; public $error = null; + /** + * Check authentication, and root rights for actions + * @param \yii\base\Action $action + * @return bool + * @throws \yii\web\BadRequestHttpException + * @throws \yii\web\ForbiddenHttpException + */ public function beforeAction($action) { if(!parent::beforeAction($action)) @@ -34,6 +41,11 @@ public function beforeAction($action) } } + /** + * Write in sessions alert messages + * @param string $type error or success + * @param string $message alert body + */ public function flash($type, $message) { Yii::$app->getSession()->setFlash($type=='error'?'danger':$type, $message); @@ -44,16 +56,31 @@ public function back() return $this->redirect(Yii::$app->request->referrer); } + /** + * Set return url for module in sessions + * @param mixed $url if not set, returnUrl will be current page + */ public function setReturnUrl($url = null) { Yii::$app->getSession()->set($this->module->id.'_return', $url ? Url::to($url) : Url::current()); } + /** + * Get return url for module from session + * @param mixed $defaultUrl if return url not found in sessions + * @return mixed + */ public function getReturnUrl($defaultUrl = null) { return Yii::$app->getSession()->get($this->module->id.'_return', $defaultUrl ? Url::to($defaultUrl) : Url::to('/admin/'.$this->module->id)); } + /** + * Formats response depending on request type (ajax or not) + * @param string $success + * @param bool $back go back or refresh + * @return mixed $result array if request is ajax. + */ public function formatResponse($success = '', $back = true) { if(Yii::$app->request->isAjax){