/** * Создание контента * @return string */ public function actionCreate() { $model = new Country(); $uploadImg = new UploadForm(); if (Yii::$app->request->isPost) { $uploadImg->img = UploadedFile::getInstance($uploadImg, 'img'); if ($uploadImg->img && $uploadImg->validate()) { $uploadImg->img->saveAs('uploads/icoflags/' . Yii::$app->translater->translit($uploadImg->img->baseName) . '.' . $uploadImg->img->extension); } else { print_r($uploadImg->getErrors()); } } if ($model->load(Yii::$app->request->post())) { $model->name = Yii::$app->request->post('Country')['name']; $model->iso_code = Yii::$app->request->post('Country')['iso_code']; $model->soc_abrev = Yii::$app->request->post('Country')['soc_abrev']; $model->soccer_code = Yii::$app->request->post('Country')['soccer_code']; if (isset($uploadImg->img)) { $model->icon = Url::base() . 'uploads/icoflags/' . Yii::$app->translater->translit($uploadImg->img->baseName) . '.' . $uploadImg->img->extension; } $model->save(false); return $this->redirect(Url::toRoute('countries/index')); } else { return $this->render('_form', ['model' => $model, 'uploadImg' => $uploadImg]); } }
/** * Creates a new Country model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Country(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->country_id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Updates an existing Country model. * If update is successful, the browser will be redirected to the 'update' page. * @param integer $id * @return mixed */ public function actionUpdate($id = null) { if ($id) { $model = $this->findModel($id); } else { $model = new Country(); } if ($model->load(Yii::$app->request->post()) && $model->save()) { Yii::$app->session->setFlash('success', __('Your changes have been saved successfully.')); return $this->redirect(['index']); } else { return $this->renderAjax('update', ['model' => $model]); } }
public static function getAll() { if (empty(self::$list)) { if ($file = file_get_contents(Yii::$app->basePath . '/web/data/countries.json')) { $list = Json::decode($file); if (!empty($list['countries'])) { foreach ($list['countries'] as $data) { $country = new Country(); $country->load(['Country' => $data]); self::$list[$data['code']] = $country; } } } } return self::$list; }
/** * Creates a new Country model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Country(); $searchModel = new CountrySearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); if ($model->load(Yii::$app->request->post())) { if (Yii::$app->request->isAjax) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ActiveForm::validate($model); } $model->attributes = $_POST['Country']; $model->created_by = Yii::$app->getid->getId(); $model->created_at = new \yii\db\Expression('NOW()'); if ($model->save()) { return $this->redirect(['index']); } else { return $this->render('index', ['model' => $model, 'searchModel' => $searchModel, 'dataProvider' => $dataProvider]); } } else { return $this->render('index', ['model' => $model, 'searchModel' => $searchModel, 'dataProvider' => $dataProvider]); } }