Portal and core fixups. (#3525)
[openemr.git] / ippf_upgrade.php
blob1936778b375fb8c6770e812ffe0e20950eb8d166
1 <?php
3 // Copyright (C) 2009 Rod Roark <rod@sunsetsystems.com>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // Disable PHP timeout. This will not work in safe mode.
11 ini_set('max_execution_time', '0');
13 $ignoreAuth = true; // no login required
15 require_once('interface/globals.php');
16 require_once('library/forms.inc');
18 $verbose = 0;
19 $debug = 0;
20 $insert_count = 0;
22 // Create a visit form from an abortion issue. This may be called
23 // multiple times for a given issue.
25 function do_visit_form($irow, $encounter, $first)
27 global $insert_count, $debug, $verbose;
29 $pid = $irow['pid'];
31 // If a gcac form already exists for this visit, get out.
32 $row = sqlQuery("SELECT COUNT(*) AS count FROM forms WHERE " .
33 "pid = '$pid' AND encounter = '$encounter' AND " .
34 "formdir = 'LBFgcac' AND deleted = 0");
35 if ($row['count']) {
36 echo "<br />*** Visit $pid.$encounter skipped, already has a GCAC visit form ***\n";
37 return;
40 $a = array(
41 'client_status' => $irow['client_status'],
42 'in_ab_proc' => $irow['in_ab_proc'],
43 'ab_location' => $irow['ab_location'],
44 'complications' => $irow['fol_compl'],
45 'contrameth' => $irow['contrameth'],
48 // logic that applies only to the first related visit
49 if ($first) {
50 if ($a['ab_location'] == 'ma') {
51 $a['ab_location'] = 'proc';
54 $a['complications'] = $irow['rec_compl'];
55 $a['contrameth'] = '';
58 $newid = 0;
59 $didone = false;
60 foreach ($a as $field_id => $value) {
61 if ($value !== '') {
62 if ($newid) {
63 $query = "INSERT INTO lbf_data " .
64 "( form_id, field_id, field_value ) " .
65 " VALUES ( '$newid', '$field_id', '$value' )";
66 if ($verbose) {
67 echo "<br />$query\n";
70 if (!$debug) {
71 sqlStatement($query);
73 } else {
74 $query = "INSERT INTO lbf_data " .
75 "( field_id, field_value ) " .
76 " VALUES ( '$field_id', '$value' )";
77 if ($verbose) {
78 echo "<br />$query\n";
81 if (!$debug) {
82 $newid = sqlInsert($query);
86 $didone = true;
90 if ($newid && !$debug) {
91 addForm($encounter, 'IPPF GCAC', $newid, 'LBFgcac', $pid, 1);
92 ++$insert_count;
95 if (!$didone) {
96 echo "<br />*** Empty issue skipped for visit $pid.$encounter ***\n";
100 <html>
101 <head>
102 <title>OpenEMR IPPF Upgrade</title>
103 <link rel='STYLESHEET' href='interface/themes/style_blue.css'>
104 </head>
105 <body>
106 <center>
107 <span class='title'>OpenEMR IPPF Upgrade</span>
108 <br />
109 </center>
110 <?php
111 if (!empty($_POST['form_submit'])) {
112 // If database is not utf8, convert it.
113 $trow = sqlQuery("SHOW CREATE DATABASE $dbase");
114 array_shift($trow);
115 $value = array_shift($trow);
116 if (!preg_match('/SET utf8/', $value)) {
117 echo "<br />Converting database to UTF-8 encoding...";
118 $tres = sqlStatement("SHOW TABLES");
119 while ($trow = sqlFetchArray($tres)) {
120 $value = array_shift($trow);
121 $query = "ALTER TABLE $value CONVERT TO CHARACTER SET utf8";
122 if ($verbose) {
123 echo "<br />$query\n";
126 sqlStatement($query);
129 $query = "ALTER DATABASE $dbase CHARACTER SET utf8";
130 if ($verbose) {
131 echo "<br />$query\n";
134 sqlStatement($query);
135 echo "<br />&nbsp;\n";
138 $ires = sqlStatement("SELECT " .
139 "l.pid, l.id, l.type, l.begdate, l.title, " .
140 "g.client_status, g.in_ab_proc, g.ab_location, " .
141 "g.rec_compl, g.contrameth, g.fol_compl " .
142 "FROM lists AS l " .
143 "JOIN lists_ippf_gcac AS g ON l.type = 'ippf_gcac' AND g.id = l.id " .
144 "ORDER BY l.pid, l.begdate");
146 while ($irow = sqlFetchArray($ires)) {
147 $patient_id = $irow['pid'];
148 $list_id = $irow['id'];
149 $first = true;
151 $ieres = sqlStatement("SELECT encounter " .
152 "FROM issue_encounter " .
153 "WHERE pid = '$patient_id' AND list_id = '$list_id' " .
154 "ORDER BY encounter");
156 if (sqlNumRows($ieres)) {
157 while ($ierow = sqlFetchArray($ieres)) {
158 do_visit_form($irow, $ierow['encounter'], $first);
159 $first = false;
161 } else {
162 echo "<br />*** Issue $list_id for pid $patient_id has no linked visits, skipped ***\n";
166 echo "<p><font color='green'>Done. Inserted $insert_count visit forms.</font></p>\n";
167 echo "</body></html>\n";
168 exit();
172 <p>This converts your OpenEMR database to UTF-8 encoding if it is not already,
173 and also converts GCAC issues to the corresponding visit forms. Both of these
174 steps are needed for IPPF sites upgrading from releases prior to 2009-08-27.</p>
175 <center>
176 <form method='post' action='ippf_upgrade.php'>
177 <p><input type='submit' name='form_submit' value='Convert Database' /></p>
178 </form>
179 </center>
180 </body>
181 </html>