~begerega/ihris-chw-sl/trunk

« back to all changes in this revision

Viewing changes to tools/import_base.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
 * © Copyright 2007, 2008 IntraHealth International, Inc.
 
4
 * 
 
5
 * This File is part of iHRIS
 
6
 * 
 
7
 * iHRIS is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 * 
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 * 
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
/**
 
21
 * The page wrangler
 
22
 * 
 
23
 * This page loads the main HTML template for the home page of the site.
 
24
 * @package iHRIS
 
25
 * @subpackage DemoManage
 
26
 * @access public
 
27
 * @author Carl Leitner <litlfred@ibiblio.org>
 
28
 * @copyright Copyright &copy; 2007, 2008 IntraHealth International, Inc. 
 
29
 * @since Demo-v2.a
 
30
 * @version Demo-v2.a
 
31
 */
 
32
 
 
33
 
 
34
 
 
35
/**
 
36
 * setting system wide variables just like in index.php
 
37
 */
 
38
 
 
39
$dir = getcwd();
 
40
chdir("../pages");
 
41
$i2ce_site_user_access_init = null;
 
42
$i2ce_site_user_database = null;
 
43
require_once( getcwd() . DIRECTORY_SEPARATOR . 'config.values.php');
 
44
 
 
45
$local_config = getcwd() . DIRECTORY_SEPARATOR .'local' . DIRECTORY_SEPARATOR . 'config.values.php';
 
46
if (file_exists($local_config)) {
 
47
    require_once($local_config);
 
48
}
 
49
 
 
50
if(!isset($i2ce_site_i2ce_path) || !is_dir($i2ce_site_i2ce_path)) {
 
51
    echo "Please set the \$i2ce_site_i2ce_path in $local_config";
 
52
    exit(55);
 
53
}
 
54
 
 
55
require_once ($i2ce_site_i2ce_path . DIRECTORY_SEPARATOR . 'I2CE_config.inc.php');
 
56
 
 
57
I2CE::raiseMessage("Connecting to DB");
 
58
putenv('nocheck=1');
 
59
if (isset($i2ce_site_dsn)) {
 
60
    @I2CE::initializeDSN($i2ce_site_dsn,   $i2ce_site_user_access_init,    $i2ce_site_module_config);         
 
61
} else if (isset($i2ce_site_database_user)) {    
 
62
    I2CE::initialize($i2ce_site_database_user,
 
63
                     $i2ce_site_database_password,
 
64
                     $i2ce_site_database,
 
65
                     $i2ce_site_user_database,
 
66
                     $i2ce_site_module_config         
 
67
        );
 
68
} else {
 
69
    die("Do not know how to configure system\n");
 
70
}
 
71
 
 
72
I2CE::raiseMessage("Connected to DB");
 
73
 
 
74
require_once($i2ce_site_i2ce_path . DIRECTORY_SEPARATOR . 'tools' . DIRECTORY_SEPARATOR . 'CLI.php');
 
75
 
 
76
 
 
77
 
 
78
/*************************************************************************
 
79
 *
 
80
 *  Classes to handle reading headers and rows from data files
 
81
 *
 
82
 ************************************************************************/
 
83
 
 
84
 
 
85
abstract class DataFile {
 
86
    
 
87
    /**
 
88
     * @var protected string $file
 
89
     */
 
90
    protected $file;    
 
91
    
 
92
    abstract public function getDataRow();
 
93
    abstract public function hasDataRow();
 
94
    abstract public function getHeaders();
 
95
    public function __construct($file) {
 
96
        $this->file = $file;
 
97
    }
 
98
    
 
99
    /**
 
100
     * get the file name for the file we are going to deal with
 
101
     * @returns string
 
102
     */
 
103
    public function getFileName() {
 
104
        return $this->file;
 
105
    }
 
106
    
 
107
    /**
 
108
     * closes a file that was open
 
109
     */
 
110
    public function close() {
 
111
 
 
112
    }
 
113
 
 
114
}
 
