PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/apps/Manager/controllers/ProjectCategoriesController.php

https://gitlab.com/HudsonNicoletti/Rhyno
PHP | 328 lines | 265 code | 57 blank | 6 comment | 22 complexity | f358319b4f852ce3b429dfcd1befda79 MD5 | raw file
  1. <?php
  2. namespace Manager\Controllers;
  3. use Manager\Controllers\RhynoException;
  4. use Manager\Models\Logs as Logs,
  5. Manager\Models\ProjectTypes as ProjectTypes,
  6. Manager\Models\Projects as Projects;
  7. use Mustache_Engine as Mustache;
  8. use Phalcon\Forms\Form,
  9. Phalcon\Forms\Element\Text,
  10. Phalcon\Forms\Element\Textarea,
  11. Phalcon\Forms\Element\Password,
  12. Phalcon\Forms\Element\Select,
  13. Phalcon\Forms\Element\File,
  14. Phalcon\Forms\Element\Hidden;
  15. class ProjectCategoriesController extends ControllerBase
  16. {
  17. public function IndexAction()
  18. {
  19. $this->permissionHandler("admin");
  20. $categories = ProjectTypes::query()
  21. ->columns([
  22. 'Manager\Models\ProjectTypes._',
  23. 'Manager\Models\ProjectTypes.title',
  24. "(SELECT COUNT(Manager\Models\Projects.type) FROM Manager\Models\Projects WHERE Manager\Models\Projects.type = Manager\Models\ProjectTypes._) as assigned"
  25. ])
  26. ->leftJoin('Manager\Models\Projects', 'Manager\Models\ProjectTypes._ = Manager\Models\Projects.type')
  27. ->groupBy('Manager\Models\ProjectTypes._')
  28. ->orderBy('Manager\Models\ProjectTypes.title')
  29. ->execute();
  30. $this->view->categories = $categories;
  31. $this->view->pick("projects/admin/categories");
  32. }
  33. public function NewAction()
  34. {
  35. $this->response->setContentType("application/json");
  36. $title = $this->request->getPost("title","string");
  37. try
  38. {
  39. if(!$this->request->isPost() || !$this->request->isAjax()):
  40. return RhynoException::InvalidRequestMethod();
  41. elseif(!$title):
  42. return RhynoException::EmptyInput("title");
  43. elseif(!$this->security->checkToken()):
  44. return RhynoException::InvalidCsrfToken();
  45. endif;
  46. }
  47. catch (\Exception $e)
  48. {
  49. $this->flags['status'] = false ;
  50. $this->flags['toast'] = "error";
  51. $this->flags['title'] = $e->getMessage();
  52. }
  53. if($this->flags['status']):
  54. try
  55. {
  56. $category = new ProjectTypes;
  57. $category->title = $title;
  58. if(!$category->save())
  59. {
  60. return RhynoException::DBError();
  61. }
  62. $this->flags['toast'] = "success";
  63. $this->flags['title'] = "Project Category successfully created!";
  64. $this->flags['redirect'] = "{$this->rhyno_url}/project/categories";
  65. }
  66. catch(\Exception $e)
  67. {
  68. $this->flags['toast'] = "warning";
  69. $this->flags['title'] = $e->getMessage();
  70. }
  71. endif;
  72. return $this->response->setJsonContent([
  73. "toast" => $this->flags['toast'],
  74. "title" => $this->flags['title'],
  75. "redirect" => $this->flags['redirect'],
  76. "time" => $this->flags['time']
  77. ]);
  78. $this->response->send();
  79. $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
  80. }
  81. public function UpdateAction()
  82. {
  83. $this->response->setContentType("application/json");
  84. $id = $this->dispatcher->getParam("category","int");
  85. $title = $this->request->getPost("title","string");
  86. try
  87. {
  88. if(!$this->request->isPost() || !$this->request->isAjax()):
  89. return RhynoException::InvalidRequestMethod();
  90. elseif(!$id):
  91. return RhynoException::WrongNumberOfParams();
  92. elseif(!$title):
  93. return RhynoException::EmptyInput("title");
  94. elseif(!$this->security->checkToken()):
  95. return RhynoException::InvalidCsrfToken();
  96. endif;
  97. }
  98. catch (\Exception $e)
  99. {
  100. $this->flags['status'] = false ;
  101. $this->flags['toast'] = "error";
  102. $this->flags['title'] = $e->getMessage();
  103. }
  104. if($this->flags['status']):
  105. try
  106. {
  107. $category = ProjectTypes::findFirst($id);
  108. $category->title = $title;
  109. if(!$category->save())
  110. {
  111. return RhynoException::DBError();
  112. }
  113. $this->flags['toast'] = "success";
  114. $this->flags['title'] = "Project Category successfully updated!";
  115. $this->flags['redirect'] = "{$this->rhyno_url}/project/categories";
  116. }
  117. catch(\Exception $e)
  118. {
  119. $this->flags['toast'] = "warning";
  120. $this->flags['title'] = $e->getMessage();
  121. }
  122. endif;
  123. return $this->response->setJsonContent([
  124. "toast" => $this->flags['toast'],
  125. "title" => $this->flags['title'],
  126. "redirect" => $this->flags['redirect'],
  127. "time" => $this->flags['time']
  128. ]);
  129. $this->response->send();
  130. $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
  131. }
  132. public function DeleteAction()
  133. {
  134. $this->response->setContentType("application/json");
  135. $id = $this->dispatcher->getParam("category","int");
  136. try
  137. {
  138. if(!$this->request->isPost() || !$this->request->isAjax()):
  139. return RhynoException::InvalidRequestMethod();
  140. elseif(!$id):
  141. return RhynoException::WrongNumberOfParams();
  142. elseif(!$this->security->checkToken()):
  143. return RhynoException::InvalidCsrfToken();
  144. endif;
  145. }
  146. catch (\Exception $e)
  147. {
  148. $this->flags['status'] = false ;
  149. $this->flags['toast'] = "error";
  150. $this->flags['title'] = $e->getMessage();
  151. }
  152. if($this->flags['status']):
  153. try
  154. {
  155. $category = ProjectTypes::findFirst($id);
  156. if(!$category->delete())
  157. {
  158. return RhynoException::Unreachable();
  159. }
  160. foreach (Projects::findByType($id) as $project)
  161. {
  162. $project->type = $this->request->getPost("category","int");
  163. $project->save();
  164. }
  165. $this->flags['toast'] = "success";
  166. $this->flags['title'] = "Project Category successfully removed!";
  167. $this->flags['redirect'] = "{$this->rhyno_url}/project/categories";
  168. }
  169. catch(\Exception $e)
  170. {
  171. $this->flags['toast'] = "warning";
  172. $this->flags['title'] = $e->getMessage();
  173. }
  174. endif;
  175. return $this->response->setJsonContent([
  176. "toast" => $this->flags['toast'],
  177. "title" => $this->flags['title'],
  178. "redirect" => $this->flags['redirect'],
  179. "time" => $this->flags['time']
  180. ]);
  181. $this->response->send();
  182. $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
  183. }
  184. public function ModalAction()
  185. {
  186. $this->response->setContentType("application/json");
  187. if(!$this->request->isGet()):
  188. $this->flags['status'] = false ;
  189. $this->flags['toast'] = "error";
  190. $this->flags['title'] = "Invalid Method.";
  191. endif;
  192. if($this->flags['status']):
  193. $form = new Form();
  194. $inputs = [];
  195. $uid = $this->dispatcher->getParam("category","int");
  196. $method = $this->dispatcher->getParam("method","string");
  197. if($uid)
  198. {
  199. $category = ProjectTypes::findFirst($uid);
  200. }
  201. if($method == "remove"):
  202. $element['category'] = new Select( "category" , ProjectTypes::find(["_ != '{$uid}'"]) ,[
  203. 'using' => ["_","title"],
  204. 'label' => "Client",
  205. ]);
  206. else:
  207. # CREATING ELEMENTS
  208. $element['title'] = new Text( "title" ,[
  209. 'label' => "Category Title",
  210. ]);
  211. endif;
  212. $element['security'] = new Hidden( "security" ,[
  213. 'name' => $this->security->getTokenKey(),
  214. 'value' => $this->security->getToken(),
  215. ]);
  216. # IF REQUEST IS TO CREATE JUST POPULATE WITH DEFAULT ELEMENTS
  217. if( $method == "create" ):
  218. $modal_form = true;
  219. $modal_action = "{$this->rhyno_url}/project/categories/new";
  220. $modal_header = "Create a new project category!";
  221. foreach($element as $e)
  222. {
  223. $form->add($e);
  224. }
  225. # IF REQUEST IS TO UPDATE POPULATE WITH VALJUE TO ELEMENT
  226. elseif ($method == "modify"):
  227. $modal_form = true;
  228. $modal_action = "{$this->rhyno_url}/project/categories/update/{$uid}";
  229. $modal_header = "Update project category information!";
  230. $element['title']->setAttribute("active",true)->setAttribute("value",$category->title);
  231. foreach($element as $e)
  232. {
  233. $form->add($e);
  234. }
  235. # IF REQUEST IS TO DELETE POPULATE WITH VALJUE TO ELEMENT
  236. elseif ($method == "remove"):
  237. $modal_form = true;
  238. $modal_action = "{$this->rhyno_url}/project/categories/delete/{$uid}";
  239. $modal_header = "Remove Project Category!";
  240. $modal_card = [
  241. "content" => "Are you sure? Please select a category to transfer all projects associated with this category."
  242. ];
  243. foreach($element as $e)
  244. {
  245. $form->add($e);
  246. }
  247. endif;
  248. # POPULATE ARRAY WITH TITLE AND INPUTS FOR RENDERING
  249. foreach($form as $f)
  250. {
  251. array_push($inputs,[ "label" => $f->getAttribute("label") , "input" => $f->render($f->getName()) , "file" => $f->getAttribute("file") , "active" => $f->getAttribute("active") , "cl" => $f->getAttribute("cl") ]);
  252. }
  253. # RENDER
  254. $body = (new Mustache)->render(file_get_contents(__DIR__."/../../../templates/modal.tpl"),[
  255. "form" => $modal_form,
  256. "header" => $modal_header,
  257. "action" => $modal_action,
  258. "card" => $modal_card,
  259. "info" => $modal_info,
  260. "bottom" => $modal_bottom,
  261. "inputs" => $inputs
  262. ]);
  263. endif;
  264. return $this->response->setJsonContent([
  265. "status" => $this->flags['status'],
  266. "data" => $body
  267. ]);
  268. $this->response->send();
  269. $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
  270. }
  271. }