PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Condominio/Controller/MoradorController.php

https://gitlab.com/fabiorf/reclameimovel
PHP | 308 lines | 218 code | 61 blank | 29 comment | 23 complexity | a92b83e808a30b8dd63dd0b9b4b1b398 MD5 | raw file
  1. <?php
  2. namespace Condominio\Controller;
  3. use Silex\Application;
  4. use Symfony\Component\Form\FormError;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Condominio\Entity\Reclamacao;
  8. use Condominio\Entity\Empreendimento;
  9. use Condominio\Entity\Imagem;
  10. use Condominio\Entity\User;
  11. use Condominio\Form\Type\UserType;
  12. use Condominio\Form\Type\ReclamacaoType;
  13. use Facebook\FacebookRequest;
  14. use Facebook\GraphObject;
  15. use Facebook\FacebookRequestException;
  16. use Facebook\FacebookSession;
  17. class MoradorController {
  18. public function indexAction(Request $request, Application $app) {
  19. $data = array(
  20. 'active'=>'morador',
  21. 'metaDescription'=>'',
  22. );
  23. return $app['twig']->render('morador.html.twig',$data);
  24. }
  25. public function minhasReclamacoesAction(Request $request, Application $app) {
  26. if($app['token']){
  27. $idu = $app['token']->getUid();
  28. }else{
  29. $idu = 1;
  30. }
  31. $page = $request->get("page", 1);
  32. $limit = 10;
  33. $total = $app['repository.reclamacao']->getCountUsuario($idu);
  34. $numPages = ceil($total / $limit);
  35. $currentPage = $page;
  36. $offset = ($currentPage - 1) * $limit;
  37. $aLista = $app['repository.reclamacao']->findReclamacaoUsuario($limit, $offset,array(),$idu);
  38. $data = array(
  39. 'active'=>'minhas_reclamacoes',
  40. 'metaDescription' => '',
  41. 'active' => 'minhas_reclamacoes',
  42. 'aLista' => $aLista,
  43. 'currentPage' => $currentPage,
  44. 'numPages' => $numPages,
  45. 'adjacentes' => 2,
  46. 'busca'=>false,
  47. 'uri'=>'/empreendimento',
  48. 'here' => "minhas-reclamacoes",
  49. );
  50. return $app['twig']->render('minhas_rec.html.twig', $data);
  51. }
  52. public function dadosAction(Request $request, Application $app) {
  53. /*
  54. * Pegar id da sessao
  55. */
  56. if($app['token']){
  57. $uid = $app['token']->getUid();
  58. $user = $app['repository.user']->find($uid);
  59. }
  60. if (!$user) {
  61. $app->abort(404, 'Erro nenhum usuário encontrado.');
  62. }
  63. $user->setIdu($uid);
  64. $form = $app['form.factory']->create(new UserType(), $user);
  65. $data = array(
  66. 'metaDescription' => '',
  67. 'form' => $form->createView(),
  68. );
  69. return $app['twig']->render('mais_dados.html.twig', $data);
  70. }
  71. public function dadosUpdateAction(Request $request, Application $app) {
  72. /*
  73. * Pegar id da sessao
  74. */
  75. if($app['token']){
  76. $uid = $app['token']->getUid();
  77. $user = $app['repository.user']->find($uid);
  78. }
  79. if (!$user) {
  80. $app->abort(404, 'Erro nenhum usuário encontrado.');
  81. }
  82. $user->setIdu($uid);
  83. $form = $app['form.factory']->create(new UserType(), $user);
  84. if ($request->isMethod('POST')) {
  85. $form->bind($request);
  86. if ($form->isValid()) {
  87. $app['repository.user']->saveAdicional($user);
  88. $message = 'Informações adicionadas com sucesso. Você já esta liberado para reclamar.';
  89. $app['session']->getFlashBag()->add('success', $message);
  90. // Redirect to the edit page.
  91. $redirect = $app['url_generator']->generate('principal');
  92. return $app->redirect($redirect);
  93. }
  94. return false;
  95. }
  96. }
  97. public function adicionarAction(Request $request, Application $app) {
  98. #$request = $app['request'];
  99. $idnome = $request->get("idnome");
  100. if(is_string($idnome)){
  101. $oEmp = $app['repository.empreendimento']->findIdNome($idnome);
  102. }
  103. if(is_numeric($idnome)){
  104. $oEmp = $app['repository.empreendimento']->find($idnome);
  105. }
  106. $reclamacao = new Reclamacao();
  107. if ($request->isMethod('GET')) {
  108. if (!$oEmp) {
  109. $message = 'Nenhum empreendimento foi escolhido por favor efetue uma busca e clique nele para adicionar.';
  110. $app['session']->getFlashBag()->add('warning', $message);
  111. // Redirect to the edit page.
  112. $redirect = $app['url_generator']->generate('principal');
  113. return $app->redirect($redirect);
  114. }
  115. /*
  116. * Pegar id do banco de dados
  117. */
  118. $reclamacao->setIde($oEmp->getId());
  119. }
  120. /*
  121. * Pegar id da sessao
  122. */
  123. if($app['token']){
  124. $uid = $app['token']->getUid();
  125. $user = $app['repository.user']->find($uid);
  126. $reclamacao->setDados($user->getDadosImovel());
  127. }else{
  128. $uid = 1;
  129. }
  130. if (!$user) {
  131. $message = 'Nenhum usuário logado para efetuar o envio de uma reclamação.';
  132. $app['session']->getFlashBag()->add('warning', $message);
  133. $redirect = $app['url_generator']->generate('principal');
  134. return $app->redirect($redirect);
  135. }
  136. $reclamacao->setIdu($uid);
  137. $form = $app['form.factory']->create(new ReclamacaoType(), $reclamacao);
  138. if ($request->isMethod('POST')) {
  139. $form->bind($request);
  140. if ($form->isValid()) {
  141. $app['repository.reclamacao']->save($reclamacao);
  142. $aImg = $request->get("imgReclamacao");
  143. //$this->imagemRepository
  144. if(count($aImg)){
  145. foreach($aImg as $File){
  146. $imagem = new Imagem();
  147. $imagem->setFile($File);
  148. $imagem->setIdr($reclamacao->getId());
  149. $app['repository.imagem']->save($imagem);
  150. $app['repository.imagem']->handleFileUpload($File);
  151. }
  152. }
  153. /*
  154. * Enviar email
  155. */
  156. /*
  157. * Pegar id da sessao
  158. */
  159. if($app['token']){
  160. $uid = $app['token']->getUid();
  161. $oUser = $app['repository.user']->find($uid);
  162. $reclamacao = $app['repository.reclamacao']->find($reclamacao->getId());
  163. $body = $app['twig']->render('emailCadastroReclamacao.html.twig',
  164. array(
  165. 'name' => $oUser->getName(),
  166. 'mail' => $oUser->getEmail(),
  167. 'idreclamacao'=>str_pad($reclamacao->getId(), 10, "0", STR_PAD_LEFT),
  168. 'titulo'=>$reclamacao->getTitulo(),
  169. 'reclamacao'=>$reclamacao
  170. ));
  171. $message = \Swift_Message::newInstance()
  172. ->setSubject('[Reclame Imóvel] Parabéns reclamação cadastrada com sucesso. ')
  173. ->setFrom(array('contato@reclameimovel.com.br'=>'Reclame Imóvel'))
  174. ->setTo(array($oUser->getEmail()=>$oUser->getName()))
  175. ->setBody($body)
  176. ->setContentType("text/html");
  177. $app['mailer']->send($message);
  178. }
  179. $message = 'Reclamação salva com sucesso.';
  180. $app['session']->getFlashBag()->add('success', $message);
  181. // Redirect to the edit page.
  182. $redirect = $app['url_generator']->generate('view');
  183. return $app->redirect($redirect."/".$reclamacao->getIde()."/".$reclamacao->getId());
  184. }
  185. return false;
  186. } else {
  187. $nome_empresa = $oEmp->getEmpresa()->getNome();
  188. $nome_emp = $oEmp->getNome();
  189. $bairro = $oEmp->getBairro();
  190. $sub_titulo = $nome_empresa. " - " . $nome_emp . " - " . $bairro;
  191. $data = array(
  192. 'metaDescription' => '',
  193. 'form' => $form->createView(),
  194. 'title' => 'Nova reclamação',
  195. 'sub_titulo' => $sub_titulo,
  196. );
  197. return $app['twig']->render('form.html.twig', $data);
  198. }
  199. }
  200. public function adicionarFotoAction(Request $request, Application $app) {
  201. // Generate filename
  202. $filename = md5(mt_rand()).'.jpg';
  203. // Read RAW data
  204. $data = file_get_contents('php://input');
  205. // Read string as an image file
  206. $image = file_get_contents('data://'.substr($data, 5));
  207. // Save to disk
  208. if ( ! file_put_contents(COND_PUBLIC_ROOT .'/tmp_send_image/'.$filename, $image)) {
  209. header('HTTP/1.1 503 Service Unavailable');
  210. exit();
  211. }
  212. // Clean up memory
  213. unset($data);
  214. unset($image);
  215. // Return file URL
  216. echo $filename;
  217. return false;
  218. }
  219. public function editAction(Request $request, Application $app) {
  220. $reclamacao = $request->attributes->get('reclamacao');
  221. if (!$reclamacao) {
  222. $app->abort(404, 'The requested reclamacao was not found.');
  223. }
  224. $form = $app['form.factory']->create(new ArtistType(), $reclamacao);
  225. if ($request->isMethod('POST')) {
  226. $form->bind($request);
  227. if ($form->isValid()) {
  228. $app['repository.reclamacao']->save($reclamacao);
  229. $message = 'The reclamacao ' . $reclamacao->getName() . ' has been saved.';
  230. $app['session']->getFlashBag()->add('success', $message);
  231. }
  232. }
  233. $data = array(
  234. 'form' => $form->createView(),
  235. 'title' => 'Edit reclamacao ' . $reclamacao->getName(),
  236. );
  237. return $app['twig']->render('form.html.twig', $data);
  238. }
  239. public function deleteAction(Request $request, Application $app) {
  240. $reclamacao = $request->attributes->get('reclamacao');
  241. if (!$reclamacao) {
  242. $app->abort(404, 'The requested reclamacao was not found.');
  243. }
  244. $app['repository.reclamacao']->delete($reclamacao);
  245. return $app->redirect($app['url_generator']->generate('admin_reclamacaos'));
  246. }
  247. }