2 /***************************************************************************
3 * Copyright (C) 2003-2007 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();
38 private $field_value = array();
40 public function __construct($table, $key = 'id', $do_sql = true
)
42 $this->table
= $table;
44 $this->do_sql
= $do_sql;
47 private function processLine($line)
49 $array = explode($this->separator
, $line);
50 if (is_null($this->index
)) {
51 $this->index
= array_map('strtolower', $array);
55 if (count($array) != count($this->index
)) {
60 foreach ($this->index
as $key) {
61 $assoc[$key] = $array[$i];
64 $this->data
[] = $assoc;
68 private function getValue($line, $key, $action)
70 if (@array_key_exists
($action, $line)) {
71 $value = $line[$action];
72 } elseif (is_callable($action, false
)) {
73 $value = call_user_func($action, $line, $key);
77 if (is_null($value) ||
$value == 'NULL') {
83 private function makeAssoc($line, $relation)
86 foreach ($relation as $key=>$ref) {
87 $ops[$key] = $this->getValue($line, $key, $ref);
92 private function makeRequestArgs($line, $relation)
95 foreach ($relation as $key=>$ref) {
96 $value = $this->getValue($line, $key, $ref);
97 if (!is_null($value) && $value != 'NULL') {
98 $value = "'" . addslashes($value) . "'";
100 $ops[$key] = "$key = $value";
105 private function makeRelation()
108 foreach ($this->index
as $title) {
109 $relation[$title] = $title;
114 private function execute($query)
116 if (!$this->do_sql
) {
120 return XDB
::execute($query);
123 private function getFieldList()
125 $res = XDB
::query("SHOW COLUMNS FROM {$this->table}");
126 if ($res->numRows()) {
127 return $res->fetchColumn();
132 public function setCSV($csv, $index = null
, $separator = ';')
135 $this->separator
= $separator;
136 $csv = preg_split("/(\r\n|\r|\n)/", $csv);
138 foreach ($csv as $line) {
139 $this->processLine($line);
143 public function run($action = CSV_UPDATE
, $insert_relation = null
, $update_relation = null
)
145 if (is_null($insert_relation)) {
146 $insert_relation = $this->makeRelation();
148 if (is_null($update_relation)) {
149 $update_relation = $insert_relation;
151 foreach ($this->data
as $line) {
152 $set = join(', ', $this->makeRequestArgs($line, $insert_relation));
155 $this->execute("INSERT IGNORE INTO {$this->table} SET $set");
158 $this->execute("REPLACE INTO {$this->table} SET $set");
161 if (!$this->execute("INSERT INTO {$this->table} SET $set")) {
162 $ops = $this->makeRequestArgs($line, $update_relation);
163 $set = join(', ', $ops);
164 $this->execute("UPDATE {$this->table} SET $set WHERE {$ops[$this->key]}");
171 static public function dynamicCond($line, $key)
173 static $fields, $conds, $values, $thens, $elses;
175 if (!isset($fields)) {
176 $fields = Env
::v('csv_cond_field');
177 $conds = Env
::v('csv_cond');
178 $values = Env
::v('csv_cond_value');
179 $thens = Env
::v('csv_cond_then');
180 $elses = Env
::v('csv_cond_else');
182 $field = $line[$fields[$key]];
183 $cond = $conds[$key];
184 $value = $values[$key];
185 if (is_numeric($field) && is_numeric($value)) {
186 $field = floatval($field);
187 $value = floatval($value);
190 case 'defined': $ok = (!empty($field)); break;
191 case 'equals': $ok = ($field == $value); break;
192 case 'contains': $ok = (strpos($field, $value) !== false
); break;
193 case 'contained': $ok = (strpos($value, $field) !== false
); break;
194 case 'greater': $ok = ($field > $value); break;
195 case 'greater_or_equal': $ok ($field >= $value); break;
196 case 'lower': $ok = ($field < $value); break;
197 case 'lower_or_equal': $ok = ($field <= $value); break;
198 default: $ok = false
;
207 public function registerFunction($name, $desc, $callback)
209 if (is_callable($callback, false
, $ref)) {
210 $this->user_functions
['func_' . $name] = array('desc' => $desc, 'callback' => $callback);
216 public function describe($name, $desc)
218 $this->field_desc
[$name] = $desc;
221 public function forceValue($name, $value)
223 $this->field_value
[$name] = $value;
226 /** Handle insertion form
227 * @param $page PlatalPage to process
228 * @param $url URI of the page
229 * @param $field Editable fields
231 public function apply(&$page, $url, $fields = null
)
233 if (is_null($fields) ||
empty($fields)) {
234 $fields = $this->getFieldList();
236 if (is_null($fields)) {
239 foreach ($this->field_value
as $key=>$value) {
240 unset($fields[$key]);
243 $current = Env
::v('csv_page');
244 if (empty($current)) {
247 $next = Env
::v('csv_next_page');
251 $csv = Env
::v('csv');
252 if ($current == 'source' && Env
::has('csv_valid')) {
253 $csv = Env
::v('csv_source');
257 $sep = Env
::v('csv_separator');
262 $this->setCSV($csv, null
, $sep);
264 if ($current == 'values' && Env
::has('csv_valid')) {
270 if ($next == 'valid') {
271 $insert = Env
::v('csv_value');
272 $values = Env
::v('csv_user_value');
273 $update = Env
::v('csv_update');
274 foreach ($insert as $key=>$value) {
276 $insert[$key] = null
;
277 } elseif ($value == 'user_value') {
278 $insert[$key] = $values[$key];
279 } elseif ($value == 'cond_value') {
280 $insert[$key] = array($this, 'dynamicCond');
281 } elseif (array_key_exists($value, $this->user_functions
)) {
282 $insert[$key] = $this->user_functions
[$value]['callback'];
284 if (isset($update[$key])) {
285 $update[$key] = $insert[$key];
288 foreach ($this->field_value
as $key=>$value) {
289 $insert[$key] = $value;
291 if ($current == 'valid' && Env
::has('csv_valid')) {
292 $this->run(Env
::v('csv_action'), $insert, $update);
293 $page->assign('csv_done', true
);
296 foreach ($this->data
as $line) {
297 $preview[] = $this->makeAssoc($line, $insert);
299 $page->assign('csv_preview', $preview);
302 $page->assign('csv_index', $this->index
);
303 $page->assign('csv_functions', $this->user_functions
);
304 $page->assign('csv_field_desc', $this->field_desc
);
305 $page->assign('csv_page', $next);
306 $page->assign('csv_path', $url);
307 $page->assign('csv_fields', $fields);
308 $page->assign('csv', $csv);