/src/Condominio/Repository/ReclamacaoRepository.php

https://gitlab.com/fabiorf/reclameimovel · PHP · 298 lines · 193 code · 40 blank · 65 comment · 9 complexity · 64315bdf78e1933a7bf73026cfe39077 MD5 · raw file

  1. <?php
  2. namespace Condominio\Repository;
  3. use Doctrine\DBAL\Connection;
  4. use Condominio\Entity\Reclamacao;
  5. /**
  6. * Reclamacao repository
  7. */
  8. class ReclamacaoRepository implements RepositoryInterface
  9. {
  10. /**
  11. * @var \Doctrine\DBAL\Connection
  12. */
  13. protected $db;
  14. /**
  15. * @var \Condominio\Repository\EmpreendimentoRepository
  16. */
  17. protected $empreendimentoRepository;
  18. protected $imagemRepository;
  19. protected $userRepository;
  20. public function __construct(Connection $db,$empreendimentoRepository,$imagemRepository,$userRepository)
  21. {
  22. $this->db = $db;
  23. $this->empreendimentoRepository = $empreendimentoRepository;
  24. $this->imagemRepository = $imagemRepository;
  25. $this->userRepository = $userRepository;
  26. }
  27. /**
  28. * Saves the reclamacao to the database.
  29. *
  30. * @param \Condominio\Entity\Reclamacao $reclamacao
  31. */
  32. public function save($reclamacao)
  33. {
  34. $reclamacaoData = array(
  35. 'idu' => $reclamacao->getIdu(),
  36. 'ide' => $reclamacao->getIde(),
  37. 'titulo' => $reclamacao->getTitulo(),
  38. 'dados' => $reclamacao->getDados(),
  39. 'idassunto' => $reclamacao->getIdassunto(),
  40. 'descricao' => $reclamacao->getDescricao(),
  41. 'youtube' => $reclamacao->getYoutube(),
  42. 'dt_cadastro'=>date('Y-m-d H:i:s')
  43. );
  44. if ($reclamacao->getId()) {
  45. $this->db->update('reclamacao', $reclamacaoData, array('id' => $reclamacao->getId()));
  46. }else {
  47. $this->db->insert('reclamacao', $reclamacaoData);
  48. $id = $this->db->lastInsertId();
  49. $reclamacao->setId($id);
  50. }
  51. }
  52. public function updateVisita($id)
  53. {
  54. $oRec = $this->find($id);
  55. $visita = $oRec->getVisita() + 1;
  56. $this->db->update('reclamacao', array('visita'=>$visita), array('id' => $id));
  57. }
  58. /**
  59. * Deletes the reclamacao.
  60. *
  61. * @param \Condominio\Entity\Reclamacao $reclamacao
  62. */
  63. public function delete($reclamacao)
  64. {
  65. // If the reclamacao had an image, delete it.
  66. $image = $reclamacao->getImage();
  67. if ($image) {
  68. unlink('images/reclamacao/' . $image);
  69. }
  70. return $this->db->delete('reclamacao', array('id' => $reclamacao->getId()));
  71. }
  72. /**
  73. * Returns the total number of reclamacao.
  74. *
  75. * @return integer The total number of reclamacao.
  76. */
  77. public function getCount() {
  78. return $this->db->fetchColumn('SELECT COUNT(id) FROM reclamacao');
  79. }
  80. /**
  81. * Returns the total number of reclamacao.
  82. *
  83. * @return integer The total number of reclamacao.
  84. */
  85. public function getCountSolucao($ide) {
  86. return $this->db->fetchColumn("SELECT COUNT(id) FROM reclamacao where ide = '$ide' and solucao=1");
  87. }
  88. public function getCountUsuario($idu) {
  89. return $this->db->fetchColumn("SELECT COUNT(id) FROM reclamacao where idu = '$idu'");
  90. }
  91. public function getCountReclamacao($ide) {
  92. return $this->db->fetchColumn("SELECT COUNT(id) FROM reclamacao where ide = '$ide' ");
  93. }
  94. /**
  95. * Returns an reclamacao matching the supplied id.
  96. *
  97. * @param integer $id
  98. *
  99. * @return \Condominio\Entity\Reclamacao|false An entity object if found, false otherwise.
  100. */
  101. public function find($id)
  102. {
  103. if($id == ""){
  104. return false;
  105. }
  106. $queryBuilder = $this->db->createQueryBuilder();
  107. $queryBuilder
  108. ->select('r.id,r.solucao,r.idu,r.ide,r.titulo,r.descricao,r.idassunto,r.dados,r.dt_cadastro,r.visita,r.youtube,emp.cidade,emp.uf,e.nome as nome')
  109. ->from('reclamacao', 'r')
  110. ->innerJoin('r',"empreendimento","emp","emp.id = r.ide")
  111. ->innerJoin('emp',"empresa","e","e.id = emp.ide")
  112. ->where("r.id = $id");
  113. $statement = $queryBuilder->execute();
  114. $reclamacaoData = $statement->fetch();
  115. return $reclamacaoData ? $this->buildReclamacao($reclamacaoData) : FALSE;
  116. }
  117. public function findRand()
  118. {
  119. $queryBuilder = $this->db->createQueryBuilder();
  120. $queryBuilder
  121. ->select('r.id,r.solucao,r.idu,r.ide,r.titulo,r.descricao,r.idassunto,r.dados,r.dt_cadastro,r.visita,r.youtube,emp.cidade,emp.uf,e.nome as nome')
  122. ->from('reclamacao', 'r')
  123. ->innerJoin('r',"empreendimento","emp","emp.id = r.ide")
  124. ->innerJoin('emp',"empresa","e","e.id = emp.ide")
  125. ->orderBy("RAND()");
  126. $queryBuilder->setMaxResults(1)
  127. ->setFirstResult(0);
  128. $statement = $queryBuilder->execute();
  129. $reclamacaoData = $statement->fetch();
  130. return $reclamacaoData ? $this->buildReclamacao($reclamacaoData) : FALSE;
  131. }
  132. /**
  133. * Returns a collection of reclamacao, sorted by name.
  134. *
  135. * @param integer $limit
  136. * The number of reclamacao to return.
  137. * @param integer $offset
  138. * The number of reclamacao to skip.
  139. * @param array $orderBy
  140. * Optionally, the order by info, in the $column => $direction format.
  141. *
  142. * @return array A collection of reclamacao, keyed by reclamacao id.
  143. */
  144. public function findAll($limit, $offset = 0, $orderBy = array())
  145. {
  146. // Provide a default orderBy.
  147. if (!$orderBy) {
  148. $orderBy = array('r.dt_cadastro' => 'DESC');
  149. }
  150. $queryBuilder = $this->db->createQueryBuilder();
  151. $queryBuilder
  152. ->select('r.id,r.solucao,r.idu,r.ide,r.titulo,r.descricao,r.idassunto,r.dados,r.dt_cadastro,r.visita,r.youtube,emp.idnome,emp.cidade,emp.uf,e.nome as nome')
  153. ->from('reclamacao', 'r')
  154. ->innerJoin('r',"empreendimento","emp","emp.id = r.ide")
  155. ->innerJoin('emp',"empresa","e","e.id = emp.ide");
  156. $queryBuilder->setMaxResults($limit)
  157. ->setFirstResult($offset)
  158. ->orderBy( key($orderBy), current($orderBy));
  159. $statement = $queryBuilder->execute();
  160. $reclamacaoData = $statement->fetchAll();
  161. $reclamacao = array();
  162. foreach ($reclamacaoData as $reclamacaoData) {
  163. $reclamacaoId = $reclamacaoData['id'];
  164. $reclamacao[$reclamacaoId] = $this->buildReclamacao($reclamacaoData);
  165. }
  166. return $reclamacao;
  167. }
  168. /**
  169. * Returns a collection of reclamacao, sorted by name.
  170. *
  171. * @return array A collection of reclamacao, keyed by reclamacao id.
  172. */
  173. public function findReclamacaoEmpreendimento($limit, $offset = 0, $orderBy = array(),$ide=null)
  174. {
  175. // Provide a default orderBy.
  176. if (!$orderBy) {
  177. $orderBy = array('r.dt_cadastro' => 'DESC');
  178. }
  179. $queryBuilder = $this->db->createQueryBuilder();
  180. $queryBuilder
  181. ->select('r.id,r.solucao,r.idu,r.ide,r.titulo,r.descricao,r.idassunto,r.dados,r.dt_cadastro,r.visita,r.youtube,emp.idnome,emp.cidade,emp.uf,e.nome as nome')
  182. ->from('reclamacao', 'r')
  183. ->innerJoin('r',"empreendimento","emp","emp.id = r.ide")
  184. ->innerJoin('emp',"empresa","e","e.id = emp.ide");
  185. $queryBuilder->setMaxResults($limit)
  186. ->setFirstResult($offset)
  187. ->orderBy( key($orderBy), current($orderBy));
  188. if($ide){
  189. $queryBuilder->where("r.ide = $ide");
  190. }
  191. $statement = $queryBuilder->execute();
  192. $reclamacaoData = $statement->fetchAll();
  193. $reclamacao = array();
  194. foreach ($reclamacaoData as $reclamacaoData) {
  195. $reclamacaoId = $reclamacaoData['id'];
  196. $reclamacao[$reclamacaoId] = $this->buildReclamacao($reclamacaoData);
  197. }
  198. return $reclamacao;
  199. }
  200. public function findReclamacaoUsuario($limit, $offset = 0, $orderBy = array(),$idu=null)
  201. {
  202. // Provide a default orderBy.
  203. if (!$orderBy) {
  204. $orderBy = array('r.dt_cadastro' => 'DESC');
  205. }
  206. $queryBuilder = $this->db->createQueryBuilder();
  207. $queryBuilder
  208. ->select('r.id,r.solucao,r.idu,r.ide,r.titulo,r.descricao,r.idassunto,r.dados,r.dt_cadastro,r.visita,r.youtube,emp.idnome,emp.cidade,emp.uf,e.nome as nome')
  209. ->from('reclamacao', 'r')
  210. ->innerJoin('r',"empreendimento","emp","emp.id = r.ide")
  211. ->innerJoin('emp',"empresa","e","e.id = emp.ide");
  212. $queryBuilder->setMaxResults($limit)
  213. ->setFirstResult($offset)
  214. ->orderBy( key($orderBy), current($orderBy));
  215. if($idu){
  216. $queryBuilder->where("r.idu = $idu");
  217. }
  218. $statement = $queryBuilder->execute();
  219. $reclamacaoData = $statement->fetchAll();
  220. $reclamacao = array();
  221. foreach ($reclamacaoData as $reclamacaoData) {
  222. $reclamacaoId = $reclamacaoData['id'];
  223. $reclamacao[$reclamacaoId] = $this->buildReclamacao($reclamacaoData);
  224. }
  225. return $reclamacao;
  226. }
  227. /**
  228. * Instantiates an reclamacao entity and sets its properties using db data.
  229. *
  230. * @param array $reclamacaoData
  231. * The array of db data.
  232. *
  233. * @return \Condominio\Entity\Reclamacao
  234. */
  235. protected function buildReclamacao($reclamacaoData)
  236. {
  237. $empreendimento = $this->empreendimentoRepository->find($reclamacaoData['ide']);
  238. $collectionImagem = $this->imagemRepository->findAllByReclamacao($reclamacaoData['id']);
  239. $collectionUser = $this->userRepository->find($reclamacaoData['idu']);
  240. $reclamacao = new Reclamacao();
  241. $reclamacao->setId($reclamacaoData['id']);
  242. $reclamacao->setIdu($reclamacaoData['idu']);
  243. $reclamacao->setIde($reclamacaoData['ide']);
  244. $reclamacao->setVisita($reclamacaoData['visita']);
  245. $reclamacao->setTitulo($reclamacaoData['titulo']);
  246. $reclamacao->setDescricao($reclamacaoData['descricao']);
  247. $reclamacao->setDados($reclamacaoData['dados']);
  248. $reclamacao->setIdassunto($reclamacaoData['idassunto']);
  249. $reclamacao->setYoutube($reclamacaoData['youtube']);
  250. $reclamacao->setSolucao($reclamacaoData['solucao']);
  251. $createdAt = new \DateTime($reclamacaoData['dt_cadastro']);
  252. $reclamacao->setDt_cadastro($createdAt);
  253. $reclamacao->setEmpreendimento($empreendimento);
  254. $reclamacao->setImagem($collectionImagem);
  255. $reclamacao->setUser($collectionUser);
  256. return $reclamacao;
  257. }
  258. }