/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Photo(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Photo'])) { $model->attributes = $_POST['Photo']; if (isset($_FILES['images']['name'][0]) && $_FILES['images']['name'][0] !== '') { $model->name = 'new'; } if ($model->validate()) { $images = CUploadedFile::getInstancesByName('images'); foreach ($images as $image) { $imageModel = new Photo(); $name = uniqid() . $image->name; $image->saveAs(Yii::getPathOfAlias('webroot.uploads.images') . DIRECTORY_SEPARATOR . $name); copy(Yii::getPathOfAlias('webroot.uploads.images') . DIRECTORY_SEPARATOR . $name, Yii::getPathOfAlias('webroot.uploads.images') . DIRECTORY_SEPARATOR . 'thumbs' . DIRECTORY_SEPARATOR . $name); $thumb = Yii::app()->image->load(Yii::getPathOfAlias('webroot.uploads.images') . DIRECTORY_SEPARATOR . 'thumbs' . DIRECTORY_SEPARATOR . $name); $thumb->resize(300, 300); $thumb->save(); $imageModel->name = $name; $imageModel->category_id = $_POST['Photo']['category_id']; $imageModel->save(); } Yii::app()->user->setFlash('success', Yii::t('main', 'Данные успешно сохранены!')); $this->refresh(); } else { Yii::app()->user->setFlash('error', Yii::t('main', 'Ошибка!')); } } $this->render('create', array('model' => $model)); }
/** * This function performs the validation work for complex object models. * * In addition to checking the current object, all related objects will * also be validated. If all pass then <code>true</code> is returned; otherwise * an aggreagated array of ValidationFailed objects will be returned. * * @param array $columns Array of column names to validate. * @return mixed <code>true</code> if all validations pass; array of <code>ValidationFailed</code> objets otherwise. */ protected function doValidate($columns = null) { if (!$this->alreadyInValidation) { $this->alreadyInValidation = true; $retval = null; $failureMap = array(); // We call the validate method on the following object(s) if they // were passed to this object by their coresponding set // method. This object relates to these object(s) by a // foreign key reference. if ($this->aProduct !== null) { if (!$this->aProduct->validate($columns)) { $failureMap = array_merge($failureMap, $this->aProduct->getValidationFailures()); } } if ($this->aPhoto !== null) { if (!$this->aPhoto->validate($columns)) { $failureMap = array_merge($failureMap, $this->aPhoto->getValidationFailures()); } } if (($retval = ProductHasPhotoPeer::doValidate($this, $columns)) !== true) { $failureMap = array_merge($failureMap, $retval); } $this->alreadyInValidation = false; } return !empty($failureMap) ? $failureMap : true; }
public function actionWeiboPost() { if ($this->isPost()) { $request = Yii::app()->getRequest(); $photo = new Photo(); $photo->weibo_id = $request->getPost('weibo_id'); $photo->url = $request->getPost('url'); $photo->screen_name = $request->getPost('screen_name'); $photo->gender = $request->getPost('gender'); $photo->location = $request->getPost('location'); $photo->sns_uid = $request->getPost('sns_uid'); $photo->avatar = $request->getPost('avatar'); $photo->content = $request->getPost('content'); $photoUpload = CUploadedFile::getInstanceByName("image"); if ($photoUpload) { $mime = $photoUpload->getType(); $allowMime = array("image/gif", "image/png", "image/jpeg", "image/jpg"); if (!in_array($mime, $allowMime)) { return $this->responseError("photo's media type is not allowed"); } else { $photo->image = savePostImage($photoUpload, $photo->weibo_id); if ($photo->validate()) { $photo->save(); return $this->responseJSON($photo, 'success'); } } } else { return $this->responseError("not get photo"); } } else { return $this->responseError("not login"); } }
public function actionPhoto() { $model = new Photo('upload'); if (isset($_POST['Photo'])) { $model->attributes = $_POST['Photo']; if ($model->validate()) { $model->id_user = Yii::app()->user->id; $model->id_photo = Photo::getMaxIdPhoto(); //max(id_photo) where current user if (!$model->save()) { throw new CHttpException('500', 'Ошибка записи в БД'); } $model->file = CUploadedFile::getInstance($model, 'file'); if ($model->file != NULL) { /** @var $model->file CUploadedFile */ // $model->file-> $model->file->saveAs(Yii::getPathOfAlias('webroot') . '/' . $model->getFullImagePath()); $model->path = $model->getLargeImagePath(); $model->saveSmallPictures(); $model->save(false); //not validate (WHY???) // if it's first photo, make it mainphoto if ($model->id_photo == 1) { $user = Anketa::model()->findByPk(Yii::app()->user->id); $user->mainphoto = $model->id_photo; // not const, for next changing $user->saveAttributes(array('mainphoto')); } /* $file_name = '/p/9000/'.Yii::app()->user->id.'_'.$model->id_photo.'_l.JPG'; $imginfo = getimagesize($_SERVER['DOCUMENT_ROOT'].$file_name); $image = Yii::app()->image->load($_SERVER['DOCUMENT_ROOT'].$file_name); if ($imginfo[0]>Photo::$sizes['full'][0] || $imginfo[1]>Photo::$sizes['full'][1] ) $image->resize(Photo::$sizes['full'][0], Photo::$sizes['full'][1]); $image->quality(100)->save(); $file_name = '/p/6000/'.Yii::app()->user->id.'_'.$model->id_photo.'_l.JPG'; if ($imginfo[0]>Photo::$sizes['large'][0] || $imginfo[1]>Photo::$sizes['large'][1] ) $image->resize(Photo::$sizes['large'][0], Photo::$sizes['large'][1]); $image->quality(100)->save($_SERVER['DOCUMENT_ROOT'].$file_name); TmpHelper::addLogo($_SERVER['DOCUMENT_ROOT'].$file_name); $model->path = $file_name; $file_name = '/p/7000/'.Yii::app()->user->id.'_'.$model->id_photo.'_l.JPG'; if ($imginfo[0]>Photo::$sizes['small'][0] || $imginfo[1]>Photo::$sizes['small'][1]) $image->resize(Photo::$sizes['small'][0], Photo::$sizes['small'][1]); $image->quality(100)->save($_SERVER['DOCUMENT_ROOT'].$file_name); TmpHelper::addLogo($_SERVER['DOCUMENT_ROOT'].$file_name); $model->saveAttributes(array('path')); */ } //echo 'uploaded'; Yii::app()->user->setFlash('profile', 'Фотография загружена'); $this->redirect(array('/profile/photos')); // form inputs are valid, do something here return; } } $this->render('_photo', array('model' => $model)); }
/** * Validate data from form post * it's must making before saving into database * @param mixed $postData */ public function validatePortfolio($postData) { $arrayPortfolio = array(); foreach ($_POST['FileForm'] as $gallery_idx => $postPortfolio) { if (empty($postPortfolio['gallery_id'])) { $portfolio = new Portfolio(); } else { $portfolio_id = $postPortfolio['gallery_id']; $portfolio = Portfolio::model()->with('photos')->findByPk($portfolio_id); if (!isset($portfolio)) { $portfolio = new Portfolio(); } } $portfolio->attributes = $postPortfolio; //assign attributes $valOK = $portfolio->validate(); $arrayPhotosIds = array(); //additional array for deleted photos search $deletedPhotos = array(); //array for deleted photos $photos = array(); //array of edited photos of portfolio $isTopPresent = false; //process photos array if (isset($postPortfolio['photos'])) { foreach ($postPortfolio['photos'] as $idx => $postPhoto) { $photo_id = $postPhoto['photo_id']; $isNewPhoto = empty($photo_id); $photo = $isNewPhoto ? new Photo() : Photo::model()->findByPk($photo_id); if ($isNewPhoto = !isset($photo)) { $photo = new Photo(); } $photo->attributes = $postPhoto; $photo->gallery_id = $portfolio->gallery_id; $photo->is_top = isset($postPortfolio['is_top']) && $idx == $postPortfolio['is_top'] ? Photo::POSITION_IS_TOP : Photo::POSITION_NO_TOP; if ($photo->is_top) { $isTopPresent = true; } //mark is_top present $valOK = $photo->validate() && $valOK; $arrayPhotosIds[$idx] = $postPhoto['photo_id']; //add item to search array $photos[$idx] = $photo; } } //validate photos (count) if ($postPortfolio['gallery_type'] == 1) { if (!count($photos)) { $portfolio->addError('is_top', Yii::t('main', 'Portfolio must have photos')); } else { if (!$isTopPresent) { $portfolio->addError('is_top', Yii::t('main', 'In photo portfolio one of photo must be in top')); } } } //search for deleted photos if (isset($portfolio->photos)) { foreach ($portfolio->photos as $photo) { if (!in_array($photo->photo_id, $arrayPhotosIds)) { $portfolio->deletedPhotos[] = $photo; } } } $portfolio->photos = $photos; //add photo items to portfolio model $arrayPortfolio[] = $portfolio; //add instance of portfolio to returning array } return $arrayPortfolio; //returning result array of portfolio instances }