From d0fd4cacf505b6bb1fdba40979c3387a1c2fa656 Mon Sep 17 00:00:00 2001 From: Rodney Figaro Date: Tue, 24 Jan 2017 15:19:32 +0100 Subject: [PATCH] simplify CSV reader, optimize fixed codes checking --- modules/training/models/ArrayExtended.php | 8 --- modules/training/models/CSVReader.php | 67 +++++++------------ modules/training/models/CSVWriter.php | 4 ++ modules/training/models/TrainingBilan.php | 4 +- .../models/TrainingFixedCodeClient.php | 14 ++-- modules/training/models/TrainingModule.php | 11 +-- 6 files changed, 46 insertions(+), 62 deletions(-) diff --git a/modules/training/models/ArrayExtended.php b/modules/training/models/ArrayExtended.php index 5f3d8fc..f36169a 100644 --- a/modules/training/models/ArrayExtended.php +++ b/modules/training/models/ArrayExtended.php @@ -48,14 +48,6 @@ class ArrayExtended implements IteratorAggregate, ArrayAccess return isset($this->collection[$offset])?$this->collection[$offset]:null; } - function addNode($parent_key, $key, $value) - { - if (!isset($this->collection[$parent_key])) { - $this->collection[$parent_key] = array(); - } - $this->collection[$parent_key][$key] = $value; - } - function filterKeys(callable $predicat) { $filtered_collection = array(); diff --git a/modules/training/models/CSVReader.php b/modules/training/models/CSVReader.php index ff368ed..a07f695 100644 --- a/modules/training/models/CSVReader.php +++ b/modules/training/models/CSVReader.php @@ -3,7 +3,7 @@ require_once(__DIR__.'/ArrayExtended.php'); class CSVReader { private $file_path = ''; - private $params = array('enable_skip_line' => false, 'separator' => ',', 'delimiter' => '', 'expected_header' => array()); + private $params = array('enable_skip_line' => false, 'separator' => ',', 'enclosure' => '"', 'expected_header' => array()); private $handlers = array('start' => null, 'error' => null); private $current_num_line = 0; @@ -22,9 +22,9 @@ class CSVReader return $this; } - function setDelimiter($delimiter) + function setEnclosure($enclosure) { - $this->params['delimiter'] = $delimiter; + $this->params['enclosure'] = $enclosure; return $this; } @@ -66,27 +66,21 @@ class CSVReader $ok = false; $this->current_num_line = 0; - $f = null; - if (file_exists($this->file_path)) { - $f = fopen($this->file_path, 'r'); - } - + $f = fopen($this->file_path, 'r'); if ($f) { call_user_func($this->handlers['start']); - $first_line = $this->params['enable_skip_line'] ? fgets($f) : ''; - if ($first_line !== false) { - - $ok = $this->checkHeader($first_line); - - while ($ok && ($line = fgets($f))) { - $ok = call_user_func($this->handlers['readline'], $this->lineToArrayExtended($line)); - $this->current_num_line++; - } - - fclose($f); - + $ok = true; + if ($this->params['enable_skip_line']) { + $ok = $this->checkHeader(fgetcsv($f, 0, $this->params['separator'], $this->params['enclosure'])); } + + while ($ok && ($cols = fgetcsv($f, 0, $this->params['separator'], $this->params['enclosure']))) { + $ok = call_user_func($this->handlers['readline'], new ArrayExtended($cols)); + $this->current_num_line++; + } + + fclose($f); } if (!$ok) { @@ -101,36 +95,21 @@ class CSVReader return $this->current_num_line; } - private function checkHeader($line) + private function checkHeader(array $cols) { $ok = true; - if (count($this->params['expected_header'])>0 && is_string($line) && $line!='') { - $cols = $this->lineToArrayExtended($line); - foreach($this->params['expected_header'] as $expected_col => $expected_pos) { - $found = false; - foreach($cols as $index => $col) { - $found = (strtolower($col) == strtolower($expected_col) && $expected_pos == $index); - if ($found) { - break; - } + foreach($this->params['expected_header'] as $expected_col => $expected_pos) { + $found = false; + foreach($cols as $index => $col) { + if (strtolower($col) == strtolower($expected_col) && $expected_pos == $index) { + $found = true; + break; } - $ok = $ok && $found; } + $ok = $ok && $found; } + return $ok; } - - private function lineToArrayExtended($line) - { - $cols = explode($this->params['separator'], $line); - - if (!empty($this->params['delimiter'])) { - foreach($cols as $i => $col) { - $cols[$i] = str_replace($this->params['delimiter'], '', $col); - } - } - - return new ArrayExtended($cols); - } } diff --git a/modules/training/models/CSVWriter.php b/modules/training/models/CSVWriter.php index 8533f6a..e28c752 100644 --- a/modules/training/models/CSVWriter.php +++ b/modules/training/models/CSVWriter.php @@ -18,16 +18,19 @@ class CSVWriter function setHeader(array $line) { $this->header = $line; + return $this; } function addLine(array $line) { $this->lines[] = $line; + return $this; } function setFooter(array $line) { $this->footer = $line; + return $this; } function sortBy($index_column, $ascendant=true) @@ -73,6 +76,7 @@ class CSVWriter else { $this->save(); } + return $this; } private function outputForDownload() diff --git a/modules/training/models/TrainingBilan.php b/modules/training/models/TrainingBilan.php index f7fcea7..b766fdf 100644 --- a/modules/training/models/TrainingBilan.php +++ b/modules/training/models/TrainingBilan.php @@ -50,11 +50,11 @@ class TrainingBilan self::$result = new ArrayExtended(self::$result); $notFounds = self::$result->filterKeys('TrainingBilan::doesCodeClientExist'); - TrainingFixedCodeClient::addCodes($notFounds->toKeys()); + TrainingFixedCodeClient::addCodes($notFounds); self::$csv_output = new CSVWriter($file_output); if (self::exportResults()) { - self::$csv_output->sortBy(self::OUTPUT_COLUMN_CODECLIENT); + //self::$csv_output->sortBy(self::OUTPUT_COLUMN_CODECLIENT); self::$csv_output->flush(); return true; } diff --git a/modules/training/models/TrainingFixedCodeClient.php b/modules/training/models/TrainingFixedCodeClient.php index 8d2ff95..0712b05 100644 --- a/modules/training/models/TrainingFixedCodeClient.php +++ b/modules/training/models/TrainingFixedCodeClient.php @@ -32,11 +32,17 @@ class TrainingFixedCodeClient extends ObjectModelExtended return $result; } - static function addCodes(array $code_clients) + static function addCodes(ArrayExtended $code_clients) { - foreach ($code_clients as $code_client) { - $sql = 'SELECT COUNT(*) FROM `'._DB_PREFIX_.'training_fixedcodeclient` WHERE codeclient_from_results = \'%s\''; - if (Db::getInstance()->getValue(sprintf($sql, pSql((string)$code_client))) == 0) { + $already_notfound = array(); + $sql = 'SELECT `codeclient_fixed`, `codeclient_from_results` + FROM `'._DB_PREFIX_.'training_fixedcodeclient`'; + foreach (Db::getInstance()->executeS($sql) as $row) { + $already_notfound[$row['codeclient_from_results']] = $row['codeclient_fixed']; + } + + foreach ($code_clients->toKeys() as $code_client) { + if (!isset($already_notfound[$code_client])) { $sql = 'INSERT INTO `'._DB_PREFIX_.'training_fixedcodeclient` (codeclient_from_results, codeclient_fixed) VALUES(\'%s\', \'\')'; Db::getInstance()->execute(sprintf($sql, pSql((string)$code_client))); } diff --git a/modules/training/models/TrainingModule.php b/modules/training/models/TrainingModule.php index 9717fe7..ceb67e7 100644 --- a/modules/training/models/TrainingModule.php +++ b/modules/training/models/TrainingModule.php @@ -31,16 +31,19 @@ class TrainingModule extends ObjectModelExtended static function loadAll() { - $result = new ArrayExtended(); + $result = array(); $sql = 'SELECT `module_name`, `category_name` FROM `'._DB_PREFIX_.'training_module` ORDER BY `category_name` ASC, `module_name` ASC'; - foreach (Db::getInstance()->executeS($sql) as $index => $row) { - $result->addNode($row['category_name'], $index, $row['module_name']); + foreach (Db::getInstance()->executeS($sql) as $row) { + if (!isset($result[$row['category_name']])) { + $result[$row['category_name']] = array(); + } + $result[$row['category_name']][] = $row['module_name']; } - return $result; + return new ArrayExtended($result); } static function getExpectedHeader()