sync
[Pman.Admin] / Import / Enum.php
1 <?php
2
3 // moved... - to 
4
5 require_once 'Pman/Roo.php';
6
7 class Pman_Admin_Import_Enum extends Pman_Roo
8 {
9     
10     function getAuth()
11     {
12         if (HTML_FlexyFramework::get()->cli) {
13             return true;
14         }
15         return parent::getAuth();
16     }
17     
18     
19     function post($v)
20     {   
21         
22         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
23
24         $this->sessionState(0); // turn off the session..
25         
26         $img = DB_DataObject::Factory('images');
27         $img->setFrom(array(
28             'onid' => 0,
29             'ontable' => 'ipshead'
30         ));
31         $img->onUpload(false);
32         
33         require_once 'File/Convert.php';
34         $fc = new File_Convert($img->getStoreName(), $img->mimetype );
35         $csv = $fc->convert('text/csv');
36         
37         $this->importCsv($csv);
38     }
39     
40     
41     function importCsv($csv)
42     {
43         
44         ini_set('memory_limit', '1024M');
45         
46         ini_set("auto_detect_line_endings", true);
47
48         if(empty($_REQUEST['etype'])){
49             $this->jerr('Missing etype');
50         }
51         
52         $enum = DB_DataObject::factory('core_enum');
53         if(!$enum->get('name', $_REQUEST['etype'])){
54             $this->jerr('Invalid etype');
55         }
56         
57         
58         
59         
60         $fh = fopen($csv, 'r');
61         if (!$fh) {
62             $this->jerr("invalid file");
63         }
64         
65         $req = array('NAME','DISPLAY NAME');
66         
67         $cols = false;
68         $rows = array();
69         
70         while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
71             if(!array_filter($n)){ // empty row
72                 continue;
73             }
74             if (!$cols) {
75                 
76                 $cols = array();
77                 foreach($n as $k) {
78                     $cols[] = strtoupper(trim($k));
79                 }
80                 if (empty($cols)) {
81                     continue;
82                 }
83                 foreach($req as $r) {
84                     if (!in_array($r,$cols)) {
85                         $cols = false;
86                         break;
87                     }
88                 }
89                 continue;
90             }
91             foreach($cols as $i=>$k) {
92                 $row[$k] = $n[$i];
93             }
94             $rows[] = $row;
95         }
96         
97          
98         
99         if (empty($cols)) {
100             $this->jerr("could not find a row with " . implode(' / ', $req));
101         }
102         
103         fclose($fh);
104         
105         $count = 0;
106         
107         foreach ($rows as $row){
108             $e = DB_DataObject::factory('core_enum');
109             $e->etype =  $_REQUEST['etype'];
110             if($e->get('name', $row['NAME'])){
111                 // allow changing active?? -- order/seqid not used at present on update.
112                 if (isset($row['ACTIVE']) && $e->active != $row['ACTIVE']) {
113                     $ee =clone($e);
114                     $e->active = $row['ACTIVE'];
115                     $e->update($ee);
116                     $count++;
117                 }
118                 continue;
119             }
120             
121             $e = DB_DataObject::factory('core_enum');
122             $e->setFrom(array(
123                 'name' => $row['NAME'],
124                 'display_name' => $row['DISPLAY NAME'],
125                 'etype' => $_REQUEST['etype'],
126                 'active' => isset($row['ACTIVE']) ? $row['ACTIVE'] : 1,
127             ));
128             if (!empty($row['ORDER #'])) {
129                 $e->seqid = $row['ORDER #'];
130             }
131             $e->insert();
132             $e->onInsert(array());
133             
134             $count++;
135         }
136         
137         $this->jok('data imported successfully! total : ' . $count .'/'. count($rows));
138     
139     }
140     
141 }