/**
  * @return string
  * @throws ServerErrorHttpException
  * @throws \yii\base\InvalidConfigException
  * @throws \yii\web\BadRequestHttpException
  */
 public function run()
 {
     $params = Yii::$app->request->getBodyParams();
     $form = new ImageUploadForm(['basePath' => $this->basePath, 'baseUrl' => $this->baseUrl]);
     $form->setAttributes($params);
     $validate = Yii::$app->request->get('validate', false);
     $validateAttributeNames = null;
     if ($validate) {
         $parts = explode(',', $validate);
         $attributeNames = array_intersect($parts, $form->attributes());
         if (!empty($attributeNames[0])) {
             $validateAttributeNames = $attributeNames;
         }
     }
     $validateResult = $form->validate($validateAttributeNames);
     if ($validate || !$validate && !$validateResult) {
         return $form;
     }
     if (!$form->save() && !$form->hasErrors()) {
         HttpError::the500('Failed to upload file for unknown reason.');
     }
     $response = Yii::$app->getResponse();
     $response->setStatusCode(201);
     $response->getHeaders()->set('Location', $form->getLink());
     return $form;
 }
 /**
  * @return string
  * @throws \yii\web\NotFoundHttpException
  */
 public function run()
 {
     $section = Yii::$app->request->get('section', $this->defaultSection);
     $model = new IndexForm(['modelClass' => $this->modelClass]);
     if (!$model->loadBySection($section)) {
         HttpError::the404('Setting section not found');
     }
     $params = Yii::$app->request->post();
     if ($params) {
         $model->load($params, $model->formName());
         if ($model->save()) {
             if ($this->successCallback) {
                 call_user_func($this->successCallback, $model);
             } else {
                 Yii::$app->session->setFlash('index:success');
             }
         } else {
             if ($this->errorCallback) {
                 call_user_func($this->errorCallback, $model);
             } else {
                 Yii::$app->session->setFlash('index:error');
             }
         }
     }
     $controller = $this->controller;
     return $controller->render($this->viewFile, ['model' => $model, 'menuItems' => $this->menuItems]);
 }
Example #3
0
 /**
  * Геттер параметров приложения
  * @param $need
  * @return mixed
  */
 public static function get($need)
 {
     $params = Yii::$app->params;
     if (!isset($params[$need])) {
         HttpError::the500("Key not found: {$need}");
     }
     return $params[$need];
 }
 /**
  * @param $folder
  * @param $id
  * @param $name
  * @param $width
  * @param $height
  * @return string
  * @throws \yii\web\NotFoundHttpException
  */
 public function run($folder, $id, $name, $width, $height)
 {
     $form = new ImageIndexForm(['placeholder' => $this->placeholder, 'basePath' => $this->basePath, 'baseUrl' => $this->baseUrl]);
     $form->setAttributes(['id' => $id, 'folder' => $folder, 'name' => $name, 'width' => $width, 'height' => $height]);
     if (!$form->validate() || !$form->save()) {
         HttpError::the404();
     }
     return Yii::$app->response->redirect($form->getUrl());
 }
 /**
  * @return string
  * @throws ServerErrorHttpException
  * @throws \yii\base\InvalidConfigException
  * @throws \yii\web\BadRequestHttpException
  */
 public function run()
 {
     $params = Yii::$app->request->getBodyParams();
     $form = new ImageUploadForm(['basePath' => $this->basePath, 'baseUrl' => $this->baseUrl]);
     $form->setAttributes($params);
     if (!$form->validate()) {
         HttpError::the400();
     }
     if (!$form->save() && !$form->hasErrors()) {
         HttpError::the500('Failed to upload file for unknown reason.');
     }
     $response = Yii::$app->getResponse();
     $response->setStatusCode(201);
     $response->getHeaders()->set('Location', $form->getLink());
     return $form;
 }
 /**
  * Init variables
  * @throws \yii\web\HttpException
  * @throws \yii\web\NotFoundHttpException
  */
 public function afterValidate()
 {
     parent::afterValidate();
     $this->_pathRootObject = $this->basePath . DIRECTORY_SEPARATOR . $this->folder;
     $this->_urlRootObject = $this->baseUrl . DIRECTORY_SEPARATOR . $this->folder;
     if (!is_dir($this->_pathRootObject)) {
         HttpError::the404();
     }
     $this->_pathObject = $this->_pathRootObject . DIRECTORY_SEPARATOR . $this->id;
     $this->_urlObject = $this->_urlRootObject . DIRECTORY_SEPARATOR . $this->id;
     if (!is_dir($this->_pathObject) && !FileHelper::createDirectory($this->_pathObject)) {
         HttpError::the500('Can not create directory: ' . $this->_pathObject);
     }
     $this->_pathObjectFile = $this->_pathObject . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->name;
     $this->_urlObjectFile = $this->_urlObject . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->name;
     $this->_pathObjectFileOrigin = $this->_pathObject . DIRECTORY_SEPARATOR . $this->name;
     $this->_pathObjectPlaceholder = $this->_pathRootObject . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->placeholder;
     $this->_urlObjectPlaceholder = $this->_urlRootObject . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->placeholder;
     $this->_pathObjectPlaceholderOrigin = $this->_pathRootObject . DIRECTORY_SEPARATOR . $this->placeholder;
     $this->_pathRootPlaceholder = $this->basePath . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->placeholder;
     $this->_urlRootPlaceholder = $this->baseUrl . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->placeholder;
     $this->_pathRootPlaceholderOrigin = $this->basePath . DIRECTORY_SEPARATOR . $this->placeholder;
 }
 /**
  * Finding model by primary key
  * @param $pk
  * @param bool $throwException
  * @return ActiveRecord
  * @throws \yii\web\NotFoundHttpException
  */
 public function findModel($pk, $throwException = true)
 {
     /** @var ActiveRecord $model */
     $model = $this->modelClass;
     $model = $model::findOne($pk);
     if (empty($model) && $throwException) {
         HttpError::the404();
     }
     return $model;
 }