Merge pull request #1346 from bradymiller/update-synd-surv-report_1
[openemr.git] / ippf_upgrade.php
blobc30f14a9bfddc15a0ab34edf4c761d320062dd7a
1 <?php
2 // Copyright (C) 2009 Rod Roark <rod@sunsetsystems.com>
3 //
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 // Disable PHP timeout. This will not work in safe mode.
10 ini_set('max_execution_time', '0');
12 $ignoreAuth = true; // no login required
14 require_once('interface/globals.php');
15 require_once('library/forms.inc');
17 $verbose = 0;
18 $debug = 0;
19 $insert_count = 0;
21 // Create a visit form from an abortion issue. This may be called
22 // multiple times for a given issue.
24 function do_visit_form($irow, $encounter, $first)
26 global $insert_count, $debug, $verbose;
28 $pid = $irow['pid'];
30 // If a gcac form already exists for this visit, get out.
31 $row = sqlQuery("SELECT COUNT(*) AS count FROM forms WHERE " .
32 "pid = '$pid' AND encounter = '$encounter' AND " .
33 "formdir = 'LBFgcac' AND deleted = 0");
34 if ($row['count']) {
35 echo "<br />*** Visit $pid.$encounter skipped, already has a GCAC visit form ***\n";
36 return;
39 $a = array(
40 'client_status' => $irow['client_status'],
41 'in_ab_proc' => $irow['in_ab_proc'],
42 'ab_location' => $irow['ab_location'],
43 'complications' => $irow['fol_compl'],
44 'contrameth' => $irow['contrameth'],
47 // logic that applies only to the first related visit
48 if ($first) {
49 if ($a['ab_location'] == 'ma') {
50 $a['ab_location'] = 'proc';
53 $a['complications'] = $irow['rec_compl'];
54 $a['contrameth'] = '';
57 $newid = 0;
58 $didone = false;
59 foreach ($a as $field_id => $value) {
60 if ($value !== '') {
61 if ($newid) {
62 $query = "INSERT INTO lbf_data " .
63 "( form_id, field_id, field_value ) " .
64 " VALUES ( '$newid', '$field_id', '$value' )";
65 if ($verbose) {
66 echo "<br />$query\n";
69 if (!$debug) {
70 sqlStatement($query);
72 } else {
73 $query = "INSERT INTO lbf_data " .
74 "( field_id, field_value ) " .
75 " VALUES ( '$field_id', '$value' )";
76 if ($verbose) {
77 echo "<br />$query\n";
80 if (!$debug) {
81 $newid = sqlInsert($query);
85 $didone = true;
89 if ($newid && !$debug) {
90 addForm($encounter, 'IPPF GCAC', $newid, 'LBFgcac', $pid, 1);
91 ++$insert_count;
94 if (!$didone) {
95 echo "<br />*** Empty issue skipped for visit $pid.$encounter ***\n";
99 <html>
100 <head>
101 <title>OpenEMR IPPF Upgrade</title>
102 <link rel='STYLESHEET' href='interface/themes/style_blue.css'>
103 </head>
104 <body>
105 <center>
106 <span class='title'>OpenEMR IPPF Upgrade</span>
107 <br>
108 </center>
109 <?php
110 if (!empty($_POST['form_submit'])) {
111 // If database is not utf8, convert it.
112 $trow = sqlQuery("SHOW CREATE DATABASE $dbase");
113 array_shift($trow);
114 $value = array_shift($trow);
115 if (!preg_match('/SET utf8/', $value)) {
116 echo "<br />Converting database to UTF-8 encoding...";
117 $tres = sqlStatement("SHOW TABLES");
118 while ($trow = sqlFetchArray($tres)) {
119 $value = array_shift($trow);
120 $query = "ALTER TABLE $value CONVERT TO CHARACTER SET utf8";
121 if ($verbose) {
122 echo "<br />$query\n";
125 sqlStatement($query);
128 $query = "ALTER DATABASE $dbase CHARACTER SET utf8";
129 if ($verbose) {
130 echo "<br />$query\n";
133 sqlStatement($query);
134 echo "<br />&nbsp;\n";
137 $ires = sqlStatement("SELECT " .
138 "l.pid, l.id, l.type, l.begdate, l.title, " .
139 "g.client_status, g.in_ab_proc, g.ab_location, " .
140 "g.rec_compl, g.contrameth, g.fol_compl " .
141 "FROM lists AS l " .
142 "JOIN lists_ippf_gcac AS g ON l.type = 'ippf_gcac' AND g.id = l.id " .
143 "ORDER BY l.pid, l.begdate");
145 while ($irow = sqlFetchArray($ires)) {
146 $patient_id = $irow['pid'];
147 $list_id = $irow['id'];
148 $first = true;
150 $ieres = sqlStatement("SELECT encounter " .
151 "FROM issue_encounter " .
152 "WHERE pid = '$patient_id' AND list_id = '$list_id' " .
153 "ORDER BY encounter");
155 if (sqlNumRows($ieres)) {
156 while ($ierow = sqlFetchArray($ieres)) {
157 do_visit_form($irow, $ierow['encounter'], $first);
158 $first = false;
160 } else {
161 echo "<br />*** Issue $list_id for pid $patient_id has no linked visits, skipped ***\n";
165 echo "<p><font color='green'>Done. Inserted $insert_count visit forms.</font></p>\n";
166 echo "</body></html>\n";
167 exit();
171 <p>This converts your OpenEMR database to UTF-8 encoding if it is not already,
172 and also converts GCAC issues to the corresponding visit forms. Both of these
173 steps are needed for IPPF sites upgrading from releases prior to 2009-08-27.</p>
174 <center>
175 <form method='post' action='ippf_upgrade.php'>
176 <p><input type='submit' name='form_submit' value='Convert Database' /></p>
177 </form>
178 </center>
179 </body>
180 </html>