2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 define('CSV_INSERT', 'insert'); // INSERT IGNORE
23 define('CSV_REPLACE', 'replace'); // REPLACE
24 define('CSV_UPDATE', 'update'); // INSERT and UPDATE on error
34 private $data = array();
36 private $user_functions = array();
37 private $field_desc = array();
39 public function CSVImporter($table, $key = 'id', $do_sql = true
)
41 $this->table
= $table;
43 $this->do_sql
= $do_sql;
46 private function processLine($line)
48 $array = split($this->separator
, $line);
49 if (is_null($this->index
)) {
50 $this->index
= $array;
54 if (count($array) != count($this->index
)) {
59 foreach ($this->index
as $key) {
60 $assoc[$key] = $array[$i];
63 $this->data
[] = $assoc;
67 private function getValue($line, $key, $action)
69 if (@array_key_exists
($action, $line)) {
70 $value = $line[$action];
71 } elseif (is_callable($action, false
)) {
72 $value = call_user_func($action, $line, $key);
76 if (is_null($value) ||
$value == 'NULL') {
82 private function makeAssoc($line, $relation)
85 foreach ($relation as $key=>$ref) {
86 $ops[$key] = $this->getValue($line, $key, $ref);
91 private function makeRequestArgs($line, $relation)
94 foreach ($relation as $key=>$ref) {
95 $value = $this->getValue($line, $key, $ref);
96 if (!is_null($value) && $value != 'NULL') {
97 $value = "'" . addslashes($value) . "'";
99 $ops[$key] = "$key = $value";
104 private function makeRelation()
107 foreach ($this->index
as $title) {
108 $relation[$title] = $title;
113 private function execute($query)
115 if (!$this->do_sql
) {
119 return XDB
::execute($query);
122 private function getFieldList()
124 $res = XDB
::query("SHOW COLUMNS FROM {$this->table}");
125 if ($res->numRows()) {
126 return $res->fetchColumn();
131 public function setCSV($csv, $index = null
, $separator = ';')
134 $this->separator
= $separator;
135 $csv = preg_split("/(\r\n|\r|\n)/", $csv);
137 foreach ($csv as $line) {
138 $this->processLine($line);
142 public function run($action = CSV_UPDATE
, $insert_relation = null
, $update_relation = null
)
144 if (is_null($insert_relation)) {
145 $insert_relation = $this->makeRelation();
147 if (is_null($update_relation)) {
148 $update_relation = $insert_relation;
150 foreach ($this->data
as $line) {
151 $set = join(', ', $this->makeRequestArgs($line, $insert_relation));
154 $this->execute("INSERT IGNORE INTO {$this->table} SET $set");
157 $this->execute("REPLACE INTO {$this->table} SET $set");
160 if (!$this->execute("INSERT INTO {$this->table} SET $set")) {
161 $ops = $this->makeRequestArgs($line, $update_relation);
162 $set = join(', ', $ops);
163 $this->execute("UPDATE {$this->table} SET $set WHERE {$ops[$this->key]}");
170 static public function dynamicCond($line, $key)
172 static $fields, $conds, $values, $thens, $elses;
174 if (!isset($fields)) {
175 $fields = Env
::v('csv_cond_field');
176 $conds = Env
::v('csv_cond');
177 $values = Env
::v('csv_cond_value');
178 $thens = Env
::v('csv_cond_then');
179 $elses = Env
::v('csv_cond_else');
181 $field = $line[$fields[$key]];
182 $cond = $conds[$key];
183 $value = $values[$key];
184 if (is_numeric($field) && is_numeric($value)) {
185 $field = floatval($field);
186 $value = floatval($value);
189 case 'defined': $ok = (!empty($field)); break;
190 case 'equals': $ok = ($field == $value); break;
191 case 'contains': $ok = (strpos($field, $value) !== false
); break;
192 case 'contained': $ok = (strpos($value, $field) !== false
); break;
193 case 'greater': $ok = ($field > $value); break;
194 case 'greater_or_equal': $ok ($field >= $value); break;
195 case 'lower': $ok = ($field < $value); break;
196 case 'lower_or_equal': $ok = ($field <= $value); break;
197 default: $ok = false
;
206 public function registerFunction($name, $desc, $callback)
208 if (is_callable($callback)) {
209 $this->user_functions
['func_' . $name] = array('desc' => $desc, 'callback' => $callback);
215 public function describe($name, $desc)
217 $this->field_desc
[$name] = $desc;
220 /** Handle insertion form
221 * @param $page PlatalPage to process
222 * @param $url URI of the page
223 * @param $field Editable fields
225 public function apply(&$page, $url, $fields = null
)
227 if (is_null($fields) ||
empty($fields)) {
228 $fields = $this->getFieldList();
230 if (is_null($fields)) {
234 $current = Env
::v('csv_page');
235 if (empty($current)) {
238 $next = Env
::v('csv_next_page');
242 $csv = Env
::v('csv');
243 if ($current == 'source' && Env
::has('csv_valid')) {
244 $csv = Env
::v('csv_source');
250 if ($current == 'values' && Env
::has('csv_valid')) {
256 if ($next == 'valid') {
257 $insert = Env
::v('csv_value');
258 $values = Env
::v('csv_user_value');
259 $update = Env
::v('csv_update');
260 foreach ($insert as $key=>$value) {
262 $insert[$key] = null
;
263 } elseif ($value == 'user_value') {
264 $insert[$key] = $values[$key];
265 } elseif ($value == 'cond_value') {
266 $insert[$key] = array($this, 'dynamicCond');
267 } elseif (array_key_exists($value, $this->user_functions
)) {
268 $insert[$key] = $this->user_functions
[$value]['callback'];
270 if (isset($update[$key])) {
271 $update[$key] = $insert[$key];
274 if ($current == 'valid' && Env
::has('csv_valid')) {
275 $this->run(Env
::v('csv_action'), $insert, $update);
276 $page->assign('csv_done', true
);
279 foreach ($this->data
as $line) {
280 $preview[] = $this->makeAssoc($line, $insert);
282 $page->assign('csv_preview', $preview);
285 $page->assign('csv_index', $this->index
);
286 $page->assign('csv_funtions', $this->user_functions
);
287 $page->assign('csv_field_desc', $this->field_desc
);
288 $page->assign('csv_page', $next);
289 $page->assign('csv_path', $url);
290 $page->assign('csv_fields', $fields);
291 $page->assign('csv', $csv);