4
* Syntax: php csv2module.php
6
* --fields=FIELD1,FIELD2,ETC.
8
* [--module=MODULENAME]
9
* [--display="Module Display Name"]
15
* [--reqs=REQUIRED_MOD1[:MINVER[:MAXVER]],REQUIRED_MOD2[:MINVER[:MAXVER]]]
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.
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.
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"
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.
34
* The who argument is the userid to use for the forms. The default is 1.
36
* If the erase argument is used then all previous values in magic data
37
* will be removed for this form.
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.
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.
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
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.
57
$args = getopt( "", array(
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 ) ) {
83
if ( array_key_exists( "reqs", $args ) ) {
84
foreach( split( ',', $args['reqs'] ) as $req ) {
85
$reqs[] = split( ':', $req, 3 );
88
$who = ( array_key_exists( "who", $args ) ?
90
$file = ( array_key_exists( "file", $args ) ?
91
$args['file'] : $form . ".csv" );
92
$version = $args['version'];
93
$internalid = array_key_exists( "internalid", $args );
96
if ( $id && ($keyidx = array_search( $id, $fields )) === false ) {
97
die( "ID field isn't listed in the fields given!\n" );
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" );
107
<?xml version="1.0"?>
108
<!DOCTYPE I2CEConfiguration SYSTEM "I2CE_Configuration.dtd">
109
<I2CEConfiguration name="$module">
111
<displayName>$display</displayName>
112
<description>Data for form: $form</description>
113
<version>$version</version>
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";
122
if ( array_key_exists( 2, $req ) && $req[2] ) {
123
echo " <lessThan version=\"$req[2]\" />\n";
125
echo " </requirement>\n";
129
<path name="configs">
130
<value>./configs</value>
135
if ( array_key_exists( 'erase', $args ) ) {
137
<erase path="/I2CE/formsData/forms/$form">
138
<lessThan version="$version" />
144
<configurationGroup name="$module" path="/I2CE/formsData/forms/$form">
145
<displayName>Form data: $form</displayName>
146
<version>$version</version>
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 ) );
156
if ( $keyidx >= 0 ) {
157
$key = $data[$keyidx];
167
<configurationGroup name="$key">
168
<displayName>$key</displayName>
169
<configuration name="last_modified">
170
<displayName>Last Modified</displayName>
171
<value>$last_mod</value>
173
<configuration name="who">
174
<displayName>Who</displayName>
177
<configurationGroup name="fields">
178
<displayName>Fields</displayName>
182
foreach( $fields as $idx => $field ) {
183
if ( $internalid && $idx == $keyidx ) {
186
$value = $data[$idx];
188
<configuration name="$field">
189
<value>$value</value>
196
</configurationGroup>
197
</configurationGroup>
204
</configurationGroup>