first commit
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* SOTESHOP/stImportExportPlugin
|
||||
*
|
||||
* Ten plik należy do aplikacji stImportExportPlugin opartej na licencji (Open License SOTE) Otwarta Licencja SOTE.
|
||||
* Nie zmieniaj tego pliku, jeśli chcesz korzystać z automatycznych aktualizacji oprogramowania.
|
||||
* Jeśli chcesz wprowadzać swoje modyfikacje do programu, zapoznaj się z dokumentacją, jak zmieniać
|
||||
* oprogramowanie bez zmiany kodu bazowego http://www.sote.pl/modifications
|
||||
*
|
||||
* @package stImportExportPlugin
|
||||
* @subpackage libs
|
||||
* @copyright SOTE (www.sote.pl)
|
||||
* @license http://www.sote.pl/license/open (Open License SOTE) Otwarta Licencja SOTE
|
||||
* @version $Id: stExporterCsv1250.class.php 13384 2011-06-02 11:30:57Z piotr $
|
||||
*/
|
||||
|
||||
class stExporterCsv1250 extends stPropelExporter
|
||||
{
|
||||
|
||||
/**
|
||||
* Separator komorek w pliku csv
|
||||
* @var string
|
||||
*/
|
||||
var $delimiter = ";";
|
||||
|
||||
/**
|
||||
* Znak ucieczki w pliku csv
|
||||
* @var unknown_type
|
||||
*/
|
||||
var $enclosure = "\"";
|
||||
|
||||
/**
|
||||
* Unikatowa nazwa exportera
|
||||
* @var string
|
||||
*/
|
||||
var $converter = 'csv';
|
||||
|
||||
/**
|
||||
* Rozszerzenie pliku eksportu
|
||||
* @var string
|
||||
*/
|
||||
var $output_file_extension = 'csv';
|
||||
|
||||
/**
|
||||
* Zapisuje pnaglowek do pliku csv
|
||||
*/
|
||||
protected function writeHeader()
|
||||
{
|
||||
|
||||
// tablica naglowkow
|
||||
$cols = array();
|
||||
|
||||
// dla kazdego elementu obiektu
|
||||
foreach ($this->config['fields'] as $field => $func_name) {
|
||||
// przypisz nazwe
|
||||
$cols[] = iconv('UTF-8', 'windows-1250', $this->getUserName($field));
|
||||
}
|
||||
// otwrzo plik eksportu
|
||||
$this->fh = fopen($this->getFilePath(), "w");
|
||||
|
||||
// zapisz dane naglwoka
|
||||
$this->fputcsv2($this->fh, $cols, $this->delimiter, $this->enclosure);
|
||||
}
|
||||
|
||||
/**
|
||||
* Przeladowanie standardowej funkcji getConvertedData
|
||||
*
|
||||
* @param object $data
|
||||
* @return array
|
||||
*/
|
||||
protected function getConvertedData($data = null)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zapisuje dane do pliku csv
|
||||
*
|
||||
* @param object $data
|
||||
*/
|
||||
protected function writeConvertedData($data = null)
|
||||
{
|
||||
$i18n = sfContext::getInstance()->getI18n();
|
||||
// jezeli plik nie zostal otwarty, otwiera go i ustawia wskaznik na koniec pliku
|
||||
if (!$this->fh) {
|
||||
$this->fh = fopen($this->getFilePath(), "a");
|
||||
}
|
||||
|
||||
// jezeli dane wejsciowe sa tablica wykonaj porcedure zapisu
|
||||
if (is_array($data)) {
|
||||
|
||||
//dla kazdego elementu tablicy wykonaj zapis wiersza
|
||||
foreach ($data as $object) {
|
||||
$export = true;
|
||||
|
||||
//tworzy tablice do zapisania
|
||||
$row = array();
|
||||
foreach ($object as $key => $value) {
|
||||
$field = $this->getUserName($key);
|
||||
|
||||
if (strlen((string) $value) > 32000) {
|
||||
$this->logger->add($object[$this->config['primary_key']], $i18n->__(self::FIELD_EXCEEDS_32K_MSG, array('%field%' => $field), 'stImportExportBackend')
|
||||
. ' ' . $i18n->__(self::FIELD_NA_REPLAGE_MSG, array('%field%' => $field), 'stImportExportBackend')
|
||||
);
|
||||
$row[] = '[N/A]';
|
||||
} else {
|
||||
|
||||
$valueTranslit = iconv('UTF-8', 'CP1250//IGNORE', $value);
|
||||
|
||||
if ($value != iconv('CP1250', 'UTF-8', $valueTranslit)) {
|
||||
|
||||
$this->logger->add($object[$this->config['primary_key']], $i18n->__(self::FIELD_INCORRECT_ENCODING, array('%field%' => $field, '%encoding%' => 'Windows-1250'), 'stImportExportBackend')
|
||||
. ' ' .$i18n->__(self::FIELD_NA_REPLAGE_MSG, array('%field%' => $field), 'stImportExportBackend')
|
||||
);
|
||||
$row[] = '[N/A]';
|
||||
} else {
|
||||
$row[] = $valueTranslit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->fputcsv2($this->fh, $row, $this->delimiter, $this->enclosure);
|
||||
}
|
||||
}
|
||||
|
||||
//zamyka plik
|
||||
fclose($this->fh);
|
||||
}
|
||||
|
||||
public function sampleFile($data = array())
|
||||
{
|
||||
if (!$this->fh) {
|
||||
$this->fh = fopen($this->getFilePath(), "a");
|
||||
}
|
||||
$this->writeHeader();
|
||||
$this->writeConvertedData($data);
|
||||
|
||||
return file_get_contents($this->getFilePath());
|
||||
}
|
||||
|
||||
public function fputcsv2($fh, array $fields, $delimiter = ',', $enclosure = '"')
|
||||
{
|
||||
fputcsv($fh, $fields, $delimiter, $enclosure);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
/**
|
||||
* SOTESHOP/stImportExportPlugin
|
||||
*
|
||||
* Ten plik należy do aplikacji stImportExportPlugin opartej na licencji (Open License SOTE) Otwarta Licencja SOTE.
|
||||
* Nie zmieniaj tego pliku, jeśli chcesz korzystać z automatycznych aktualizacji oprogramowania.
|
||||
* Jeśli chcesz wprowadzać swoje modyfikacje do programu, zapoznaj się z dokumentacją, jak zmieniać
|
||||
* oprogramowanie bez zmiany kodu bazowego http://www.sote.pl/modifications
|
||||
*
|
||||
* @package stImportExportPlugin
|
||||
* @subpackage libs
|
||||
* @copyright SOTE (www.sote.pl)
|
||||
* @license http://www.sote.pl/license/open (Open License SOTE) Otwarta Licencja SOTE
|
||||
* @version $Id: stImportExport2csv1250Listener.class.php 13384 2011-06-02 11:30:57Z piotr $
|
||||
* @author Piotr Halas <piotr.halas@sote.pl>
|
||||
*/
|
||||
|
||||
/**
|
||||
* Klasa słuchacza stImportExport2csv
|
||||
*
|
||||
* @package stImportExportPlugin
|
||||
* @subpackage libs
|
||||
*/
|
||||
class stImportExport2csv1250Listener {
|
||||
public static function generate(sfEvent $event)
|
||||
{
|
||||
// możemy wywoływać podaną metodę wielokrotnie co powoduje dołączenie kolejnych plików
|
||||
$event->getSubject()->attachAdminGeneratorFile('stImportExportPlugin', 'stImportExport2csv1250.yml');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* SOTESHOP/stImportExportPlugin
|
||||
*
|
||||
* Ten plik należy do aplikacji stImportExportPlugin opartej na licencji (Open License SOTE) Otwarta Licencja SOTE.
|
||||
* Nie zmieniaj tego pliku, jeśli chcesz korzystać z automatycznych aktualizacji oprogramowania.
|
||||
* Jeśli chcesz wprowadzać swoje modyfikacje do programu, zapoznaj się z dokumentacją, jak zmieniać
|
||||
* oprogramowanie bez zmiany kodu bazowego http://www.sote.pl/modifications
|
||||
*
|
||||
* @package stImportExportPlugin
|
||||
* @subpackage libs
|
||||
* @copyright SOTE (www.sote.pl)
|
||||
* @license http://www.sote.pl/license/open (Open License SOTE) Otwarta Licencja SOTE
|
||||
* @version $Id: stImporterCsv1250.class.php 13384 2011-06-02 11:30:57Z piotr $
|
||||
*/
|
||||
|
||||
class stImporterCsv1250 extends stPropelImporter
|
||||
{
|
||||
|
||||
var $converter = 'csv';
|
||||
|
||||
/**
|
||||
* Separator komorek w pliku csv
|
||||
* @var string
|
||||
*/
|
||||
var $delimiter = ";";
|
||||
|
||||
/**
|
||||
* Rozszerzenie pliku eksportu
|
||||
* @var string
|
||||
*/
|
||||
var $input_file_extension = 'csv';
|
||||
|
||||
public function getDataCount()
|
||||
{
|
||||
if (is_readable($this->getFilePath()))
|
||||
{
|
||||
return filesize($this->getFilePath());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function loadFile()
|
||||
{
|
||||
|
||||
if (is_readable($this->getFilePath()))
|
||||
{
|
||||
$this->fh = fopen($this->getFilePath(), "r");
|
||||
}
|
||||
}
|
||||
|
||||
public function readHeaderRow()
|
||||
{
|
||||
if ($this->fh)
|
||||
{
|
||||
$this->header_order = $this->removeUserName($this->fgetcsv());
|
||||
}
|
||||
}
|
||||
|
||||
public function skipToData($offset = 0)
|
||||
{
|
||||
if ($offset)
|
||||
{
|
||||
fseek($this->fh, $offset);
|
||||
}
|
||||
}
|
||||
|
||||
protected function readRow()
|
||||
{
|
||||
if ($this->fh)
|
||||
{
|
||||
$row = $this->fgetcsv();
|
||||
|
||||
if ($row)
|
||||
{
|
||||
$row = array_slice($row, 0, count($this->header_order), true);
|
||||
|
||||
if (!$this->loadPrimaryKey(array_combine($this->header_order, $row)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
$i18n = sfContext::getInstance()->getI18n();
|
||||
$wrongEncoding = false;
|
||||
$data = array();
|
||||
|
||||
foreach ($this->header_order as $key => $name)
|
||||
{
|
||||
if (isset($row[$key]) && trim($row[$key]) != "[N/A]")
|
||||
{
|
||||
if (!$this->hasValidEncoding($row[$key]))
|
||||
{
|
||||
$wrongEncoding = true;
|
||||
$this->logger->add(
|
||||
null,
|
||||
$i18n->__('Niepoprawne kodowanie dla pola %field%', array(
|
||||
'%field%' => $this->getUserName($name)
|
||||
)),
|
||||
stImportExportLog::$FATAL
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
$data[$name] = iconv('WINDOWS-1250', 'UTF-8', $row[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$wrongEncoding)
|
||||
{
|
||||
$this->processData($data);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public function hasValidEncoding($value)
|
||||
{
|
||||
return iconv("WINDOWS-1250", "WINDOWS-1250//IGNORE", $value) == $value && (!preg_match('/[\xA5\xC6\xCA\xA3\xD1\xD3\x8C\x8F\xAF\xB9\xE6\xEA\xB3\xF1\xF3\x9C\x9F\xBF]/', $value) || iconv("UTF-8", "UTF-8//IGNORE", $value) != $value);
|
||||
}
|
||||
|
||||
public function getOffset()
|
||||
{
|
||||
return ftell($this->fh);
|
||||
}
|
||||
|
||||
public function validateFile(array &$errors = null)
|
||||
{
|
||||
$errors = array();
|
||||
|
||||
if (is_readable($this->getFilePath()))
|
||||
{
|
||||
$this->fh = fopen($this->getFilePath(), "r");
|
||||
if ($this->fh)
|
||||
{
|
||||
$header = $this->fgetcsv();
|
||||
|
||||
foreach ($header as $name)
|
||||
{
|
||||
if (!$this->hasValidEncoding($name))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$this->header_order = $this->removeUserName($header);
|
||||
|
||||
$primaryKeys = is_array($this->config['primary_key']) ? $this->config['primary_key'] : array($this->config['primary_key']);
|
||||
$requiredFields = array();
|
||||
|
||||
foreach ($this->requireFields as $name)
|
||||
{
|
||||
$params = $this->config['fields'][$name];
|
||||
|
||||
if (!isset($params['require_with_fields']) || !empty(array_intersect($params['require_with_fields'], $this->header_order)))
|
||||
{
|
||||
$requiredFields[] = $name;
|
||||
}
|
||||
}
|
||||
|
||||
$requiredFields = array_merge($primaryKeys, $requiredFields);
|
||||
$missingFields = array_diff($requiredFields, $this->header_order);
|
||||
|
||||
if (empty($missingFields))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
$fields = array();
|
||||
|
||||
foreach ($missingFields as $name)
|
||||
{
|
||||
$fields[] = $this->getUserName($name);
|
||||
}
|
||||
|
||||
$errors[] = sfContext::getInstance()->getI18n()->__('Brak wymaganych pól: "%%fields%%"', array(
|
||||
'%%fields%%' => implode('", "', $fields),
|
||||
), 'stImportExportBackend');
|
||||
}
|
||||
|
||||
fclose($this->fh);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function sampleFile($data = array())
|
||||
{
|
||||
|
||||
$exporter = new stExporterCsv($this->model, $this->config, $this->getFilePath());
|
||||
|
||||
return $exporter->sampleFile($data);;
|
||||
}
|
||||
|
||||
public function fgetcsv()
|
||||
{
|
||||
return fgetcsv($this->fh, null, ";", '"', "\0");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user