✏️ 正在编辑: ContratoAnexo.php
路径:
/srv/systems_dir/yuppiecred-lidernegocios/application/models/ContratoAnexo.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class Model_ContratoAnexo { protected $_id; protected $_contratoId; protected $_tipoId; protected $_descricao; protected $_arquivo; public function getId() { return $this->_id; } public function getContratoId() { return $this->_contratoId; } public function getTipoId() { return $this->_tipoId; } public function getDescricao() { return $this->_descricao; } public function getArquivo() { return $this->_arquivo; } public function setId($id) { $this->_id = $id; } public function setContratoId($contratoId) { $this->_contratoId = $contratoId; } public function setTipoId($tipoId) { $this->_tipoId = $tipoId; } public function setDescricao($descricao) { $this->_descricao = $descricao; } public function setArquivo($arquivo) { $this->_arquivo = $arquivo; } public function __construct($options = null) { if( is_array($options) ){ $this->setOptions($options); } } public function setOptions(array $options) { $methods = get_class_methods($this); foreach ( $options as $key => $value ) { $explode = explode('_', $key); $methodName = ''; foreach ( $explode as $v ) { $methodName .= ucfirst($v); } $key = $methodName != '' ? $methodName : $key; $method = 'set' . ucfirst($key); if( in_array($method, $methods) ){ $this->$method($value); } } return $this; } public function _toArray() { $dbtable = new Model_DbTable_ContratoAnexo(); $info = $dbtable->info(); $methods = get_class_methods($this); $data = array(); foreach ( $info['cols'] as $key ) { $col = $key; $explode = explode('_', $key); $methodName = ''; foreach ( $explode as $v ) { $methodName .= ucfirst($v); } $key = $methodName != '' ? $methodName : $key; $method = 'get' . ucfirst($key); if( in_array($method, $methods) ){ $data[$col] = $this->$method(); } } return array_filter($data); } /** * Insere ou altera o registro passado por parâmetro * @param Model_ContratoAnexo $contratoAnexo */ public function salvar() { $dbtable = new Model_DbTable_ContratoAnexo(); if( $this->getId() > 0 ){ $dbtable->update($this->_toArray(), "id = " . $this->getId()); return $this->getId(); } else { return $dbtable->insert($this->_toArray()); } } /** * Retorna uma lista de anexos * @param array $where * @param string $order * @return array */ public function listar($where = null, $order = null) { $dbContratoAnexo = new Model_DbTable_ContratoAnexo(); $select = $dbContratoAnexo->getAdapter()->select() ->from(array('contratos_anexos' => 'tb_contratos_anexos'), array('*')) ->joinLeft(array( "tipos" => "tb_tipos" ), "contratos_anexos.tipo_id = tipos.id", array('tipo' => 'descricao')) ->where("contratos_anexos.excluido <> 1"); if ($where) { if (is_array($where)) { foreach ($where as $cond => $value) { if (is_numeric($cond)) { $select->where($value); } else { $select->where($cond, $value); } } } else { $select->where($where); } } if( $order ){ $select->order($order); } return $dbContratoAnexo->getAdapter()->fetchAll($select); } /** * Retorna um fetchRow do item passado por parâmetro * @param int $id * @return Zend_Db_Table_Row_Abstract | null */ public function exibir($id) { $dbtable = new Model_DbTable_ContratoAnexo(); return $dbtable->fetchRow("id = " . $id); } /** * Exclui o registro passado por parâmetro * @param int $id * @return int The number of rows deleted */ public function excluir($id) { if( $id > 0 ){ $dbtable = new Model_DbTable_ContratoAnexo(); return $dbtable->delete("id = {$id}"); } return null; } public function getAnexo(int $idProposta) { $modelAnexos = new Model_DbTable_ContratoAnexo(); $tipo = (new Model_Tipo())->getTipoAnexoContratoByDescricao('Capa do contrato'); return $modelAnexos->fetchRow([ 'contrato_id = ?' => $idProposta, 'tipo_id = ?' => $tipo->id ]); } public static function downloadAnexo($id) { $model = self::exibir($id); if ($model->arquivo) { $config = new Zend_Config_Ini(APPLICATION_PATH . "/configs/application.ini"); $s3Options = $config->toArray(); $s3 = new Yuppie_Amazon_S3($s3Options['production']['amazon']['s3']); $info = pathinfo($model->arquivo); $result = $s3->getObject(str_replace("https://{$s3Options['production']['amazon']['s3']['bucket_name']}.s3.amazonaws.com/","",$model->arquivo)); header('Content-Disposition: attachment; filename=' . $info['basename']); header('Content-Type: ' . $result['ContentType']); return $result['Body']; } } /** * @param Zend_Controller_Request_Abstract|null $request * @param int $contratoId * @return mixed */ public function adicionarCapaContrato(?Zend_Controller_Request_Abstract $request, int $contratoId) { $upload = new Zend_File_Transfer_Adapter_Http(); if (!$upload->isUploaded()) { return null; } $modelTipo = new Model_Tipo(); $tipo = $modelTipo->getAnexoContratoByDescricao("Capa do contrato"); $anexoContrato = new Model_ContratoAnexo(); $capaExiste = $anexoContrato->listar(['contrato_id = ?' => $contratoId, 'tipo_id = ?' => $tipo['id']]); if ($capaExiste) { throw new Exception("A capa do contrato já foi anexada."); } try { $fields = $request->getPost(); $fields['tipo_id'] = $tipo['id']; $fields['descricao'] = $tipo['descricao']; $fields['contrato_id'] = $contratoId; Upload::save($upload, $fields, "propostas"); $documento = new Model_ContratoAnexo($fields); return $documento->salvar(); } catch (Throwable $throwable) { throw new Exception("Capa não anexada. {$throwable->getMessage()}"); } } }
💾 保存文件
← 返回文件管理器