| 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 $data = array(); |
| 34 | |
| 35 | private $user_functions = array(); |
| 36 | private $field_desc = array(); |
| 37 | private $field_value = array(); |
| 38 | |
| 39 | public function __construct($table, $key = 'id', $do_sql = true) |
| 40 | { |
| 41 | $this->table = $table; |
| 42 | $this->key = $key; |
| 43 | $this->do_sql = $do_sql; |
| 44 | } |
| 45 | |
| 46 | private function processLine(array $array) |
| 47 | { |
| 48 | if (is_null($this->index)) { |
| 49 | $this->index = array_map('strtolower', $array); |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | if (count($array) != count($this->index)) { |
| 54 | return false; |
| 55 | } |
| 56 | $assoc = array(); |
| 57 | $i = 0; |
| 58 | foreach ($this->index as $key) { |
| 59 | $assoc[$key] = $array[$i]; |
| 60 | $i++; |
| 61 | } |
| 62 | $this->data[] = $assoc; |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | private function getValue($line, $key, $action) |
| 67 | { |
| 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); |
| 72 | } else { |
| 73 | $value = $action; |
| 74 | } |
| 75 | if (is_null($value) || $value == 'NULL') { |
| 76 | $value = 'NULL'; |
| 77 | } |
| 78 | return $value; |
| 79 | } |
| 80 | |
| 81 | private function makeAssoc($line, $relation) |
| 82 | { |
| 83 | $ops = array(); |
| 84 | foreach ($relation as $key=>$ref) { |
| 85 | $ops[$key] = $this->getValue($line, $key, $ref); |
| 86 | } |
| 87 | return $ops; |
| 88 | } |
| 89 | |
| 90 | private function makeRequestArgs($line, $relation) |
| 91 | { |
| 92 | $ops = array(); |
| 93 | foreach ($relation as $key=>$ref) { |
| 94 | $value = $this->getValue($line, $key, $ref); |
| 95 | if (!is_null($value) && $value != 'NULL') { |
| 96 | $value = "'" . addslashes($value) . "'"; |
| 97 | } |
| 98 | $ops[$key] = "$key = $value"; |
| 99 | } |
| 100 | return $ops; |
| 101 | } |
| 102 | |
| 103 | private function makeRelation() |
| 104 | { |
| 105 | $relation = array(); |
| 106 | foreach ($this->index as $title) { |
| 107 | $relation[$title] = $title; |
| 108 | } |
| 109 | return $relation; |
| 110 | } |
| 111 | |
| 112 | private function execute($query) |
| 113 | { |
| 114 | if (!$this->do_sql) { |
| 115 | echo "$query;\n"; |
| 116 | return false; |
| 117 | } |
| 118 | return XDB::execute($query); |
| 119 | } |
| 120 | |
| 121 | private function getFieldList() |
| 122 | { |
| 123 | $res = XDB::query("SHOW COLUMNS FROM {$this->table}"); |
| 124 | if ($res->numRows()) { |
| 125 | return $res->fetchColumn(); |
| 126 | } |
| 127 | return null; |
| 128 | } |
| 129 | |
| 130 | public function setCSV($csv, $index = null, $separator = ';') |
| 131 | { |
| 132 | require_once dirname(__FILE__) . '/varstream.php'; |
| 133 | VarStream::init(); |
| 134 | global $csv_source; |
| 135 | $this->index = null; |
| 136 | |
| 137 | $csv_source = $csv; |
| 138 | $res = fopen('var://csv_source', 'r'); |
| 139 | |
| 140 | while (!feof($res)) { |
| 141 | $this->processLine(fgetcsv($res, 0, $separator)); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | public function run($action = CSV_UPDATE, $insert_relation = null, $update_relation = null) |
| 146 | { |
| 147 | if (is_null($insert_relation)) { |
| 148 | $insert_relation = $this->makeRelation(); |
| 149 | } |
| 150 | if (is_null($update_relation)) { |
| 151 | $update_relation = $insert_relation; |
| 152 | } |
| 153 | foreach ($this->data as $line) { |
| 154 | $set = join(', ', $this->makeRequestArgs($line, $insert_relation)); |
| 155 | switch ($action) { |
| 156 | case CSV_INSERT: |
| 157 | $this->execute("INSERT IGNORE INTO {$this->table} SET $set"); |
| 158 | break; |
| 159 | case CSV_REPLACE: |
| 160 | $this->execute("REPLACE INTO {$this->table} SET $set"); |
| 161 | break; |
| 162 | case CSV_UPDATE: |
| 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]}"); |
| 167 | } |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | static public function dynamicCond($line, $key) |
| 174 | { |
| 175 | static $fields, $conds, $values, $thens, $elses; |
| 176 | |
| 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'); |
| 183 | } |
| 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); |
| 190 | } |
| 191 | switch ($cond) { |
| 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; |
| 201 | } |
| 202 | if ($ok) { |
| 203 | return $thens[$key]; |
| 204 | } else { |
| 205 | return $elses[$key]; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | public function registerFunction($name, $desc, $callback) |
| 210 | { |
| 211 | if (is_callable($callback, false, $ref)) { |
| 212 | $this->user_functions['func_' . $name] = array('desc' => $desc, 'callback' => $callback); |
| 213 | return true; |
| 214 | } |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | public function describe($name, $desc) |
| 219 | { |
| 220 | $this->field_desc[$name] = $desc; |
| 221 | } |
| 222 | |
| 223 | public function forceValue($name, $value) |
| 224 | { |
| 225 | $this->field_value[$name] = $value; |
| 226 | } |
| 227 | |
| 228 | /** Handle insertion form |
| 229 | * @param $page PlatalPage to process |
| 230 | * @param $url URI of the page |
| 231 | * @param $field Editable fields |
| 232 | */ |
| 233 | public function apply(&$page, $url, $fields = null) |
| 234 | { |
| 235 | if (is_null($fields) || empty($fields)) { |
| 236 | $fields = $this->getFieldList(); |
| 237 | } |
| 238 | if (is_null($fields)) { |
| 239 | return false; |
| 240 | } |
| 241 | foreach ($this->field_value as $key=>$value) { |
| 242 | $search = array_search($key, $fields); |
| 243 | unset($fields[$search]); |
| 244 | } |
| 245 | |
| 246 | $current = Env::v('csv_page'); |
| 247 | if (empty($current)) { |
| 248 | $current = 'source'; |
| 249 | } |
| 250 | $next = Env::v('csv_next_page'); |
| 251 | if (empty($next)) { |
| 252 | $next = $current; |
| 253 | } |
| 254 | $csv = Env::v('csv'); |
| 255 | if ($current == 'source' && Env::has('csv_valid')) { |
| 256 | $csv = Env::v('csv_source'); |
| 257 | $next = 'values'; |
| 258 | } |
| 259 | if ($csv) { |
| 260 | $sep = Env::v('csv_separator'); |
| 261 | if (empty($sep)) { |
| 262 | $sep = ';'; |
| 263 | } |
| 264 | $this->setCSV($csv, null, $sep); |
| 265 | } |
| 266 | if ($current == 'values' && Env::has('csv_valid')) { |
| 267 | $next = 'valid'; |
| 268 | } |
| 269 | if (empty($csv)) { |
| 270 | $next = 'source'; |
| 271 | } |
| 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) { |
| 277 | if (empty($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']; |
| 285 | } |
| 286 | if (isset($update[$key])) { |
| 287 | $update[$key] = $insert[$key]; |
| 288 | } |
| 289 | } |
| 290 | foreach ($this->field_value as $key=>$value) { |
| 291 | $insert[$key] = $value; |
| 292 | $fields[] = $key; |
| 293 | } |
| 294 | if ($current == 'valid' && Env::has('csv_valid')) { |
| 295 | $this->run(Env::v('csv_action'), $insert, $update); |
| 296 | $page->assign('csv_done', true); |
| 297 | } else { |
| 298 | $preview = array(); |
| 299 | foreach ($this->data as $line) { |
| 300 | $preview[] = $this->makeAssoc($line, $insert); |
| 301 | } |
| 302 | $page->assign('csv_preview', $preview); |
| 303 | } |
| 304 | } |
| 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); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | ?> |