3 namespace app\controllers;
6 use app\models\Regions;
7 use app\models\RegionsSearch;
8 use yii\web\Controller;
9 use yii\web\NotFoundHttpException;
10 use yii\filters\VerbFilter;
14 * RegionsController implements the CRUD actions for Regions model.
16 class RegionsController extends Controller
21 public function behaviors()
25 'class' => VerbFilter::className(),
27 'delete' => [ 'POST' ],
34 * Добавление региона/подразделения
36 * @param array $options
37 string 'name' - наименование
38 * @return integer|boolean - Идентификатор записи или FALSE, если записать не удалось
40 public function addIfNeed($options)
42 if (is_array($options) && isset($options[ 'name' ]))
45 $region = Regions::find()
46 ->where(['like', 'name', $options[ 'name' ]])
48 if (count($region) > 0)
50 return $region[0]->id; // Нашёлся, вернём первый найденный
52 $region = new Regions(); // Не нашёлся, добавляем новый
53 $region->name = $options[ 'name' ];
54 if ($region->validate() && $region->save()) // Пробуем записать
56 return $region->id; // Если удалось записать, возвращаем идентификатор
59 return FALSE; // Если не удалась запись, вернём FALSE
63 * Список всех регионов/подразделений.
66 public function actionIndex()
68 if (! User::canPermission('createRecord'))
70 return $this->redirect(['site/index']);
72 $searchModel = new RegionsSearch();
73 $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
75 return $this->render('index', [
76 'searchModel' => $searchModel,
77 'dataProvider' => $dataProvider,
82 * Плказ одного региона/подразделения (не используется).
85 * @throws NotFoundHttpException если отсутствует регион/подразделение
87 public function actionView($id)
89 if (! User::canPermission('updateRecord'))
91 return $this->redirect(['site/index']);
93 return $this->render('view', [
94 'model' => $this->findModel($id),
99 * Создание нового региона/подразделения.
100 * В случае успешного создания региона/подразделения, происходит переход к списку всех регионов/подразделений.
103 public function actionCreate()
105 if (! User::canPermission('createRecord'))
107 return $this->redirect(['site/index']);
109 $model = new Regions();
111 if ($model->load(Yii::$app->request->post()) && $model->save())
113 return $this->redirect([ 'index', 'id' => $model->id ]);
116 return $this->render('create', [
122 * Изменение существующего региона/подразделения.
123 * В случае успешного редактирования, происходит переход к списку всех ергионов/подразделений.
126 * @throws NotFoundHttpException если отсутствует регион/подразделение
128 public function actionUpdate($id)
130 if (! User::canPermission('updateRecord'))
132 return $this->redirect(['site/index']);
134 $model = $this->findModel($id);
136 if ($model->load(Yii::$app->request->post()) && $model->save())
138 return $this->redirect([ 'index', 'id' => $model->id ]);
141 return $this->render('update', [
147 * Удаление существующего региона/подразделения.
148 * В случае успешного удаления, происходит переход к списку всех регоинов/подразделений.
151 * @throws NotFoundHttpException если отсутсвует регион/подразделение
153 public function actionDelete($id)
155 if (! User::canPermission('updateRecord'))
157 return $this->redirect(['site/index']);
159 $this->findModel($id)->delete();
161 return $this->redirect([ 'index' ]);
165 * Finds the Regions model based on its primary key value.
166 * If the model is not found, a 404 HTTP exception will be thrown.
168 * @return Regions the loaded model
169 * @throws NotFoundHttpException if the model cannot be found
171 protected function findModel($id)
173 if (($model = Regions::findOne($id)) !== null)
178 throw new NotFoundHttpException(Yii::t('regions', 'The requested page does not exist.'));