Exemplo n.º 1
0
 /**
  * Создание источника
  * @return string
  */
 public function actionCreate()
 {
     $model = new Source();
     $uploadImg = new UploadForm();
     if (Yii::$app->request->isPost) {
         $uploadImg->img = UploadedFile::getInstance($uploadImg, 'img');
         if ($uploadImg->img && $uploadImg->validate()) {
             $uploadImg->img->saveAs('uploads/covers/' . Yii::$app->translater->translit($uploadImg->img->baseName) . '.' . $uploadImg->img->extension);
         } else {
             print_r($uploadImg->getErrors());
         }
     }
     if ($model->load(Yii::$app->request->post())) {
         $model->title = Yii::$app->request->post('Source')['title'];
         $model->author_id = Yii::$app->request->post('Source')['author_id'];
         $model->status = Yii::$app->request->post('Source')['status'];
         $model->cat_id = Yii::$app->request->post('Source')['cat_id'];
         if (isset($uploadImg->img)) {
             $model->cover = Url::base() . 'uploads/covers/' . Yii::$app->translater->translit($uploadImg->img->baseName) . '.' . $uploadImg->img->extension;
         }
         $model->save(false);
         $sources = Source::find();
         $dataProvider = new ActiveDataProvider(['query' => $sources]);
         return $this->redirect(Url::toRoute('source/index'));
     } else {
         return $this->render('_form', ['model' => $model, 'uploadImg' => $uploadImg]);
     }
 }
Exemplo n.º 2
0
 public function actionCreateAjax()
 {
     $model = new Source();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         echo Json::encode(Source::find()->asArray()->all());
     } else {
         echo "0";
     }
 }
Exemplo n.º 3
0
 /**
  * Creates a new Source model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Source();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }