У меня вопрос по виджету, он получился слишком громоздким, можно его в 2 раза сократить или вообще никак? Там выходит код пишет количество активных записей, для каждой роли свои условия, допустим для роли shop в меню "Поломки" должны быть все активные задачи для определенного узера, а для system в меню "Поломки" должны высвечивается количество задач всех активных задач.
class Counter extends Widget
{
private $params;
public function init()
{
parent::init();
$zakaz = Zakaz::find();
$todoist = Todoist::find();
$custom = Custom::find();
$helpdesk = Helpdesk::find();
$shipping = Courier::find();
if (Yii::$app->user->can('admin')){
$this->view->params['scoreZakaz'] = $zakaz->andWhere(['action' => 1])->count();
$this->view->params['scoreTodoist'] = $todoist->andWhere(['activate' => 0])->count();
$this->view->params['scoreCustom'] = $custom->andWhere(['id_user' => Yii::$app->user->id, 'action' => 0])->count();
$this->view->params['scoreHelp'] = $helpdesk->andWhere(['id_user' => Yii::$app->user->id, 'status' => 0])->count();
$this->view->params['scoreShipping'] = $shipping->andWhere(['<','status', Courier::RECEIVE])->count();
} elseif (Yii::$app->user->can('shop')){
$this->view->params['scoreZakaz'] = $zakaz->andWhere(['id_sotrud' => Yii::$app->user->id, 'action' => 1])->count();
$this->view->params['scoreTodoist'] = $todoist->andWhere(['id_user' => Yii::$app->user->id, 'activate' => 0])->count();
$this->view->params['scoreCustom'] = $custom->andWhere(['id_user' => Yii::$app->user->id, 'action' => 0])->count();
$this->view->params['scoreHelp'] = $helpdesk->andWhere(['id_user' => Yii::$app->user->id, 'status' => 0])->count();
} elseif(Yii::$app->user->can('disain')){
$this->view->params['scoreDisain'] = $zakaz->andWhere(['status' => [Zakaz::STATUS_DISAIN, Zakaz::STATUS_SUC_DISAIN, Zakaz::STATUS_DECLINED_DISAIN], 'action' => 1])->count();
$this->view->params['scoreTodoist'] = $todoist->andWhere(['id_user' => Yii::$app->user->id, 'activate' => 0])->count();
$this->view->params['scoreCustom'] = $custom->andWhere(['id_user' => Yii::$app->user->id, 'action' => 0])->count();
$this->view->params['scoreHelp'] = $helpdesk->andWhere(['id_user' => Yii::$app->user->id, 'status' => 0])->count();
} elseif (Yii::$app->user->can('master')){
$this->view->params['scoreMaster'] = $zakaz->andWhere(['status' => [Zakaz::STATUS_MASTER, Zakaz::STATUS_SUC_MASTER, Zakaz::STATUS_DECLINED_MASTER], 'action' => 1])->count();
$this->view->params['scoreTodoist'] = $todoist->andWhere(['id_user' => Yii::$app->user->id, 'activate' => 0])->count();
$this->view->params['scoreCustom'] = $custom->andWhere(['id_user' => Yii::$app->user->id, 'action' => 0])->count();
$this->view->params['scoreHelp'] = $helpdesk->andWhere(['id_user' => Yii::$app->user->id, 'status' => 0])->count();
} elseif (Yii::$app->user->can('courier')){
$this->view->params['scoreShipping'] = $shipping->andWhere(['<','status', Courier::DELIVERED])->count();
} elseif (Yii::$app->user->can('zakyp')){
$this->view->params['scoreCustom'] = $custom->andWhere(['action' => 0])->count();
$this->view->params['scoreHelp'] = $helpdesk->andWhere(['id_user' => Yii::$app->user->id, 'status' => 0])->count();
$this->view->params['scoreTodoist'] = $todoist->andWhere(['id_user' => Yii::$app->user->id, 'activate' => 0])->count();
} elseif (Yii::$app->user->can('system')){
$this->view->params['scoreHelp'] = $helpdesk->andWhere(['status' => 0])->count();
}
}
/**
* @return string
*/
public function run()
{
return Nav::widget([
'options' => ['class' => 'nav nav-pills headerNav'],
'items' => [
['label' => 'Заказы <span class="badge pull-right">'.$this->view->params['scoreZakaz'].'</span>', 'encode' => false, 'url' => ['zakaz/admin'], 'visible' => Yii::$app->user->can('seeAdmin')],
['label' => 'Заказы <span class="badge pull-right">'.$this->view->params['scoreDisain'].'</span>', 'encode' => false, 'url' => ['zakaz/disain'], 'visible' => Yii::$app->user->can('disain')],
['label' => 'Заказы <span class="badge pull-right">'.$this->view->params['scoreMaster'].'</span>', 'encode' => false, 'url' => ['zakaz/master'], 'visible' => Yii::$app->user->can('master')],
['label' => 'Заказы <span class="badge pull-right">'.$this->view->params['scoreZakaz'].'</span>', 'encode' => false, 'url' => ['zakaz/shop'], 'visible' => Yii::$app->user->can('seeShop')],
['label' => 'Доставки <span class="badge pull-right">'.$this->view->params['scoreShipping'].'</span>', 'encode' => false, 'url' => ['courier/index'], 'visible' => Yii::$app->user->can('courier')],
['label' => 'Задачи <span class="badge pull-right">'.$this->view->params['scoreTodoist'].'</span>', 'url' => ['todoist/index'], 'encode' => false, 'visible' => Yii::$app->user->can('admin')],
['label' => 'Поломки <span class="badge pull-right">'.$this->view->params['scoreHelp'].'</span>', 'encode' => false, 'url' => ['helpdesk/index'], 'visible' => !Yii::$app->user->can('courier')],
['label' => 'Закупки <span class="badge pull-right">'.$this->view->params['scoreCustom'].'</span>', 'encode' => false, 'url' => ['custom/adop'], 'visible' => Yii::$app->user->can('seeAdop')],
['label' => 'Доставки <span class="badge pull-right">'.$this->view->params['scoreShipping'].'</span>', 'encode' => false, 'url' => ['courier/shipping'], 'visible' => Yii::$app->user->can('admin')],
['label' => 'Закупки <span class="badge pull-right">'.$this->view->params['scoreCustom'].'</span>', 'encode' => false,'url' => ['custom/index'], 'visible' => Yii::$app->user->can('zakup')],
['label' => 'Задачи <span class="badge pull-right">'.$this->view->params['scoreTodoist'].'</span>', 'encode' => false,'url' => ['todoist/shop'], 'visible' => Yii::$app->user->can('todoist')],
],
]);
}
И второй момент, как моно это все закешировать, потому что если я закеширую то соответственно она при изменение в базе данных количество будет одно и то же пр перезагрузки и будет виджется после того как снимется кеширование. Или можно сделать так что если что-то изменилось и количество отличается от обычной, то идет новая кэширование данных