fix to prior commit
[openemr.git] / ippf_upgrade.php
blobe7f2f0a75c45c993d30536597419bc5513e746dc
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) {
25 global $insert_count, $debug, $verbose;
27 $pid = $irow['pid'];
29 // If a gcac form already exists for this visit, get out.
30 $row = sqlQuery("SELECT COUNT(*) AS count FROM forms WHERE " .
31 "pid = '$pid' AND encounter = '$encounter' AND " .
32 "formdir = 'LBFgcac' AND deleted = 0");
33 if ($row['count']) {
34 echo "<br />*** Visit $pid.$encounter skipped, already has a GCAC visit form ***\n";
35 return;
38 $a = array(
39 'client_status' => $irow['client_status'],
40 'in_ab_proc' => $irow['in_ab_proc'],
41 'ab_location' => $irow['ab_location'],
42 'complications' => $irow['fol_compl'],
43 'contrameth' => $irow['contrameth'],
46 // logic that applies only to the first related visit
47 if ($first) {
48 if ($a['ab_location'] == 'ma') $a['ab_location'] = 'proc';
49 $a['complications'] = $irow['rec_compl'];
50 $a['contrameth'] = '';
53 $newid = 0;
54 $didone = false;
55 foreach ($a as $field_id => $value) {
56 if ($value !== '') {
57 if ($newid) {
58 $query = "INSERT INTO lbf_data " .
59 "( form_id, field_id, field_value ) " .
60 " VALUES ( '$newid', '$field_id', '$value' )";
61 if ($verbose) echo "<br />$query\n";
62 if (!$debug) sqlStatement($query);
64 else {
65 $query = "INSERT INTO lbf_data " .
66 "( field_id, field_value ) " .
67 " VALUES ( '$field_id', '$value' )";
68 if ($verbose) echo "<br />$query\n";
69 if (!$debug) $newid = sqlInsert($query);
71 $didone = true;
75 if ($newid && !$debug) {
76 addForm($encounter, 'IPPF GCAC', $newid, 'LBFgcac', $pid, 1);
77 ++$insert_count;
80 if (!$didone) echo "<br />*** Empty issue skipped for visit $pid.$encounter ***\n";
83 <html>
84 <head>
85 <title>OpenEMR IPPF Upgrade</title>
86 <link rel='STYLESHEET' href='interface/themes/style_blue.css'>
87 </head>
88 <body>
89 <center>
90 <span class='title'>OpenEMR IPPF Upgrade</span>
91 <br>
92 </center>
93 <?php
94 if (!empty($_POST['form_submit'])) {
96 // If database is not utf8, convert it.
97 $trow = sqlQuery("SHOW CREATE DATABASE $dbase");
98 array_shift($trow);
99 $value = array_shift($trow);
100 if (!preg_match('/SET utf8/', $value)) {
101 echo "<br />Converting database to UTF-8 encoding...";
102 $tres = sqlStatement("SHOW TABLES");
103 while ($trow = sqlFetchArray($tres)) {
104 $value = array_shift($trow);
105 $query = "ALTER TABLE $value CONVERT TO CHARACTER SET utf8";
106 if ($verbose) echo "<br />$query\n";
107 sqlStatement($query);
109 $query = "ALTER DATABASE $dbase CHARACTER SET utf8";
110 if ($verbose) echo "<br />$query\n";
111 sqlStatement($query);
112 echo "<br />&nbsp;\n";
115 $ires = sqlStatement("SELECT " .
116 "l.pid, l.id, l.type, l.begdate, l.title, " .
117 "g.client_status, g.in_ab_proc, g.ab_location, " .
118 "g.rec_compl, g.contrameth, g.fol_compl " .
119 "FROM lists AS l " .
120 "JOIN lists_ippf_gcac AS g ON l.type = 'ippf_gcac' AND g.id = l.id " .
121 "ORDER BY l.pid, l.begdate");
123 while ($irow = sqlFetchArray($ires)) {
124 $patient_id = $irow['pid'];
125 $list_id = $irow['id'];
126 $first = true;
128 $ieres = sqlStatement("SELECT encounter " .
129 "FROM issue_encounter " .
130 "WHERE pid = '$patient_id' AND list_id = '$list_id' " .
131 "ORDER BY encounter");
133 if (sqlNumRows($ieres)) {
134 while ($ierow = sqlFetchArray($ieres)) {
135 do_visit_form($irow, $ierow['encounter'], $first);
136 $first = false;
139 else {
140 echo "<br />*** Issue $list_id for pid $patient_id has no linked visits, skipped ***\n";
144 echo "<p><font color='green'>Done. Inserted $insert_count visit forms.</font></p>\n";
145 echo "</body></html>\n";
146 exit();
150 <p>This converts your OpenEMR database to UTF-8 encoding if it is not already,
151 and also converts GCAC issues to the corresponding visit forms. Both of these
152 steps are needed for IPPF sites upgrading from releases prior to 2009-08-27.</p>
153 <center>
154 <form method='post' action='ippf_upgrade.php'>
155 <p><input type='submit' name='form_submit' value='Convert Database' /></p>
156 </form>
157 </center>
158 </body>
159 </html>