~ihris-india-team/indian-ihris/india-manage-demo

« back to all changes in this revision

Viewing changes to importSample.php

  • Committer: Luke Duncan
  • Date: 2010-12-17 08:52:46 UTC
  • Revision ID: lduncan@intrahealth.org-20101217085246-lyzlwsawihulnsap
Initial import of India demonstration site.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * The best way to run this is:
 
5
 * php importCSV.php 2> convert.log
 
6
 * There's lots of notice messages you probably want to ignore for the most
 
7
 * part.
 
8
 * You'll need to change the include file to find the right config file
 
9
 * as well as the path to I2CE which may not work right using the one
 
10
 * from the config file.
 
11
 * The ID for the User object should be valid in your user table.
 
12
 * The $forms array is an associative array with the value being
 
13
 * an array of forms that are required for the given form to work e.g. 
 
14
 * region needs country first since it uses country as a map for a field.
 
15
 *
 
16
 * 
 
17
 *
 
18
 */
 
19
 
 
20
$i2ce_site_user_access_init = null;
 
21
$script = array_shift( $argv );
 
22
if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pages/local' . DIRECTORY_SEPARATOR . 'config.values.php')) {
 
23
        require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pages/local' . DIRECTORY_SEPARATOR . 'config.values.php');
 
24
} else {
 
25
        require_once( dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pages/config.values.php');
 
26
}
 
27
 
 
28
$i2ce_site_i2ce_path = "../../I2CE";
 
29
 
 
30
require_once ($i2ce_site_i2ce_path . DIRECTORY_SEPARATOR . 'I2CE_config.inc.php');
 
31
 
 
32
@I2CE::initializeDSN($i2ce_site_dsn,   $i2ce_site_user_access_init,    $i2ce_site_module_config);
 
33
 
 
34
unset($i2ce_site_user_access_init);
 
35
unset($i2ce_site_dsn);
 
36
unset($i2ce_site_i2ce_path);
 
37
unset($i2ce_site_module_config);
 
38
 
 
39
 
 
40
 
 
41
$user = new I2CE_User(1, false, false, false);
 
42
$db = MDB2::singleton();
 
43
if ( PEAR::isError( $db ) ) {
 
44
        die( $db->getMessage() );
 
45
}
 
46
$form_factory = I2CE_FormFactory::instance();
 
47
 
 
48
echo "Memory Limit: " . ini_get( "memory_limit" ) . "\n";
 
49
echo "Execution Time: " . ini_get( "max_execution_time" ) . "\n";
 
50
 
 
51
if ( $argv[0] == "erase" ) {
 
52
    echo "Erasing all records and entries...";
 
53
    $db->query( "TRUNCATE TABLE record" );
 
54
    $db->query( "TRUNCATE TABLE entry" );
 
55
    $db->query( "TRUNCATE TABLE last_entry" );
 
56
    echo "Done\n";
 
57
    $tmp = array_shift( $argv );
 
58
}
 
59
 
 
60
function dotrim(&$value){
 
61
  $value = trim($value);
 
62
}
 
63
 
 
64
$fh = fopen( $argv[0], "r" );
 
65
if ( $fh === false ) {
 
66
    die( "Couldn't update file: $argv[0].  Syntax: importCSV.php [erase] file.csv\n" );
 
67
}
 
68
 
 
69
$districts = array_flip( I2CE_List::listOptions( "district" ) );
 
70
$facilities = array_flip( I2CE_List::listOptions( "facility", "facility_type", "facility_type|6" ) );
 
71
$jobs = array_flip( I2CE_List::listOptions( "job" ) );
 
72
$degrees = array_flip( I2CE_List::listOptions( "degree" ) );
 
73
$competencies = array_flip( I2CE_List::listOptions( "competency" ) );
 
74
 
 
75
 
 
76
$p_code = 0;
 
77
 
 
78
while ( ( $data = fgetcsv( $fh ) ) !== false ) {
 
79
 
 
80
    array_walk( $data, "dotrim" );
 
81
 
 
82
    $name = explode( ' ', strtoupper( $data[1] ) );
 
83
    $surname = array_pop( $name );
 
84
    $firstname = array_shift( $name );
 
85
    $othernames = implode( ' ', $name );
 
86
 
 
87
    if ( !array_key_exists( $data[4], $jobs ) ) {
 
88
        echo "Couldn't find job: $data[4]\n";
 
89
    } else {
 
90
        $p_code++;
 
91
        $post_code = sprintf( "%05d", $p_code );
 
92
  
 
93
        $post = $form_factory->createContainer( "position" );
 
94
        $post->code = $post_code;
 
95
        $post->getField("job")->setFromDB( "job|" . $jobs[$data[4]] );
 
96
        $post->title = $data[4];
 
97
    
 
98
        if ( array_key_exists( $data[3] . " District Hospital", $facilities ) ) {
 
99
            $post->getField("facility")->setFromDB( "facility|" 
 
100
                    . $facilities[ $data[3] . " District Hospital" ] );
 
101
        }
 
102
        $post->proposed_hiring_date = I2CE_Date::blank();
 
103
        $post->proposed_end_date = I2CE_Date::blank();
 
104
        $post->posted_date = I2CE_Date::blank();
 
105
        $post->getField("status")->setFromDB("position_status|closed");
 
106
        $post->save( $user );
 
107
    }
 
108
  
 
109
    $person = $form_factory->createContainer( "person" );
 
110
    $person->surname = $surname;
 
111
    $person->firstname = $firstname;
 
112
    $person->othername = $othernames;
 
113
    $person->getField("nationality")->setFromDB( "country|IN" );
 
114
    if ( array_key_exists( $data[2], $districts ) ) {
 
115
        $person->getField("residence")->setFromDB( "district|" . $districts[ $data[2] ] );
 
116
    }
 
117
    $person->surname_ignore = true;
 
118
    $person->save( $user );
 
119
  
 
120
    if ( array_key_exists( $data[4], $jobs ) ) {
 
121
        $pers_pos = $form_factory->createContainer( "person_position" );
 
122
        $pers_pos->setParent( $person->getFormId() );
 
123
        $pers_pos->getField("position")->setFromDB( $post->getFormId() );
 
124
        $appt_date = explode( '-', $data[7] );
 
125
        if ( count( $appt_date ) == 3 ) {
 
126
            $appt_date_obj = I2CE_Date::getDate( $appt_date[0], $appt_date[1], $appt_date[2] );
 
127
        } else {
 
128
            $appt_date_obj = I2CE_Date::getDate( 1, 1, 1900 );
 
129
        }
 
130
        $pers_pos->start_date = $appt_date_obj;
 
131
        $pers_pos->save( $user );
 
132
  
 
133
        $salary = $form_factory->createContainer("salary");
 
134
        $salary->setParent( $pers_pos->getFormId() );
 
135
        $salary->start_date = $appt_date_obj;
 
136
        $salary->end_date = I2CE_Date::blank();
 
137
        $salary->save( $user );
 
138
        $salary->cleanup();
 
139
        unset( $salary );
 
140
    
 
141
        $pers_pos->cleanup();
 
142
        unset( $pers_pos );
 
143
    }
 
144
  
 
145
    if ( array_key_exists( $data[8], $degrees ) ) {
 
146
        $education = $form_factory->createContainer("education");
 
147
        $education->setParent( $person->getFormId() );
 
148
        $education->getField("degree")->setFromDB( "degree|" . $degrees[ $data[8] ] );
 
149
        $education->year = I2CE_Date::blank( I2CE_Date::YEAR_ONLY );
 
150
        $education->save( $user );
 
151
        $education->cleanup();
 
152
        unset( $education );
 
153
    }
 
154
  
 
155
    $first_date = explode( '-', $data[7] );
 
156
    if ( count( $first_date ) == 3 && array_key_exists( $data[4], $jobs ) ) {
 
157
        $employment = $form_factory->createContainer("joining_job");
 
158
        $employment->setParent( $person->getFormId() );
 
159
        $first_date_obj = I2CE_Date::getDate( $first_date[0], $first_date[1], $first_date[2] );
 
160
        $employment->date_of_joining = $first_date_obj;
 
161
        $employment->getField("job")->setFromDB( "job|" . $jobs[$data[4]] );
 
162
        $employment->save( $user );
 
163
        $employment->cleanup();
 
164
        unset( $employment );
 
165
    }
 
166
  
 
167
    if ( $data[6] != "" ) {
 
168
        $demo = $form_factory->createContainer( "demographic" );
 
169
        $demo->setParent( $person->getFormId() );
 
170
  
 
171
        $dob = explode( '-', $data[6] );
 
172
        if ( count( $dob ) == 3 ) {
 
173
            $demo->birth_date = I2CE_Date::getDate( $dob[0], $dob[1], $dob[2] );
 
174
        }
 
175
        $demo->save( $user );
 
176
        $demo->cleanup();
 
177
        unset( $demo );
 
178
    }
 
179
  
 
180
    if ( $data[5] != "" ) {
 
181
        $id = $form_factory->createContainer( "person_id" );
 
182
        $id->getField("id_type")->setFromDB("id_type|BR.1");
 
183
        $id->id_num = $data[5];
 
184
        $id->setParent( $person->getFormId() );
 
185
        $id->save( $user );
 
186
        $id->cleanup();
 
187
        unset( $id );
 
188
    }
 
189
 
 
190
    for( $i = 9; $i <= 11; $i++ ) {
 
191
        if ( array_key_exists( $data[$i], $competencies ) ) {
 
192
            $comp = $form_factory->createContainer( "person_competency" );
 
193
            $comp->setParent( $person->getFormId() );
 
194
            $comp->evaluation_date = I2CE_Date::blank();
 
195
            $comp->getField("competency_evaluation")->setFromDB("competency_evaluation|competent");
 
196
            $comp->getField("competency")->setFromDB("competency|" . $competencies[$data[$i]] );
 
197
            $comp->save( $user );
 
198
            $comp->cleanup();
 
199
            unset( $comp );
 
200
        }
 
201
    }
 
202
  
 
203
    
 
204
    if ( array_key_exists( $data[4], $jobs ) ) {
 
205
        $post->cleanup();
 
206
        unset( $post );
 
207
    }
 
208
 
 
209
    $person->cleanup();
 
210
    unset( $person );
 
211
    echo "Added $firstname $surname\n";
 
212
                
 
213
}
 
214
 
 
215
?>