~begerega/ihris-chw-sl/trunk

« back to all changes in this revision

Viewing changes to modules/NCFacility/csv2module.php

  • Committer: Ese Egerega
  • Date: 2018-05-03 14:17:04 UTC
  • Revision ID: egerega@gmail.com-20180503141704-3br8dto013rgx65x
Initial import of Sierra Leone  CHW registry

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Syntax:  php csv2module.php
 
5
 *      --form=FORMNAME
 
6
 *      --fields=FIELD1,FIELD2,ETC.
 
7
 *      --version=X.X.X
 
8
 *      [--module=MODULENAME]
 
9
 *      [--display="Module Display Name"]
 
10
 *      [--id=FIELD1]
 
11
 *      [--who=USERID]
 
12
 *      [--file=FILE.CSV]
 
13
 *      [--internalid]
 
14
 *      [--erase]
 
15
 *      [--reqs=REQUIRED_MOD1[:MINVER[:MAXVER]],REQUIRED_MOD2[:MINVER[:MAXVER]]]
 
16
 *
 
17
 * The output module will be displayed to the console, to save it to a file
 
18
 * redirect the output to the filename you'd like to use.
 
19
 *
 
20
 * The fields must be in the order they appear in the CSV file.
 
21
 * If any fields are MAPPED fields, then the mapped value needs to be in the
 
22
 * CSV file, e.g. district|10.  No lookups are done during this process, the exact
 
23
 * values must be used in the CSV.
 
24
 *
 
25
 * If no module name is given, the default will be "Default-data-FORMNAME"
 
26
 * If no display name is given, the default will be "Default data for: FORMNAME"
 
27
 * If no filename is given, the default will be "FORMNAME.csv"
 
28
 *
 
29
 * The id argument will be the fieldname from the fields argument to use
 
30
 * as the id of the form.  If the internalid argument is used, then the
 
31
 * id field will not be present as a field for the form and only used as the id.
 
32
 * If there is no id field, then an incremented number will be used.
 
33
 *
 
34
 * The who argument is the userid to use for the forms.  The default is 1.
 
35
 *
 
36
 * If the erase argument is used then all previous values in magic data
 
37
 * will be removed for this form.
 
38
 *
 
39
 * The reqs argument can be used to set any required modules for this module with
 
40
 * an optional minimum and maximum version to be used.
 
41
 *
 
42
 * A few examples:
 
43
 *   php csv2module.php --form=district --fields=code,name,region --id=code --version=4.1.8.0 --erase --reqs=Geography:4.1:4.2 > Default-data-district.xml
 
44
 *   The code field will be used as the ID for each district.  
 
45
 *   This would read the file district.csv in the current directory 
 
46
 *   and create the file Default-data-district.xml.
 
47
 *
 
48
 *   php csv2module.php --form=marital_status --fields=name --version=4.1.8.0 --erase --reqs=PersonDemographic:4.1:4.2 > Default-data-marital_status.xml
 
49
 *   This will use numbers for the ids starting at 1.  If you'd like to include 
 
50
 *   your own ids:
 
51
 *   php csv2module.php --form=marital_status --fields=id,name --id=id --internalid --version=4.1.8.0 --erase --reqs=PersonDemographic:4.1:4.2 > Default-data-marital_status.xml
 
52
 *   These would read the file marital_status.csv in the current directory 
 
53
 *   and create the file Default-data-marital_status.xml.
 
54
 *
 
55
 */
 
56
 
 
57
$args = getopt( "", array(
 
58
            "module:",
 
59
            "form:",
 
60
            "display::",
 
61
            "fields:",
 
62
            "id::",
 
63
            "who::",
 
64
            "file:",
 
65
            "version:",
 
66
            "internalid",
 
67
            "erase",
 
68
            "reqs:",
 
69
) );
 
70
 
 
71
$form = $args['form'];
 
72
$module = ( array_key_exists( "module", $args ) ?
 
73
        $args['module'] : "Default-data-" . $form );
 
74
$display = ( array_key_exists( "display", $args ) ? 
 
75
        $args['display'] : "Default data for: " . $form );
 
76
$fields = split( ',', $args['fields'] );
 
77
if ( array_key_exists( "id", $args ) ) {
 
78
    $id = $args['id'];
 
79
} else {
 
80
    $id = false;
 
81
}
 
82
$reqs = array();
 
83
if ( array_key_exists( "reqs", $args ) ) {
 
84
    foreach( split( ',', $args['reqs'] ) as $req ) {
 
85
        $reqs[] = split( ':', $req, 3 );
 
86
    }
 
87
}
 
