/app/Controller/PerguntaRespostasController.php

https://github.com/decapattack/fishbox · PHP · 186 lines · 140 code · 27 blank · 19 comment · 22 complexity · bb61c2ec0367c4f1d43cea2189d81b99 MD5 · raw file

  1. <?php
  2. class PerguntaRespostasController extends AppController {
  3. public $components = array('Session','Paginator');
  4. var $uses = array('CategoriaPergunta', 'Resposta', 'PerguntaResposta');
  5. public $paginate = array(
  6. 'limit' => 10,
  7. 'order' => array(
  8. 'PerguntaResposta.nome' => 'asc'
  9. )
  10. );
  11. /*
  12. * Index
  13. */
  14. public function index() {
  15. $this->loadModel('CategoriaPergunta');
  16. $options = $this->CategoriaPergunta->find('list', array(
  17. 'fields' => array(
  18. 'id',
  19. 'nome'
  20. )
  21. ));
  22. // similar to findAll(), but fetches paged results
  23. $this->Paginator->settings = $this->paginate;
  24. $data = $this->Paginator->paginate('PerguntaResposta');
  25. $this->set('categoriaPerguntas',$options);
  26. $this->set('perguntas', $data);
  27. }
  28. /*
  29. * Tela de edição
  30. * @param id
  31. */
  32. public function view($id) {
  33. $perguntaResposta = $this->PerguntaResposta->findById($id);
  34. $this->loadModel('CategoriaPergunta');
  35. $options = $this->CategoriaPergunta->find('list', array(
  36. 'fields' => array(
  37. 'id',
  38. 'nome'
  39. )
  40. ));
  41. $this->loadModel('Resposta');
  42. $respostas = $this->Resposta->find('all',array(
  43. 'conditions' => array('Resposta.pergunta_respostas_id' => $id)
  44. ));
  45. if (!$id) {
  46. throw new NotFoundException(__('Pergunta Inválida'));
  47. }
  48. if (!$perguntaResposta) {
  49. throw new NotFoundException(__('Pergunta Inválida'));
  50. }
  51. $this->set('categoriaPerguntas',$options);
  52. $this->set('respostas', $respostas);
  53. $this->set('perguntaResposta', $perguntaResposta);
  54. }
  55. /*
  56. * Adiciona Perguntas e Respostas
  57. *
  58. */
  59. public function add() {
  60. $this->loadModel('CategoriaPergunta');
  61. $options = $this->CategoriaPergunta->find('list', array(
  62. 'fields' => array(
  63. 'id',
  64. 'nome'
  65. )
  66. ));
  67. $this->set('categoriaPerguntas', $options);
  68. if ($this->request->is('post')) {
  69. $this->PerguntaResposta->create();
  70. if($this->PerguntaResposta->saveAll($this->request->data,array("deep"=>true))){
  71. $this->Session->setFlash(__('Gravado com sucesso.'));
  72. }else{
  73. $this->Session->setFlash(__('Erro ao gravar o premio.'));
  74. }
  75. return $this->redirect(array('action' => 'add'));
  76. }
  77. }
  78. /*
  79. * Edicao de premio
  80. * @param id
  81. */
  82. public function edit($id = null) {
  83. if($this->Auth->user('role')!='admin'){
  84. $this->Session->setFlash("Sem permissão!");
  85. return $this->redirect(array('action'=>'index'));
  86. }
  87. if ($this->request->is(array('post', 'put'))) {
  88. $this->loadModel('Resposta');
  89. $this->Resposta->query("DELETE FROM respostas WHERE pergunta_respostas_id = ".$id);
  90. $this->PerguntaResposta->id = $id;
  91. if ($this->PerguntaResposta->saveAll($this->request->data)) {
  92. $this->Session->setFlash("Editado com sucesso!");
  93. return $this->redirect(array('action' => 'index'));
  94. }
  95. }
  96. $perguntaResposta = $this->PerguntaResposta->findById($id);
  97. $this->loadModel('CategoriaPergunta');
  98. $options = $this->CategoriaPergunta->find('list', array(
  99. 'fields' => array(
  100. 'id',
  101. 'nome'
  102. )
  103. ));
  104. $this->loadModel('Resposta');
  105. $respostas = $this->Resposta->find('all',array(
  106. 'conditions' => array('Resposta.pergunta_respostas_id' => $id)
  107. ));
  108. $this->set('categoriaPerguntas',$options);
  109. $this->set('respostas', $respostas);
  110. $this->set('perguntaResposta', $perguntaResposta);
  111. /*if ($this->request->data) {
  112. $this->request->data = $perguntaResposta;
  113. }*/
  114. }
  115. public function delete($id = null) {
  116. if($this->Auth->user('role')!='admin'){
  117. $this->Session->setFlash("Sem permissão!");
  118. return $this->redirect(array('action'=>'index'));
  119. }
  120. $this->request->onlyAllow('post');
  121. $this->PerguntaResposta->id = $id;
  122. if (!$this->PerguntaResposta->exists()) {
  123. throw new NotFoundException(__('Pergunta não encontrada.'));
  124. }
  125. if ($this->PerguntaResposta->delete()) {
  126. $this->Session->setFlash(__('Pergunta deletada.'));
  127. return $this->redirect(array('action' => 'index'));
  128. }
  129. $this->Session->setFlash(__('Erro ao deletar pergunta.'));
  130. return $this->redirect(array('action' => 'index'));
  131. }
  132. public function search() {
  133. $this->Paginator->settings = $this->paginate;
  134. $this->loadModel('CategoriaPergunta');
  135. $options = $this->CategoriaPergunta->find('list', array(
  136. 'fields' => array(
  137. 'id',
  138. 'nome'
  139. )
  140. ));
  141. $nome = $this->request->query['nome'];
  142. $categoria = $this->request->query['categoria_perguntas_id'];
  143. $conditions = array();
  144. if($nome != "" && $categoria == ""){
  145. $conditions = array(array('PerguntaResposta.nome LIKE' => "%$nome%"));
  146. }else if($nome == "" && $categoria != ""){
  147. $conditions = array('PerguntaResposta.categoria_perguntas_id =' => $categoria);
  148. }else if($nome != "" && $categoria != ""){
  149. $conditions = array('AND' => array(
  150. array('PerguntaResposta.nome LIKE' => "%$nome%"),
  151. array('PerguntaResposta.categoria_perguntas_id =' => $categoria)
  152. ));
  153. }
  154. $data = $this->Paginator->paginate('PerguntaResposta', $conditions);
  155. $this->set('categoriaPerguntas',$options);
  156. $this->set('perguntas', $data);
  157. }
  158. }
  159. ?>