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
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.
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');
25
require_once( dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pages/config.values.php');
28
$i2ce_site_i2ce_path = "../../I2CE";
30
require_once ($i2ce_site_i2ce_path . DIRECTORY_SEPARATOR . 'I2CE_config.inc.php');
32
@I2CE::initializeDSN($i2ce_site_dsn, $i2ce_site_user_access_init, $i2ce_site_module_config);
34
unset($i2ce_site_user_access_init);
35
unset($i2ce_site_dsn);
36
unset($i2ce_site_i2ce_path);
37
unset($i2ce_site_module_config);
41
$user = new I2CE_User(1, false, false, false);
42
$db = MDB2::singleton();
43
if ( PEAR::isError( $db ) ) {
44
die( $db->getMessage() );
46
$form_factory = I2CE_FormFactory::instance();
48
echo "Memory Limit: " . ini_get( "memory_limit" ) . "\n";
49
echo "Execution Time: " . ini_get( "max_execution_time" ) . "\n";
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" );
57
$tmp = array_shift( $argv );
60
function dotrim(&$value){
61
$value = trim($value);
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" );
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" ) );
78
while ( ( $data = fgetcsv( $fh ) ) !== false ) {
80
array_walk( $data, "dotrim" );
82
$name = explode( ' ', strtoupper( $data[1] ) );
83
$surname = array_pop( $name );
84
$firstname = array_shift( $name );
85
$othernames = implode( ' ', $name );
87
if ( !array_key_exists( $data[4], $jobs ) ) {
88
echo "Couldn't find job: $data[4]\n";
91
$post_code = sprintf( "%05d", $p_code );
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];
98
if ( array_key_exists( $data[3] . " District Hospital", $facilities ) ) {
99
$post->getField("facility")->setFromDB( "facility|"
100
. $facilities[ $data[3] . " District Hospital" ] );
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 );
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] ] );
117
$person->surname_ignore = true;
118
$person->save( $user );
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] );
128
$appt_date_obj = I2CE_Date::getDate( 1, 1, 1900 );
130
$pers_pos->start_date = $appt_date_obj;
131
$pers_pos->save( $user );
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 );
141
$pers_pos->cleanup();
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();
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 );
167
if ( $data[6] != "" ) {
168
$demo = $form_factory->createContainer( "demographic" );
169
$demo->setParent( $person->getFormId() );
171
$dob = explode( '-', $data[6] );
172
if ( count( $dob ) == 3 ) {
173
$demo->birth_date = I2CE_Date::getDate( $dob[0], $dob[1], $dob[2] );
175
$demo->save( $user );
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() );
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 );
204
if ( array_key_exists( $data[4], $jobs ) ) {
211
echo "Added $firstname $surname\n";