Skip to content

Commit

Permalink
added some code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
noumo committed May 21, 2015
1 parent a688f03 commit 9936ff6
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
29 changes: 25 additions & 4 deletions components/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 4 additions & 0 deletions components/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down
16 changes: 16 additions & 0 deletions components/CategoryModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,32 @@ 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(){
return self::generateTree();
});
}

/**
* Get cached flat array of category objects
* @return array
*/
public static function cats()
{
return Data::cache(static::tableName().'_flat', 3600, function(){
return self::generateFlat();
});
}

/**
* Generates tree from categories
* @return array
*/
public static function generateTree()
{
$collection = self::find()->with('seo')->sort()->asArray()->all();
Expand Down Expand Up @@ -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();
Expand Down
27 changes: 27 additions & 0 deletions components/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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);
Expand All @@ -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){
Expand Down

0 comments on commit 9936ff6

Please sign in to comment.