sync
[Pman.Admin] / Import / Core_templatestr.php
1 <?php
2
3 /**
4  * generic import routine..
5  *
6  * - upload a file..
7  *
8  * - preview the result. / edit?
9  *
10  * - import..
11  * 
12  *
13  */
14
15
16 require_once 'Pman.php';
17 class Pman_Admin_Import_Core_templatestr extends Pman
18 {
19     var $cli  = false;
20      
21      
22      
23     function getAuth()
24     {
25         $cli = HTML_FlexyFramework::get()->cli;
26         if ($cli) {
27             $this->cli = true;
28             return true;
29         }
30         parent::getAuth();
31         $au = $this->getAuthUser();
32         if (!$au) {
33             $this->jerrAuth();
34         }
35         return true;
36     }
37     
38     function post($base = '' )
39     {
40         
41            // should handle uploaded file..
42         if (empty($_FILES['imageUpload']['tmp_name']) || 
43             empty($_FILES['imageUpload']['name']) || 
44             empty($_FILES['imageUpload']['type'])
45         ) {
46             $this->jerr ("Missing file details");
47             exit;
48         }
49         
50         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
51     
52           
53         $this->transObj = DB_DataObject::factory('core_enum');
54         
55         $this->transObj->query('BEGIN');
56         
57         // first convert the file to a readable format..
58         //$rows = $this->readXLS("/home/alan/Downloads/oll_example.xls") ;
59         $rows = $this->readXLS($_FILES['imageUpload']['tmp_name']) ;
60         
61         $ret = $this->processRows($rows);
62         
63          
64         $this->jok("DONE", false, array('extra'=> $ret));
65         
66     }
67     
68     /*
69     function get($base = '',  $opts = array())
70     {
71         $rows = $this->readXLS("/var/lib/php/sessions/pressrelease-translations.xls") ;
72         
73        
74         
75         $ret = $this->processRows($rows);
76          
77         $this->jdata($ret);
78         exit;
79         
80     }
81     */
82     function processRows($rows)
83     { 
84         $ret = array();
85         foreach($rows as $r) {
86             if (isset($r['table id'])) {
87                 $ret[] = $this->updateTableTranslation($r);
88                 continue;
89             }
90             
91             $ret[] = $this->updateTranslation($r);
92             
93         }
94         return $ret;
95     }        
96     function updateTranslation($r)
97     {
98         // determine the type of file:
99         
100         if (empty($r['template']) || empty($r['module']) || empty($r['code'])) {
101             $this->jerr("missing template / module or code column");
102         }
103         if (isset($r['language'])) {
104             $this->updateTableTranslationRow($r);
105             return;
106         }
107         $ff = HTML_FlexyFramework::get();
108         if (!isset($ff->Pman_Admin['languages'])) {
109             $this->jerr("invalid language configuration");
110         }
111         
112         
113         foreach($ff->Pman_Admin['languages'] as $lang) {
114             if (!isset($r[strtolower($lang)])) {
115                 //echo "SKIP $lang\n";
116                 continue;
117             }
118             $rr = $r;
119             $rr['language'] = $lang;
120             $rr['translation'] = $r[strtolower($lang)];
121             $this->updateTranslationRow($rr);
122         }
123         
124         
125     }
126     
127     var $seq = 1;
128     function updateTranslationRow($r)
129     {
130         //print_R($r); DB_DataObject::DebugLevel(1);
131         $tr = DB_DataObject::Factory('core_templatestr');
132         $tr->autoJoin();
133
134        
135         $tr->whereAdd("join_template_id_id.template='{$tr->escape($r['template'])}'");
136         $tr->whereAdd("join_template_id_id.view_name='{$tr->escape($r['module'])}'");
137         $tr->whereAdd("join_src_id_id.mdsum='{$tr->escape($r['code'])}'");
138         $tr->lang = $r['language'];
139         if ($tr->find(true)) {
140             $tt = DB_DataObject::Factory('core_templatestr');
141             $tt->get($tr->id);
142             $tr= clone($tt);
143             $tt->txt = $r['translation'];
144             $tt->updated = date('Y-m-d H:i:s');
145             $tt->update($tr);
146             return 1;
147         }
148         return 0;
149     }
150     
151     
152     
153     function updateTableTranslation($r)
154     {
155       //   print_R($r); DB_DataObject::DebugLevel(1);
156         $tr = DB_DataObject::Factory('core_templatestr');
157         $tr->autoJoin();
158
159        
160         $tr->whereAdd("core_templatestr.on_id='{$tr->escape($r['table id'])}'");
161         $tr->whereAdd("core_templatestr.on_table='{$tr->escape($r['table'])}'");
162         $tr->whereAdd("core_templatestr.on_col='{$tr->escape($r['column'])}'");
163         $tr->whereAdd("join_src_id_id.mdsum='{$tr->escape($r['code'])}'");
164         $tr->lang = $r['translation'];
165         if ($tr->find(true) && strlen(trim($r['txt']))) {
166             $tt = DB_DataObject::Factory('core_templatestr');
167             $tt->get($tr->id);
168             $tr= clone($tt);
169             $tt->txt = $r['txt'];
170             $tt->updated = date('Y-m-d H:i:s');
171             $tt->update($tr);
172              
173             return 1;
174         }
175         return 0;
176     }
177     
178     
179     function readXLS($file) {
180         
181         require_once 'System.php';
182         $ssconvert = System::which('ssconvert');
183         if (!$ssconvert) die("ssconvert not installed");
184         $csv = $this->tempName('csv');
185         $cmd = "$ssconvert  -T Gnumeric_stf:stf_csv ". escapeshellarg($file) . ' '.$csv;
186         
187          $data= `$cmd`;
188         
189         clearstatcache();
190          if (!file_exists($csv)) {
191             $this->jerr("Failed to make file . \n".
192                         $cmd . "\n" .
193                         $data
194                         );
195             
196             
197         }
198          //echo $data;
199         $fh = fopen($csv, 'r');
200         if (!$fh) {
201             $this->jerr("file invalid");
202         }
203         $rows = array();
204         $head = false;
205         $blank = 0;
206         while (false !== ($row = fgetcsv($fh))) {
207              
208             if ($head === false) {
209                 
210                 $h = $row;
211                 // skip header lines.
212                 array_shift($h);
213                 if (!strlen(implode('', $h))) {
214                     continue;
215                 }
216                 // how many empty rows..
217                 if (count(explode(',',(rtrim(implode(',', $h), " ,")))) < 3) {
218                     continue;
219                 }
220                 
221                 $head = array();
222                 foreach($row as $i=>$c) {
223                    $c = preg_replace('/[\t \n]+/', ' ', $c);
224                    $head[$i] = strtolower(trim($c));
225                 }
226                 continue;
227             }
228             
229             if (!strlen(trim(implode('', $h)))) {
230                 $blank++;
231                 if ($blank > 3) { 
232                     break;
233                 }
234                 continue;
235             }
236             
237             $blank = 0;
238             $rrow = array();
239             foreach($row as $i=>$c) {
240                 $c = preg_replace('/[\t \n]+/', ' ', $c);
241                 $rrow[$head[$i]] = trim($c);
242             }
243            // var_dump($row);
244           //  print_r($max);
245             $rows[] = $rrow;
246         }
247         unlink($csv);
248         return $rows;
249         
250         
251     }
252     
253 }