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 = $_SESSION['csv_cond_field'];
179 $conds = $_SESSION['csv_cond'];
180 $values = $_SESSION['csv_cond_value'];
181 $thens = $_SESSION['csv_cond_then'];
182 $elses = $_SESSION['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 private function cleanSession($fields)
230 foreach ($fields as $field) {
231 unset($_SESSION[$field]);
235 /** Handle insertion form
236 * @param $page PlatalPage to process
237 * @param $url URI of the page
238 * @param $field Editable fields
240 public function apply(&$page, $url, $fields = null
)
242 $sesfields = array('csv_value', 'csv_user_value', 'csv_cond_field',
243 'csv_update', 'csv_action', 'csv_cond_field',
244 'csv_cond', 'csv_cond_value', 'csv_cond_then',
245 'csv_cond_else', 'csv', 'csv_separator', 'csv_url');
246 if ($url != @$_SESSION['csv_url']) {
247 $this->cleanSession($sesfields);
248 $_SESSION['csv_url'] = $url;
251 if (is_null($fields) ||
empty($fields)) {
252 $fields = $this->getFieldList();
254 if (is_null($fields)) {
257 foreach ($this->field_value
as $key=>$value) {
258 $search = array_search($key, $fields);
259 unset($fields[$search]);
262 $current = Env
::v('csv_page');
263 if (empty($current)) {
266 $next = Env
::v('csv_next_page');
270 $csv = @$_SESSION['csv'];
271 if ($current == 'source' && Env
::has('csv_valid')) {
272 $csv = Env
::v('csv_source');
273 $_SESSION['csv'] = $csv;
277 if (Env
::has('csv_separator')) {
278 $sep = Env
::v('csv_separator');
282 $_SESSION['csv_separator'] = $sep;
284 $this->setCSV($csv, null
, $_SESSION['csv_separator']);
286 if ($current == 'values' && Env
::has('csv_valid')) {
292 if (Env
::has('csv_action')) {
293 $_SESSION['csv_action'] = Env
::v('csv_action');
295 if ($next == 'valid') {
296 if ($current != 'valid') {
297 $cpyfields = array('csv_value', 'csv_user_value', 'csv_cond_field',
298 'csv_update', 'csv_action', 'csv_cond_field',
299 'csv_cond', 'csv_cond_value', 'csv_cond_then',
301 foreach ($cpyfields as $field) {
302 $_SESSION[$field] = Env
::v($field);
305 $insert = $_SESSION['csv_value'];
306 $values = $_SESSION['csv_user_value'];
307 $update = $_SESSION['csv_update'];
308 foreach ($insert as $key=>$value) {
310 $insert[$key] = null
;
311 } elseif ($value == 'user_value') {
312 $insert[$key] = $values[$key];
313 } elseif ($value == 'cond_value') {
314 $insert[$key] = array($this, 'dynamicCond');
315 } elseif (array_key_exists($value, $this->user_functions
)) {
316 $insert[$key] = $this->user_functions
[$value]['callback'];
318 if (isset($update[$key])) {
319 $update[$key] = $insert[$key];
322 foreach ($this->field_value
as $key=>$value) {
323 $insert[$key] = $value;
326 if ($current == 'valid' && Env
::has('csv_valid')) {
327 $this->run($_SESSION['csv_action'], $insert, $update);
328 $page->assign('csv_done', true
);
329 $this->cleanSession($sesfields);
332 foreach ($this->data
as $line) {
333 $preview[] = $this->makeAssoc($line, $insert);
335 $page->assign('csv_preview', $preview);
338 $page->assign('csv_index', $this->index
);
339 $page->assign('csv_functions', $this->user_functions
);
340 $page->assign('csv_field_desc', $this->field_desc
);
341 $page->assign('csv_page', $next);
342 $page->assign('csv_path', $url);
343 $page->assign('csv_fields', $fields);
347 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: