Exemplo n.º 1
0
 /**
  * @param array $data
  */
 private function saveData(array $data)
 {
     foreach ($this->getLanguages() as $locale => $language) {
         /** @var ActiveRecord $translation */
         $translation = $this->owner->getTranslation($locale)->one();
         if ($translation === null) {
             $translation = new $this->translationModelName();
             $translation->{$this->translationOwnerField} = $this->owner->{$this->ownerPrimaryKey};
             $translation->{$this->languageField} = $language->id;
         }
         if (isset($data[$locale])) {
             $translation->setAttributes($data[$locale]);
         }
         if (!$translation->save()) {
             $this->owner->addError($locale, $translation->getErrors());
         }
     }
 }
Exemplo n.º 2
0
 public function addError($error, $message = null)
 {
     if ($message != null) {
         parent::addError($error, $message);
     } else {
         list($attribute, $error) = $error;
         parent::addError($attribute, $error);
     }
 }
 /**
  * @param $event
  * Try to apply crop before validate, because it may generate errors
  */
 public function beforeValidate($event)
 {
     $file = $this->file = UploadedFile::getInstance($this->_model, "__{$this->attribute}_file__");
     if ($file && !$file->hasError) {
         $asp = $this->propValue('aspectRatio');
         if ($asp > 30) {
             // Uploader widget sets aspect ratio at 1000 x real value
             $asp /= 1000;
             // compensate
             $this->_model->{$this->aspectRatio} = $asp;
         }
         // convert crop data from Json to array
         $crop = Json::decode($this->crop);
         // open image
         $image = $this->_image = Image::getImagine()->open($file->tempName);
         $imgSize = $image->getSize();
         $ww = $imgSize->getWidth();
         $hh = $imgSize->getHeight();
         $error = !$this->allowTooSmall;
         $cropSize = $this->propValue('cropSize');
         // Apply crop, if possible
         if ($crop['w'] > 0 && $crop['h'] > 0) {
             $error = $asp > 1 ? $crop['w'] < $cropSize : $crop['h'] < $cropSize;
             if (!$error) {
                 $image->crop(new Point($crop['x'], $crop['y']), new Box($crop['w'], $crop['h']));
             }
         } else {
             $asp = $ww / $hh;
             if (!is_numeric($this->aspectRatio)) {
                 $this->_model->{$this->aspectRatio} = $asp;
             }
         }
         if ($error) {
             // set error in model
             $this->_model->addError($this->attribute, sprintf($this->tooSmallMsg, $file->name, $ww, $hh));
             $event->isValid = false;
         }
     }
 }
Exemplo n.º 4
-1
 public function afterTransaction()
 {
     if ($this->hasErrors()) {
         if ($this->ownerAddErrors) {
             foreach ($this->relations as $relation) {
                 if ($this->errors[$relation]) {
                     $this->owner->addErrors($this->errors[$relation]);
                 }
             }
         } else {
             $this->owner->addError('ALL', 'error');
             // что бы работал owner->hasErrors
         }
     }
     if ($this->transaction !== null) {
         if ($this->hasErrors()) {
             $this->transaction->rollBack();
         } else {
             $this->transaction->commit();
         }
     }
 }