| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2007 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 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. * |
| 10 | * * |
| 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. * |
| 15 | * * |
| 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 * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | define('CSV_INSERT', 'insert'); // INSERT IGNORE |
| 23 | define('CSV_REPLACE', 'replace'); // REPLACE |
| 24 | define('CSV_UPDATE', 'update'); // INSERT and UPDATE on error |
| 25 | |
| 26 | class CSVImporter |
| 27 | { |
| 28 | private $table; |
| 29 | private $key; |
| 30 | private $do_sql; |
| 31 | |
| 32 | private $index; |
| 33 | private $separator; |
| 34 | private $data = array(); |
| 35 | |
| 36 | private $user_functions = array(); |
| 37 | private $field_desc = array(); |
| 38 | private $field_value = array(); |
| 39 | |
| 40 | public function __construct($table, $key = 'id', $do_sql = true) |
| 41 | { |
| 42 | $this->table = $table; |
| 43 | $this->key = $key; |
| 44 | $this->do_sql = $do_sql; |
| 45 | } |
| 46 | |
| 47 | private function processLine($line) |
| 48 | { |
| 49 | $array = explode($this->separator, $line); |
| 50 | if (is_null($this->index)) { |
| 51 | $this->index = array_map('strtolower', $array); |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | if (count($array) != count($this->index)) { |
| 56 | return false; |
| 57 | } |
| 58 | $assoc = array(); |
| 59 | $i = 0; |
| 60 | foreach ($this->index as $key) { |
| 61 | $assoc[$key] = $array[$i]; |
| 62 | $i++; |
| 63 | } |
| 64 | $this->data[] = $assoc; |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | private function getValue($line, $key, $action) |
| 69 | { |
| 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); |
| 74 | } else { |
| 75 | $value = $action; |
| 76 | } |
| 77 | if (is_null($value) || $value == 'NULL') { |
| 78 | $value = 'NULL'; |
| 79 | } |
| 80 | return $value; |
| 81 | } |
| 82 | |
| 83 | private function makeAssoc($line, $relation) |
| 84 | { |
| 85 | $ops = array(); |
| 86 | foreach ($relation as $key=>$ref) { |
| 87 | $ops[$key] = $this->getValue($line, $key, $ref); |
| 88 | } |
| 89 | return $ops; |
| 90 | } |
| 91 | |
| 92 | private function makeRequestArgs($line, $relation) |
| 93 | { |
| 94 | $ops = array(); |
| 95 | foreach ($relation as $key=>$ref) { |
| 96 | $value = $this->getValue($line, $key, $ref); |
| 97 | if (!is_null($value) && $value != 'NULL') { |
| 98 | $value = "'" . addslashes($value) . "'"; |
| 99 | } |
| 100 | $ops[$key] = "$key = $value"; |
| 101 | } |
| 102 | return $ops; |
| 103 | } |
| 104 | |
| 105 | private function makeRelation() |
| 106 | { |
| 107 | $relation = array(); |
| 108 | foreach ($this->index as $title) { |
| 109 | $relation[$title] = $title; |
| 110 | } |
| 111 | return $relation; |
| 112 | } |
| 113 | |
| 114 | private function execute($query) |
| 115 | { |
| 116 | if (!$this->do_sql) { |
| 117 | echo "$query;\n"; |
| 118 | return false; |
| 119 | } |
| 120 | return XDB::execute($query); |
| 121 | } |
| 122 | |
| 123 | private function getFieldList() |
| 124 | { |
| 125 | $res = XDB::query("SHOW COLUMNS FROM {$this->table}"); |
| 126 | if ($res->numRows()) { |
| 127 | return $res->fetchColumn(); |
| 128 | } |
| 129 | return null; |
| 130 | } |
| 131 | |
| 132 | public function setCSV($csv, $index = null, $separator = ';') |
| 133 | { |
| 134 | $this->index = null; |
| 135 | $this->separator = $separator; |
| 136 | $csv = preg_split("/(\r\n|\r|\n)/", $csv); |
| 137 | |
| 138 | foreach ($csv as $line) { |
| 139 | $this->processLine($line); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | public function run($action = CSV_UPDATE, $insert_relation = null, $update_relation = null) |
| 144 | { |
| 145 | if (is_null($insert_relation)) { |
| 146 | $insert_relation = $this->makeRelation(); |
| 147 | } |
| 148 | if (is_null($update_relation)) { |
| 149 | $update_relation = $insert_relation; |
| 150 | } |
| 151 | foreach ($this->data as $line) { |
| 152 | $set = join(', ', $this->makeRequestArgs($line, $insert_relation)); |
| 153 | switch ($action) { |
| 154 | case CSV_INSERT: |
| 155 | $this->execute("INSERT IGNORE INTO {$this->table} SET $set"); |
| 156 | break; |
| 157 | case CSV_REPLACE: |
| 158 | $this->execute("REPLACE INTO {$this->table} SET $set"); |
| 159 | break; |
| 160 | case CSV_UPDATE: |
| 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]}"); |
| 165 | } |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | static public function dynamicCond($line, $key) |
| 172 | { |
| 173 | static $fields, $conds, $values, $thens, $elses; |
| 174 | |
| 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'); |
| 181 | } |
| 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); |
| 188 | } |
| 189 | switch ($cond) { |
| 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; |
| 199 | } |
| 200 | if ($ok) { |
| 201 | return $thens[$key]; |
| 202 | } else { |
| 203 | return $elses[$key]; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | public function registerFunction($name, $desc, $callback) |
| 208 | { |
| 209 | if (is_callable($callback, false, $ref)) { |
| 210 | $this->user_functions['func_' . $name] = array('desc' => $desc, 'callback' => $callback); |
| 211 | return true; |
| 212 | } |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | public function describe($name, $desc) |
| 217 | { |
| 218 | $this->field_desc[$name] = $desc; |
| 219 | } |
| 220 | |
| 221 | public function forceValue($name, $value) |
| 222 | { |
| 223 | $this->field_value[$name] = $value; |
| 224 | } |
| 225 | |
| 226 | /** Handle insertion form |
| 227 | * @param $page PlatalPage to process |
| 228 | * @param $url URI of the page |
| 229 | * @param $field Editable fields |
| 230 | */ |
| 231 | public function apply(&$page, $url, $fields = null) |
| 232 | { |
| 233 | if (is_null($fields) || empty($fields)) { |
| 234 | $fields = $this->getFieldList(); |
| 235 | } |
| 236 | if (is_null($fields)) { |
| 237 | return false; |
| 238 | } |
| 239 | foreach ($this->field_value as $key=>$value) { |
| 240 | unset($fields[$key]); |
| 241 | } |
| 242 | |
| 243 | $current = Env::v('csv_page'); |
| 244 | if (empty($current)) { |
| 245 | $current = 'source'; |
| 246 | } |
| 247 | $next = Env::v('csv_next_page'); |
| 248 | if (empty($next)) { |
| 249 | $next = $current; |
| 250 | } |
| 251 | $csv = Env::v('csv'); |
| 252 | if ($current == 'source' && Env::has('csv_valid')) { |
| 253 | $csv = Env::v('csv_source'); |
| 254 | $next = 'values'; |
| 255 | } |
| 256 | if ($csv) { |
| 257 | $sep = Env::v('csv_separator'); |
| 258 | if (empty($sep)) { |
| 259 | $sep = ';'; |
| 260 | } |
| 261 | echo $sep; |
| 262 | $this->setCSV($csv, null, $sep); |
| 263 | } |
| 264 | if ($current == 'values' && Env::has('csv_valid')) { |
| 265 | $next = 'valid'; |
| 266 | } |
| 267 | if (empty($csv)) { |
| 268 | $next = 'source'; |
| 269 | } |
| 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) { |
| 275 | if (empty($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']; |
| 283 | } |
| 284 | if (isset($update[$key])) { |
| 285 | $update[$key] = $insert[$key]; |
| 286 | } |
| 287 | } |
| 288 | foreach ($this->field_value as $key=>$value) { |
| 289 | $insert[$key] = $value; |
| 290 | } |
| 291 | if ($current == 'valid' && Env::has('csv_valid')) { |
| 292 | $this->run(Env::v('csv_action'), $insert, $update); |
| 293 | $page->assign('csv_done', true); |
| 294 | } else { |
| 295 | $preview = array(); |
| 296 | foreach ($this->data as $line) { |
| 297 | $preview[] = $this->makeAssoc($line, $insert); |
| 298 | } |
| 299 | $page->assign('csv_preview', $preview); |
| 300 | } |
| 301 | } |
| 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); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | ?> |