public function executeEditDoc(sfWebRequest $request) { $op = $request->getParameter("op", "d"); $id = $request->getParameter("id", 0); $titulo = $request->getParameter("titulo"); $descripcion = $request->getParameter("descripcion"); $this->propuesta = PropuestaPeer::retrieveByPk($id); $this->forward404Unless($this->propuesta); $files = $request->getFiles(); if ($op == 'et') { $this->propuesta->setTitulo(SfVoUtil::cutToLength($titulo, 80, '', false)); $this->propuesta->save(); $this->redirect("propuesta/show?id=" . $this->propuesta->getVanity()); } elseif ($op == 'ed') { $this->propuesta->setDescripcion(SfVoUtil::cutToLength($descripcion, 600, '', false)); $this->propuesta->save(); $this->redirect("propuesta/show?id=" . $this->propuesta->getVanity()); } elseif ($op == 'ep') { $img = $files['img']; if ($img) { $arr = array_reverse(explode(".", $img['name'])); $ext = strtolower($arr[0]); if (!$ext || $ext == "") { $ext = "png"; } $docName = SfVoUtil::fixVanityChars($arr[1]); $docName .= "-" . sprintf("%04d", rand(0, 999)); $docName .= ".{$ext}"; $fileName = sfConfig::get('sf_upload_dir') . '/propuestas/' . $docName; move_uploaded_file($img['tmp_name'], $fileName); $s = new S3Voota(); $s->createFromFile('propuestas', $fileName); $this->propuesta->setImagen($docName); $this->propuesta->save(); } $this->redirect("propuesta/show?id=" . $this->propuesta->getVanity()); } else { switch ($op) { case 's': return 'Saved'; break; case 'd': break; case 'f': $this->propuesta->setDoc(null); $this->propuesta->setDocSize(null); $this->propuesta->save(); return 'Form'; case 'u': $doc = $files['doc']; if ($doc) { $arr = array_reverse(explode(".", $doc['name'])); $ext = strtolower($arr[0]); if (!$ext || $ext == "") { $ext = "png"; } $docName = SfVoUtil::fixVanityChars($arr[1]); $docName .= "-" . sprintf("%04d", rand(0, 999)); $docName .= ".{$ext}"; $fileName = sfConfig::get('sf_upload_dir') . '/docs/' . $docName; move_uploaded_file($doc['tmp_name'], $fileName); $s = new S3Voota(); $s->createDocFromFile('docs', $fileName); $this->propuesta->setDoc($docName); $this->propuesta->setDocSize($s->getSize("docs/{$docName}")); $this->propuesta->save(); echo "0"; die; } return 'Form'; } } }
/** * Saves the uploaded file. * * This method can throw exceptions if there is a problem when saving the file. * * If you don't pass a file name, it will be generated by the generateFilename method. * This will only work if you have passed a path when initializing this instance. * * @param string $file The file path to save the file * @param int $fileMode The octal mode to use for the new file * @param bool $create Indicates that we should make the directory before moving the file * @param int $dirMode The octal mode to use when creating the directory * * @return string The filename without the $this->path prefix * * @throws Exception */ public function save($file = null, $fileMode = 0666, $create = true, $dirMode = 0777) { if (is_null($file)) { $file = $this->getOriginalName(); $parts = explode(".", $file); $ext = $parts[count($parts) - 1]; $file = preg_replace("/\\.{$ext}/i", sprintf("%04d", "-" . rand(0, 999)) . ".{$ext}", $file); } if ($file[0] != '/' && $file[0] != '\\' && !(strlen($file) > 3 && ctype_alpha($file[0]) && $file[1] == ':' && ($file[2] == '\\' || $file[2] == '/'))) { if (is_null($this->path)) { throw new RuntimeException('You must give a "path" when you give a relative file name.'); } $file = $this->path . DIRECTORY_SEPARATOR . $file; } // get our directory path from the destination filename $directory = dirname($file); if (!is_readable($directory)) { if ($create && !mkdir($directory, $dirMode, true)) { // failed to create the directory throw new Exception(sprintf('Failed to create file upload directory "%s".', $directory)); } // chmod the directory since it doesn't seem to work on recursive paths chmod($directory, $dirMode); } if (!is_dir($directory)) { // the directory path exists but it's not a directory throw new Exception(sprintf('File upload path "%s" exists, but is not a directory.', $directory)); } if (!is_writable($directory)) { // the directory isn't writable throw new Exception(sprintf('File upload path "%s" is not writable.', $directory)); } $this->savedName = $file; //return is_null($this->path) ? $file : str_replace($this->path.DIRECTORY_SEPARATOR, '', $file); $ret = parent::save($file, $fileMode, $create, $dirMode); //$img = new sfImage( $file ); if ($ret) { $s3 = new S3Voota(); if (strpos($file, "politicos")) { $s3->createPoliticoFromFile($file); } else { if (strpos($file, "instituciones")) { $s3->createInstitucionFromFile($file); } else { if (strpos($file, "usuarios")) { $ret = $s3->createUsuarioFromFile($file); } else { if (strpos($file, "partidos")) { $s3->createPartidoFromFile($file); } else { if (strpos($file, "propuestas")) { $s3->createFromFile("propuestas", $file); } else { if (strpos($file, "docs")) { $s3->createDocFromFile("docs", $file); } else { if (strpos($file, "elecciones")) { $s3->createFromFile("elecciones", $file); } } } } } } } } return $ret; }