Controller.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App;
  3. use App\Model\Student;
  4. use App\Repository\FamilyRepository;
  5. use App\Repository\StudentRepository;
  6. class Controller
  7. {
  8. protected $action;
  9. public function __construct($action)
  10. {
  11. $this->action = $action;
  12. }
  13. public function execute()
  14. {
  15. switch ($this->action) {
  16. case '':
  17. case 'main':
  18. $this->mainAction();
  19. break;
  20. case 'index-student':
  21. $this->indexStudentAction();
  22. break;
  23. case 'create-student':
  24. $this->createStudentAction();
  25. break;
  26. case 'view-student':
  27. $this->viewStudentAction();
  28. break;
  29. case 'update-student':
  30. $this->updateStudentAction();
  31. break;
  32. case 'delete-student':
  33. $this->deleteStudentAction();
  34. break;
  35. case 'index-family':
  36. $this->indexFamilyAction();
  37. break;
  38. default:
  39. $this->errorAction();
  40. }
  41. }
  42. protected function mainAction()
  43. {
  44. require_once('view/main.php');
  45. }
  46. protected function errorAction()
  47. {
  48. require_once('view/error.php');
  49. exit();
  50. }
  51. protected function indexStudentAction()
  52. {
  53. $studentRepository = new StudentRepository();
  54. $students = $studentRepository->findAll();
  55. require_once('view/student/index.php');
  56. }
  57. protected function viewStudentAction()
  58. {
  59. $id = $this->getId();
  60. $studentRepository = new StudentRepository();
  61. $student = $studentRepository->findById($id);
  62. if (!$student) {
  63. $this->errorAction();
  64. }
  65. require_once('view/student/view.php');
  66. }
  67. protected function updateStudentAction()
  68. {
  69. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  70. $id = $this->getId();
  71. $studentRepository = new StudentRepository();
  72. $student = $studentRepository->findById($id);
  73. if (!$student) {
  74. $this->errorAction();
  75. }
  76. $action = 'update';
  77. require_once('view/student/form.php');
  78. return;
  79. }
  80. $firstName = isset($_POST['firstName']) ? trim($_POST['firstName']) : null;
  81. $lastName = isset($_POST['lastName']) ? trim($_POST['lastName']) : null;
  82. $class = isset($_POST['class']) ? trim($_POST['class']) : null;
  83. $id = isset($_POST['id']) ? trim($_POST['id']) : null;
  84. $student = new Student($firstName, $lastName, $class, $id);
  85. if ($student->validate()) {
  86. $studentRepository = new StudentRepository();
  87. $studentRepository->update($student);
  88. header('Location: index.php?action=index-student');
  89. return;
  90. }
  91. $this->errorAction();
  92. }
  93. protected function createStudentAction()
  94. {
  95. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  96. $action = 'create';
  97. require_once('view/student/form.php');
  98. return;
  99. }
  100. $firstName = isset($_POST['firstName']) ? trim($_POST['firstName']) : null;
  101. $lastName = isset($_POST['lastName']) ? trim($_POST['lastName']) : null;
  102. $class = isset($_POST['class']) ? trim($_POST['class']) : null;
  103. $student = new Student($firstName, $lastName, $class);
  104. if ($student->validate()) {
  105. $studentRepository = new StudentRepository();
  106. $studentRepository->save($student);
  107. }
  108. header('Location: index.php?action=index-student');
  109. }
  110. protected function deleteStudentAction()
  111. {
  112. $id = $this->getId();
  113. $studentRepository = new StudentRepository();
  114. $studentRepository->delete($id);
  115. header('Location: index.php?action=index-student');
  116. }
  117. protected function indexFamilyAction()
  118. {
  119. $familyRepository = new FamilyRepository();
  120. $families = $familyRepository->findAll();
  121. require_once('view/family/index.php');
  122. }
  123. protected function getId()
  124. {
  125. $id = isset($_GET['id']) ? $_GET['id'] : null;
  126. if (!empty($id)) {
  127. return $id;
  128. }
  129. $this->errorAction();
  130. }
  131. }