115
 
 
116
class CSVDataFile extends DataFile {
 
117
    protected $fp;
 
118
    protected $in_file_sep = false;
 
119
    protected $file_size = false;
 
120
    public function __construct($file) {
 
121
        parent::__construct($file);
 
122
        $this->filesize = filesize($file);
 
123
        if ( ($this->fp = fopen($this->file,"r")) === false) {
 
124
            usage("Please specify the name of a spreadsheet to import: " . $file . " is not openable");
 
125
        }
 
126
    }
 
127
    
 
128
    /**
 
129
     * checks to confirm if the file has rows of data
 
130
     * @returns string
 
131
     */
 
132
    public function hasDataRow() {
 
133
        $currpos =  ftell($this->fp);
 
134
        if ($currpos === false) {
 
135
            return false;
 
136
        } else {
 
137
            return ($currpos < $this->filesize);
 
138
        }
 
139
    }
 
140
    
 
141
    /**
 
142
     * reads all the column headers from the CSV file
 
143
     * @returns array
 
144
     */
 
145
    public function getHeaders() {
 
146
        $this->in_file_sep = false;
 
147
        fseek($this->fp,0);
 
148
        foreach (array("\t",",",";") as $sep) {
 
149
            $headers = fgetcsv($this->fp, 4000, $sep);
 
150
            if ( $headers === FALSE|| count($headers) < 2 || !simple_prompt("Do these look like the correct headers?\n". print_r($headers,true))) {
 
151
                fseek($this->fp,0);
 
152
                continue;
 
153
            }
 
154
            $this->in_file_sep = $sep;
 
155
            break;
 
156
        }
 
157
        if (!$this->in_file_sep) {
 
158
            die("Could not get headers\n");
 
159
        }
 
160
        foreach ($headers as &$header) {
 
161
            $header = trim($header);
 
162
        }
 
163
        unset($header);
 
164
        return $headers;
 
165
    }
 
166
 
 
167
    public function getDataRow() {
 
168
        return $data = fgetcsv($this->fp, 4000, $this->in_file_sep);
 
169
    }
 
170
    
 
171
    /**
 
172
     * closes the open CSV file
 
173
     */
 
174
    public function close() {
 
175
        fclose($this->fp);
 
176
    }
 
177
}
 
178
 
 
179
 
 
180
class ExcelDataFile extends DataFile {    
 
181
 
 
182
    protected $rowIterator;
 
183
 
 
184
    public function __construct($file) {
 
185
        parent::__construct($file);
 
186
        include_once('PHPExcel/PHPExcel.php'); 
 
187
        if (!class_exists('PHPExcel',false)) {
 
188
            usage("You must have PHPExcel installed to load excel spreadsheets");
 
189
        }
 
190
        $readerType = PHPExcel_IOFactory::identify($this->file);
 
191
        $reader = PHPExcel_IOFactory::createReader($readerType);
 
192
        $reader->setReadDataOnly(false);
 
193
        $excel = $reader->load($this->file);        
 
194
        $worksheet = $excel->getActiveSheet();
 
195
        $this->rowIterator = $worksheet->getRowIterator();
 
196
    }
 
197
 
 
198
    
 
199
    /**
 
200
     * confirms if the excel file we are reading has rows with data
 
201
     * @returns boolean
 
202
     */
 
203
    public function hasDataRow() {
 
204
        return $this->rowIterator->valid();
 
205
    }
 
206
 
 
207
    /**
 
208
     * reads the file to get the headers
 
209
     * @returns array
 
210
     */
 
211
    public function getHeaders() {
 
212
        $this->rowIterator->rewind();
 
213
        $row = $this->rowIterator->current();
 
214
        if (!$this->rowIterator->valid()) {
 
215
            I2CE::raiseMessage("Could not find header row");
 
216
            return false;
 
217
        }
 
218
        return $this->_readRow($row);
 
219
    }
 
220
    
 
221
    /**
 
222
     * reads one data row at a time
 
223
     * @returns array
 
224
     */
 
225
    public function getDataRow() {
 
226
        $this->rowIterator->next();
 
227
        if (!$this->rowIterator->valid()) {
 
228
            return false;
 
229
        }
 
230
        return $this->_readRow($this->rowIterator->current());
 
231
    }
 
232
    
 
233
    /**
 
234
     * read the entire row and parse for data
 
235
     * @param string $row. If not an excel worksheet row, issue a message and return false
 
236
     * @returns array
 
237
     */
 
238
    protected function _readRow($row) {
 
239
        if (!$row instanceof PHPExcel_Worksheet_Row) {
 
240
            I2CE::raiseMessage("Invalid row object" . get_class($row));
 
241
            return false;
 
242
        }
 
243
        $cellIterator = $row->getCellIterator();
 
244
        $cellIterator->setIterateOnlyExistingCells(false);
 
245
        $data = array();
 
246
        foreach ($cellIterator as $cell) {
 
247
            $data[] =  $cell->getValue();
 
248
        }
 
249
        return $data;
 
250
    }
 
251
 
 
252
 
 
253
}
 
