Advertisement
Guest User

action.php

a guest
May 25th, 2023
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.64 KB | None | 0 0
  1. <?php
  2. session_start();
  3. //var_dump($_SESSION);
  4. //error_reporting(E_ALL);
  5. include('classes/Core.php');
  6.  
  7.  
  8. function validateDate($date, $format = 'Y-m-d H:i:s')
  9. {
  10. $d = DateTime::createFromFormat($format, $date);
  11. return $d && $d->format($format) == $date;
  12. }
  13.  
  14. function parseBirthday($birthday, &$msg, $errMsg){
  15.  
  16. if (validateDate($birthday, 'd.m.Y')) {
  17. return $birthday = date("Y-m-d", strtotime(urldecode($birthday)));
  18. } else {
  19. $msg .= $errMsg;
  20. }
  21. return null;
  22. }
  23.  
  24. function parseTreatmentDateTime($treatmentDateTime, &$msg, $errMsg){
  25. if(validateDate($treatmentDateTime, 'd.m.Y H:i')){
  26. return $treatmentDateTime = date("Y-m-d H:i", strtotime(urldecode($treatmentDateTime)));
  27. } else {
  28. $msg .= $errMsg;
  29. }
  30. return null;
  31. }
  32.  
  33. function parseDate($date, &$msg, $errMsg){
  34. if(validateDate($date, 'd.m.Y')){
  35. return $date = date("Y-m-d", strtotime(urldecode($date)));
  36. } else {
  37. $msg .= $errMsg;
  38. }
  39. return null;
  40. }
  41.  
  42. function getPatientTreatmentDayAction($date){
  43. $date = date("Y-m-d", strtotime(urldecode($date)));
  44.  
  45. Core::connectDB();
  46. $treatmentDates = Core::getTreatmentDate($date);
  47. echo json_encode($treatmentDates);
  48. Core::closeDB();
  49.  
  50. exit;
  51. }
  52.  
  53. function insertWorkingTimeAction($id_staff, $beginning_date, $end_date, $matter = 'HOLIDAY'){
  54.  
  55. echo $beginning_date;
  56.  
  57. if(validateDate($beginning_date, 'd.m.Y H:i')){
  58. $beginning_date = parseTreatmentDateTime($beginning_date, $msg, "Leider ist beim Parsen ein Fehler aufgetreten.");
  59. }
  60. else if(validateDate($beginning_date, 'd.m.Y')){
  61. $beginning_date = parseDate($beginning_date, $msg, "Leider ist beim Parsen ein Fehler aufgetreten.");
  62. }
  63. else{
  64. echo "Leider konnte das Anfangsdatum nicht geparst werden.";
  65. exit;
  66. }
  67. //echo "'". $end_date. "'";
  68. if(validateDate($end_date, 'd.m.Y H:i')){
  69. $end_date = parseTreatmentDateTime($end_date, $msg, "Leider ist beim Parsen ein Fehler aufgetreten.");
  70. }
  71. else if(validateDate($end_date, 'd.m.Y')){
  72. $end_date = parseDate($end_date, $msg, "Leider ist beim Parsen ein Fehler aufgetreten.");
  73. }
  74. else{
  75. echo "Leider konnte das Enddatum nicht geparst werden.";
  76. exit;
  77. }
  78.  
  79. Core::connectDB();
  80.  
  81. if (Core::insertWorkingTime($id_staff, $beginning_date, $end_date, $matter)) {
  82. // everything is ok. return msg to user.
  83. echo "Zeiten wurde gespeichert.";
  84. echo $msg;
  85. } else {
  86. // cannot insert
  87. echo "Zeiten konnte nicht gespeichert werden.";
  88. echo $msg;
  89. }
  90. Core::closeDB();
  91.  
  92.  
  93.  
  94.  
  95. exit;
  96. }
  97.  
  98. function insertPatientTreatmentAction($id_patient, $id_staff, $id_treatment, $treatment_date, $treatment_time_end, $treatment_date_end, $description){
  99. $description = (isset($description) ? urldecode($description) : '');
  100. $description = trim($description);
  101.  
  102. $treatment_date = parseTreatmentDateTime($treatment_date, $msg, 'Leider ist beim Parsen des Termins ein Fehler unterlaufen.');
  103.  
  104. $t_start = explode(" ", $treatment_date);
  105. $treatment_time_start = $t_start[1];
  106.  
  107. $is_treatment_date_end = true;
  108. if($treatment_date_end===""){
  109. $treatment_date_end = $t_start[0];
  110. $is_treatment_date_end = false;
  111. }
  112. else{
  113. $treatment_date_end = date('Y-m-d', strtotime(urldecode($treatment_date_end)));
  114.  
  115. }
  116.  
  117.  
  118.  
  119. Core::connectDB();
  120.  
  121. $treatment_obj = Core::getTreatment($id_treatment);
  122. $is_multiple_patients = $treatment_obj['is_multiple_patients'];
  123.  
  124. $i_date = strtotime($t_start[0]);
  125.  
  126.  
  127. while ($i_date <= (strtotime($treatment_date_end))) {
  128.  
  129.  
  130. $i_treatment_date = date('Y-m-d', $i_date) . ' ' . $treatment_time_start;
  131.  
  132. $i_date = strtotime( date('Y-m-d', $i_date) . ' +1 week');
  133.  
  134. $isOnHoliday = Core::isOnHoliday($id_staff, $i_treatment_date, $treatment_time_end);
  135.  
  136. $isNotOverlapping = Core::checkOverlappingTreatmentDates($id_staff, $i_treatment_date, $treatment_time_end);
  137. $isHitExactly = false;
  138. if($is_multiple_patients === 'true'){
  139. $isHitExactly = Core::isHitExactlyTreatmentDate($id_staff, $i_treatment_date, $treatment_time_end);
  140. }
  141.  
  142. if (!$isOnHoliday && ($isNotOverlapping || $isHitExactly)) {
  143.  
  144. $msg_insert = Core::insertTreatmentDate($id_patient, $id_staff, $id_treatment, $i_treatment_date, $treatment_time_end, $description);
  145. //$msg_insert=true;
  146. if ($msg_insert === true) {
  147. // everything is ok. return msg to user.
  148. echo "Termin ".$i_treatment_date." wurde gespeichert.\n";
  149. echo $msg;
  150. } else {
  151. // cannot insert
  152. echo "Fataler Fehler: Termin konnte nicht gespeichert werden.\n";
  153. echo $msg . '<br />' . $msg_insert;
  154. Core::closeDB();
  155. exit;
  156. }
  157. } else {
  158. if($isOnHoliday){
  159. echo "Leider konnte der Termin am ".$i_treatment_date." nicht hinzugefügt werden, da sich der Mitarbeiter im Urlaub befindet.\n";
  160. }
  161. else{
  162. echo "Es existiert bereits ein Termin innerhalb des Zeitintervalls am ".$i_treatment_date.".\n";
  163. }
  164.  
  165. //exit;
  166. }
  167.  
  168.  
  169. }
  170. Core::closeDB();
  171. exit;
  172. }
  173.  
  174. function insertStaffAction($staff) {
  175. $msg = "";
  176. if( $staff['birthday'] !== null && $staff['birthday'] !== ""){
  177. $birthday = parseBirthday($staff['birthday'], $msg, 'Leider ist beim Parsen des Geburtsdatums ein Fehler unterlaufen.');
  178. }
  179. $new_staff = array();
  180. $new_staff['surname'] = (isset($staff['surname']) ? urldecode($staff['surname']) : '');
  181. $new_staff['prename'] = (isset($staff['prename']) ? urldecode($staff['prename']) : '');
  182. $new_staff['birthday'] = $birthday;
  183. $new_staff['telephone'] = (isset($staff['telephone']) ? urldecode($staff['telephone']) : '');
  184. $new_staff['description_text'] = (isset($staff['description']) ? urldecode($staff['description']) : '');
  185.  
  186. Core::connectDB();
  187. if (Core::addStaff($new_staff)) {
  188. // everything is ok. return msg to user.
  189. echo "Neuer Mitarbeiter wurde gespeichert.";
  190. echo $msg;
  191. } else {
  192. // cannot insert
  193. echo "Mitarbeiter konnte nicht gespeichert werden.";
  194. echo $msg;
  195. }
  196. Core::closeDB();
  197. exit;
  198. }
  199.  
  200.  
  201. function insertPatientAction($patient) {
  202. $msg = "";
  203. if (isset($patient['birthday']) && $patient['birthday'] !== "") {
  204. $birthday = parseBirthday($patient['birthday'], $msg, 'Leider ist beim Parsen des Geburtsdatums ein Fehler unterlaufen.');
  205. }
  206. $new_patient = array();
  207. $new_patient['surname'] = (isset($patient['surname']) ? urldecode($patient['surname']) : '');
  208. $new_patient['prename'] = (isset($patient['prename']) ? urldecode($patient['prename']) : '');
  209. //$new_patient['birthday'] = (isset($patient['birthday']) ? urldecode($patient['birthday']) : '');
  210. $new_patient['birthday'] = $birthday;
  211. $new_patient['telephone'] = (isset($patient['telephone']) ? urldecode($patient['telephone']) : '');
  212. $new_patient['description_text'] = (isset($patient['description']) ? urldecode($patient['description']) : '');
  213. $new_patient['town'] = (isset($patient['town']) ? urldecode($patient['town']) : '');
  214. $new_patient['zipcode'] = (isset($patient['zipcode']) ? urldecode($patient['zipcode']) : '');
  215. $new_patient['street'] = (isset($patient['street']) ? urldecode($patient['street']) : '');
  216. Core::connectDB();
  217. if (Core::addPatient($new_patient)) {
  218. // everything is ok. return msg to user.
  219. echo "Patient wurde gespeichert.";
  220. echo $msg;
  221. } else {
  222. // cannot insert
  223. echo "Patient konnte nicht gespeichert werden.";
  224. echo $msg;
  225. }
  226. Core::closeDB();
  227. exit;
  228. }
  229.  
  230.  
  231. function updatePatientAction($patient){
  232. $msg = "";
  233. if (isset($patient['birthday']) && $patient['birthday'] !== "") {
  234. $birthday = parseBirthday($patient['birthday'], $msg, 'Leider ist beim Parsen des Geburtsdatums ein Fehler unterlaufen.');
  235. }
  236. $new_patient = array();
  237. $new_patient['id_patient'] = $patient['id_patient'];
  238. $new_patient['surname'] = (isset($patient['surname']) ? urldecode($patient['surname']) : '');
  239. $new_patient['prename'] = (isset($patient['prename']) ? urldecode($patient['prename']) : '');
  240. //$new_patient['birthday'] = (isset($patient['birthday']) ? urldecode($patient['birthday']) : '');
  241. $new_patient['birthday'] = $birthday;
  242. $new_patient['telephone'] = (isset($patient['telephone']) ? urldecode($patient['telephone']) : '');
  243. $new_patient['description_text'] = (isset($patient['description']) ? urldecode($patient['description']) : '');
  244. $new_patient['town'] = (isset($patient['town']) ? urldecode($patient['town']) : '');
  245. $new_patient['zipcode'] = (isset($patient['zipcode']) ? urldecode($patient['zipcode']) : '');
  246. $new_patient['street'] = (isset($patient['street']) ? urldecode($patient['street']) : '');
  247. Core::connectDB();
  248. if (Core::updatePatient($new_patient)) {
  249. // everything is ok. return msg to user.
  250. echo "Änderungen wurden gespeichert.";
  251. echo $msg;
  252. } else {
  253. // cannot insert
  254. echo "Änderungen konnten nicht gespeichert werden.";
  255. echo $msg;
  256. }
  257. Core::closeDB();
  258. exit;
  259. }
  260.  
  261. function updateStaffAction($staff){
  262. $msg = "";
  263. if (isset($staff['birthday']) && $staff['birthday'] !== "") {
  264. $birthday = parseBirthday($staff['birthday'], $msg, 'Leider ist beim Parsen des Geburtsdatums ein Fehler unterlaufen.');
  265. }
  266.  
  267. $new_staff = array();
  268. $new_staff['id_staff'] = $staff['id_staff'];
  269. $new_staff['active'] = (isset($staff['active']) ? urldecode($staff['active']) : 'true');
  270. $new_staff['surname'] = (isset($staff['surname']) ? urldecode($staff['surname']) : '');
  271. $new_staff['prename'] = (isset($staff['prename']) ? urldecode($staff['prename']) : '');
  272.  
  273. $new_staff['birthday'] = $birthday;
  274. $new_staff['telephone'] = (isset($staff['telephone']) ? urldecode($staff['telephone']) : '');
  275. $new_staff['description_text'] = (isset($staff['description']) ? urldecode($staff['description']) : '');
  276.  
  277. Core::connectDB();
  278. if (Core::updateStaff($new_staff)) {
  279. // everything is ok. return msg to user.
  280. echo "Änderungen wurden gespeichert.";
  281. echo $msg;
  282. } else {
  283. // cannot insert
  284. echo "Änderungen konnten nicht gespeichert werden.";
  285. echo $msg;
  286. }
  287. Core::closeDB();
  288. exit;
  289. }
  290.  
  291.  
  292. function listPatientsAction(){
  293. Core::connectDB();
  294. $patients = Core::getPatients();
  295. echo json_encode($patients);
  296. //echo var_dump($patients);
  297. Core::closeDB();
  298. exit;
  299. }
  300.  
  301. function getPatientAction($id_patient){
  302. Core::connectDB();
  303. $patient = Core::getPatient($id_patient);
  304. echo json_encode($patient);
  305. Core::closeDB();
  306. exit;
  307. }
  308.  
  309. function getPatientTreatmentAction($id_patient_treatment){
  310. Core::connectDB();
  311. $treatmentdate = Core::getPatientTreatment($id_patient_treatment);
  312. echo json_encode($treatmentdate);
  313. Core::closeDB();
  314. exit;
  315. }
  316.  
  317. function getTreatmentAction($id_treatment){
  318. Core::connectDB();
  319. $treatment = Core::getTreatment($id_treatment);
  320. echo json_encode($treatment);
  321. Core::closeDB();
  322. exit;
  323. }
  324.  
  325. function getStaffAction($id_staff){
  326. Core::connectDB();
  327. $staff = Core::getStaff($id_staff);
  328. echo json_encode($staff);
  329. Core::closeDB();
  330. exit;
  331. }
  332.  
  333. function getTreatmentDatesAction($id_patient){
  334. Core::connectDB();
  335. $treatmentDates = Core::getTreatmentDates($id_patient);
  336. echo json_encode($treatmentDates);
  337. Core::closeDB();
  338. exit;
  339. }
  340.  
  341. function getWorkingTimesAction($id_staff, $date=null){
  342. Core::connectDB();
  343. if(isset($date) && $date !== ""){
  344. $date = date("Y-m-d", strtotime(urldecode($date)));
  345. }
  346. $workingtimes = Core::getWorkingTimes($id_staff, $date);
  347. //$workingtimes = null;
  348. echo json_encode($workingtimes);
  349. Core::closeDB();
  350. exit;
  351. }
  352.  
  353. function removePatientAction($id_patient){
  354. Core::connectDB();
  355. $result = Core::removePatient($id_patient);
  356. echo $result;
  357. //echo "Patient".$id_patient;
  358. Core::closeDB();
  359. exit;
  360. }
  361.  
  362. function removeStaffAction($id_staff){
  363. Core::connectDB();
  364. $result = Core::removeStaff($id_staff);
  365. echo $result;
  366. //echo "staff".$id_staff;
  367. Core::closeDB();
  368. exit;
  369. }
  370.  
  371. function removeWorkingTimeAction($id_staff_workingtime){
  372. //echo "removeWorkingTimeAction: ". $id_staff_workingtime."\n";
  373. Core::connectDB();
  374. $result = Core::removeStaffWorkingTime($id_staff_workingtime);
  375. //echo $result;
  376. //echo "Patient".$id_patient;
  377. Core::closeDB();
  378. exit;
  379. }
  380.  
  381. function removePatientTreatmentAction($id_patient_treatment){
  382. Core::connectDB();
  383. $result = Core::removeTreatmentDate($id_patient_treatment);
  384. echo $result;
  385. //echo "testiyeah: ".$id_patient_treatment;
  386. //echo "Patient".$id_patient;
  387. Core::closeDB();
  388. exit;
  389. }
  390. function getStaffListAction(){
  391. Core::connectDB();
  392. $staff = Core::getStaffList();
  393. echo json_encode($staff);
  394. //echo var_dump($patients);
  395. Core::closeDB();
  396. exit;
  397. }
  398.  
  399. function getTreatmentsAction(){
  400. Core::connectDB();
  401. $treatments = Core::getTreatments();
  402. echo json_encode($treatments);
  403. //echo var_dump($treatments);
  404. Core::closeDB();
  405. exit;
  406. }
  407.  
  408. function setStaffActiveCheckAction($id_staff, $checked){
  409. $isChecked = false;
  410. if($checked === "true"){
  411. $isChecked = true;
  412. }
  413.  
  414. Core::connectDB();
  415. if (Core::updateStaffActiveCheck($id_staff, $checked)) {
  416. // everything is ok. return msg to user.
  417. echo "Änderungen wurden gespeichert.";
  418. echo $msg;
  419. } else {
  420. // cannot insert
  421. echo "Änderungen konnten nicht gespeichert werden.";
  422. echo $msg;
  423. }
  424. Core::closeDB();
  425. }
  426.  
  427. function setPatientTreatmentCheckAction($id_patient_treatment, $checked){
  428. $isChecked = false;
  429. if($checked === "true"){
  430. $isChecked = true;
  431. }
  432.  
  433. //exit;
  434. Core::connectDB();
  435. if (Core::updatePatientTreatmentCheck($id_patient_treatment, $checked)) {
  436. // everything is ok. return msg to user.
  437. //echo "Änderungen wurden gespeichert.";
  438. //echo $msg;
  439. } else {
  440. // cannot insert
  441. echo "Änderungen konnten nicht gespeichert werden.";
  442. echo $msg;
  443. }
  444. Core::closeDB();
  445.  
  446. }
  447.  
  448. function checkOverlappingPatientTreatmentAction($id_patient_treatment){
  449. Core::connectDB();
  450.  
  451. $patient_treatment = Core::getPatientTreatment($id_patient_treatment);
  452. if(Core::checkOverlappingTreatmentDates($patient_treatment['id_staff'], $patient_treatment['treatment_date'], $patient_treatment['treatment_time_end'])){
  453. echo "overlap";
  454.  
  455. }
  456. else {
  457. echo "no overlap";
  458. }
  459. Core::closeDB();
  460. exit;
  461. }
  462.  
  463. switch($_POST['action']){
  464. case 'insert_patient':
  465. //echo var_dump($_POST['patient']);
  466. insertPatientAction($_POST['patient']);
  467. break;
  468. case 'remove_patient':
  469. removePatientAction($_POST['id_patient']);
  470. break;
  471. case 'remove_staff':
  472. removeStaffAction($_POST['id_staff']);
  473. break;
  474. case 'update_patient':
  475. updatePatientAction($_POST['patient']);
  476. break;
  477. case 'update_staff':
  478. updateStaffAction($_POST['staff']);
  479. break;
  480. case 'get_patients':
  481. listPatientsAction();
  482. break;
  483. case 'get_patient':
  484. getPatientAction($_POST['id_patient']);
  485. break;
  486. case 'get_staff':
  487. getStaffAction($_POST['id_staff']);
  488. break;
  489. case 'get_stafflist':
  490. getStaffListAction();
  491. break;
  492. case 'insert_staff':
  493. insertStaffAction($_POST['staff']);
  494. break;
  495. case 'get_treatment':
  496. getTreatmentAction($_POST['id_treatment']);
  497. break;
  498. case 'get_treatments':
  499. getTreatmentsAction();
  500. break;
  501. case 'set_staff_active_check':
  502. setStaffActiveCheckAction($_POST['id_staff'], $_POST['checked']);
  503. break;
  504. case 'set_patient_treatment_check':
  505. setPatientTreatmentCheckAction($_POST['id_patient_treatment'], $_POST['checked']);
  506. break;
  507. case 'get_patient_treatment':
  508. getPatientTreatmentAction($_POST['id_patient_treatment']);
  509. break;
  510. case 'insert_patient_treatment':
  511. insertPatientTreatmentAction($_POST['id_patient'], $_POST['id_staff'], $_POST['id_treatment'], $_POST['treatment_date'], $_POST['treatment_time_end'], $_POST['treatment_date_end'], $_POST['description']);
  512. break;
  513. case 'remove_patient_treatment':
  514. removePatientTreatmentAction($_POST['id_patient_treatment']);
  515. break;
  516. case 'get_treatment_dates':
  517. getTreatmentDatesAction($_POST['id_patient']);
  518. break;
  519. case 'get_patient_treatment_day':
  520. getPatientTreatmentDayAction($_POST['date']);
  521. break;
  522. case 'insert_working_time':
  523. insertWorkingTimeAction($_POST['id_staff'], $_POST['beginning_date'], $_POST['end_date'], $_POST['matter']);
  524. break;
  525. case 'get_working_times':
  526. getWorkingTimesAction((isset($_POST['id_staff']) ? $_POST['id_staff'] : null), $_POST['date']) ;
  527. break;
  528. case 'remove_staff_workingtime':
  529. removeWorkingTimeAction($_POST['id_staff_workingtime']);
  530. break;
  531. default:
  532. break;
  533. }
  534.  
  535.  
  536.  
  537. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement