✏️ 正在编辑: MensagemController.php
路径:
/srv/systems_dir/yuppiecred/application/controllers/MensagemController.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class MensagemController extends Zend_Controller_Action { protected $_bucketFolder = 'mensagens'; public function indexAction() { if( Yuppie_Auth::getIdentity()->grupo == 'Corretor' ){ $this->_helper->layout->setLayout('layout_corretor'); } $form = new Form_Filtro_Mensagem(); $request = $this->getRequest(); $mensagem = new Model_Mensagem(); $enviadas = array(); $fields = array_filter($request->getPost()); if( $request->isPost() ){ unset($fields['btn_filtrar']); $enviadas = $mensagem->fetchEnviadas( Yuppie_Auth::getIdentity()->id, $fields); $form->populate($fields); } $this->view->enviadas = $enviadas; $this->view->form = $form; } public function saveAction() { if( Yuppie_Auth::getIdentity()->grupo == 'Corretor' ){ $this->_helper->layout->setLayout('layout_corretor'); } $request = $this->getRequest(); $id = (int) $request->getParam('id'); $form = new Form_Mensagem(); $corretorId = $request->getParam('destinatario_id'); $cont = 0; if( $id ){ $mensagem = new Model_Mensagem(); $dados = $mensagem->exibir($id); if( $dados->remetente_id != Yuppie_Auth::getIdentity()->id ){ $this->_helper->flashMessenger(['error' => 'Permissão negada.']); $this->_helper->redirector('index'); } } if ($request->isPost() && $form->isValid($request->getPost())) { $dados = array_filter($request->getPost()); unset($dados['btn_save']); try{ Upload::save((new Zend_File_Transfer_Adapter_Http()), $dados, $this->_bucketFolder); } catch (Exception $ex) { $this->_helper->flashMessenger(['error' => $ex->getMessage()]); } $mensagem = new Model_Mensagem($request->getPost()); $texto = preg_replace('/\n\r?/', "<br />", $dados['mensagem']); $assunto = $dados['assunto'] ?? NULL; try { foreach ( $corretorId as $c ) { $dadosCorretor = (new Model_Corretor())->exibir($c); if (empty($request->getPost('enviar_para_desativados')) && $dadosCorretor->corretor_desativado == 1) { continue; } $options = [ 'rementente_id' => Yuppie_Auth::getIdentity()->id, 'data_envio' => Zend_Date::now()->get('yyyy-MM-dd HH:mm:ss'), 'destinatario_id' => $c, 'assunto' => $assunto, 'mensagem' => $texto, 'data_final' => $dados['data_final'], 'anexo' => $dados['anexo'] ]; $mensagem->setOptions($options); $id = $mensagem->salvar($mensagem); if(!empty($dados['disparar_email'])) { $this->dispararEmail($id, $c, $assunto, $texto); } $cont++; } if (count($id) > 0) { $this->_helper->flashMessenger([ 'success' => "{$cont} Registro(s) salvo(s) com sucesso." ]); } } catch (Exception $e) { $this->_helper->flashMessenger(['error' => $e->getMessage()]); } } elseif($id) { $form->populate($dados->toArray()); } $this->view->form = $form; } public function saveSupervisorAction() { if( Yuppie_Auth::getIdentity()->grupo == 'Corretor' ){ $this->_helper->layout->setLayout('layout_corretor'); } $request = $this->getRequest(); $id = (int) $request->getParam('id'); $form = new Form_MensagemSupervisor(); $supervisorId = $request->getParam('supervisor_id'); $cont = 0; if( $id ){ $mensagem = new Model_Mensagem(); $dados = $mensagem->exibir($id); if( $dados->remetente_id != Yuppie_Auth::getIdentity()->id ){ $this->_helper->flashMessenger(array( 'error' => 'Permissão negada.' )); $this->_helper->redirector('index'); } } if( $request->isPost() ){ if( $form->isValid($request->getPost()) ){ $dados = array_filter($request->getPost()); unset($dados['btn_save']); $mensagem = new Model_Mensagem($request->getPost()); $texto = preg_replace('/\n\r?/', "<br />", $dados['mensagem']); try { foreach ( $supervisorId as $superId ) { $mensagem->setOptions(array( 'rementente_id' => Yuppie_Auth::getIdentity()->id, 'data_envio' => Zend_Date::now()->get('yyyy-MM-dd HH:mm:ss'), 'supervisor_id' => $superId, 'mensagem' => $texto, 'data_final' => $dados['data_final'] )); $id = $mensagem->salvar($mensagem); $cont++; } if( count($id) > 0 ){ $this->_helper->flashMessenger(array( 'success' => "{$cont} Registro(s) salvo(s) com sucesso." )); } } catch ( Zend_Exception $e ) { $this->_helper->flashMessenger($e->getMessage()); } } } else if( $id ){ $form->populate($dados->toArray()); } $this->view->form = $form; } public function deleteAction() { $request = $this->getRequest(); $id = (int) $request->getParam('id'); $mensagens = $request->getPost('mensagens'); $mensagem = new Model_Mensagem(); if( $id > 0 ){ $mensagem->excluir($id); } if( is_array($mensagens) && count($mensagens) > 0 ){ foreach ( $mensagens as $mensagemId ) { $result = $mensagem->excluir($mensagemId); } if( $result > 0 ){ $this->_helper->FlashMessenger(array( 'success' => 'Dados excluidos com sucesso!' )); } } $this->_helper->redirector('index'); } public function viewAction() { $request = $this->getRequest(); $id = (int) $request->getParam('id'); $mensagem = new Model_Mensagem(); $this->view->mensagem = $mensagem->ler($id); } protected function dispararEmail(int $idMensagem, int $corretorId, $assunto = null, $mensagem = null) { if( !Yuppie_Auth::getIdentity()->email ){ throw new Core_Exception("O sistema não pode enviar sua notificação" . " pois você não tem e-mail cadastrado."); } $corretor = new Model_Corretor(); $dadosCorretor = $corretor->exibir($corretorId); $modelEmpresa = new Model_Empresa(); $dadosEmpresa = $modelEmpresa->exibir(Yuppie_Auth::getIdentity()->empresa_id); if (!$dadosCorretor) { throw new Core_Exception("Corretor não identificado"); } //faz o envio da mensagem try { $modelMensagem = new Model_Mensagem(); $model = $modelMensagem->exibir($idMensagem); if ($model->anexo) { $info = pathinfo($model->anexo); $file = UPLOAD_PATH . "/{$info['basename']}"; $config = new Zend_Config_Ini(CLIENT_PATH . "/configs/application.ini"); $s3Options = $config->toArray(); $s3 = new Yuppie_Amazon_S3($s3Options['production']['amazon']['s3']); $siteKey = Yuppie_Auth::getSiteKey(); $result = $s3->getObject("{$siteKey}/mensagens/" . $info['basename']); file_put_contents($file, $result['Body']); } $mail = new Core_Mail(); $assunto = $assunto ? $assunto : 'Informativo da - ' . $dadosEmpresa->nome; $this->view->mensagem = $mensagem; $mail->enviarEmailCorretor($assunto, $this->view->render('mensagem/disparar-email.phtml'), $corretorId, $info['basename']); if ($file) unlink($file); } catch (Zend_Mail_Exception $e) { $this->_helper->flashMessenger([ 'error' => 'Uma falha pode ter ocorrido. ' . 'Sua mensagem não foi enviada. ' . 'Por favor, tente novamente! (' . $dadosCorretor->nome . ' - ' . $dadosCorretor->email . ') ' . $e->getMessage() ]); } catch(Exception $e) { $this->_helper->flashMessenger(['error' => $e->getMessage()]); } } public function downloadAnexoAction() { $this->_helper->layout->disableLayout(); $request = $this->getRequest(); $id = (int)$request->getParam('id'); if ($id) { $model = (new Model_Mensagem())->exibir($id); if( $model->anexo ){ Upload::download($model->anexo, $this->_bucketFolder); } else { $this->_helper->flashMessenger(['alert' => "Anexo não encontrado!"]); } } else { $this->_helper->flashMessenger(['alert' => "Registro não identificado."]); } $this->_redirect('/index'); } }
💾 保存文件
← 返回文件管理器