3 define('CSV_INSERT', 'insert'); // INSERT IGNORE
4 define('CSV_REPLACE', 'replace'); // REPLACE
5 define('CSV_UPDATE', 'update'); // INSERT and UPDATE on error
15 private $data = array();
17 private $user_functions = array();
19 public function CSVImporter($table, $key = 'id', $do_sql = true
)
21 $this->table
= $table;
23 $this->do_sql
= $do_sql;
26 private function processLine($line)
28 $array = split($this->separator
, $line);
29 if (is_null($this->index
)) {
30 $this->index
= $array;
34 if (count($array) != count($this->index
)) {
39 foreach ($this->index
as $key) {
40 $assoc[$key] = $array[$i];
43 $this->data
[] = $assoc;
47 private function makeAssoc($line, $relation)
50 foreach ($relation as $key=>$ref) {
51 if (@array_key_exists
($ref, $line)) {
53 } elseif (is_callable($ref, false
)) {
54 $value = call_user_func($ref, $line, $key);
58 if (is_null($value) ||
$value == 'NULL') {
66 private function makeRequestArgs($line, $relation)
69 foreach ($relation as $key=>$ref) {
70 if (@array_key_exists
($ref, $line)) {
72 } elseif (is_callable($ref, false
)) {
73 $value = call_user_func($ref, $line, $key);
77 if (is_null($value) ||
$value == 'NULL') {
80 $value = "'" . addslashes($value) . "'";
82 $ops[$key] = "$key = $value";
87 private function makeRelation()
90 foreach ($this->index
as $title) {
91 $relation[$title] = $title;
96 private function execute($query)
102 return XDB
::execute($query);
105 private function getFieldList()
107 $res = XDB
::query("SHOW COLUMNS FROM {$this->table}");
108 if ($res->numRows()) {
109 return $res->fetchColumn();
114 public function setCSV($csv, $index = null
, $separator = ';')
117 $this->separator
= $separator;
118 $csv = preg_split("/(\r\n|\r|\n)/", $csv);
120 foreach ($csv as $line) {
121 $this->processLine($line);
125 public function run($action = CSV_UPDATE
, $insert_relation = null
, $update_relation = null
)
127 if (is_null($insert_relation)) {
128 $insert_relation = $this->makeRelation();
130 if (is_null($update_relation)) {
131 $update_relation = $insert_relation;
133 foreach ($this->data
as $line) {
134 $set = join(', ', $this->makeRequestArgs($line, $insert_relation));
137 $this->execute("INSERT IGNORE INTO {$this->table} SET $set");
140 $this->execute("REPLACE INTO {$this->table} SET $set");
143 if (!$this->execute("INSERT INTO {$this->table} SET $set")) {
144 $ops = $this->makeRequestArgs($line, $update_relation);
145 $set = join(', ', $ops);
146 $this->execute("UPDATE {$this->table} SET $set WHERE {$ops[$this->key]}");
153 static public function dynamicCond($line, $key)
155 static $fields, $conds, $values, $thens, $elses;
157 if (!isset($fields)) {
158 $fields = Env
::v('csv_cond_field');
159 $conds = Env
::v('csv_cond');
160 $values = Env
::v('csv_cond_value');
161 $thens = Env
::v('csv_cond_then');
162 $elses = Env
::v('csv_cond_else');
164 $field = $line[$fields[$key]];
165 $cond = $conds[$key];
166 $value = $values[$key];
167 if (is_numeric($field) && is_numeric($value)) {
168 $field = floatval($field);
169 $value = floatval($value);
172 case 'defined': $ok = (!empty($field)); break;
173 case 'equals': $ok = ($field == $value); break;
174 case 'contains': $ok = (strpos($field, $value) !== false
); break;
175 case 'contained': $ok = (strpos($value, $field) !== false
); break;
176 case 'greater': $ok = ($field > $value); break;
177 case 'greater_or_equal': $ok ($field >= $value); break;
178 case 'lower': $ok = ($field < $value); break;
179 case 'lower_or_equal': $ok = ($field <= $value); break;
180 default: $ok = false
;
189 public function registerFunction($name, $desc, $callback)
191 if (is_callable($callback)) {
192 $this->user_functions
['func_' . $name] = array('desc' => $desc, 'callback' => $callback);
198 /** Handle insertion form
199 * @param $page PlatalPage to process
200 * @param $url URI of the page
201 * @param $field Editable fields
203 public function apply(&$page, $url, $fields = null
)
205 if (is_null($fields)) {
206 $fields = $this->getFieldList();
208 if (is_null($fields)) {
212 $current = Env
::v('csv_page');
213 if (empty($current)) {
216 $next = Env
::v('csv_next_page');
220 $csv = Env
::v('csv');
221 if ($current == 'source' && Env
::has('csv_valid')) {
222 $csv = Env
::v('csv_source');
228 if ($current == 'values' && Env
::has('csv_valid')) {
234 if ($next == 'valid') {
235 $insert = Env
::v('csv_value');
236 $values = Env
::v('csv_user_value');
237 $update = Env
::v('csv_update');
238 foreach ($insert as $key=>$value) {
240 $insert[$key] = null
;
241 } elseif ($value == 'user_value') {
242 $insert[$key] = $values[$key];
243 } elseif ($value == 'cond_value') {
244 $insert[$key] = array($this, 'dynamicCond');
245 } elseif (array_key_exists($value, $this->user_functions
)) {
246 $insert[$key] = $this->user_functions
[$value]['callback'];
248 if (isset($update[$key])) {
249 $update[$key] = $insert[$key];
252 if ($current == 'valid' && Env
::has('csv_valid')) {
253 $this->run(Env
::v('csv_action'), $insert, $update);
254 $page->assign('csv_done', true
);
257 foreach ($this->data
as $line) {
258 $preview[] = $this->makeAssoc($line, $insert);
260 $page->assign('csv_preview', $preview);
263 $page->assign('csv_index', $this->index
);
264 $page->assign('csv_funtions', $this->user_functions
);
265 $page->assign('csv_page', $next);
266 $page->assign('csv_path', $url);
267 $page->assign('csv_fields', $fields);
268 $page->assign('csv', $csv);