Files
2024-10-25 14:16:28 +02:00

107 lines
3.1 KiB
PHP

<?php
/**
* NOTICE OF LICENSE
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* You can not resell and redistribute this file.
*
* @author Dalibor Stojcevski <dal_sto@yahoo.com>
* @copyright 2019 Dalibor Stojcevski
* @license Dalibor Stojcevski
*/
class Import_ApiAjaxModuleFrontController extends ModuleFrontController
{
/**
* Assign template vars related to page content
* @see FrontController::initContent()
*/
private $fields = array();
public function initContent()
{
$response = $this->find_fields();
$json = Tools::jsonEncode($response);
$this->ajaxDie($json);
}
public function find_fields()
{
Db::getInstance()->execute("SET session wait_timeout=28800", FALSE);
Db::getInstance()->execute("SET session net_read_timeout=28800", FALSE);
Db::getInstance()->execute("SET session interactive_timeout=28800", FALSE);
$file_id = Tools::getValue('file_id');
$shop = 'default';
$json['error'] = '';
$php_array = array();
$query_file = Db::getInstance()->executeS("SELECT * FROM ". _DB_PREFIX_ . "ia_files WHERE file_id = '" . (int)$file_id . "' AND shop = '" . pSQL($shop) . "' LIMIT 1");
//$type = Tools::getValue('type');
//$link = html_entity_decode($this->request->post['import_api_link'], ENT_QUOTES, "UTF-8");
if ($query_file) {
require_once(_PS_MODULE_DIR_ . 'import_api/classes/filereader.php');
$FileReader = new FileReader();
$file = $query_file[0];
//$file['link'] = html_entity_decode($file['link'], ENT_QUOTES, "UTF-8");
list($php_array, $json['error']) = $FileReader->getArrayFromLink($file);
if($php_array === null){
$json['error'] = 'File is not in selected format';
}
} else {
$json['error'] = 'File not found';
}
if (!$json['error']){
if (count($php_array) > 10 && !empty(array_key_first($php_array))) { // if is not 0
$new_array = array();
foreach ($php_array as $key => $value ) {
$value['GENERATED_UNIQUE'] = $key;
$new_array[] = $value;
}
$php_array = $new_array;
unset($new_array);
}
$this->search_keys($php_array, 'FEED');
$json['field'] = $this->fields;
$json['fields'] = implode('##', $this->fields);
//$json['mapping'] = implode('##', $this->fields);
if ($this->fields) {
Db::getInstance()->execute("UPDATE ". _DB_PREFIX_ . "ia_files SET fields = '" . pSQL($json['fields']) . "', date_edited = '" . time() . "' WHERE file_id = " . (int)$file_id ." AND shop = '" . pSQL($shop) . "'");
}
}
return $json;
}
function search_keys($array, $parent){
$total = count($array);
foreach($array as $key => $value){
if(!is_array($value)){
if (is_int($key)) {
$path = $parent;
} else {
$path = $parent .'->'. $key;
}
$path = str_replace('->array()', '',$path);
if (!in_array($path, $this->fields))
$this->fields[] = $path;
} else {
if (is_int($key)) {
$key = "array()";
}
$this->search_keys($value, $parent .'->'. $key);
}
}
}
}