garancia/modules/training/models/TrainingPharmacy.php
2017-03-20 09:38:44 +01:00

81 lines
2.3 KiB
PHP

<?php
require_once(__DIR__.'/CSVReader.php');
class TrainingPharmacy extends ObjectModel
{
const CODE_CLIENT = 1;
public $id_training_pharmacy;
public $codeclient;
public static $definition = array(
'table' => 'training_pharmacy',
'primary' => 'id_training_pharmacy',
'multilang' => false,
'fields' => array(
'id_training_pharmacy' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'codeclient' => array('type' => self::TYPE_STRING, 'lang' => false, 'validate' => 'isString'),
),
);
private static $failed_line_number = 0;
static function importFromCsvFile($file_path)
{
$values = array();
$reader = new CSVReader($file_path);
$ok = $reader
->enableSkipHeader()
->setExpectedHeader(array(
'CodeClt'=>self::CODE_CLIENT
))
->setOnStart(function() {
self::deleteAll();
})
->setOnEachLine(function(ArrayExtended $cols) use (&$values) {
$values[] = '\''.pSql((string)$cols[self::CODE_CLIENT]).'\'';
return true;
})
->setOnError(function($failed_line_number) {
self::$failed_line_number = $failed_line_number;
self::deleteAll();
})
->readLines();
if ($ok && count($values)>0) {
$ok = Db::getInstance()->execute('
INSERT INTO `'._DB_PREFIX_.'training_pharmacy`
(`codeclient`)
VALUES
('.implode('),(', $values).')
');
}
unset($values);
return $ok;
}
static function deleteAll()
{
Db::getInstance()->execute('TRUNCATE `'._DB_PREFIX_.'training_pharmacy`');
}
static function hasAnyRow()
{
return Db::getInstance()->getValue('SELECT COUNT(*) FROM `'._DB_PREFIX_.'training_pharmacy`') > 0;
}
static function loadAll()
{
$result = new ArrayExtended();
$sql = 'SELECT `codeclient`, `id_training_pharmacy`
FROM `'._DB_PREFIX_.'training_pharmacy`';
foreach (Db::getInstance()->executeS($sql) as $row) {
$result[$row['codeclient']] = $row['id_training_pharmacy'];
}
return $result;
}
}