158 lines
5.8 KiB
PHP
158 lines
5.8 KiB
PHP
<?php
|
|
require_once(__DIR__.'/ArrayExtended.php');
|
|
|
|
class TrainingFixedCodeClient extends ObjectModel
|
|
{
|
|
public $id_training_fixedcodeclient;
|
|
public $codeclient_from_results;
|
|
public $codeclient_fixed;
|
|
|
|
const CODE_CLIENT_WRONG = 1;
|
|
const CODE_CLIENT_RIGHT = 2;
|
|
const CODE_EMPTY = "__code_vide__";
|
|
|
|
private static $header = array(
|
|
'CC faux'=>self::CODE_CLIENT_WRONG,
|
|
'CC bon'=>self::CODE_CLIENT_RIGHT
|
|
);
|
|
|
|
public static $definition = array(
|
|
'table' => 'training_fixedcodeclient',
|
|
'primary' => 'id_training_fixedcodeclient',
|
|
'multilang' => false,
|
|
'fields' => array(
|
|
'id_training_fixedcodeclient' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
|
|
'codeclient_from_results' => array('type' => self::TYPE_STRING, 'lang' => false, 'validate' => 'isString'),
|
|
'codeclient_fixed' => array('type' => self::TYPE_STRING, 'lang' => false, 'validate' => 'isString'),
|
|
),
|
|
);
|
|
|
|
static function exportIntoCsvFile($file_path)
|
|
{
|
|
$csv_output = new CSVWriter($file_path);
|
|
|
|
$csv_output->setHeader(array_keys(self::$header));
|
|
|
|
$sql = 'SELECT `codeclient_from_results`, `codeclient_fixed`
|
|
FROM `'._DB_PREFIX_.'training_fixedcodeclient`
|
|
';
|
|
foreach (Db::getInstance()->executeS($sql) as $row) {
|
|
$csv_output->addLine(array($row['codeclient_from_results'], $row['codeclient_fixed']));
|
|
}
|
|
|
|
$csv_output->save();
|
|
}
|
|
|
|
static function importFromCsvFile($file_path)
|
|
{
|
|
$imported_codes = array();
|
|
|
|
$reader = new CSVReader($file_path);
|
|
$ok = $reader
|
|
->setExpectedHeader(self::$header)
|
|
->setOnEachLine(function(ArrayExtended $cols) use (&$imported_codes) {
|
|
$wrong_code = strtolower(trim((string)$cols[self::CODE_CLIENT_WRONG]));
|
|
$right_code = strtolower(trim((string)$cols[self::CODE_CLIENT_RIGHT]));
|
|
|
|
if (empty($wrong_code)) {
|
|
$wrong_code = self::CODE_EMPTY;
|
|
}
|
|
$imported_codes[$wrong_code] = $right_code;
|
|
return true;
|
|
})
|
|
->readLines();
|
|
|
|
if ($ok && count($imported_codes)>0) {
|
|
|
|
$existing_codes = array();
|
|
|
|
// load all already saved codes
|
|
foreach (Db::getInstance()->executeS('
|
|
SELECT `codeclient_from_results`, `codeclient_fixed`
|
|
FROM `'._DB_PREFIX_.'training_fixedcodeclient`
|
|
') as $row) {
|
|
$existing_codes[trim($row['codeclient_from_results'])] = trim($row['codeclient_fixed']);
|
|
}
|
|
|
|
// insert new wrong codes with their correction
|
|
$values = array();
|
|
foreach(array_diff_key($imported_codes, $existing_codes) as $wrong_code => $right_code) {
|
|
$values[] = '(\''.pSql($wrong_code).'\', \''.pSql($right_code).'\')';
|
|
}
|
|
|
|
if (count($values)>0) {
|
|
$ok = Db::getInstance()->execute('
|
|
INSERT INTO `'._DB_PREFIX_.'training_fixedcodeclient`
|
|
(`codeclient_from_results`, `codeclient_fixed`)
|
|
VALUES
|
|
'.implode(',', $values).'
|
|
');
|
|
}
|
|
|
|
// replace existing uncorrected code with their correction
|
|
foreach(array_intersect_key($imported_codes, $existing_codes) as $wrong_code => $right_code) {
|
|
$ok = $ok && Db::getInstance()->execute('
|
|
UPDATE `'._DB_PREFIX_.'training_fixedcodeclient`
|
|
SET `codeclient_fixed` = \''.pSql($right_code).'\'
|
|
WHERE `codeclient_from_results` = \''.pSql($wrong_code).'\'
|
|
');
|
|
}
|
|
}
|
|
unset($imported_codes);
|
|
|
|
return $ok;
|
|
}
|
|
|
|
static function loadOnlyFixed()
|
|
{
|
|
$result = new ArrayExtended();
|
|
|
|
$sql = 'SELECT `codeclient_from_results`, `codeclient_fixed`
|
|
FROM `'._DB_PREFIX_.'training_fixedcodeclient`
|
|
WHERE TRIM(`codeclient_fixed`) <> \'\'';
|
|
foreach (Db::getInstance()->executeS($sql) as $row) {
|
|
$result[trim($row['codeclient_from_results'])] = trim($row['codeclient_fixed']);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
static function addIfNotExist(array $pharmacies_not_found)
|
|
{
|
|
// get already saved codes to correct
|
|
$existing_codes = array();
|
|
|
|
$sql = 'SELECT LOWER(`codeclient_from_results`) as `codeclient_from_results`
|
|
FROM `'._DB_PREFIX_.'training_fixedcodeclient`';
|
|
foreach (Db::getInstance()->executeS($sql) as $row) {
|
|
$existing_codes[] = trim($row['codeclient_from_results']);
|
|
}
|
|
|
|
// remove duplicate keys
|
|
$unique_pharmacies_not_found = array();
|
|
foreach ($pharmacies_not_found as $key => $val) {
|
|
$key = strtolower(trim($key));
|
|
if (!isset($unique_pharmacies_not_found[$key])) {
|
|
$unique_pharmacies_not_found[$key] = $val;
|
|
}
|
|
}
|
|
|
|
$unique_pharmacies_not_found = array_keys($unique_pharmacies_not_found);
|
|
|
|
// filter new codes not already present in DB (and create the SQL value string)
|
|
$values = array();
|
|
foreach(array_diff($unique_pharmacies_not_found, $existing_codes) as $code_client) {
|
|
$values[] = '\''.pSql(trim((string)$code_client)).'\', \'\'';
|
|
}
|
|
|
|
// if any new codes, add them
|
|
if (count($values)>0) {
|
|
Db::getInstance()->execute(
|
|
'INSERT INTO `'._DB_PREFIX_.'training_fixedcodeclient`
|
|
(codeclient_from_results, codeclient_fixed)
|
|
VALUES
|
|
('.implode('),(', $values).')'
|
|
);
|
|
}
|
|
}
|
|
} |