public function executeUploadImage($request) { $this->checkEntity($request); $params = $request->getParameter('image'); $this->upload_form = new ImageUploadForm(); $this->has_image = EntityTable::hasProfileImage($this->entity); if ($request->isMethod('post')) { $db = Doctrine_Manager::connection(); $this->upload_form->bind($params, $request->getFiles('image')); if ($this->upload_form->isValid()) { try { $db->beginTransaction(); $files = $request->getFiles('image'); //set filename and path based on upload type if (isset($files['file']['size']) && $files['file']['size']) { $path = $request->getFilePath('image'); $path = $path['file']; $originalFilename = $request->getFileName('image'); $originalFilename = $originalFilename['file']; } else { $path = $params['url']; $pathParts = explode('?', basename($path)); $originalFilename = $pathParts[0]; } //if image files can't be created, assume remote url was bad if (!($filename = ImageTable::createFiles($path, $originalFilename))) { $validatorSchema = $this->upload_form->getValidatorSchema(); $this->upload_form->getErrorSchema()->addError(new sfValidatorError($validatorSchema['url'], 'invalid')); return sfView::SUCCESS; } //create image $image = new Image(); $image->entity_id = $this->entity['id']; $image->filename = $filename; $image->title = $params['title']; $image->caption = $params['caption']; $image->url = $params['url']; if (!$this->has_image) { $image->is_featured = true; } elseif (isset($params['is_featured'])) { $db = Doctrine_Manager::connection(); $sql = 'UPDATE image SET is_featured = 0 WHERE entity_id = ?'; $stmt = $db->execute($sql, array($this->entity['id'])); $image->is_featured = true; } else { $image->is_featured = 0; } $image->is_free = isset($params['is_free']) ? true : null; $image->save(); //if featured, unfeature any other images if (isset($params['featured']) && ($profileImage = EntityTable::getProfileImageById($entity))) { $profileImage->is_featured = false; $profileImage->save(); } $db->commit(); } catch (Exception $e) { $db->rollback(); throw $e; } $this->clearCache($this->entity); $this->redirect($image->url ? EntityTable::getInternalUrl($this->entity, 'images') : 'entity/image?id=' . $image->id); } } }