|
@@ -0,0 +1,237 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App;
|
|
|
+
|
|
|
+use App\Model\Student;
|
|
|
+use App\Model\Languages;
|
|
|
+use App\Repository\LanguagesRepository;
|
|
|
+use App\Repository\StudentLanguagesRepository;
|
|
|
+use App\Repository\StudentRepository;
|
|
|
+
|
|
|
+class Controller
|
|
|
+{
|
|
|
+ protected $action;
|
|
|
+
|
|
|
+ public function __construct($action)
|
|
|
+ {
|
|
|
+ $this->action = $action;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function execute()
|
|
|
+ {
|
|
|
+ switch ($this->action) {
|
|
|
+ case '':
|
|
|
+ case 'main':
|
|
|
+ $this->mainAction();
|
|
|
+ break;
|
|
|
+ case 'index-student':
|
|
|
+ $this->indexStudentAction();
|
|
|
+ break;
|
|
|
+ case 'create-student':
|
|
|
+ $this->createStudentAction();
|
|
|
+ break;
|
|
|
+ case 'view-student':
|
|
|
+ $this->viewStudentAction();
|
|
|
+ break;
|
|
|
+ case 'update-student':
|
|
|
+ $this->updateStudentAction();
|
|
|
+ break;
|
|
|
+ case 'delete-student':
|
|
|
+ $this->deleteStudentAction();
|
|
|
+ break;
|
|
|
+ case 'index-languages':
|
|
|
+ $this->indexLanguagesAction();
|
|
|
+ break;
|
|
|
+ case 'delete-all-languages':
|
|
|
+ $this->deleteAllLanguagesAction();
|
|
|
+ break;
|
|
|
+ case 'create-languages':
|
|
|
+ $this->createLanguagesAction();
|
|
|
+ break;
|
|
|
+ case 'view-languages':
|
|
|
+ $this->viewLanguagesAction();
|
|
|
+ break;
|
|
|
+ case 'update-languages':
|
|
|
+ $this->updateLanguagesAction();
|
|
|
+ break;
|
|
|
+ case 'delete-languages':
|
|
|
+ $this->deleteLanguagesAction();
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ $this->errorAction();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function mainAction()
|
|
|
+ {
|
|
|
+ require_once('view/main.php');
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function errorAction()
|
|
|
+ {
|
|
|
+ require_once('view/error.php');
|
|
|
+ exit();
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function indexStudentAction()
|
|
|
+ {
|
|
|
+ $studentRepository = new StudentRepository();
|
|
|
+ $students = $studentRepository->findAll();
|
|
|
+ require_once('view/student/index.php');
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function viewStudentAction()
|
|
|
+ {
|
|
|
+ $id = $this->getId();
|
|
|
+ $studentRepository = new StudentRepository();
|
|
|
+ $student = $studentRepository->findById($id);
|
|
|
+ if (!$student) {
|
|
|
+ $this->errorAction();
|
|
|
+ }
|
|
|
+ require_once('view/student/view.php');
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function updateStudentAction()
|
|
|
+ {
|
|
|
+ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
|
|
+ $id = $this->getId();
|
|
|
+ $studentRepository = new StudentRepository();
|
|
|
+ $student = $studentRepository->findById($id);
|
|
|
+ if (!$student) {
|
|
|
+ $this->errorAction();
|
|
|
+ }
|
|
|
+ $action = 'update';
|
|
|
+ require_once('view/student/form.php');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $firstName = isset($_POST['firstName']) ? trim($_POST['firstName']) : null;
|
|
|
+ $lastName = isset($_POST['lastName']) ? trim($_POST['lastName']) : null;
|
|
|
+ $class = isset($_POST['class']) ? trim($_POST['class']) : null;
|
|
|
+ $id = isset($_POST['id']) ? trim($_POST['id']) : null;
|
|
|
+ $student = new Student($firstName, $lastName, $class, $id);
|
|
|
+
|
|
|
+ if ($student->validate()) {
|
|
|
+ $studentRepository = new StudentRepository();
|
|
|
+ $studentRepository->update($student);
|
|
|
+
|
|
|
+ header('Location: index.php?action=index-student');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $this->errorAction();
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function createStudentAction()
|
|
|
+ {
|
|
|
+ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
|
|
+ $action = 'create';
|
|
|
+ require_once('view/student/form.php');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $firstName = isset($_POST['firstName']) ? trim($_POST['firstName']) : null;
|
|
|
+ $lastName = isset($_POST['lastName']) ? trim($_POST['lastName']) : null;
|
|
|
+ $class = isset($_POST['class']) ? trim($_POST['class']) : null;
|
|
|
+
|
|
|
+ $student = new Student($firstName, $lastName, $class);
|
|
|
+
|
|
|
+ if ($student->validate()) {
|
|
|
+ $studentRepository = new StudentRepository();
|
|
|
+ $studentRepository->save($student);
|
|
|
+ }
|
|
|
+
|
|
|
+ header('Location: index.php?action=index-student');
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function deleteStudentAction()
|
|
|
+ {
|
|
|
+ $id = $this->getId();
|
|
|
+ $studentRepository = new StudentRepository();
|
|
|
+ $studentRepository->delete($id);
|
|
|
+ header('Location: index.php?action=index-student');
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function indexLanguagesAction()
|
|
|
+ {
|
|
|
+ $languagesRepository = new LanguagesRepository();
|
|
|
+ $languages = $languagesRepository->findAll();
|
|
|
+ require_once('view/languages/index.php');
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function viewLanguagesAction()
|
|
|
+ {
|
|
|
+ $id = $this->getId();
|
|
|
+ $languagesRepository = new StudentRepository();
|
|
|
+ $languages = $languagesRepository->findById($id);
|
|
|
+ if (!$languages) {
|
|
|
+ $this->errorAction();
|
|
|
+ }
|
|
|
+ require_once('view/languages/view.php');
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function updateLanguageAction()
|
|
|
+ {
|
|
|
+ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
|
|
+ $id = $this->getId();
|
|
|
+ $languagesRepository = new LanguagesRepository();
|
|
|
+ $languages = $languagesRepository->findById($id);
|
|
|
+ if (!$languages) {
|
|
|
+ $this->errorAction();
|
|
|
+ }
|
|
|
+ $action = 'update';
|
|
|
+ require_once('view/languages/form.php');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $name = isset($_POST['name']) ? trim($_POST['name']) : null;
|
|
|
+ $id = isset($_POST['id']) ? trim($_POST['id']) : null;
|
|
|
+ $name = new Languages($name, $id);
|
|
|
+
|
|
|
+ if ($languages->validate()) {
|
|
|
+ $languagesRepository = new LanguagesRepository();
|
|
|
+ $languagesRepository->update($languages);
|
|
|
+
|
|
|
+ header('Location: index.php?action=index-languages');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $this->errorAction();
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function createLanguagesAction()
|
|
|
+ {
|
|
|
+ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
|
|
+ $action = 'create';
|
|
|
+ require_once('view/languages/form.php');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $name = isset($_POST['name']) ? trim($_POST['name']) : null;
|
|
|
+
|
|
|
+
|
|
|
+ $languages = new Languages($name);
|
|
|
+
|
|
|
+ if ($languages->validate()) {
|
|
|
+ $languagesRepository = new LanguagesRepository();
|
|
|
+ $languagesRepository->save($languages);
|
|
|
+ }
|
|
|
+
|
|
|
+ header('Location: index.php?action=index-languages');
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function deleteLanguagesAction()
|
|
|
+ {
|
|
|
+ $id = $this->getId();
|
|
|
+ $LanguagesRepository = new LanguagesRepository();
|
|
|
+ $LanguagesRepository->delete($id);
|
|
|
+ header('Location: index.php?action=index-languages');
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function getId()
|
|
|
+ {
|
|
|
+ $id = isset($_GET['id']) ? $_GET['id'] : null;
|
|
|
+ if (!empty($id)) {
|
|
|
+ return $id;
|
|
|
+ }
|
|
|
+ $this->errorAction();
|
|
|
+ }
|
|
|
+}
|