Controller.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace App;
  3. use App\Model\Student;
  4. use App\Model\Languages;
  5. use App\Repository\LanguagesRepository;
  6. use App\Repository\StudentLanguagesRepository;
  7. use App\Repository\StudentRepository;
  8. class Controller
  9. {
  10. protected $action;
  11. public function __construct($action)
  12. {
  13. $this->action = $action;
  14. }
  15. public function execute()
  16. {
  17. switch ($this->action) {
  18. case '':
  19. case 'main':
  20. $this->mainAction();
  21. break;
  22. case 'index-student':
  23. $this->indexStudentAction();
  24. break;
  25. case 'create-student':
  26. $this->createStudentAction();
  27. break;
  28. case 'view-student':
  29. $this->viewStudentAction();
  30. break;
  31. case 'update-student':
  32. $this->updateStudentAction();
  33. break;
  34. case 'delete-student':
  35. $this->deleteStudentAction();
  36. break;
  37. case 'index-languages':
  38. $this->indexLanguagesAction();
  39. break;
  40. case 'delete-all-languages':
  41. $this->deleteAllLanguagesAction();
  42. break;
  43. case 'create-languages':
  44. $this->createLanguagesAction();
  45. break;
  46. case 'view-languages':
  47. $this->viewLanguagesAction();
  48. break;
  49. case 'update-languages':
  50. $this->updateLanguagesAction();
  51. break;
  52. case 'delete-languages':
  53. $this->deleteLanguagesAction();
  54. break;
  55. default:
  56. $this->errorAction();
  57. }
  58. }
  59. protected function mainAction()
  60. {
  61. require_once('view/main.php');
  62. }
  63. protected function errorAction()
  64. {
  65. require_once('view/error.php');
  66. exit();
  67. }
  68. protected function indexStudentAction()
  69. {
  70. $studentRepository = new StudentRepository();
  71. $students = $studentRepository->findAll();
  72. require_once('view/student/index.php');
  73. }
  74. protected function viewStudentAction()
  75. {
  76. $id = $this->getId();
  77. $studentRepository = new StudentRepository();
  78. $student = $studentRepository->findById($id);
  79. if (!$student) {
  80. $this->errorAction();
  81. }
  82. require_once('view/student/view.php');
  83. }
  84. protected function updateStudentAction()
  85. {
  86. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  87. $id = $this->getId();
  88. $studentRepository = new StudentRepository();
  89. $student = $studentRepository->findById($id);
  90. if (!$student) {
  91. $this->errorAction();
  92. }
  93. $action = 'update';
  94. require_once('view/student/form.php');
  95. return;
  96. }
  97. $firstName = isset($_POST['firstName']) ? trim($_POST['firstName']) : null;
  98. $lastName = isset($_POST['lastName']) ? trim($_POST['lastName']) : null;
  99. $class = isset($_POST['class']) ? trim($_POST['class']) : null;
  100. $id = isset($_POST['id']) ? trim($_POST['id']) : null;
  101. $student = new Student($firstName, $lastName, $class, $id);
  102. if ($student->validate()) {
  103. $studentRepository = new StudentRepository();
  104. $studentRepository->update($student);
  105. header('Location: index.php?action=index-student');
  106. return;
  107. }
  108. $this->errorAction();
  109. }
  110. protected function createStudentAction()
  111. {
  112. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  113. $action = 'create';
  114. require_once('view/student/form.php');
  115. return;
  116. }
  117. $firstName = isset($_POST['firstName']) ? trim($_POST['firstName']) : null;
  118. $lastName = isset($_POST['lastName']) ? trim($_POST['lastName']) : null;
  119. $class = isset($_POST['class']) ? trim($_POST['class']) : null;
  120. $student = new Student($firstName, $lastName, $class);
  121. if ($student->validate()) {
  122. $studentRepository = new StudentRepository();
  123. $studentRepository->save($student);
  124. }
  125. header('Location: index.php?action=index-student');
  126. }
  127. protected function deleteStudentAction()
  128. {
  129. $id = $this->getId();
  130. $studentRepository = new StudentRepository();
  131. $studentRepository->delete($id);
  132. header('Location: index.php?action=index-student');
  133. }
  134. protected function indexLanguagesAction()
  135. {
  136. $languagesRepository = new LanguagesRepository();
  137. $languages = $languagesRepository->findAll();
  138. require_once('view/languages/index.php');
  139. }
  140. protected function viewLanguagesAction()
  141. {
  142. $id = $this->getId();
  143. $languagesRepository = new StudentRepository();
  144. $languages = $languagesRepository->findById($id);
  145. if (!$languages) {
  146. $this->errorAction();
  147. }
  148. require_once('view/languages/view.php');
  149. }
  150. protected function updateLanguageAction()
  151. {
  152. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  153. $id = $this->getId();
  154. $languagesRepository = new LanguagesRepository();
  155. $languages = $languagesRepository->findById($id);
  156. if (!$languages) {
  157. $this->errorAction();
  158. }
  159. $action = 'update';
  160. require_once('view/languages/form.php');
  161. return;
  162. }
  163. $name = isset($_POST['name']) ? trim($_POST['name']) : null;
  164. $id = isset($_POST['id']) ? trim($_POST['id']) : null;
  165. $name = new Languages($name, $id);
  166. if ($languages->validate()) {
  167. $languagesRepository = new LanguagesRepository();
  168. $languagesRepository->update($languages);
  169. header('Location: index.php?action=index-languages');
  170. return;
  171. }
  172. $this->errorAction();
  173. }
  174. protected function createLanguagesAction()
  175. {
  176. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  177. $action = 'create';
  178. require_once('view/languages/form.php');
  179. return;
  180. }
  181. $name = isset($_POST['name']) ? trim($_POST['name']) : null;
  182. $languages = new Languages($name);
  183. if ($languages->validate()) {
  184. $languagesRepository = new LanguagesRepository();
  185. $languagesRepository->save($languages);
  186. }
  187. header('Location: index.php?action=index-languages');
  188. }
  189. protected function deleteLanguagesAction()
  190. {
  191. $id = $this->getId();
  192. $LanguagesRepository = new LanguagesRepository();
  193. $LanguagesRepository->delete($id);
  194. header('Location: index.php?action=index-languages');
  195. }
  196. protected function getId()
  197. {
  198. $id = isset($_GET['id']) ? $_GET['id'] : null;
  199. if (!empty($id)) {
  200. return $id;
  201. }
  202. $this->errorAction();
  203. }
  204. }