H
H
hollanditkzn2017-07-05 11:56:47
Yii
hollanditkzn, 2017-07-05 11:56:47

How to facilitate the code to display counters to display the number of active records?

I have such a routine, I have a repeat code in the templates to add the number of active posts on the pages. Example, there is a navigation where all pages are visible and there is a counter of active records

<?php echo Nav::widget([
    'options' => ['class' => 'nav nav-pills headerNav'],
    'items' => [
        ['label' => 'Заказы <span class="badge pull-right">'.$this->params['scoreZakaz'].'</span>', 'encode' => false, 'url' => ['zakaz/admin'], 'visible' => Yii::$app->user->can('seeAdmin')],
    ['label' => 'Заказы <span class="badge pull-right">'.$this->params['scoreDisain'].'</span>', 'encode' => false, 'url' => ['zakaz/disain'], 'visible' => Yii::$app->user->can('disain')],
    ['label' => 'Заказы <span class="badge pull-right">'.$this->params['scoreMaster'].'</span>', 'encode' => false, 'url' => ['zakaz/master'], 'visible' => Yii::$app->user->can('master')],
    ['label' => 'Заказы <span class="badge pull-right">'.$this->params['scoreZakaz'].'</span>', 'encode' => false, 'url' => ['zakaz/shop'], 'visible' => Yii::$app->user->can('seeShop')],
    ['label' => 'Доставки <span class="badge pull-right">'.$this->params['scoreShipping'].'</span>', 'encode' => false, 'url' => ['courier/index'], 'visible' => Yii::$app->user->can('courier')],
    ['label' => 'Задачи <span class="badge pull-right">'.$this->params['scoreTodoist'].'</span>', 'url' => ['todoist/index'], 'encode' => false, 'visible' => Yii::$app->user->can('admin')],
    ['label' => 'Поломки <span class="badge pull-right">'.$this->params['scoreHelp'].'</span>', 'encode' => false, 'url' => ['helpdesk/index'], 'visible' => !Yii::$app->user->can('courier')],
    ['label' => 'Закупки <span class="badge pull-right">'.$this->params['scoreCustom'].'</span>', 'encode' => false, 'url' => ['custom/adop'], 'visible' => Yii::$app->user->can('seeAdop')],
    ['label' => 'Доставки <span class="badge pull-right">'.$this->params['scoreShipping'].'</span>', 'encode' => false, 'url' => ['courier/shipping'], 'visible' => Yii::$app->user->can('admin')],
    ['label' => 'Закупки', 'url' => ['custom/index'], 'visible' => Yii::$app->user->can('zakup')],
    ['label' => 'Задачи <span class="badge pull-right">'.$this->params['scoreTodoist'].'</span>', 'encode' => false,'url' => ['todoist/shop'], 'visible' => Yii::$app->user->can('todoist')],
    ],
]); ?>
<?php endif ?>

And I have to write such a construction in the controller for each action
public function actionMaster()
    {
        $searchModel = new ZakazSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, 'master');
        $notification = $this->findNotification();
        $scoreZakaz = $this->findScorezakaz('master');//количество заказов для роли мастер
        $scoreTodoist = $this->findScoretodoist('adop');//количество задач
        $scoreHelp = $this->findScorehelp();//количество запросов

        return $this->render('master', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'scoreZakaz' => $scoreZakaz,
            'scoreTodoist' => $scoreTodoist,
            'scoreHelp' => $scoreHelp,
            'notification' => $notification,
        ]);
    }
/**
     * Finds the Zakaz model based on its role value
     * @param $role
     * @return int|string
     */
    protected function findScorezakaz($role)
    {
        $score = Zakaz::find();
//Пришел какая роль, то выполняется одно из условии и активных заказов в базе данных у данной роли
        switch ($role) {
            case 'admin':
                return $this->view->params['scoreZakaz'] = $score->andWhere(['action' => 1])->count();
                break;
            case 'shop':
                return $this->view->params['scoreZakaz'] = $score->andWhere(['id_sotrud' => Yii::$app->user->id, 'action' => 1])->count();
                break;
            case 'disain':
                return $this->view->params['scoreDisain'] = $score->andWhere(['status' => [Zakaz::STATUS_DISAIN, Zakaz::STATUS_SUC_DISAIN, Zakaz::STATUS_DECLINED_DISAIN], 'action' => 1])->count();
                break;
            case 'master':
                return $this->view->params['scoreMaster'] = $score->andWhere(['status' => [Zakaz::STATUS_MASTER, Zakaz::STATUS_SUC_MASTER, Zakaz::STATUS_DECLINED_MASTER], 'action' => 1])->count();
                break;
        }
    }

    /**
     * Finds the Todoist model based on its role value
     * @param $role
     * @return int|string
     */
    protected function findScoretodoist($role)
    {
        $score = Todoist::find();
//если админ, то количество задач пишут все активные, если пришло что-то другое то высвечиваются все активные задачи для определенного пользователя
        switch ($role) {
            case 'admin':
                return $this->view->params['scoreTodoist'] = $score->andWhere(['activate' => 0])->count();
                break;
            case 'adop':
                return $this->view->params['scoreTodoist'] = $score->andWhere(['id_user' => Yii::$app->user->id, 'activate' => 0])->count();
                break;
        }
    }

    /**
     * Finds the Helpdesk model
     */
    protected function findScorehelp()
    {
        $score = Helpdesk::find();
        $this->view->params['scoreHelp'] = $score->andWhere(['id_user' => Yii::$app->user->id, 'status' => 0])->count();
    }

    /**
     * Finds the Custom model
     */
    protected function findScorecustom()
    {
        $score = Custom::find();
        $this->view->params['scoreCustom'] = $score->andWhere(['id_user' => Yii::$app->user->id, 'action' => 0])->count();
    }

    /**
     * Finds the Courier model
     */
    protected function findScoreshipping()
    {
        $score = Courier::find();
        $this->view->params['scoreShipping'] = $score->andWhere(['<','status', Courier::DELIVERED])->count();
    }

How can I make this code easier, I just thought it might be an event, but since I can say I have never used them, how could I improve them?
That is, this code is repeated
$scoreZakaz = $this->findScorezakaz('master');
        $scoreTodoist = $this->findScoretodoist('adop');
        $scoreHelp = $this->findScorehelp();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-07-05
@hollanditkzn

It's probably worth moving this into a component, providing for caching in the component. Display using a widget that will receive data from the component.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question