2 // Copyright (C) 2009 Rod Roark <rod@sunsetsystems.com>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // This provides enhancement functions for the LBFgcac visit form.
10 // It is invoked by interface/forms/LBF/new.php.
12 // Private function. Constructs a query to find a given lbf_data field's
13 // values from visits within the past 2 weeks.
15 function _LBFgcac_query_recent($more) {
16 global $pid, $encounter, $formname, $formid;
18 // Get the date of this visit.
19 $encrow = sqlQuery("SELECT date FROM form_encounter WHERE " .
20 "pid = '$pid' AND encounter = '$encounter'");
21 $encdate = $encrow['date'];
23 // Query complications from the two weeks prior to this visit.
24 $query = "SELECT d.field_value " .
25 "FROM forms AS f, form_encounter AS fe, lbf_data AS d " .
26 "WHERE f.pid = '$pid' AND " .
27 "f.formdir = '$formname' AND " .
28 "f.deleted = 0 AND " .
29 "fe.pid = f.pid AND fe.encounter = f.encounter AND " .
30 "fe.date <= '$encdate' AND " .
31 "DATE_ADD(fe.date, INTERVAL 14 DAY) > '$encdate' AND " .
32 "d.form_id = f.form_id AND $more";
37 // Private function. Given a field name, gets its value from the most
38 // recent instance of this form type that is not more than 2 weeks old.
40 function _LBFgcac_recent_default($name) {
43 // This logic only makes sense for a new form.
44 if ($formid) return '';
46 $query = _LBFgcac_query_recent(
47 "d.field_id = '$name' " .
48 "ORDER BY f.form_id DESC LIMIT 1");
49 $row = sqlQuery($query);
51 if (empty($row['field_value'])) return '';
52 return $row['field_value'];
55 // The purpose of this function is to create JavaScript for the <head>
56 // section of the page. This in turn defines desired javaScript
59 function LBFgcac_javascript() {
62 // Query complications from the two weeks prior to this visit.
63 $res = sqlStatement(_LBFgcac_query_recent(
64 "f.form_id != '$formid' AND " .
65 "d.field_id = 'complications'"));
67 // This JavaScript function is to enable items in the "Main complications"
68 // list that have been selected, and to disable all others.
69 // Option.disabled seems to work for Firefox 3 and IE8 but not IE6.
70 echo "// Enable recent complications and disable all others.
71 function set_main_compl_list() {
72 var f = document.forms[0];
73 var sel = f.form_main_compl;
76 // We use the checkbox object values as a scratch area to note which
77 // complications were already selected from other forms.
78 while ($row = sqlFetchArray($res)) {
79 $a = explode('|', $row['field_value']);
80 foreach ($a as $complid) {
81 if (empty($complid)) continue;
82 echo " n = 'form_complications[$complid]'; if (f[n]) f[n].value = 2;\n";
85 echo " // Scan the list items and set their disabled flags.
86 for (var i = 1; i < sel.options.length; ++i) {
87 n = 'form_complications[' + sel.options[i].value + ']';
88 sel.options[i].disabled = (f[n] && (f[n].checked || f[n].value == '2')) ? false : true;
93 // Disable most form fields if refusing abortion.
94 function client_status_changed() {
95 var f = document.forms[0];
97 var cs = f.form_client_status;
98 if (cs.type) { // cs = select list
99 dis = cs.selectedIndex >= 0 && cs.options[cs.selectedIndex].value == 'mara';
101 else { // cs = array of radio buttons
102 for (var i = 0; i < cs.length; ++i) {
104 dis = cs[i].value == 'mara';
109 for (var i = 0; i < f.elements.length; ++i) {
110 var e = f.elements[i];
112 e.name.substring(0,18) == 'form_complications' ||
113 e.name.substring(0,15) == 'form_contrameth' ||
114 e.name == 'form_ab_location' ||
115 e.name == 'form_in_ab_proc' ||
116 e.name == 'form_main_compl'
126 // The purpose of this function is to create JavaScript that is run
127 // once when the page is loaded.
129 function LBFgcac_javascript_onload() {
131 set_main_compl_list();
132 client_status_changed();
133 var f = document.forms[0];
134 for (var i = 0; i < f.elements.length; ++i) {
135 var e = f.elements[i];
136 if (e.name.substring(0,18) == 'form_complications')
137 e.onclick = function () { set_main_compl_list(); };
139 var cs = f.form_client_status;
140 if (cs.type) { // cs = select list
141 cs.onchange = function () { client_status_changed(); };
143 else { // cs = array of radio buttons
144 for (var i = 0; i < cs.length; ++i) {
145 cs[i].onclick = function () { client_status_changed(); };
151 // Generate default for client status.
153 function LBFgcac_default_client_status() {
154 return _LBFgcac_recent_default('client_status');
157 // Generate default for visit type. If there are no recent prior visits,
158 // then default to new procedure.
160 function LBFgcac_default_ab_location() {
162 if ($formid) return '';
163 $vt = _LBFgcac_recent_default('ab_location');
164 if (empty($vt)) return 'proc';
168 // Generate default for the induced procedure type.
170 function LBFgcac_default_in_ab_proc() {
171 return _LBFgcac_recent_default('in_ab_proc');
174 // Generate default for the main complication.
176 function LBFgcac_default_main_compl() {
177 return _LBFgcac_recent_default('main_compl');