/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Users::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $sort = $dataProvider->getSort(); $sort->attributes['officeName'] = ['asc' => ['questionlist_office.name' => SORT_ASC], 'desc' => ['questionlist_office.name' => SORT_DESC], 'label' => 'Имя офиса']; $dataProvider->setSort($sort); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); $query->joinWith(['questionlist_office']); return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); if ($this->roleName) { $this->profile_office_role = $this->roleName; } $query->andFilterWhere(['like', 'profile_id', $this->profile_id])->andFilterWhere(['like', 'profile_office_role', $this->profile_office_role]); if ($this->scenario == 'managerSearch') { $userRoles = Users::findAll(['profile_id' => Yii::$app->user->identity->username, 'profile_office_role' => 'commercial_director']); $userRegions = array_values(ArrayHelper::map($userRoles, 'region_id', 'region_id')); $query->andFilterWhere(['like', 'profile_office_role', 'manager']); $query->andFilterWhere(['in', 'questionlist_users_offices.region_id', $userRegions]); } $query->joinWith(['office' => function ($q) { $q->andFilterWhere(['like', 'questionlist_office.name', $this->officeName]); }]); $query->joinWith(['region' => function ($q) { $q->andFilterWhere(['like', 'questionlist_region.name', $this->regionName]); }]); return $dataProvider; }
public function beforeAction($action) { $profileId = Yii::$app->user->identity->username; $userData = Users::findAll(['profile_id' => $profileId]); $roles = array_values(ArrayHelper::map($userData, 'id', 'profile_office_role')); //echo'<pre>';var_dump($action->controller->id);echo'</pre>';die; if (in_array('admin', $roles)) { $access = true; } else { switch ($action->controller->id) { //Назначать управляющих могут только роли коммерческий директор и админ case 'managers': $access = array_intersect(['commercial_director'], $roles); break; // Отвесать на опросные листы могут только менеджеры и коммерческий директор // Отвесать на опросные листы могут только менеджеры и коммерческий директор case 'write-test': $access = array_intersect(['manager', 'commercial_director'], $roles); break; //Назначать управляющих и коммерческих директоров и админов могут только роли админ //Назначать управляющих и коммерческих директоров и админов могут только роли админ case 'users-offices': $access = in_array('admin', $roles); break; // Назначать опросы отделениям могут коммерческие директора и админы // Назначать опросы отделениям могут коммерческие директора и админы case 'answer-list': $access = array_intersect(['commercial_director'], $roles); break; // Редактировать офисы и регионы может только админ // Редактировать офисы и регионы может только админ case 'office': case 'region': $access = in_array('admin', $roles); break; default: $access = true; } } return $access ? true : $this->denyAccess(); }
/** * Delete multiple existing Users model. * For ajax request will return json object * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionBulkDelete() { $request = Yii::$app->request; $pks = $request->post('pks'); // Array or selected records primary keys foreach (Users::findAll(json_decode($pks)) as $model) { $model->delete(); } if ($request->isAjax) { /* * Process for ajax request */ Yii::$app->response->format = Response::FORMAT_JSON; return ['forceClose' => true, 'forceReload' => '#crud-datatable-pjax']; } else { /* * Process for non-ajax request */ return $this->redirect(['index']); } }
/** * Delete multiple existing Users model. * For ajax request will return json object * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionBulkDelete() { $request = Yii::$app->request; $pks = explode(',', $request->post('pks')); // Array or selected records primary keys $userRoles = Users::findAll(['profile_id' => Yii::$app->user->identity->username, 'profile_office_role' => 'commercial_director']); $userRegions = array_values(ArrayHelper::map($userRoles, 'region_id', 'region_id')); foreach ($pks as $pk) { $model = $this->findModel($pk); /* Если удалить можно */ if (in_array($model->region_id, $userRegions)) { $model->delete(); } $model->delete(); } if ($request->isAjax) { /* * Process for ajax request */ Yii::$app->response->format = Response::FORMAT_JSON; return ['forceClose' => true, 'forceReload' => '#crud-datatable-pjax']; } else { /* * Process for non-ajax request */ return $this->redirect(['index']); } }
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; use yii\helpers\ArrayHelper; use app\modules\unicred\questionlist\models\Region; use app\modules\unicred\questionlist\models\Users; use app\modules\unicred\questionlist\models\Office; /* @var $this yii\web\View */ /* @var $model app\modules\unicred\questionlist\models\Users */ /* @var $form yii\widgets\ActiveForm */ $userRoles = Users::findAll(['profile_id' => Yii::$app->user->identity->username, 'profile_office_role' => 'commercial_director']); $userRegions = ArrayHelper::map($userRoles, 'region_id', 'region_id'); $offices = Office::find()->where(['region_id' => array_values($userRegions)])->orderBy('region_id')->all(); $offices = ArrayHelper::map($offices, 'id', 'name'); //$regions = ArrayHelper::map(Region::findAll(array_values($userRegions)), 'id', 'name'); ?> <div class="users-offices-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'profile_id')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, "office_id")->dropDownList($offices, [$model->office_id => ['selected' => 'selected'], 'prompt' => 'Выберите ..']); ?>
/** * @param $profile_id * @return array * Возвращает массив ID отделений, где пользователь является управляющим */ protected function getOffiсeIds($profile_id) { $modelsUsers = Users::findAll(['profile_id' => $profile_id, 'profile_office_role' => 'manager']); return ArrayHelper::map($modelsUsers, 'office_id', 'office_id'); }