254
 
 
255
 
 
256
 
 
257
/*********************************************
 
258
*
 
259
*      Process Classes
 
260
*
 
261
*********************************************/
 
262
 
 
263
function convert($size)
 
264
{
 
265
    $unit=array('b','kb','mb','gb','tb','pb');
 
266
    return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
 
267
}
 
268
 
 
269
 
 
270
 
 
271
abstract  class Processor {
 
272
    abstract protected function _processRow();
 
273
    abstract protected function getExpectedHeaders();
 
274
    protected $headers;
 
275
    protected $mapped_data;
 
276
    protected $data;
 
277
    protected $user;
 
278
    protected $testmode = true;
 
279
    protected $db;
 
280
    protected $row = 1;
 
281
    protected $ff; 
 
282
    protected $processRows =null;
 
283
    protected $dataFile;
 
284
    protected $file;
 
285
 
 
286
    public function __construct($file) {
 
287
        $this->file = $file;
 
288
        $file_ext = strtolower(substr($file, strrpos($file, '.') + 1));
 
289
        if ($file_ext == 'csv') {
 
290
            //although CSV can be processed by PHPExcel, we keep this separate in case PHPExcel cannot be installed, we can still export the file as a CSV and process it
 
291
            $this->dataFile = new CSVDataFile($file);
 
292
        } else {
 
293
            $this->dataFile = new ExcelDataFile($file);
 
294
        }
 
295
        $this->user = new I2CE_User();
 
296
        $this->db= MDB2::singleton();
 
297
        $this->ff = I2CE_FormFactory::instance();
 
298
        $this->mapHeaders();
 
299
        $this->initBadFile();
 
300
        $this->testmode = simple_prompt("Is this a test run?");
 
301
        $this->createLogger();
 
302
    }
 
303
    
 
304
    /**
 
305
     * read the current row
 
306
     * @return $array
 
307
     */
 
308
    public function getCurrentRow() {
 
309
        return $this->row;
 
310
    }
 
311
    
 
312
    /**
 
313
     * check if the file has data 
 
314
     * @returns bool
 
315
     */
 
316
    public function hasDataRow() {
 
317
        return $this->dataFile->hasDataRow();
 
318
    }
 
319
 
 
320
    
 
321
    protected $success = 0;
 
322
 
 
323
    protected $blank_lines = 0;
 
324
    protected $skip = 2;
 
325
    /**
 
326
     * process the import giving the user choices to run in test mode or in production mode
 
327
     * every unsuccessful processing for a row is recorded into a log file
 
328
     */
 
329
    public function run() {
 
330
        if (simple_prompt("Skip rows?")) {
 
331
            $this->skip = ask("Skip to which row?  Start row =2 (b/c row 1 is header)");
 
332
        }
 
333
        $this->success = 0;
 
334
        while ( $this->hasDataRow()) {
 
335
            if ($this->blank_lines > 10) {
 
336
                if (simple_prompt("Encounted 10 consective blank rows ending at row " . $this->row . ". Should we stop processing?")) {
 
337
                    break;
 
338
                } else {
 
339
                    $this->blank_lines = 0;
 
340
                }
 
341
            }
 
342
            if ($this->processRow()) {
 
343
                $this->success++;
 
344
                if ($this->testmode) {
 
345
                    //$this->addBadRecord("SUCCESS ON TEST");
 
346
                }
 
347
            }
 
348
        }
 
349
    }
 
350
    
 
351
    /**
 
352
     * collect statistics for the import process:total attempts and successes
 
353
     * @returns array
 
354
     */
 
355
    public function getStats() {
 
356
        return array('success'=>$this->success,'attempts'=>($this->row -1)); //this may be off by one.
 
357
        $row = $processor->getCurrentRow();
 
358
    }
 
359
    
 
360
    /**
 
361
     * processes each row encountered from the file
 
362
     * @returns bool
 
363
     */
 
364
    public function processRow() {
 
365
        if (!$this->dataFile->hasDataRow()) {
 
366
            return false;
 
367
        }
 
368
        //starts off with $row= 1, but we do a row++ below to get to row 2
 
369
        $this->data = $this->dataFile->getDataRow();
 
370
        if ($this->row < ($this->skip - 1)) { //if skip =2, the default, then 1 < (2-1)  is false so we don't skup
 
371
            $this->row++;
 
372
            return true;
 
373
        }
 
374
        if (!is_array($this->data)) {
 
375
            $this->blank_lines++;
 
376
            return false;
 
377
        }
 
378
        $is_blank = true;
 
379
        foreach ($this->data as $cell) {
 
380
            if (is_string($cell) && strlen(trim($cell)) != 0) {
 
381
                $is_blank = false;
 
382
                $this->blank_lines = 0;
 
383
                break;
 
384
            }
 
385
        }
 
386
        if ($is_blank) {
 
387
            $this->blank_lines++;
 
388
        } 
 
389
        $this->row++;
 
390
        if ( ! ($this->mapped_data = $this->mapData())) {
 
391
            return false;
 
392
        }
 
393
        if (($hash_val = $this->alreadyProcessed()) === true) {
 
394
            return true;
 
395
        }
 
396
        if (!prompt("Process row $this->row ?",$this->processRows,print_r(array('mapped'=>$this->mapped_data,'raw'=>$this->data),true))) {
 
397
            return false;
 
398
        }
 
399
        if ( $this->_processRow()) {
 
400
            $this->markProcessed($hash_val);    
 
401
            return true;
 
402
        } else {
 
403
            return false;
 
404
        }
 
405
    }
 
406
    
 
407
    
 
408
    /**
 
409
     * sets up the mode to run the script: test or production
 
410
     * @param string $testmode defaults yes
 
411
     */
 
412
    public function setTestMode($testmode) {
 
413
        $this->testmode = $testmode;
 
414
    }
 
415
 
 
416
    protected $continue_save = null;
 
417
 
 
418
    /**
 
419
     * save the data into the database
 
420
     * @param string $obj, the form object, 
 
421
     * @param bool $cleanup defaults
 
422
     * @returns string
 
423
     */
 
424
    protected function save($obj,$cleanup = true) {
 
425
        if (!$obj instanceof I2CE_Form) {
 
426
            return false;
 
427
        }
 
428
        echo "Row {$this->row}, Form " . $obj->getName() . " Used Memory  = " . convert(memory_get_usage(true)) . "\n"; 
 
429
        if ($this->testmode) {            
 
430
            echo "Saving " . $obj->getName() . "\n";
 
431
            foreach ($obj as $fieldName=>$fieldObj) {
 
432
                echo "\t$fieldName=>" . $fieldObj->getDBValue() . "\n";
 
433
            }
 
434
            if ($cleanup) {
 
435
                $obj->cleanup();
 
436
            }
 
437
            return "0";
 
438
        } else {
 
439
            $obj->save($this->user);    
 
440
            $id = $obj->getID();
 
441
            if ($cleanup) {
 
442
                $obj->cleanup();
 
443
            }
 
444
            prompt("Saved id $id. Continue?",$this->continue_save);
 
445
            return $id;
 
446
        }
 
447
    }
 
448
 
 
449
 
 
450
 
 
451
    protected $header_map;
 
452
    
 
453
 
 
454
 
 
455
    function getHeaderMap(&$headers,$expected_headers) {
 
456
        foreach ($headers as &$header) {
 
457
            $header = strtolower(trim($header));
 
458
        }
 
459
        unset($header);
 
460
 
 
461
        $header_map = array();
 
462
        foreach ($expected_headers as $expected_header_ref => $expected_header) {
 
463
            $expected_header = strtolower(trim($expected_header));
 
464
            if (($header_col = array_search($expected_header,$headers)) === false) {
 
465
                I2CE::raiseError("Could not find $expected_header in the following headers:\n\t" . implode(" ", $headers). "\nFull list of found headers is:\n\t" . implode(" ", $headers));
 
466
                die();
 
467
            }
 
468
            $header_map[$expected_header_ref] = $header_col;
 
469
        }
 
470
        return $header_map;
 
471
    }
 
472
 
 
473
 
 
474
    /**
 
475
     * map expected header references to header columns in the data file 
 
476
     */
 
477
    protected function mapHeaders() {
 
478
        $this->headers = $this->dataFile->getHeaders();
 
479
        $expected_headers = $this->getExpectedHeaders();
 
480
        $this->header_map = $this->getHeaderMap($this->headers,$expected_headers);
 
481
    }
 
482
 
 
483
 
 
484
 
 
485
    /**
 
486
     * Utility function to remap data in an associative array to an associative array based on a map.
 
487
     * @param array $data the source data
 
488
     * @param array $header_map, the mapping array of data.  Keys are keys for the source data.  Values will be the keys for the mapped data
 
489
     * @param boolean $normalize.  Defaults to false.  If true we trim and upcase the data that is being remapped
 
490
     * @returns array, data mapped 
 
491
     */
 
492
    function mapByHeaderData($data,$header_map,$normalize = false) {
 
493
        $mapped_data = array();
 
494
        foreach ($header_map as $header_ref=>$header_col) {
 
495
            if (!array_key_exists($header_col,$data)) {
 
496
                $mapped_data[$header_ref] = false;
 
497
            } else if ($normalize) {
 
498
                $mapped_data[$header_ref] = strtoupper(trim($data[$header_col]));
 
499
            } else {
 
500
                $mapped_data[$header_ref] = $data[$header_col];
 
501
            }
 
502
        }
 
503
    
 
504
        return $mapped_data;
 
505
    }
 
506
 
 
507
 
 
508
    /**
 
509
     * map the data found from a column in the data file to a specific 
 
510
     * header reference for saving into the database
 
511
     * @returns array, data mapped into header references
 
512
     */
 
513
    protected function mapData() {
 
514
        return $this->mapByHeaderData($this->data,$this->header_map);
 
515
    }
 
516
 
 
517
    
 
518
 
 
519
    protected $bad_headers;
 
520
    protected $bad_fp;        
 
521
    protected $bad_file_name;
 
522
    
 
523
    /**
 
524
     * initialize a file into which we record all the bad data/unsuccessful row imports
 
525
     * 
 
526
     */
 
527
    protected function initBadFile() {
 
528
        $info = pathinfo($this->file);
 
529
        $bad_fp =false;
 
530
        if ($this->testmode) {
 
531
            $append = 'test_bad_';
 
532
        } else{
 
533
            $append = 'bad_';
 
534
        }
 
535
        $this->bad_file_name = dirname($this->file) . DIRECTORY_SEPARATOR . basename($this->file,'.'.$info['extension']) . '.' . $append .date('d-m-Y_G:i') .'.csv';
 
536
        I2CE::raiseMessage("Will put any bad records in $this->bad_file_name");
 
537
        $this->bad_headers = $this->headers;
 
538
        $this->bad_headers[] = "Row In " . basename($this->file);
 
539
        $this->bad_headers[] = "Reasons for failure";
 
540
    }
 
541
 
 
542
 
 
543
 
 
544
    /**
 
545
     * add a bad record to the file holding all unsuccessful imports
 
546
     * @param string $reason, the reason for the failure of import of this record
 
547
     */
 
548
    function addBadRecord($reason) {
 
549
        if (!is_resource($this->bad_fp)) {
 
550
            $this->bad_fp = fopen($this->bad_file_name,"w");
 
551
            if (!is_resource($this->bad_fp)) {
 
552
                I2CE::raiseMessage("Could not open $this->bad_file_name for writing.", E_USER_ERROR);
 
553
                die();
 
554
            }        
 
555
            fputcsv($this->bad_fp, $this->bad_headers);
 
556
        }
 
557
        I2CE::raiseMessage("Skipping processing of row $this->row: $reason");
 
558
        $raw_data = $this->data;
 
559
        $raw_data[] = $this->row;
 
560
        $raw_data[] = $reason;
 
561
        fputcsv($this->bad_fp, $raw_data);
 
562
    }
 
563
    protected $log_table;
 
564
    
 
565
    /**
 
566
     * creating a logger table in the database to track every import process timing
 
567
     * 
 
568
     */
 
569
    protected function createLogger() {
 
570
        $this->log_table  = 'import_logger_' . get_class($this);
 
571
        $sql = "CREATE TABLE IF NOT EXISTS `" . $this->log_table . "` (`hash` BINARY(16) NOT NULL, UNIQUE KEY `hash` (`hash`))";
 
572
        if (I2CE::pearError($this->db->exec($sql),"Cannot create logging table", E_USER_ERROR)) {
 
573
            die();
 
574
        }
 
575
    }
 
576
 
 
577
    /**
 
578
     * checks to see if a row from the data file has been processed
 
579
     *  @returns string $hash_val. value of the hash from logger table
 
580
     */
 
581
    protected function alreadyProcessed() {
 
582
        $hash_data = '';
 
583
        $expected_headers = $this->getExpectedHeaders();
 
584
        foreach (array_keys($expected_headers) as $header_ref) {
 
585
            $hash_data .= $this->mapped_data[$header_ref];
 
586
        }
 
587
        if (strlen($hash_data) == 0) {
 
588
            I2CE::raiseError("No data");
 
589
            return true;
 
590
        }
 
591
        $hash_val = md5($hash_data,true);
 
592
        if (!is_string($hash_val) || strlen($hash_val) == 0) {
 
593
            die("bad has val:\n" . print_r($this->mapped_data,true) . "\n$hash_data\n" . self::raw2hex($hash_val) );
 
594
        }
 
595
        $esc_hash_val = mysql_real_escape_string($hash_val);
 
596
        $hash_check = 'SELECT hash FROM `'. $this->log_table . '` WHERE hash = \''. $esc_hash_val  .'\'';
 
597
        if (I2CE::pearError(($res = $this->db->query($hash_check)),"Cannot check value in logging table", E_USER_ERROR)) {
 
598
            die();
 
599
        }
 
600
        if ( $res->fetchRow()) {
 
601
            $this->addBadRecord('Row has already been processed');
 
602
            return true;
 
603
        }
 
604
        return $hash_val;
 
605
    }
 
606
 
 
607
    
 
608
    public static function raw2hex($s) {
 
609
        //thanks to: functionifelse at gmail dot com     09-Dec-2004 10:34  on http://theserverpages.com/php/manual/en/function.md5.php
 
610
        $op = '';
 
611
        for($i = 0; $i < strlen($s); $i++){
 
612
            $op .= str_pad(dechex(ord($s[$i])),2,"0",STR_PAD_LEFT);
 
613
        }
 
614
        return $op;
 
615
    }
 
616
 
 
617
    /**
 
618
     * marks every record that is processed inserting its $hash_val to the logger table
 
619
     * @param string $hash_val. the hash value generated from the import process
 
620
     */
 
621
    protected function markProcessed($hash_val) {
 
622
        if ($this->testmode) {
 
623
            return;
 
624
        }
 
625
        $esc_hash_val = mysql_real_escape_string($hash_val);
 
626
        $hash_insert = 'INSERT into   `' . $this->log_table . '` VALUES (\''.  $esc_hash_val .'\')';
 
627
        if (I2CE::pearError($this->db->exec($hash_insert),"Cannot add value " . self::raw2hex($hash_val). "to logging table from\n" . print_r($this->mapped_data,true) . "\n" . strlen($hash_val), E_USER_ERROR)) {
 
628
            die();
 
629
        }   
 
630
    }    
 
631
 
 
632
 
 
633
 
 
634
    /**
 
635
     * gets a date value from records read from the datafile
 
636
     * @param date $date, a date value as read from the data file
 
637
     * @returns date. formatted
 
638
     */
 
639
    protected function getDate($date,$date_format = 'm/d/Y' , $excel_date_format = 'DD/MM/YYYY') {
 
640
        //first check the date e.g 16/05/2011
 
641
        $matches = array();
 
642
        if (is_numeric($date) && class_exists('PHPExcel',false)) {
 
643
            //in case we are reading it from excel which returns 40777 instead of 22/08/2011 for example
 
644
            $date = PHPExcel_Style_NumberFormat::toFormattedString($date, $excel_date_format);
 
645
        }
 
646
        if (($datetime = DateTime::createFromFormat($date_format,$date)) === false) {
 
647
            $this->addBadRecord("Bad date format [$date] for $date_format");
 
648
            return false;
 
649
        }
 
650
        $date = I2CE_Date::now(I2CE_Date::DATE, $datetime->getTimestamp(),true);
 
651
        if (!$date) {
 
652
            $this->addBadRecord("Invalid date ($date)");
 
653
            return false;
 
654
        }
 
655
        return $date;
 
656
    }
 
657
    
 
658
    /**
 
659
     * gets a year value from records read from the datafile
 
660
     * @param date $date, a date value as read from the data file
 
661
     * @returns date. formatted
 
662
     */
 
663
    protected function getYear($date,$year_format = 'Y' , $excel_date_format = 'YYYY') {
 
664
        //first check the date e.g 16/05/2011
 
665
        $matches = array();
 
666
        if (is_numeric($date) && class_exists('PHPExcel',false)) {
 
667
            //in case we are reading it from excel which returns 40777 instead of 22/08/2011 for example
 
668
            $date = PHPExcel_Style_NumberFormat::toFormattedString($date, $excel_date_format);
 
669
        }
 
670
        if (($datetime = DateTime::createFromFormat($year_format,$date)) === false) {
 
671
            $this->addBadRecord("Bad date format [$date] for $date_format");
 
672
            return false;
 
673
        }
 
674
        $date = I2CE_Date::now(I2CE_Date::DATE, $datetime->getTimestamp(),true);
 
675
        if (!$date) {
 
676
            $this->addBadRecord("Invalid date ($date)");
 
677
            return false;
 
678
        }
 
679
        return $date;
 
680
    }
 
681
 
 
682
}
 
683
 
 
684
 
 
685
 
 
686
 
 
687
 
 
688
 
 
689
# Local Variables:
 
690
# mode: php
 
691
# c-default-style: "bsd"
 
692
# indent-tabs-mode: nil
 
693
# c-basic-offset: 4
 
694
# End: