id_lang_default = (int)Configuration::get('PS_LANG_DEFAULT');
$this->module = Module::getInstanceByName(self::MOD_NAME);
$this->languages = Language::getLanguages(false);
$this->context = Context::getContext();
}
public static function getInstance()
{
if (!self::$_INSTANCE)
self::$_INSTANCE = new self();
return self::$_INSTANCE;
}
// Export data:
public function archiveThisFile($obj, $file, $server_path, $archive_path)
{
if ($obj instanceof ZipArchive) {
if (is_dir($server_path . $file)) {
$dir = scandir($server_path . $file);
foreach ($dir as $folder) {
if ($folder[0] != '.') {
$this->archiveThisFile($obj, $folder, $server_path . $file . '/', $archive_path . $file . '/');
}
}
} else {
$obj->addFile($server_path . $file, $archive_path . $file);
}
}
}
public function _count($table, $qa, $answer)
{
$dq = new DbQuery();
$dq
->select('COUNT(*)')
->from($table, 'a');
if (in_array($table, self::$table)) {
$dq
->where('a.question=' . (int)$qa);
}
switch ($table) {
case 'ets_rv_comment_usefulness':
case 'ets_rv_comment_report':
$dq
->leftJoin('ets_rv_comment', 'cm', 'cm.id_ets_rv_comment=a.id_ets_rv_comment')
->where('cm.answer=' . (int)$answer);
break;
case 'ets_rv_reply_comment_report':
case 'ets_rv_reply_comment_usefulness':
$dq
->leftJoin('ets_rv_reply_comment', 'rm', 'rm.id_ets_rv_reply_comment=a.id_ets_rv_reply_comment')
->leftJoin('ets_rv_comment', 'cm', 'cm.id_ets_rv_comment=rm.id_ets_rv_comment')
->where('cm.answer=' . (int)$answer)
->where('rm.id_ets_rv_reply_comment is NOT NULL AND cm.id_ets_rv_comment is NOT NULL');
break;
}
return Db::getInstance()->getValue($dq, false);
}
public function addFileXMl($def, $qa = 0, $answer = 0)
{
if (!($count = $this->_count($def['table'], $qa, $answer)))
return $this->emptyXML();
self::$export_tables[$def['table']] = $count;
return $this->exportData($def, $qa, $answer);
}
public function toSQL($table, $qa = 0, $answer = 0)
{
$qd = new DbQuery();
$qd
->select('a.*')
->from(pSQL($table), 'a');
$columns = Db::getInstance()->executeS('SHOW COLUMNS FROM `' . _DB_PREFIX_ . pSQL($table) . '`', true, false);
if ($columns) {
foreach ($columns as $column) {
$key = $column['Field'];
switch ($key) {
case 'id_customer':
$qd
->select('c.email `customer_email`')
->leftJoin('customer', 'c', 'c.id_customer = a.id_customer');
break;
case 'employee':
$qd
->select('e.email `employee_email`')
->leftJoin('employee', 'e', 'e.id_employee = a.employee');
break;
case 'question':
$qd
->where('a.question=' . (int)$qa);
break;
case 'answer':
$qd
->where('a.answer=' . (int)$answer);
break;
}
}
}
switch ($table) {
case 'ets_rv_comment_usefulness':
case 'ets_rv_comment_report':
$qd
->leftJoin('ets_rv_comment', 'cm', 'cm.id_ets_rv_comment=a.id_ets_rv_comment')
->where('cm.answer=' . (int)$answer);
break;
case 'ets_rv_reply_comment_report':
case 'ets_rv_reply_comment_usefulness':
$qd
->leftJoin('ets_rv_reply_comment', 'rm', 'rm.id_ets_rv_reply_comment=a.id_ets_rv_reply_comment')
->leftJoin('ets_rv_comment', 'cm', 'cm.id_ets_rv_comment=rm.id_ets_rv_comment')
->where('cm.answer=' . (int)$answer)
->where('rm.id_ets_rv_reply_comment is NOT NULL AND cm.id_ets_rv_comment is NOT NULL');
break;
}
return $qd;
}
public function exportData($def, $qa = 0, $answer = 0)
{
if (($rows = Db::getInstance()->executeS($this->toSQL($def['table'], $qa, $answer), true, false)) && !empty($def['multilang'])) {
foreach ($rows as &$raw) {
$this->exportLang($def['table'], $def['primary'], $raw);
}
}
$xml_output = '' . PHP_EOL;
$xml_output .= '' . PHP_EOL;
$fields = $def['fields'];
if ($rows) {
foreach ($rows as $item) {
$xml_output .= '<' . $def['table'] . '>' . PHP_EOL;
$xml_output .= '<' . $def['primary'] . '>' . $def['primary'] . '>' . PHP_EOL;
if ($fields)
$xml_output .= $this->exportFields($item, $fields, false, ['languages']);
if (!empty($item['languages'])) {
$this->exportFieldLang($item['languages'], $fields, [], $xml_output);
}
$xml_output .= '' . $def['table'] . '>' . PHP_EOL;
}
}
$xml_output .= '' . PHP_EOL;
// format data xml:
return $rows ? $this->sanitizeXML($xml_output) : $xml_output;
}
public function emptyXML()
{
return false;
}
public function addFileXMl14($table, $primary = '', $multiLang = false, $qa = 0, $answer = 0)
{
if (!($count = $this->_count($table, $qa, $answer)))
return $this->emptyXML();
self::$export_tables[$table] = $count;
return preg_match('/(publish_lang|origin_lang)$/', $table) ? $this->exportDataLang($table, $qa, $answer) : $this->exportData14($table, $primary, $multiLang, $qa, $answer);
}
public function geneInfoXML()
{
$xml = new SimpleXMLElement('');
$xml->addAttribute('export_time', date('l jS \of F Y h:i:s A'));
$xml->addAttribute('export_source', $this->context->link->getPageLink('index', Configuration::get('PS_SSL_ENABLED')));
$xml->addAttribute('module_version', $this->module->version);
if (self::$export_tables) {
foreach (self::$export_tables as $table => $count) {
$xml->addChild($table, $count);
}
}
return $xml->asXML();
}
public function exportLang($table, $primary, &$item)
{
$group_val = isset($item[$primary]) && $item[$primary] ? $item[$primary] : 0;
if ($group_val) {
$table .= (preg_match('/^[0-9a-z\_]+(_lang)$/i', $table) ? '' : '_lang');
$qd = new DbQuery();
$qd
->select('a.*, b.iso_code')
->from(pSQL($table), 'a')
->innerJoin('lang', 'b', 'a.id_lang = b.id_lang')
->where('a.' . pSQL($primary) . ' = ' . (int)$group_val);
$item['languages'] = Db::getInstance()->executeS($qd, true, false);
}
}
public function exportData14($table, $primary = '', $multiLang = false, $qa = 0, $answer = 0)
{
if (($rows = Db::getInstance()->executeS($this->toSQL($table, $qa, $answer), true, false)) && $multiLang && $primary) {
foreach ($rows as &$raw)
$this->exportLang($table, $primary, $raw);
}
$xml_output = '' . PHP_EOL;
$xml_output .= '' . PHP_EOL;
if ($rows) {
foreach ($rows as $item) {
$xml_output .= '<' . $table . '>' . PHP_EOL;
$xml_output .= $this->exportFields($item, [], false, ['languages']);
if (!empty($item['languages']))
$this->exportFieldLang($item['languages'], [], $primary, $xml_output);
$xml_output .= '' . $table . '>' . PHP_EOL;
}
}
$xml_output .= '' . PHP_EOL;
return $rows ? $this->sanitizeXML($xml_output) : $xml_output;
}
public function exportDataLang($table, $qa, $answer)
{
$qd = new DbQuery();
$qd
->select('a.*, l.iso_code')
->from($table, 'a')
->leftJoin('lang', 'l', 'a.id_lang=l.id_lang');
switch ($table) {
case 'ets_rv_comment_origin_lang':
$qd
->leftJoin('ets_rv_comment', 'cm', 'cm.id_ets_rv_comment = a.id_ets_rv_comment')
->where('cm.question = ' . (int)$qa)
->where('cm.answer = ' . (int)$answer)
->where('cm.id_ets_rv_comment is NOT NULL');
break;
case 'ets_rv_reply_comment_origin_lang':
$qd
->leftJoin('ets_rv_reply_comment', 'rm', 'rm.id_ets_rv_reply_comment = a.id_ets_rv_reply_comment')
->leftJoin('ets_rv_comment', 'cm', 'cm.id_ets_rv_comment = rm.id_ets_rv_comment')
->where('rm.question = ' . (int)$qa)
->where('cm.answer = ' . (int)$answer)
->where('cm.id_ets_rv_comment is NOT NULL AND rm.id_ets_rv_reply_comment is NOT NULL');
break;
}
$rows = Db::getInstance()->executeS($qd, true, false);
$xml_output = '' . PHP_EOL;
$xml_output .= '' . PHP_EOL;
if ($rows) {
foreach ($rows as $item) {
$xml_output .= '<' . $table . '>' . PHP_EOL;
$xml_output .= $this->exportFields($item, [], false, ['id_lang']);
$xml_output .= '' . $table . '>' . PHP_EOL;
}
}
$xml_output .= '' . PHP_EOL;
return $rows ? $this->sanitizeXML($xml_output) : $xml_output;
}
public function exportFieldLang($data, $fields, $primary, &$xml_output)
{
if ($data) {
if (!$fields) {
foreach ($data as $item) {
$xml_output .= 'id_lang_default ? ' default="1"' : '') . '>' . PHP_EOL;
$xml_output .= $this->exportFields($item, [], false, ['iso_code', $primary, 'id_lang'], true);
$xml_output .= '' . PHP_EOL;
}
} else {
foreach ($data as $item) {
$xml_output .= 'id_lang_default ? ' default="1"' : '') . ' >' . PHP_EOL;
$xml_output .= $this->exportFields($item, $fields, true, [], true);
if (isset($item['id_shop'])) {
$xml_output .= '' . PHP_EOL;
}
$xml_output .= '' . PHP_EOL;
}
}
}
}
public function exportFields($data, $fields = [], $fieldLang = false, $ignore = [], $strip_tags = false)
{
$xml_output = null;
if (!$data)
return $xml_output;
if (!$fields) {
foreach ($data as $key => $val) {
if (!in_array($key, $ignore))
$xml_output .= '<' . $key . '>stripTags($val) : $val) . ']]>' . $key . '>' . PHP_EOL;
}
} else {
foreach ($fields as $prop => $field) {
if ($fieldLang && !empty($field['lang']) || !$fieldLang && empty($field['lang'])) {
$xml_output .= '<' . $prop . '>stripTags($data[$prop]) : $data[$prop]) : '') . ']]>' . $prop . '>' . PHP_EOL;
}
if (!$fieldLang) {
switch ($prop) {
case 'id_customer':
$xml_output .= '' . PHP_EOL;
break;
case 'employee':
$xml_output .= '' . PHP_EOL;
break;
}
}
}
}
return $xml_output;
}
// End:
// Export configs:
public function exportConfig($key, $config, &$xml_output)
{
$id_lang_default = Configuration::get('PS_LANG_DEFAULT');
$languages = Language::getLanguages(false);
if (isset($config['lang']) && $config['lang'] && $languages) {
$xml_output .= '<' . $key . '>' . PHP_EOL;
foreach ($languages as $l) {
$xml_output .= '';
$xml_output .= '';
$xml_output .= '' . PHP_EOL;
}
$xml_output .= '' . $key . '>' . PHP_EOL;
} else
$xml_output .= '<' . $key . '>' . $key . '>' . PHP_EOL;
}
public function geneConfigXML()
{
if (!($configs = EtsRVDefines::getInstance()->getConfigs()))
return false;
$xml_output = '' . PHP_EOL;
$xml_output .= '' . PHP_EOL;
$ik = 0;
foreach ($configs as $config) {
if (isset($config['tab']) && $config['tab'] != 'import_export' && isset($config['name'])) {
$this->exportConfig($config['name'], $config, $xml_output);
$ik++;
}
}
$xml_output .= '';
self::$export_tables['configuration'] = $ik;
return $xml_output;
}
// End:
// Common:
public function stripTags($string)
{
$string = preg_replace('/