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
33 private $data = array();
35 private $user_functions = array();
36 private $field_desc = array();
37 private $field_value = array();
39 public function __construct($table, $key = 'id', $do_sql = true
)
41 $this->table
= $table;
43 $this->do_sql
= $do_sql;
46 private function processLine(array $array)
48 if (is_null($this->index
)) {
49 $this->index
= array_map('strtolower', $array);
53 if (count($array) != count($this->index
)) {
58 foreach ($this->index
as $key) {
59 $assoc[$key] = $array[$i];
62 $this->data
[] = $assoc;
66 private function getValue($line, $key, $action)
68 if (@array_key_exists
($action, $line)) {
69 $value = $line[$action];
70 } elseif (is_callable($action, false
)) {
71 $value = call_user_func($action, $line, $key);
75 if (is_null($value) ||
$value == 'NULL') {
81 private function makeAssoc($line, $relation)
84 foreach ($relation as $key=>$ref) {
85 $ops[$key] = $this->getValue($line, $key, $ref);
90 private function makeRequestArgs($line, $relation)
93 foreach ($relation as $key=>$ref) {
94 $value = $this->getValue($line, $key, $ref);
95 if (!is_null($value) && $value != 'NULL') {
96 $value = "'" . addslashes($value) . "'";
98 $ops[$key] = "$key = $value";
103 private function makeRelation()
106 foreach ($this->index
as $title) {
107 $relation[$title] = $title;
112 private function execute($query)
114 if (!$this->do_sql
) {
118 return XDB
::execute($query);
121 private function getFieldList()
123 $res = XDB
::query("SHOW COLUMNS FROM {$this->table}");
124 if ($res->numRows()) {
125 return $res->fetchColumn();
130 public function setCSV($csv, $index = null
, $separator = ';')
132 require_once dirname(__FILE__
) . '/varstream.php';
138 $res = fopen('var://csv_source', 'r');
140 while (!feof($res)) {
141 $this->processLine(fgetcsv($res, 0, $separator));
145 public function run($action = CSV_UPDATE
, $insert_relation = null
, $update_relation = null
)
147 if (is_null($insert_relation)) {
148 $insert_relation = $this->makeRelation();
150 if (is_null($update_relation)) {
151 $update_relation = $insert_relation;
153 foreach ($this->data
as $line) {
154 $set = join(', ', $this->makeRequestArgs($line, $insert_relation));
157 $this->execute("INSERT IGNORE INTO {$this->table} SET $set");
160 $this->execute("REPLACE INTO {$this->table} SET $set");
163 if (!$this->execute("INSERT INTO {$this->table} SET $set")) {
164 $ops = $this->makeRequestArgs($line, $update_relation);
165 $set = join(', ', $ops);
166 $this->execute("UPDATE {$this->table} SET $set WHERE {$ops[$this->key]}");
173 static public function dynamicCond($line, $key)
175 static $fields, $conds, $values, $thens, $elses;
177 if (!isset($fields)) {
178 $fields = Env
::v('csv_cond_field');
179 $conds = Env
::v('csv_cond');
180 $values = Env
::v('csv_cond_value');
181 $thens = Env
::v('csv_cond_then');
182 $elses = Env
::v('csv_cond_else');
184 $field = $line[$fields[$key]];
185 $cond = $conds[$key];
186 $value = $values[$key];
187 if (is_numeric($field) && is_numeric($value)) {
188 $field = floatval($field);
189 $value = floatval($value);
192 case 'defined': $ok = (!empty($field)); break;
193 case 'equals': $ok = ($field == $value); break;
194 case 'contains': $ok = (strpos($field, $value) !== false
); break;
195 case 'contained': $ok = (strpos($value, $field) !== false
); break;
196 case 'greater': $ok = ($field > $value); break;
197 case 'greater_or_equal': $ok ($field >= $value); break;
198 case 'lower': $ok = ($field < $value); break;
199 case 'lower_or_equal': $ok = ($field <= $value); break;
200 default: $ok = false
;
209 public function registerFunction($name, $desc, $callback)
211 if (is_callable($callback, false
, $ref)) {
212 $this->user_functions
['func_' . $name] = array('desc' => $desc, 'callback' => $callback);
218 public function describe($name, $desc)
220 $this->field_desc
[$name] = $desc;
223 public function forceValue($name, $value)
225 $this->field_value
[$name] = $value;
228 /** Handle insertion form
229 * @param $page PlatalPage to process
230 * @param $url URI of the page
231 * @param $field Editable fields
233 public function apply(&$page, $url, $fields = null
)
235 if (is_null($fields) ||
empty($fields)) {
236 $fields = $this->getFieldList();
238 if (is_null($fields)) {
241 foreach ($this->field_value
as $key=>$value) {
242 $search = array_search($key, $fields);
243 unset($fields[$search]);
246 $current = Env
::v('csv_page');
247 if (empty($current)) {
250 $next = Env
::v('csv_next_page');
254 $csv = Env
::v('csv');
255 if ($current == 'source' && Env
::has('csv_valid')) {
256 $csv = Env
::v('csv_source');
260 $sep = Env
::v('csv_separator');
264 $this->setCSV($csv, null
, $sep);
266 if ($current == 'values' && Env
::has('csv_valid')) {
272 if ($next == 'valid') {
273 $insert = Env
::v('csv_value');
274 $values = Env
::v('csv_user_value');
275 $update = Env
::v('csv_update');
276 foreach ($insert as $key=>$value) {
278 $insert[$key] = null
;
279 } elseif ($value == 'user_value') {
280 $insert[$key] = $values[$key];
281 } elseif ($value == 'cond_value') {
282 $insert[$key] = array($this, 'dynamicCond');
283 } elseif (array_key_exists($value, $this->user_functions
)) {
284 $insert[$key] = $this->user_functions
[$value]['callback'];
286 if (isset($update[$key])) {
287 $update[$key] = $insert[$key];
290 foreach ($this->field_value
as $key=>$value) {
291 $insert[$key] = $value;
294 if ($current == 'valid' && Env
::has('csv_valid')) {
295 $this->run(Env
::v('csv_action'), $insert, $update);
296 $page->assign('csv_done', true
);
299 foreach ($this->data
as $line) {
300 $preview[] = $this->makeAssoc($line, $insert);
302 $page->assign('csv_preview', $preview);
305 $page->assign('csv_index', $this->index
);
306 $page->assign('csv_functions', $this->user_functions
);
307 $page->assign('csv_field_desc', $this->field_desc
);
308 $page->assign('csv_page', $next);
309 $page->assign('csv_path', $url);
310 $page->assign('csv_fields', $fields);
311 $page->assign('csv', $csv);