/** * This method accepts a projectId and delete all available amenities for a particular project. * Returns number of amenities deleted if projectId is found. * Returns 0 if validation fails. * * * @param string $projectId * * @return integer */ public static function deleteAllAmenitiesForProject($projectId) { return ProjectAmenities::model()->deleteAll('project_id=:projectId', array(':projectId' => $projectId)); }
public function actionPost() { Yii::beginProfile('project_post'); $model = new Projects(); $modelCity = new GeoCity(); $modelState = new GeoState(); $modelAmenities = new ProjectAmenities(); $modelImages = new ProjectImages(); $amenities = null; if (isset($_POST['submit'])) { $model->attributes = $_POST['Projects']; $modelImages->attributes = $_POST['ProjectImages']; $amenities = isset($_POST['amenity_id']) ? $_POST['amenity_id'] : null; /*if($amenities){ $amenity_data = null; foreach($amenities as $i=>$amenity){ if($i!=0) $amenity_data.=','; $amenity_data.=$amenity; } $amenities = $amenity_data; }*/ $valid = true; $model->user_id = Yii::app()->user->id; $valid = $valid && $model->validate(); if ($amenities == null) { $modelAmenities->addError('amenity_id', 'Amenities cannot be blank.'); $valid = false; } $modelImages->image = CUploadedFile::getInstance($modelImages, 'image'); if ($modelImages->image == "") { $modelImages->addError('image', 'Image cannot be blank.'); $valid = false; } if ($valid) { $data = $_POST['Projects']; $project = ProjectApi::createProject('1', $data); if (!$project->hasErrors()) { $amenities = ProjectAmenitiesApi::addAmenitiesForProject($project->id, $_POST['amenity_id']); $imagePath = ImageUtils::uploadImage($modelImages, 'image'); if ($amenities && $imagePath) { $image = ProjectImagesApi::addImage($project->id, $imagePath); if (!$image) { ProjectApi::deleteProjectById($project->id); ProjectAmenitiesApi::deleteAllAmenitiesForProject($project->id); } if (file_exists($imagePath)) { unlink($imagePath); } $data = array(); $user = UserApi::getUserById($project->user_id); $user ? $data["user"] = $user->id : null; $data["project"] = $project->id; EmailApi::sendEmail($user->email_id, "ACTIVITY.PROJECT.NEW", $data); $this->redirect('/project/' . $project->id); } else { ProjectApi::deleteProjectById($project->id); ProjectAmenitiesApi::deleteAllAmenitiesForProject($project->id); } } else { if (isset($property->id)) { ProjectApi::deleteProjectById($property->id); } } } } $this->render('post', array('model' => $model, 'modelState' => $modelState, 'modelCity' => $modelCity, 'modelAmenities' => $modelAmenities, 'modelImages' => $modelImages, 'amenities' => $amenities)); Yii::endProfile('project_post'); }