88
$who = ( array_key_exists( "who", $args ) ?
 
89
        $args['who'] : 1 );
 
90
$file = ( array_key_exists( "file", $args ) ?
 
91
        $args['file'] : $form . ".csv" );
 
92
$version = $args['version'];
 
93
$internalid = array_key_exists( "internalid", $args );
 
94
 
 
95
$keyidx = -1;
 
96
if ( $id && ($keyidx = array_search( $id, $fields )) === false ) {
 
97
    die( "ID field isn't listed in the fields given!\n" );
 
98
}
 
99
 
 
100
$last_mod = date( 'Y-m-d H:i:s' );
 
101
$csv = fopen( $file, "r" );
 
102
if ( $csv === false ) {
 
103
    die( "Can't open file: $file\n" );
 
104
}
 
105
 
 
106
echo <<<HEAD1
 
107
<?xml version="1.0"?>
 
108
<!DOCTYPE I2CEConfiguration SYSTEM "I2CE_Configuration.dtd">
 
109
<I2CEConfiguration name="$module">
 
110
  <metadata>
 
111
    <displayName>$display</displayName>
 
112
    <description>Data for form: $form</description>
 
113
    <version>$version</version>
 
114
 
 
115
HEAD1;
 
116
 
 
117
foreach( $reqs as $req ) {
 
118
    echo "    <requirement name=\"$req[0]\">\n";
 
119
    if ( array_key_exists( 1, $req ) && $req[1] ) {
 
120
        echo "      <atLeast version=\"$req[1]\" />\n";
 
121
    }
 
122
    if ( array_key_exists( 2, $req ) && $req[2] ) {
 
123
        echo "      <lessThan version=\"$req[2]\" />\n";
 
124
    }
 
125
    echo "    </requirement>\n";
 
126
}
 
127
 
 
128
echo <<<HEAD2
 
129
    <path name="configs">
 
130
      <value>./configs</value>
 
131
    </path>
 
132
  </metadata>
 
133
 
 
134
HEAD2;
 
135
if ( array_key_exists( 'erase', $args ) ) {
 
136
    echo <<<ERASE
 
137
  <erase path="/I2CE/formsData/forms/$form">
 
138
    <lessThan version="$version" />
 
139
  </erase>
 
140
 
 
141
ERASE;
 
142
}
 
143
echo <<<HEAD3
 
144
  <configurationGroup name="$module" path="/I2CE/formsData/forms/$form">
 
145
    <displayName>Form data: $form</displayName>
 
146
    <version>$version</version>
 
147
 
 
148
HEAD3;
 
149
 
 
150
$key = false;
 
151
while ( ($data = fgetcsv( $csv ) ) ) {
 
152
    if ( count($data) != count( $fields ) ) {
 
153
        die( "Data doesn't match given fields: " . print_r( $data, true ) . print_r( $fields, true ) );
 
154
    }
 
155
 
 
156
    if ( $keyidx >= 0 ) {
 
157
        $key = $data[$keyidx];
 
158
    } else {
 
159
        if ( !$key ) {
 
160
            $key = 1;
 
161
        } else {
 
162
            $key++;
 
163
        }
 
164
    }
 
165
 
 
166
    echo <<<DATAHEAD
 
167
    <configurationGroup name="$key">
 
168
      <displayName>$key</displayName>
 
169
      <configuration name="last_modified">
 
170
        <displayName>Last Modified</displayName>
 
171
        <value>$last_mod</value>
 
172
      </configuration>
 
173
      <configuration name="who">
 
174
        <displayName>Who</displayName>
 
175
        <value>$who</value>
 
176
      </configuration>
 
177
      <configurationGroup name="fields">
 
178
        <displayName>Fields</displayName>
 
179
 
 
180
DATAHEAD;
 
181
 
 
182
    foreach( $fields as $idx => $field ) {
 
183
        if ( $internalid && $idx == $keyidx ) {
 
184
            continue;
 
185
        }
 
186
        $value = $data[$idx];
 
187
        echo <<<DATA
 
188
        <configuration name="$field">
 
189
          <value>$value</value>
 
190
        </configuration>
 
191
 
 
192
DATA;
 
193
    }
 
194
 
 
195
    echo <<<DATAFOOT
 
196
      </configurationGroup>
 
197
    </configurationGroup>
 
198
 
 
199
DATAFOOT;
 
200
 
 
201
}
 
202
 
 
203
echo <<<FOOT
 
204
  </configurationGroup>
 
205
</I2CEConfiguration>
 
206
 
 
207
FOOT;
 
208
 
 
209
 
 
210
 
 
211
?>