✏️ 正在编辑: ContratoAnexoController.php
路径:
/srv/systems_dir/yuppiecred-bkp/application/controllers/ContratoAnexoController.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class ContratoAnexoController extends Zend_Controller_Action { protected $_bucketFolder = 'propostas'; public function init() { $this->_helper->layout->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); $excecoes = array('download-formulario', 'view'); if (!$this->getRequest()->isXmlHttpRequest() && !in_array($this->getRequest()->getActionName(), $excecoes)) { $this->_helper->redirector('index', 'proposta'); } } /** * Visualizar anexos */ public function loadAnexosAction() { $request = $this->getRequest(); $contratoId = $request->getParam('id'); $token = $request->getParam('token'); $salvos = []; if ($contratoId) { $dbTipos = new Model_DbTable_Tipos(); $tipos = $dbTipos->fetchPairs('descricao', "tipo = 'ANEXO_CONTRATO' AND descricao <> 'Outros documentos'"); $dbAnexos = new Model_ContratoAnexo(); $salvos = $dbAnexos->listar([ 'contrato_id = ?' => $contratoId, 'tipo_id IN (' . implode(',', array_keys($tipos)) . ')' ]); } $temporarios = []; if ($token) { $ns = new Zend_Session_Namespace($token); if (!empty($ns->{$token})) { $temporarios = $ns->{$token}; } } $this->view->anexos = array_merge($salvos, $temporarios); $json['content'] = $this->view->render($request->getParam("controller") . "/_anexos.phtml"); $this->_helper->json($json); } public function saveAction() { $request = $this->getRequest(); $contratoId = $request->getParam('contrato_id'); $tipoId = $request->getParam('tipo_id'); $tipoDescricao = $request->getParam('tipo_descricao'); $token = $request->getParam('token'); if (empty($token)) { $this->_helper->json(['alert' => "Anexo não foi salvo. Tente recarregar a tela para resolver o problema."]); } $ns = new Zend_Session_Namespace($token); if (empty($ns->{$token})) { $ns->{$token} = []; } $fields = [ 'contrato_id' => $contratoId, 'tipo_id' => $tipoId, 'tipo' => $tipoDescricao ]; try { Upload::save((new Zend_File_Transfer_Adapter_Http()), $fields, $this->_bucketFolder); $ns->{$token}[] = $fields; $anexo = new Model_ContratoAnexo($fields); $salvo = $anexo->salvar(); if (count($ns->{$token}) > 0 && $salvo > 0) { if ($contratoId) { unset($ns->{$token}); } $this->_helper->json(['success' => $fields]); } } catch (DomainException $e) { $this->_helper->json(['alert' => $e->getMessage()]); } catch (Throwable $throwable) { $this->_helper->json(['alert' => 'Arquivo inválido, verifique o tamanho ou tipo.']); } $this->_helper->json(['alert' => "Não foi possível realizar o upload do arquivo"]); } public function deleteAction() { $request = $this->getRequest(); $token = $request->getParam('token'); $arquivo = $request->getParam('arquivo'); if (!$arquivo) { $this->_helper->flashMessenger(array("error" => "Requisição inválida")); $this->_helper->redirector('index', 'proposta'); } try { $ns = new Zend_Session_Namespace($token); if (!empty($ns->{$token})) { foreach ($ns->{$token} as $key => $value) { if ($value['arquivo'] == $arquivo) { unset($ns->{$token}[$key]); } } } Upload::deleteObject($arquivo, $this->_bucketFolder); $dbAnexo = new Model_DbTable_ContratoAnexo(); $anexo = $dbAnexo->fetchRow("arquivo = '$arquivo'"); if ($anexo) { $modelAnexo = new Model_ContratoAnexo(); Yuppie_Log::write("Exclusão de anexo pelo deleteAction: " . print_r($anexo->toArray(), true), $this); $modelAnexo->excluir($anexo['id']); } $json['success'] = "Registro removido com sucesso"; } catch (Exception $ex) { $json['alert'] = $ex->getMessage(); } $this->_helper->json($json); } /** * Limpar anexos */ public function destroyAnexosAction() { $request = $this->getRequest(); $tokens = $request->getParam('token'); $tokens = is_array($tokens) ? $tokens : array($tokens); foreach ($tokens as $token) { if (empty($token)) { $json['alert'] = "Nenhuma sessão registrada"; continue; } $ns = new Zend_Session_Namespace($token); if (!empty($ns->{$token})) { foreach ($ns->{$token} as $anexo) { $filename = UPLOAD_PATH . "/{$anexo['arquivo']}"; if (file_exists($filename)) { unlink($filename); } } $json['success'] = "Arquivos excluidos com sucesso"; unset($ns->{$token}); } else { $json['alert'] = "Nenhuma sessão registrada"; } } $this->_helper->json($json); } /** * Baixar formulário */ public function downloadFormularioAction() { $request = $this->getRequest(); $contrato = $request->getParam('contrato'); if ($contrato) { $modelAnexo = new Model_ContratoAnexo(); $anexos = $modelAnexo->listar(array("contrato_id = ?" => $contrato, "tipos.descricao = ?" => "Formulário digitalizado")); $filename = time() . ".zip"; $zip = new ZipArchive(); $zip->open(UPLOAD_PATH . "/" . $filename, ZipArchive::CREATE); $tmpFiles = []; foreach ($anexos as $registro) { $info = pathinfo($registro['arquivo']); $file = UPLOAD_PATH . "/{$info['basename']}"; if (strstr($registro['arquivo'], "amazonaws.com")){ $objectBody = Upload::getRawData($registro['arquivo'], $this->_bucketFolder); file_put_contents($file, $objectBody); $tmpFiles[] = $file; } if (file_exists($file)) { $zip->addFile($file, $info['basename']); } } $zip->close(); //remove os arquivos temporários baixados do S3 foreach( $tmpFiles as $fRemove ){ unlink($fRemove); } header('Content-Type: application/zip'); header('Content-disposition: attachment; filename=' . $filename); header('Content-Length: ' . filesize(UPLOAD_PATH . "/" . $filename)); readfile(UPLOAD_PATH . "/" . $filename); unlink(UPLOAD_PATH . "/" . $filename); } } public function viewAction() { $anexoUrl = $this->getRequest()->getParam('anexo_url'); Upload::download($anexoUrl, 'propostas'); } }
💾 保存文件
← 返回文件管理器