Exemple #1
0
 /**
  * recebe a imagem por post
  * @return ReturnResultVO
  */
 public function insert()
 {
     //inicia um retorno de ReturnResultVO
     $ReturnResultVO = new ReturnResultVO();
     //pega os dados baseado na infoPost
     $VO = new ImageVO();
     //var_dump($this->infoPost);
     //exit();
     if ($this->infoPost->image_info_id > 0) {
         //passou o id, vai atualizar essa VO
         //					echo Debug::li("image_info_id >>>>>>>>>> ".$this->infoPost->image_info_id);
         $VO->setId($this->infoPost->image_info_id, TRUE);
     }
     $VO->setActive($this->infoPost->image_info_active);
     $VO->setName($this->infoPost->image_info_name);
     $VO->setDescription($this->infoPost->image_info_description);
     $VO->setType($this->infoPost->image_info_type);
     $VO->setAuthor($this->infoPost->image_info_author);
     $VO->setLocale($this->infoPost->image_info_locale);
     $VO->setOrder($this->infoPost->image_info_order);
     //				var_dump($_FILES);
     //				var_dump($this->infoPost->file_data);
     //comitando as infos enviadas, dados apenas
     if ($VO->getId() > 0 || $this->infoPost->file_data['tmp_name']) {
         //só comita a imagem se tiver ou id ou enviado o file_data, se não nem tem o que fazer
         $ReturnResultImageVO = $VO->commit();
     } else {
         //nem enviou o id e nem o file_data, retorna
         $ReturnResultVO->addMessage(Translation::text("Have nothing to commit."));
         return $ReturnResultVO;
     }
     if ($ReturnResultImageVO->success) {
         $ReturnResultImageVO->result = $VO->getId();
     } else {
         //erro, os motivos estão na ReturnResultVO abaixo
         return $ReturnResultImageVO;
     }
     //pega o id da imagem
     $IMAGE_ID = $VO->getId();
     $ReturnResultImageVO = new ReturnResultVO();
     //echo Debug::li("this->infoPost->file_data: ".$this->infoPost->file_data);
     if (isset($this->infoPost->file_data) && $this->infoPost->file_data['tmp_name']) {
         set_time_limit(0);
         //var_dump($_FILES);
         $sentFileData = $this->infoPost->file_data;
         //$_FILES['Filedata'];
         $name = $sentFileData['name'];
         // extens�o enviada
         $sentExtension = DataHandler::returnExtensionOfFile($name);
         // remove caracteres escrotos
         $name = DataHandler::returnFilenameWithoutExtension($name);
         $name = DataHandler::removeAccent($name);
         $name = DataHandler::removeSpecialCharacters($name);
         $name = trim(substr($name, 0, 80));
         switch ($sentFileData["type"]) {
             case "image/pjpeg":
             case "image/jpeg":
             case "image/jpg":
                 $extension = "jpg";
                 break;
             case "image/gif":
                 $extension = "gif";
                 break;
             case "image/png":
             case "image/x-png":
                 $extension = "png";
                 break;
             case "image/bmp":
                 $extension = "bmp";
                 break;
             default:
                 $extension = strtolower($sentExtension);
                 break;
         }
         //verifica se a pasta existe, se não existir, inventa
         DataHandler::createFolderIfNotExist($this->defaultFolderForNewImages);
         // pasta de upload de imagens est� no config.php
         $tempFolder = DataHandler::removeDobleBars($this->defaultFolderForNewImages . "/" . $IMAGE_ID);
         DataHandler::createFolderIfNotExist($tempFolder);
         //echo Debug::li("name: $name");
         $tempUrl = $tempFolder . "/original_" . strtolower($name . "." . $extension);
         //echo Debug::li("tempUrl: $tempUrl");
         //exit();
         $i = 2;
         while (file_exists($tempUrl)) {
             $tempUrl = $tempFolder . "/original_" . strtolower($name . "-" . $i . "." . $extension);
             $i++;
         }
         $VO->setUrl($tempUrl);
         $ReturnResultImageVO = $VO->commit();
         //Debug::li("aaa");
         //Debug::print_r($ReturnResultImageVO);
         if ($ReturnResultImageVO->success) {
             //incluir o vinculo com a linked_table e linked_table_id
             //receber 	table
             //			table_id
             if ($this->infoPost->table) {
                 $table = $this->infoPost->table;
                 $table_id = $this->infoPost->table_id;
                 include_once "library/facil3/core/dao/LinkDAO.class.php";
                 $LinkDAO = new LinkDAO();
                 //vincula a foto ao table e table_id enviado
                 $ReturnResultVinculoVO = $LinkDAO->insert($table, $table_id, $this->moduleName, $VO->getId(), 1);
                 if (!$ReturnResultVinculoVO->success) {
                     //deu erro ao vincular
                     $ReturnResultVO->success = false;
                     $ReturnResultVO->appendMessage($ReturnResultVinculoVO->array_messages);
                     return $ReturnResultVO;
                 }
             } else {
                 $ReturnResultVO->addMessage("WARNING IMAGE NO LINKED TABLE");
             }
             //movendo a foto original para sua respectiva pasta.
             $originalImage = $VO->getUrl();
             if (!move_uploaded_file($sentFileData['tmp_name'], $originalImage)) {
                 $ReturnResultVO->success = false;
                 $ReturnResultVO->addMessage(LibraryLanguage::ERROR_IMAGE_MOVE_FILE_FAIL);
                 return $ReturnResultVO;
             } else {
                 $ReturnResultVO->success = TRUE;
                 $ReturnResultVO->result = $VO->getId();
                 $ReturnResultVO->addMessage("Foto enviada com sucesso.");
             }
         } else {
             return $ReturnResultImageVO;
         }
         return $ReturnResultVO;
     } else {
         if ($VO->getId() > 0) {
             //se tem id, ele manda atualizar a parada
             $ReturnResultImageVO = $VO->commit();
             return $ReturnResultImageVO;
         }
         $ReturnResultImageVO = new ReturnResultVO();
         $ReturnResultImageVO->addMessage(Translation::text("Send file data."));
         // nao veio filedata
         return $ReturnResultImageVO;
     }
 }