PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/app/Controllers/adminController.php

https://gitlab.com/higordaniell/SIN-352
PHP | 403 lines | 283 code | 87 blank | 33 comment | 93 complexity | f9245cc9dc38a7bc3b1630c8ce155cfd MD5 | raw file
  1. <?php
  2. class adminController extends Controller {
  3. public function __construct()
  4. {
  5. parent::__construct();
  6. if(!$this->_page->usuario || $this->_page->usuario->getRole() != "admin") {
  7. $this->redirect("erro/e401");
  8. }
  9. }
  10. public function index() {
  11. $this->_page->adminView('admin/index');
  12. }
  13. public function vendas() {
  14. $vt = new VendasTable();
  15. $venda_id = $this->getParam(0);
  16. if($venda_id) {
  17. $venda = $vt->getById($venda_id);
  18. $this->_page->adminView('admin/vendas/detalhes', compact('venda'));
  19. }
  20. else {
  21. $vendas = $vt->getAll();
  22. $this->_page->adminView('admin/vendas/view', compact('vendas'));
  23. }
  24. }
  25. public function clientes() {
  26. $ut = new UsuariosTable();
  27. $usuario_id = $this->getParam(0);
  28. if($usuario_id) {
  29. $usuario = $ut->getById($usuario_id);
  30. $this->_page->adminView('admin/clientes/detalhes', compact('usuario'));
  31. }
  32. else {
  33. $usuarios = $ut->getAll();
  34. $this->_page->adminView('admin/clientes/view', compact('usuarios'));
  35. }
  36. }
  37. /*
  38. *
  39. * LIVROS
  40. *
  41. */
  42. public function livros() {
  43. $action = $this->getParam(0);
  44. $id = $this->getParam(1);
  45. if($action == "add") {
  46. return $this->livros_edit(-1);
  47. }
  48. else if($action == "remove") {
  49. return $this->livros_remove($id);
  50. }
  51. else if($action == "edit") {
  52. return $this->livros_edit($id);
  53. }
  54. else {
  55. $lt = new LivrosTable();
  56. $livros = $lt->getAll();
  57. $this->_page->adminView("admin/livros/view", compact('livros'));
  58. }
  59. }
  60. public function livros_remove($id) {
  61. $lt = new LivrosTable();
  62. $it = new ImagesTable();
  63. //Se o livro existe
  64. if($livro = $lt->getById($id)) {
  65. //Remove a imagem
  66. $it->getById( $livro->getImageId() );
  67. $it->delete();
  68. //Remove o livro
  69. $lt->delete();
  70. }
  71. $this->redirect("admin/livros");
  72. }
  73. public function livros_edit($livro_id) {
  74. $lt = new LivrosTable();
  75. $ct = new CategoriasTable();
  76. $et = new EditorasTable();
  77. $it = new ImagesTable();
  78. $titulo = "";
  79. $erro = "";
  80. $categorias = $ct->getAll();
  81. $editoras = $et->getAll();
  82. if($livro_id < 0) { //Novo livro
  83. $livro = new Livro();
  84. $image = new Image();
  85. $titulo = "Adicionar novo livro";
  86. }
  87. else {
  88. $livro = $lt->getById($livro_id);
  89. $image = $livro->getImage();
  90. $titulo = "Editar livro '{$livro->getTitulo()}'";
  91. }
  92. if($this->isPost()) {
  93. if(!$this->getPost('titulo'))
  94. $erro = "Você não inseriu um titulo!";
  95. if(!$this->getPost('preco'))
  96. $erro = "Você não inseriu um preço!";
  97. if(!$this->getPost('autor'))
  98. $erro = "Você não inseriu um autor!";
  99. if(!$this->getPost('descricao'))
  100. $erro = "Você não inseriu uma descrição!";
  101. if(!$this->getPost('categoria_id'))
  102. $erro = "Você não selecionou uma categoria!";
  103. if(!$this->getPost('editora_id'))
  104. $erro = "Você não selecionou uma editora!";
  105. }
  106. //Se enviou algo, tenta atualizar (ou cadastrar)
  107. if($this->isPost() && $erro == '') {
  108. $livro->setAll($_POST);
  109. $lt->setLivro($livro);
  110. //Se enviou imagem
  111. if(is_uploaded_file( $_FILES["image"]["tmp_name"] ) && $_FILES["image"]["error"] === 0 )
  112. {
  113. $it->setImage($image);
  114. $image->setData( file_get_contents($_FILES["image"]["tmp_name"], 'rb') );
  115. $image->setMime( $_FILES["image"]["type"] );
  116. if($livro_id < 0) { //Novo livro
  117. $it->insert();
  118. } else {
  119. $it->update();
  120. }
  121. $livro->setImageId( $image->getId() );
  122. }
  123. if($livro_id < 0) { //Novo livro
  124. $lt->insert();
  125. } else {
  126. $lt->update();
  127. }
  128. $this->redirect("admin/livros");
  129. }
  130. $this->_page->adminView("admin/livros/edit", compact('livro', 'editoras', 'categorias', 'titulo', 'erro'));
  131. }
  132. /*
  133. *
  134. * Categorias
  135. *
  136. */
  137. public function categorias() {
  138. $action = $this->getParam(0);
  139. $id = $this->getParam(1);
  140. if($action == "add") {
  141. return $this->categorias_edit(-1);
  142. }
  143. else if($action == "remove") {
  144. return $this->categorias_remove($id);
  145. }
  146. else if($action == "edit") {
  147. return $this->categorias_edit($id);
  148. }
  149. else {
  150. $ct = new CategoriasTable();
  151. $categorias = $ct->getAll();
  152. $this->_page->adminView("admin/categorias/view", compact('categorias'));
  153. }
  154. }
  155. public function categorias_remove($id) {
  156. $ct = new CategoriasTable();
  157. //Se a Categoria existe
  158. if($categoria = $ct->getById($id)) {
  159. //Remove a categoria
  160. $ct->delete();
  161. }
  162. $this->redirect("admin/categorias");
  163. }
  164. public function categorias_edit($categoria_id) {
  165. $ct = new CategoriasTable();
  166. $titulo = "";
  167. $erro = "";
  168. if($categoria_id < 0) { //Nova categoria
  169. $categoria = new Categoria();
  170. $titulo = "Adicionar nova categoria";
  171. }
  172. else {
  173. $categoria = $ct->getById($categoria_id);
  174. $titulo = "Editar categoria '{$categoria->getNome()}'";
  175. }
  176. if($this->isPost()) {
  177. if(!$this->getPost('nome'))
  178. $erro = "Você não inseriu um nome!";
  179. }
  180. //Se enviou algo, tenta atualizar (ou cadastrar)
  181. if($this->isPost() && $erro == '') {
  182. $categoria->setAll($_POST);
  183. $ct->setCategoria($categoria);
  184. if($categoria_id < 0) { //Nova categoria
  185. $ct->insert();
  186. } else {
  187. $ct->update();
  188. }
  189. $this->redirect("admin/categorias");
  190. }
  191. $this->_page->adminView("admin/categorias/edit", compact('categoria','titulo', 'erro'));
  192. }
  193. /*
  194. *
  195. * Editoras
  196. *
  197. */
  198. public function editoras() {
  199. $action = $this->getParam(0);
  200. $id = $this->getParam(1);
  201. if($action == "add") {
  202. return $this->editoras_edit(-1);
  203. }
  204. else if($action == "remove") {
  205. return $this->editoras_remove($id);
  206. }
  207. else if($action == "edit") {
  208. return $this->editoras_edit($id);
  209. }
  210. else {
  211. $et = new EditorasTable();
  212. $editoras = $et->getAll();
  213. $this->_page->adminView("admin/editoras/view", compact('editoras'));
  214. }
  215. }
  216. public function editoras_remove($id) {
  217. $et = new EditorasTable();
  218. //Se a Editora existe
  219. if($editora = $et->getById($id)) {
  220. $et->delete();
  221. }
  222. $this->redirect("admin/editoras");
  223. }
  224. public function editoras_edit($id) {
  225. $et = new EditorasTable();
  226. $titulo = "";
  227. $erro = "";
  228. if($id < 0) { //Nova Editora
  229. $editora = new Editora();
  230. $titulo = "Adicionar nova editora";
  231. }
  232. else {
  233. $editora = $et->getById($id);
  234. $titulo = "Editar editora '{$editora->getNome()}'";
  235. }
  236. if($this->isPost()) {
  237. if(!$this->getPost('nome'))
  238. $erro = "Você não inseriu um nome!";
  239. }
  240. //Se enviou algo, tenta atualizar (ou cadastrar)
  241. if($this->isPost() && $erro == '') {
  242. $editora->setAll($_POST);
  243. $et->setEditora($editora);
  244. if($id < 0) { //Nova
  245. $et->insert();
  246. } else {
  247. $et->update();
  248. }
  249. $this->redirect("admin/editoras");
  250. }
  251. $this->_page->adminView("admin/editoras/edit", compact('editora','titulo', 'erro'));
  252. }
  253. /*
  254. *
  255. * Atendimentos
  256. *
  257. */
  258. public function atendimentos() {
  259. $action = $this->getParam(0);
  260. $id = $this->getParam(1);
  261. if($action == "add") {
  262. return $this->atendimentos_edit(-1);
  263. }
  264. else if($action == "remove") {
  265. return $this->atendimentos_remove($id);
  266. }
  267. else if($action == "edit") {
  268. return $this->atendimentos_edit($id);
  269. }
  270. else {
  271. $at = new AtendimentosTable();
  272. $atendimentos = $at->getAll();
  273. $this->_page->adminView("admin/atendimentos/view", compact('atendimentos'));
  274. }
  275. }
  276. public function atendimentos_remove($id) {
  277. $at = new AtendimentosTable();
  278. //Se o atendimento existe
  279. if($atendimento = $at->getById($id)) {
  280. //Remove o atendimento
  281. $at->delete();
  282. }
  283. $this->redirect("admin/atendimentos");
  284. }
  285. public function atendimentos_edit($atendimento_id) {
  286. $at = new AtendimentosTable();
  287. $titulo = "";
  288. $erro = '';
  289. if($atendimento_id < 0) { //Novo Atendimento
  290. $atendimento = new Atendimento();
  291. $titulo = "Adicionar novo Atendimento";
  292. }
  293. else {
  294. $atendimento = $at->getById($atendimento_id);
  295. $titulo = "Editar atendimento '{$atendimento->getPergunta()}'";
  296. }
  297. if($this->isPost()) {
  298. if(!$this->getPost('pergunta'))
  299. $erro = "Você não inseriu uma pergunta!";
  300. if(!$this->getPost('resposta'))
  301. $erro = "Você não inseriu uma resposta!";
  302. }
  303. //Se enviou algo, tenta atualizar (ou cadastrar)
  304. if($this->isPost() && $erro == '') {
  305. $atendimento->setAll($_POST);
  306. $at->setAtendimento($atendimento);
  307. if($atendimento_id < 0) { //Novo atendimento
  308. $at->insert();
  309. } else {
  310. $at->update();
  311. }
  312. $this->redirect("admin/atendimentos");
  313. }
  314. $this->_page->adminView("admin/atendimentos/edit", compact('atendimento','titulo', 'erro'));
  315. }
  316. }