123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- <?php
- namespace App;
- use App\Model\Pharmacy;
- use App\Model\Drug;
- use App\Model\PharmacyDrug;
- use App\Repository\PharmacyRepository;
- use App\Repository\DrugRepository;
- use App\Repository\PharmacyDrugRepository;
- 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-pharmacy':
- $this->indexPharmacyAction();
- break;
- case 'create-pharmacy':
- $this->createPharmacyAction();
- break;
- case 'view-pharmacy':
- $this->viewPharmacyAction();
- break;
- case 'update-pharmacy':
- $this->updatePharmacyAction();
- break;
- case 'delete-pharmacy':
- $this->deletePharmacyAction();
- break;
- case 'index-drug':
- $this->indexDrugAction();
- break;
- case 'create-drug':
- $this->createDrugAction();
- break;
- case 'view-drug':
- $this->viewDrugAction();
- break;
- case 'update-drug':
- $this->updateDrugAction();
- break;
- case 'delete-drug':
- $this->deleteDrugAction();
- break;
- case 'index-pharmacydrug':
- $this->indexPharmacyDrugAction();
- break;
- case 'create-pharmacydrug':
- $this->createPharmacyDrugAction();
- break;
- case 'view-pharmacydrug':
- $this->viewPharmacyDrugAction();
- break;
- case 'update-pharmacydrug':
- $this->updatePharmacyDrugAction();
- break;
- case 'delete-pharmacydrug':
- $this->deletePharmacyDrugAction();
- break;
-
- default:
- $this->errorAction();
- }
- }
- protected function mainAction()
- {
- require_once('view/main.php');
- }
- protected function errorAction()
- {
- require_once('view/error.php');
- exit();
- }
- protected function indexPharmacyAction()
- {
- $pharmacyRepository = new PharmacyRepository();
- $pharmacies = $pharmacyRepository->findAll();
- require_once('view/pharmacy/index.php');
- }
- protected function viewPharmacyAction()
- {
- $id = $this->getId();
- $pharmacyRepository = new PharmacyRepository();
- $pharmacy = $pharmacyRepository->findById($id);
- if (!$pharmacy) {
- $this->errorAction();
- }
- require_once('view/pharmacy/view.php');
- }
- protected function updatePharmacyAction()
- {
- if ($_SERVER['REQUEST_METHOD'] === 'GET') {
- $id = $this->getId();
- $pharmacyRepository = new PharmacyRepository();
- $pharmacy = $pharmacyRepository->findById($id);
- if (!$pharmacy) {
- $this->errorAction();
- }
- $action = 'update';
- require_once('view/pharmacy/form.php');
- return;
- }
- $pharmacyName = isset($_POST['pharmacyName']) ? trim($_POST['pharmacyName']) : null;
- $pharmacyCity = isset($_POST['pharmacyCity']) ? trim($_POST['pharmacyCity']) : null;
- $pharmacyStreet = isset($_POST['pharmacyStreet']) ? trim($_POST['pharmacyStreet']) : null;
- $pharmacyHouse = isset($_POST['pharmacyHouse']) ? trim($_POST['pharmacyHouse']) : null;
- $pharmacyPhone = isset($_POST['pharmacyPhone']) ? trim($_POST['pharmacyPhone']) : null;
- $id = isset($_POST['id']) ? trim($_POST['id']) : null;
- $pharmacy = new Pharmacy($pharmacyName, $pharmacyCity, $pharmacyStreet, $pharmacyHouse, $pharmacyPhone, $id);
- if ($pharmacy->validate()) {
- $pharmacyRepository = new PharmacyRepository();
- $pharmacyRepository->update($pharmacy);
- header('Location: index.php?action=index-pharmacy');
- return;
- }
- $this->errorAction();
- }
- protected function createPharmacyAction()
- {
- if ($_SERVER['REQUEST_METHOD'] === 'GET') {
- $action = 'create';
- require_once('view/pharmacy/form.php');
- return;
- }
- $pharmacyName = isset($_POST['pharmacyName']) ? trim($_POST['pharmacyName']) : null;
- $pharmacyCity = isset($_POST['pharmacyCity']) ? trim($_POST['pharmacyCity']) : null;
- $pharmacyStreet = isset($_POST['pharmacyStreet']) ? trim($_POST['pharmacyStreet']) : null;
- $pharmacyHouse = isset($_POST['pharmacyHouse']) ? trim($_POST['pharmacyHouse']) : null;
- $pharmacyPhone = isset($_POST['pharmacyPhone']) ? trim($_POST['pharmacyPhone']) : null;
- $pharmacy = new Pharmacy($pharmacyName, $pharmacyCity, $pharmacyStreet, $pharmacyHouse, $pharmacyPhone);
- if ($pharmacy->validate()) {
- $pharmacyRepository = new PharmacyRepository();
- $pharmacyRepository->save($pharmacy);
- }
- header('Location: index.php?action=index-pharmacy');
- }
- protected function deletePharmacyAction()
- {
- $id = $this->getId();
- $pharmacyRepository = new PharmacyRepository();
- $pharmacyRepository->delete($id);
- header('Location: index.php?action=index-pharmacy');
- }
- protected function indexDrugAction()
- {
- $drugRepository = new DrugRepository();
- $drugs = $drugRepository->findAll();
- require_once('view/drug/index.php');
- }
- protected function viewDrugAction()
- {
- $id = $this->getId();
- $drugRepository = new DrugRepository();
- $drug = $drugRepository->findById($id);
- if (!$drug) {
- $this->errorAction();
- }
- require_once('view/drug/view.php');
- }
- protected function updateDrugAction()
- {
- if ($_SERVER['REQUEST_METHOD'] === 'GET') {
- $id = $this->getId();
- $drugRepository = new DrugRepository();
- $drug = $drugRepository->findById($id);
- if (!$drug) {
- $this->errorAction();
- }
- $action = 'update';
- require_once('view/drug/form.php');
- return;
- }
- $drugName = isset($_POST['drugName']) ? trim($_POST['drugName']) : null;
- $drugGroup = isset($_POST['drugGroup']) ? trim($_POST['drugGroup']) : null;
- $id = isset($_POST['id']) ? trim($_POST['id']) : null;
- $drug = new Drug($drugName, $drugGroup, $id);
- if ($drug->validate()) {
- $drugRepository = new DrugRepository();
- $drugRepository->update($drug);
- header('Location: index.php?action=index-drug');
- return;
- }
- $this->errorAction();
- }
- protected function createDrugAction()
- {
- if ($_SERVER['REQUEST_METHOD'] === 'GET') {
- $action = 'create';
- require_once('view/drug/form.php');
- return;
- }
- $drugName = isset($_POST['drugName']) ? trim($_POST['drugName']) : null;
- $drugGroup = isset($_POST['drugGroup']) ? trim($_POST['drugGroup']) : null;
- $drug = new Drug($drugName, $drugGroup);
- if ($drug->validate()) {
- $drugRepository = new DrugRepository();
- $drugRepository->save($drug);
- }
- header('Location: index.php?action=index-drug');
- }
- protected function deleteDrugAction()
- {
- $id = $this->getId();
- $drugRepository = new DrugRepository();
- $drugRepository->delete($id);
- header('Location: index.php?action=index-drug');
- }
-
- protected function getId()
- {
- $id = isset($_GET['id']) ? $_GET['id'] : null;
- if (!empty($id)) {
- return $id;
- }
- $this->errorAction();
- }
- protected function indexPharmacyDrugAction()
- {
- $pharmacyDrugRepository = new PharmacyDrugRepository();
- $pharmacyDrugs = $pharmacyDrugRepository->findAll();
- require_once('view/pharmacyDrug/index.php');
- }
- protected function viewPharmacyDrugAction()
- {
- $idPharmacyArr = $this->getPharmacyDrugId();
- $pharmacyDrugRepository = new PharmacyDrugRepository();
- $pharmacyDrug = $pharmacyDrugRepository->findById($idPharmacyArr['idPharmacy'], $idPharmacyArr['idDrug']);
- if (!$pharmacyDrug) {
- $this->errorAction();
- }
- require_once('view/pharmacyDrug/view.php');
- }
- protected function updatePharmacyDrugAction()
- {
- if ($_SERVER['REQUEST_METHOD'] === 'GET') {
- $idPharmacyArr = $this->getPharmacyDrugId();
- $pharmacyRepository = new PharmacyRepository();
- $pharmacies[] = $pharmacyRepository->findById($idPharmacyArr['idPharmacy']);
- $drugRepository = new DrugRepository();
- $drugs[] = $drugRepository->findById($idPharmacyArr['idDrug']);
- $pharmacyDrugRepository = new PharmacyDrugRepository();
- $pharmacyDrug = $pharmacyDrugRepository->findById($idPharmacyArr['idPharmacy'], $idPharmacyArr['idDrug']);
- if (!$pharmacyDrug) {
- $this->errorAction();
- }
- $action = 'update';
- require_once('view/pharmacyDrug/form.php');
- return;
- }
- $pharmacyDrugIdPharmacy = isset($_POST['idPharmacy']) ? trim($_POST['idPharmacy']) : null;
- $pharmacyDrugIdDrug = isset($_POST['idDrug']) ? trim($_POST['idDrug']) : null;
- $pharmacyDrugQuantity = isset($_POST['pharmacyDrugQuantity']) ? trim($_POST['pharmacyDrugQuantity']) : null;
- $pharmacyDrug = new PharmacyDrug($pharmacyDrugIdPharmacy, $pharmacyDrugIdDrug, $pharmacyDrugQuantity);
- if ($pharmacyDrug->validate()) {
- $pharmacyDrugRepository = new PharmacyDrugRepository();
- $pharmacyDrugRepository->update($pharmacyDrug);
- header('Location: index.php?action=index-pharmacydrug');
- return;
- }
- $this->errorAction();
- }
- protected function createPharmacyDrugAction()
- {
- if ($_SERVER['REQUEST_METHOD'] === 'GET') {
- $action = 'create';
- //Get from database all Pharmacies array $pharmacies and all Drugs array $drugs
- //for List name = "pharmacydrugPharmacy" option value="$pharmacy->id" $pharmacy->Name
- //for List name = "pharmacydrugDrug" option value="$drug->id" $drug->Name
- $pharmacyRepository = new PharmacyRepository();
- $pharmacies = $pharmacyRepository->findAll();
- $drugRepository = new DrugRepository();
- $drugs = $drugRepository->findAll();
- require_once('view/pharmacyDrug/form.php');
- return;
- }
-
- $pharmacyDrugPharmacy = isset($_POST['pharmacydrugPharmacy']) ? trim($_POST['pharmacydrugPharmacy']) : null;
- $pharmacyDrugDrug = isset($_POST['pharmacydrugDrug']) ? trim($_POST['pharmacydrugDrug']) : null;
- $pharmacyDrugQuantity = isset($_POST['pharmacyDrugQuantity']) ? trim($_POST['pharmacyDrugQuantity']) : null;
- $pharmacyDrug = new PharmacyDrug($pharmacyDrugPharmacy, $pharmacyDrugDrug, $pharmacyDrugQuantity);
- if ($pharmacyDrug->validate()) {
- $pharmacyDrugRepository = new PharmacyDrugRepository();
- $pharmacyDrugRepository->save($pharmacyDrug);
- }
- header('Location: index.php?action=index-pharmacydrug');
- }
- protected function deletePharmacyDrugAction()
- {
- $idPharmacyArr = $this->getPharmacyDrugId();
- $pharmacyDrugRepository = new PharmacyDrugRepository();
- $pharmacyDrugRepository->delete($idPharmacyArr['idPharmacy'], $idPharmacyArr['idDrug']);
- header('Location: index.php?action=index-pharmacydrug');
- }
- protected function getPharmacyDrugId() : array
- {
- $idPharmacy = isset($_GET['idpharmacy']) ? $_GET['idpharmacy'] : null;
- $idDrug = isset($_GET['iddrug']) ? $_GET['iddrug'] : null;
- if (!empty($idPharmacy) && !empty($idDrug)) {
- return array(
- 'idPharmacy' => $idPharmacy,
- 'idDrug' => $idDrug
- );
- }
- $this->errorAction();
- }
- }
|