]> Softwares of Agnibho - simpleipd.git/blob - lib/functions.php
c3b6923361ae4c678f60b6bb5b9f7f4f4c403b5e
[simpleipd.git] / lib / functions.php
1 <?php
2 function schema2form($file, $pid=null, $id=null, $cat=null){
3 global $db;
4 $schema=json_decode(file_get_contents($file));
5
6 $lockpid="";
7 if(!empty($pid) && !empty($id) && !empty($cat)){
8 $data=json_decode($db->getData($pid, $id, $cat)->fetchArray()["data"]);
9 }
10 elseif(!empty($pid) && $file=="forms/admission.schema.json"){
11 $data=json_decode($db->getAdmission($pid)->fetchArray()["data"]);
12 $lockpid="readonly";
13 }
14 else{
15 $data=null;
16 }
17
18 $form="<form method='post'>";
19 $form=$form."<input type='hidden' name='form' value='".str_replace(["forms/",".schema.json"], "", $file)."'>";
20
21 foreach($schema->properties as $field=>$prop){
22 if($prop->type == "integer") $prop->type="number";
23 if($prop->type == "string") $prop->type="text";
24 if(!empty($data)){
25 $value="value='".$data->$field."'";
26 }
27 else{
28 $value="";
29 }
30 if(in_array($field, $schema->required)){
31 $req="required";
32 }
33 else{
34 $req="";
35 }
36 if(isSet($prop->format)){
37 $type=$prop->format;
38 }
39 else{
40 $type=$prop->type;
41 }
42
43 $form=$form."<div>";
44 $form=$form."<label class='form-label' for='".$field."'>".$prop->description."</label>";
45 if(isSet($prop->enum)){
46 $form=$form."<select class='form-control' ".$req." name='".$field."' id='".$field."'>";
47 foreach($prop->enum as $opt){
48 $form=$form."<option>".$opt."</option>";
49 }
50 $form=$form."</select>";
51 }
52 elseif($field=="pid"){
53 $form=$form."<input class='form-control' ".$lockpid." ".$req." type='".$type."' name='".$field."' id='".$field."' ".$value.">";
54 }
55 else{
56 $form=$form."<input class='form-control' ".$req." type='".$type."' name='".$field."' id='".$field."' ".$value.">";
57 }
58 $form=$form."</div>";
59
60 }
61 $form=$form."<div><label class='form-label' for='extra-note'>Notes</label><textarea class='form-control' name='extra-note' id='extra-note'></textarea></div>";
62 $form=$form."<button class='btn btn-primary mt-3' type='submit'>Save</button>";
63 $form=$form."</form>";
64 return $form;
65 }
66
67 function getInfo($pid){
68 global $db;
69 $info="<table class='table'>";
70 $info=$info."<tr><td>ID</td><td>".$pid."</td></tr>";
71 $info=$info."<tr><td>Name</td><td>".$db->getName($pid)->fetchArray()["name"]."</td></tr>";
72 $info=$info."<tr><td>Age</td><td>".$db->getAge($pid)->fetchArray()["age"]."</td></tr>";
73 $info=$info."<tr><td>Sex</td><td>".$db->getSex($pid)->fetchArray()["sex"]."</td></tr>";
74 $info=$info."</table>";
75 return $info;
76 }
77
78 function viewData($data, $edit=null){
79 $data=json_decode($data);
80 unset($data->cat);
81 $view="<table class='table'>";
82 foreach($data as $field=>$value){
83 $view=$view."<tr><td>".$field."</td><td>".$value."</td></tr>";
84 }
85 if(!empty($edit)){
86 $view=$view."<tr><td><a href='".$edit."'>Edit</a>";
87 }
88 $view=$view."</table>";
89 return $view;
90 }
91
92 function view_drug($file){
93 if(is_file($file)){
94 $druglist=json_decode(file_get_contents($file));
95 }
96 else{
97 $druglist=[];
98 }
99 $view="<form method='post' id='omitter'></form><table class='table'>";
100 $view=$view."<tr><th>Drug</th><th>Dose</th><th>Route</th><th>Frequency</th><th>Duration</th></tr>";
101 foreach($druglist as $id=>$drug){
102 if($drug->omit){
103 $omit="style='display:none'";
104 }
105 else{
106 $omit="";
107 }
108 $view=$view."<tr><td>".$drug->drug."</td><td>".$drug->dose."</td><td>".$drug->route."</td><td>".$drug->freq."</td><td>".$drug->duration."</td><td>".$drug->note."</td><td><button name='omit' value='".$id."' form='omitter' ".$omit.">Omit</button></td></tr>";
109 }
110 $view=$view."</table>";
111 return $view;
112 }
113 ?>