use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
+use app\models\User;
/**
* LocationsController implements the CRUD actions for Locations model.
*/
public static function addIfNeed($options)
{
- if (is_array($options) && isset($options[ 'name' ]) && (isset($options[ 'region' ]) || isset($options[ 'region_id' ])))
+ $result = [
+ 'id' => FALSE,
+ 'error' => Yii::t('locations', 'Locations: Key field missing "location" and "region": ') . print_r($options, TRUE),
+ ];
+ if (is_array($options) && isset($options[ 'location' ]) && (isset($options[ 'region' ]) || isset($options[ 'region_id' ])))
{
if (isset($options[ 'region' ]))
{
- $region_id = RegionsController::addIfNeed([ 'name' => $options[ 'region' ]]);
+ $region = RegionsController::addIfNeed($options);
}
else
{
- $region_id = $options[ 'region_id' ];
+ $region[ 'id' ] = $options[ 'region_id' ];
}
- if ($region_id !== FALSE) {
+ if ($region[ 'id' ] !== FALSE) {
// Ищем расположение, совпадающее по наименованию и региону/подразделению
- $location = Locations::find()
- ->where([ 'like', 'name', $options[ 'name' ]])
- ->andWhere([ 'region_id' => $region_id ])
+ $model = Locations::find()
+ ->where([ 'like', 'name', $options[ 'location' ]])
+ ->andWhere([ 'region_id' => $region[ 'id' ]])
->all();
- if (count($location) > 0)
+ if (count($model) > 0)
{
- return $location[0]->id; // Если нашли, возвращаем идентификатор записи
+ // Если нашли, возвращаем идентификатор записи
+ $result[ 'id' ] = $model[0]->id;
+ $result[ 'error' ] = '';
}
- // Не нашли, пробуем добавить место расположения
- $location = new Locations();
- $location->name = $options[ 'name' ];
- $location->region_id = $region_id;
- if($location->validate() && $location->save())
+ else
{
- return $location->id; // Если удалось сохранить, вернём идентификатор места расположения
+ // Не нашли, пробуем добавить место расположения
+ $model = new Locations();
+ $model->name = $options[ 'location' ];
+ $model->region_id = $region[ 'id' ];
+ if($model->validate() && $model->save())
+ {
+ // Если удалось сохранить, вернём идентификатор места расположения
+ $result[ 'id' ] = $model->id;
+ $result[ 'error' ] = '';
+ }
+ else
+ {
+ $result[ 'error' ] = Yii::t('locations', 'Error to create location "{location}": ', $options) . print_r($model->errors, TRUE);
+ }
}
}
+ else
+ {
+ $result[ 'error' ] .= '<br />' . $region[ 'error' ];
+ }
}
- return FALSE; // Записать не удалось, вернём FALSE
+ return $result; // Записать не удалось, вернём FALSE
}
/**
*/
public function actionIndex()
{
+ if (! User::canPermission('createRecord'))
+ {
+ return $this->redirect(['site/index']);
+ }
+
$searchModel = new LocationsSearch();
+ if (isset(Yii::$app->request->queryParams['id'])) {
+ $id = Yii::$app->request->queryParams['id'];
+ $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
+ //$dataProvider->query->select([Locations::tableName() . '.id', Locations::tableName() . '.name']);
+ $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', [
*/
public function actionView($id)
{
+ if (! User::canPermission('updateRecord'))
+ {
+ return $this->redirect(['index']);
+ }
return $this->render('view', [
'model' => $this->findModel($id),
]);
*/
public function actionCreate()
{
+ if (! User::canPermission('createRecord'))
+ {
+ return $this->redirect(['site/index']);
+ }
$model = new Locations();
if ($model->load(Yii::$app->request->post()) && $model->save())
*/
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())
*/
public function actionDelete($id)
{
+ if (! User::canPermission('updateRecord'))
+ {
+ return $this->redirect(['index']);
+ }
$this->findModel($id)->delete();
return $this->redirect([ 'index' ]);