X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=controllers%2FTypesController.php;h=c5e73076e94fe40ee76663fed3f913d5e61a0526;hb=fdecb86935b63355c9d7258dd25aa5fa63f2e6b6;hp=b2af417de77680c5cc1f4fed53543267849b2d6c;hpb=9788de720837b34d5f8098fc87422a1ad824d412;p=invent%2Finvent.git diff --git a/controllers/TypesController.php b/controllers/TypesController.php index b2af417..c5e7307 100644 --- a/controllers/TypesController.php +++ b/controllers/TypesController.php @@ -8,6 +8,7 @@ use app\models\TypesSearch; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; +use app\models\User; /** * TypesController implements the CRUD actions for Types model. @@ -21,25 +22,85 @@ class TypesController extends Controller { return [ 'verbs' => [ - 'class' => VerbFilter::className(), + 'class' => VerbFilter::className(), 'actions' => [ - 'delete' => ['POST'], + 'delete' => [ 'POST' ], ], ], ]; } /** + * Добавление типа, если необходимо + * @param array $options + * string 'name' - наименование + * @return integer|boolean - Идентификатор типа или FALSE в случае неудачи + */ + public function addIfNeed($options) + { + $result = [ + 'id' => FALSE, + 'error' => Yii::t('types', 'Types: key field "type" missing: ') . print_r($options, TRUE), + ]; + if (is_array($options) && isset($options[ 'type' ])) + { + $model = Types::find() + ->where([ 'like', 'name', $options[ 'type' ] ]) + ->all(); + if (count($model) > 0) + { + $result['id'] = $model[0]->id; + $result['error'] = ''; + } + else + { + $model = new Types(); + $model->name = $options[ 'type' ]; + if ($model->validate() && $model->save()) + { + $result['id'] = $model->id; + $result['error'] = ''; + } + else + { + $result['error'] = Yii::t('types', 'Failed to add entry "{type}": ', $options) . print_r($model->errors['name'], TRUE); + } + } + } + return $result; + } + + /** * Список всех типов предметов/оборудования. * @return mixed */ public function actionIndex() { - $searchModel = new TypesSearch(); + if (! User::canPermission('createRecord')) + { + return $this->redirect(['site/index']); + } + $searchModel = new TypesSearch(); + if (isset(Yii::$app->request->queryParams['id'])) { + $id = Yii::$app->request->queryParams['id']; + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + //$dataProvider->query->select(Types::tableName() . '.id'); + $pageSize = $dataProvider->pagination->pageSize; + $dataProvider->pagination = FALSE; + $rows = $dataProvider->getModels(); + $page = 0; + foreach ($rows as $key => $val) { + if ($id == $val->id) { + $page = ceil(($key + 1) / $pageSize); + break; + } + } + return $this->redirect(['index', 'page' => $page]); + } $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ - 'searchModel' => $searchModel, + 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } @@ -52,6 +113,10 @@ class TypesController extends Controller */ public function actionView($id) { + if (! User::canPermission('updateRecord')) + { + return $this->redirect(['index']); + } return $this->render('view', [ 'model' => $this->findModel($id), ]); @@ -64,10 +129,15 @@ class TypesController extends Controller */ public function actionCreate() { + if (! User::canPermission('createRecord')) + { + return $this->redirect(['site/index']); + } $model = new Types(); - if ($model->load(Yii::$app->request->post()) && $model->save()) { - return $this->redirect(['index', 'id' => $model->id]); + if ($model->load(Yii::$app->request->post()) && $model->save()) + { + return $this->redirect([ 'index', 'id' => $model->id ]); } return $this->render('create', [ @@ -84,10 +154,15 @@ class TypesController extends Controller */ public function actionUpdate($id) { + if (! User::canPermission('updateRecord')) + { + return $this->redirect(['site/index']); + } $model = $this->findModel($id); - if ($model->load(Yii::$app->request->post()) && $model->save()) { - return $this->redirect(['index', 'id' => $model->id]); + if ($model->load(Yii::$app->request->post()) && $model->save()) + { + return $this->redirect([ 'index', 'id' => $model->id ]); } return $this->render('update', [ @@ -104,9 +179,13 @@ class TypesController extends Controller */ public function actionDelete($id) { + if (! User::canPermission('updateRecord')) + { + return $this->redirect(['site/index']); + } $this->findModel($id)->delete(); - return $this->redirect(['index']); + return $this->redirect([ 'index' ]); } /** @@ -118,7 +197,8 @@ class TypesController extends Controller */ protected function findModel($id) { - if (($model = Types::findOne($id)) !== null) { + if (($model = Types::findOne($id)) !== null) + { return $model; }