Controller.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php
  2. namespace App;
  3. use App\Model\Pharmacy;
  4. use App\Model\Drug;
  5. use App\Model\PharmacyDrug;
  6. use App\Repository\PharmacyRepository;
  7. use App\Repository\DrugRepository;
  8. use App\Repository\PharmacyDrugRepository;
  9. class Controller
  10. {
  11. protected $action;
  12. public function __construct($action)
  13. {
  14. $this->action = $action;
  15. }
  16. public function execute()
  17. {
  18. switch ($this->action) {
  19. case '':
  20. case 'main':
  21. $this->mainAction();
  22. break;
  23. case 'index-pharmacy':
  24. $this->indexPharmacyAction();
  25. break;
  26. case 'create-pharmacy':
  27. $this->createPharmacyAction();
  28. break;
  29. case 'view-pharmacy':
  30. $this->viewPharmacyAction();
  31. break;
  32. case 'update-pharmacy':
  33. $this->updatePharmacyAction();
  34. break;
  35. case 'delete-pharmacy':
  36. $this->deletePharmacyAction();
  37. break;
  38. case 'index-drug':
  39. $this->indexDrugAction();
  40. break;
  41. case 'create-drug':
  42. $this->createDrugAction();
  43. break;
  44. case 'view-drug':
  45. $this->viewDrugAction();
  46. break;
  47. case 'update-drug':
  48. $this->updateDrugAction();
  49. break;
  50. case 'delete-drug':
  51. $this->deleteDrugAction();
  52. break;
  53. case 'index-pharmacydrug':
  54. $this->indexPharmacyDrugAction();
  55. break;
  56. case 'create-pharmacydrug':
  57. $this->createPharmacyDrugAction();
  58. break;
  59. case 'view-pharmacydrug':
  60. $this->viewPharmacyDrugAction();
  61. break;
  62. case 'update-pharmacydrug':
  63. $this->updatePharmacyDrugAction();
  64. break;
  65. case 'delete-pharmacydrug':
  66. $this->deletePharmacyDrugAction();
  67. break;
  68. default:
  69. $this->errorAction();
  70. }
  71. }
  72. protected function mainAction()
  73. {
  74. require_once('view/main.php');
  75. }
  76. protected function errorAction()
  77. {
  78. require_once('view/error.php');
  79. exit();
  80. }
  81. protected function indexPharmacyAction()
  82. {
  83. $pharmacyRepository = new PharmacyRepository();
  84. $pharmacies = $pharmacyRepository->findAll();
  85. require_once('view/pharmacy/index.php');
  86. }
  87. protected function viewPharmacyAction()
  88. {
  89. $id = $this->getId();
  90. $pharmacyRepository = new PharmacyRepository();
  91. $pharmacy = $pharmacyRepository->findById($id);
  92. if (!$pharmacy) {
  93. $this->errorAction();
  94. }
  95. require_once('view/pharmacy/view.php');
  96. }
  97. protected function updatePharmacyAction()
  98. {
  99. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  100. $id = $this->getId();
  101. $pharmacyRepository = new PharmacyRepository();
  102. $pharmacy = $pharmacyRepository->findById($id);
  103. if (!$pharmacy) {
  104. $this->errorAction();
  105. }
  106. $action = 'update';
  107. require_once('view/pharmacy/form.php');
  108. return;
  109. }
  110. $pharmacyName = isset($_POST['pharmacyName']) ? trim($_POST['pharmacyName']) : null;
  111. $pharmacyCity = isset($_POST['pharmacyCity']) ? trim($_POST['pharmacyCity']) : null;
  112. $pharmacyStreet = isset($_POST['pharmacyStreet']) ? trim($_POST['pharmacyStreet']) : null;
  113. $pharmacyHouse = isset($_POST['pharmacyHouse']) ? trim($_POST['pharmacyHouse']) : null;
  114. $pharmacyPhone = isset($_POST['pharmacyPhone']) ? trim($_POST['pharmacyPhone']) : null;
  115. $id = isset($_POST['id']) ? trim($_POST['id']) : null;
  116. $pharmacy = new Pharmacy($pharmacyName, $pharmacyCity, $pharmacyStreet, $pharmacyHouse, $pharmacyPhone, $id);
  117. if ($pharmacy->validate()) {
  118. $pharmacyRepository = new PharmacyRepository();
  119. $pharmacyRepository->update($pharmacy);
  120. header('Location: index.php?action=index-pharmacy');
  121. return;
  122. }
  123. $this->errorAction();
  124. }
  125. protected function createPharmacyAction()
  126. {
  127. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  128. $action = 'create';
  129. require_once('view/pharmacy/form.php');
  130. return;
  131. }
  132. $pharmacyName = isset($_POST['pharmacyName']) ? trim($_POST['pharmacyName']) : null;
  133. $pharmacyCity = isset($_POST['pharmacyCity']) ? trim($_POST['pharmacyCity']) : null;
  134. $pharmacyStreet = isset($_POST['pharmacyStreet']) ? trim($_POST['pharmacyStreet']) : null;
  135. $pharmacyHouse = isset($_POST['pharmacyHouse']) ? trim($_POST['pharmacyHouse']) : null;
  136. $pharmacyPhone = isset($_POST['pharmacyPhone']) ? trim($_POST['pharmacyPhone']) : null;
  137. $pharmacy = new Pharmacy($pharmacyName, $pharmacyCity, $pharmacyStreet, $pharmacyHouse, $pharmacyPhone);
  138. if ($pharmacy->validate()) {
  139. $pharmacyRepository = new PharmacyRepository();
  140. $pharmacyRepository->save($pharmacy);
  141. }
  142. header('Location: index.php?action=index-pharmacy');
  143. }
  144. protected function deletePharmacyAction()
  145. {
  146. $id = $this->getId();
  147. $pharmacyRepository = new PharmacyRepository();
  148. $pharmacyRepository->delete($id);
  149. header('Location: index.php?action=index-pharmacy');
  150. }
  151. protected function indexDrugAction()
  152. {
  153. $drugRepository = new DrugRepository();
  154. $drugs = $drugRepository->findAll();
  155. require_once('view/drug/index.php');
  156. }
  157. protected function viewDrugAction()
  158. {
  159. $id = $this->getId();
  160. $drugRepository = new DrugRepository();
  161. $drug = $drugRepository->findById($id);
  162. if (!$drug) {
  163. $this->errorAction();
  164. }
  165. require_once('view/drug/view.php');
  166. }
  167. protected function updateDrugAction()
  168. {
  169. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  170. $id = $this->getId();
  171. $drugRepository = new DrugRepository();
  172. $drug = $drugRepository->findById($id);
  173. if (!$drug) {
  174. $this->errorAction();
  175. }
  176. $action = 'update';
  177. require_once('view/drug/form.php');
  178. return;
  179. }
  180. $drugName = isset($_POST['drugName']) ? trim($_POST['drugName']) : null;
  181. $drugGroup = isset($_POST['drugGroup']) ? trim($_POST['drugGroup']) : null;
  182. $id = isset($_POST['id']) ? trim($_POST['id']) : null;
  183. $drug = new Drug($drugName, $drugGroup, $id);
  184. if ($drug->validate()) {
  185. $drugRepository = new DrugRepository();
  186. $drugRepository->update($drug);
  187. header('Location: index.php?action=index-drug');
  188. return;
  189. }
  190. $this->errorAction();
  191. }
  192. protected function createDrugAction()
  193. {
  194. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  195. $action = 'create';
  196. require_once('view/drug/form.php');
  197. return;
  198. }
  199. $drugName = isset($_POST['drugName']) ? trim($_POST['drugName']) : null;
  200. $drugGroup = isset($_POST['drugGroup']) ? trim($_POST['drugGroup']) : null;
  201. $drug = new Drug($drugName, $drugGroup);
  202. if ($drug->validate()) {
  203. $drugRepository = new DrugRepository();
  204. $drugRepository->save($drug);
  205. }
  206. header('Location: index.php?action=index-drug');
  207. }
  208. protected function deleteDrugAction()
  209. {
  210. $id = $this->getId();
  211. $drugRepository = new DrugRepository();
  212. $drugRepository->delete($id);
  213. header('Location: index.php?action=index-drug');
  214. }
  215. protected function getId()
  216. {
  217. $id = isset($_GET['id']) ? $_GET['id'] : null;
  218. if (!empty($id)) {
  219. return $id;
  220. }
  221. $this->errorAction();
  222. }
  223. protected function indexPharmacyDrugAction()
  224. {
  225. $pharmacyDrugRepository = new PharmacyDrugRepository();
  226. $pharmacyDrugs = $pharmacyDrugRepository->findAll();
  227. require_once('view/pharmacyDrug/index.php');
  228. }
  229. protected function viewPharmacyDrugAction()
  230. {
  231. $idPharmacyArr = $this->getPharmacyDrugId();
  232. $pharmacyDrugRepository = new PharmacyDrugRepository();
  233. $pharmacyDrug = $pharmacyDrugRepository->findById($idPharmacyArr['idPharmacy'], $idPharmacyArr['idDrug']);
  234. if (!$pharmacyDrug) {
  235. $this->errorAction();
  236. }
  237. require_once('view/pharmacyDrug/view.php');
  238. }
  239. protected function updatePharmacyDrugAction()
  240. {
  241. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  242. $idPharmacyArr = $this->getPharmacyDrugId();
  243. $pharmacyRepository = new PharmacyRepository();
  244. $pharmacies[] = $pharmacyRepository->findById($idPharmacyArr['idPharmacy']);
  245. $drugRepository = new DrugRepository();
  246. $drugs[] = $drugRepository->findById($idPharmacyArr['idDrug']);
  247. $pharmacyDrugRepository = new PharmacyDrugRepository();
  248. $pharmacyDrug = $pharmacyDrugRepository->findById($idPharmacyArr['idPharmacy'], $idPharmacyArr['idDrug']);
  249. if (!$pharmacyDrug) {
  250. $this->errorAction();
  251. }
  252. $action = 'update';
  253. require_once('view/pharmacyDrug/form.php');
  254. return;
  255. }
  256. $pharmacyDrugIdPharmacy = isset($_POST['idPharmacy']) ? trim($_POST['idPharmacy']) : null;
  257. $pharmacyDrugIdDrug = isset($_POST['idDrug']) ? trim($_POST['idDrug']) : null;
  258. $pharmacyDrugQuantity = isset($_POST['pharmacyDrugQuantity']) ? trim($_POST['pharmacyDrugQuantity']) : null;
  259. $pharmacyDrug = new PharmacyDrug($pharmacyDrugIdPharmacy, $pharmacyDrugIdDrug, $pharmacyDrugQuantity);
  260. if ($pharmacyDrug->validate()) {
  261. $pharmacyDrugRepository = new PharmacyDrugRepository();
  262. $pharmacyDrugRepository->update($pharmacyDrug);
  263. header('Location: index.php?action=index-pharmacydrug');
  264. return;
  265. }
  266. $this->errorAction();
  267. }
  268. protected function createPharmacyDrugAction()
  269. {
  270. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  271. $action = 'create';
  272. //Get from database all Pharmacies array $pharmacies and all Drugs array $drugs
  273. //for List name = "pharmacydrugPharmacy" option value="$pharmacy->id" $pharmacy->Name
  274. //for List name = "pharmacydrugDrug" option value="$drug->id" $drug->Name
  275. $pharmacyRepository = new PharmacyRepository();
  276. $pharmacies = $pharmacyRepository->findAll();
  277. $drugRepository = new DrugRepository();
  278. $drugs = $drugRepository->findAll();
  279. require_once('view/pharmacyDrug/form.php');
  280. return;
  281. }
  282. $pharmacyDrugPharmacy = isset($_POST['pharmacydrugPharmacy']) ? trim($_POST['pharmacydrugPharmacy']) : null;
  283. $pharmacyDrugDrug = isset($_POST['pharmacydrugDrug']) ? trim($_POST['pharmacydrugDrug']) : null;
  284. $pharmacyDrugQuantity = isset($_POST['pharmacyDrugQuantity']) ? trim($_POST['pharmacyDrugQuantity']) : null;
  285. $pharmacyDrug = new PharmacyDrug($pharmacyDrugPharmacy, $pharmacyDrugDrug, $pharmacyDrugQuantity);
  286. if ($pharmacyDrug->validate()) {
  287. $pharmacyDrugRepository = new PharmacyDrugRepository();
  288. $pharmacyDrugRepository->save($pharmacyDrug);
  289. }
  290. header('Location: index.php?action=index-pharmacydrug');
  291. }
  292. protected function deletePharmacyDrugAction()
  293. {
  294. $idPharmacyArr = $this->getPharmacyDrugId();
  295. $pharmacyDrugRepository = new PharmacyDrugRepository();
  296. $pharmacyDrugRepository->delete($idPharmacyArr['idPharmacy'], $idPharmacyArr['idDrug']);
  297. header('Location: index.php?action=index-pharmacydrug');
  298. }
  299. protected function getPharmacyDrugId() : array
  300. {
  301. $idPharmacy = isset($_GET['idpharmacy']) ? $_GET['idpharmacy'] : null;
  302. $idDrug = isset($_GET['iddrug']) ? $_GET['iddrug'] : null;
  303. if (!empty($idPharmacy) && !empty($idDrug)) {
  304. return array(
  305. 'idPharmacy' => $idPharmacy,
  306. 'idDrug' => $idDrug
  307. );
  308. }
  309. $this->errorAction();
  310. }
  311. }