280 lines
8.9 KiB
PHP
280 lines
8.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* File from http://PrestaShow.pl
|
|
*
|
|
* DISCLAIMER
|
|
* Do not edit or add to this file if you wish to upgrade this module to newer
|
|
* versions in the future.
|
|
*
|
|
* @authors PrestaShow.pl <kontakt@prestashow.pl>
|
|
* @copyright 2015 PrestaShow.pl
|
|
* @license http://PrestaShow.pl/license
|
|
*/
|
|
class PShow_Config
|
|
{
|
|
|
|
public static $currentFieldOptions = array();
|
|
private static $cache_file_config = array();
|
|
private static $cache_option_value = array();
|
|
|
|
/**
|
|
*
|
|
* @param string $filename
|
|
* @param array $new_data
|
|
*/
|
|
public static function saveFileConfig($filename, array $new_data)
|
|
{
|
|
if (!is_dir(_IMPORT_CONFIG_PATH_ . $filename)) {
|
|
mkdir(_IMPORT_CONFIG_PATH_ . $filename, 0777, true);
|
|
}
|
|
|
|
$filepath = _IMPORT_CONFIG_PATH_ . $filename . '/config.yml';
|
|
|
|
$yml_data = self::getFileConfig($filename);
|
|
|
|
$data = array_replace_recursive($yml_data, $new_data);
|
|
|
|
$toReplace = array("primary", "skipped_manufacturers", "matched");
|
|
|
|
foreach ($toReplace as $key) {
|
|
if (array_key_exists($key, $new_data)) {
|
|
$data[$key] = $new_data[$key];
|
|
}
|
|
}
|
|
|
|
if (isset($new_data['additional']) && isset($new_data['additional'])) {
|
|
foreach ($new_data['additional'] as $key => $arr) {
|
|
$data['additional'][$key] = $arr;
|
|
}
|
|
unset($data['additional']['replacement']['temp']);
|
|
}
|
|
|
|
PShow_Yaml::emitToFile($filepath, $data);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param string $filename
|
|
* @return array
|
|
*/
|
|
public static function getFileConfig($filename, $refresh = false)
|
|
{
|
|
if (!$refresh || !array_key_exists($filename, self::$cache_file_config)) {
|
|
|
|
$filepath = _IMPORT_CONFIG_PATH_ . $filename . '/config.yml';
|
|
|
|
if (!file_exists($filepath)) {
|
|
return array();
|
|
}
|
|
|
|
$file_contents = file_get_contents($filepath);
|
|
|
|
if (empty($file_contents)) {
|
|
return array();
|
|
}
|
|
|
|
|
|
self::$cache_file_config[$filename] = PShow_Yaml::parseFile($filepath);
|
|
}
|
|
|
|
return self::$cache_file_config[$filename];
|
|
}
|
|
|
|
/**
|
|
* Get file field configuration by prestashop field
|
|
*
|
|
* @param string|bool $filename
|
|
* @param array $prestafields
|
|
* @return array
|
|
*/
|
|
public static function getFileConfigByMatchedPrestaFields($filename, array $prestafields)
|
|
{
|
|
if ($filename === true) {
|
|
$filename = PShow_Import::getInstance()->filename;
|
|
}
|
|
|
|
$config = self::getFileConfig($filename);
|
|
$results = array();
|
|
|
|
foreach ($config['matched'] as $matchedtype) {
|
|
if (!is_array($matchedtype)) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($matchedtype as $field) {
|
|
if (in_array($field['value'], $prestafields)) {
|
|
array_push($results, $field);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
/**
|
|
* Get configuration availe bo config in matchings
|
|
*
|
|
* @param string $field_
|
|
* @return array
|
|
*/
|
|
public static function getAvailableFieldConfig($field_)
|
|
{
|
|
list($type, $field) = explode('.', $field_);
|
|
|
|
$cachekey = 'pshowimporter_getAvailableFieldConfig_' . $type . '_' . Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT');
|
|
if (_PS_CACHE_ENABLED_ && Cache::getInstance()->exists($cachekey)) {
|
|
$data = Cache::getInstance()->get($cachekey);
|
|
|
|
return $data[$field];
|
|
}
|
|
|
|
// at first get global options
|
|
|
|
$filepath = _IMPORT_OPTIONS_PATH_ . 'en_' . $type . '.yml';
|
|
|
|
if (!file_exists($filepath)) {
|
|
return array();
|
|
}
|
|
|
|
$file_contents = file_get_contents($filepath);
|
|
|
|
if (empty($file_contents)) {
|
|
return array();
|
|
}
|
|
|
|
$data = PShow_Yaml::parseFile($filepath);
|
|
|
|
// ASM addon
|
|
if (!PShow_Addon::getInstance('ASM')) {
|
|
foreach ($data[$field] as $k => $v) {
|
|
if (stripos($k, 'asm_') === 0) {
|
|
unset($data[$field][$k]);
|
|
}
|
|
}
|
|
} elseif (!PShow_Addon::getInstance('ASM')->isEnabled()) {
|
|
foreach ($data[$field] as $k => $v) {
|
|
if (stripos($k, 'asm_') === 0) {
|
|
$data[$field][$k]['disabled'] = true;
|
|
unset($data[$field][$k]['required']);
|
|
}
|
|
}
|
|
}
|
|
|
|
// at second get localized options
|
|
|
|
$filepath = _IMPORT_OPTIONS_PATH_ . Context::getContext()->language->iso_code . '_' . $type . '.yml';
|
|
if (file_exists($filepath)) {
|
|
// merge global and localized options
|
|
$file_contents = file_get_contents($filepath);
|
|
if (!empty($file_contents)) {
|
|
$data = array_replace_recursive($data, PShow_Yaml::parseFile($filepath));
|
|
}
|
|
}
|
|
|
|
if (!array_key_exists($field, $data)) {
|
|
return array();
|
|
}
|
|
|
|
if (_PS_CACHE_ENABLED_) {
|
|
Cache::getInstance()->set($cachekey, $data);
|
|
}
|
|
|
|
return $data[$field];
|
|
}
|
|
|
|
/**
|
|
* Check is option checked in matchings and primary config
|
|
*
|
|
* @param string $optionName
|
|
* @param boolean $forceSearch
|
|
* @return boolean
|
|
*/
|
|
public static function isChecked($optionName, $forceSearch = false)
|
|
{
|
|
if (isset(self::$currentFieldOptions[0]) && array_key_exists($optionName, self::$currentFieldOptions[0]['options'])) {
|
|
return (bool) (self::$currentFieldOptions[0]['options'][$optionName]);
|
|
}
|
|
|
|
if (array_key_exists($optionName, PShow_Import::getInstance()->config[0])) {
|
|
return (bool) (PShow_Import::getInstance()->config[0][$optionName]);
|
|
}
|
|
|
|
if ($forceSearch === true) {
|
|
$filename = PShow_Import::getInstance()->filename;
|
|
|
|
if (!array_key_exists($filename . '.' . $optionName, self::$cache_option_value)) {
|
|
$fileConfig = self::getFileConfig($filename);
|
|
if (isset($fileConfig['matched']) && is_array($fileConfig['matched'])) {
|
|
foreach ($fileConfig['matched'] as $typematched) {
|
|
if (is_array($typematched)) {
|
|
foreach ($typematched as $matched) {
|
|
if (is_array($matched['options']) && array_key_exists($optionName, $matched['options'])) {
|
|
self::$cache_option_value[$filename . '.' . $optionName] = (bool) $matched['options'][$optionName];
|
|
break;
|
|
}
|
|
}
|
|
if (array_key_exists($filename . '.' . $optionName, self::$cache_option_value)) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (array_key_exists($filename . '.' . $optionName, self::$cache_option_value)) {
|
|
return self::$cache_option_value[$filename . '.' . $optionName];
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Get option value from matchings and primary config
|
|
*
|
|
* @param string $optionName
|
|
* @param boolean $forceSearch
|
|
* @return mixed
|
|
*/
|
|
public static function get($optionName, $filterFunc = null, $forceSearch = false, $fieldNum = 0)
|
|
{
|
|
$result = false;
|
|
|
|
if (isset(self::$currentFieldOptions[$fieldNum]) && array_key_exists($optionName, self::$currentFieldOptions[$fieldNum]['options'])) {
|
|
$result = self::$currentFieldOptions[$fieldNum]['options'][$optionName];
|
|
}
|
|
|
|
if (array_key_exists($optionName, PShow_Import::getInstance()->config[0])) {
|
|
$result = PShow_Import::getInstance()->config[0][$optionName];
|
|
}
|
|
|
|
if (!$result && $forceSearch === true) {
|
|
$filename = PShow_Import::getInstance()->filename;
|
|
|
|
if (!array_key_exists($filename . '.' . $optionName, self::$cache_option_value)) {
|
|
$fileConfig = self::getFileConfig(PShow_Import::getInstance()->filename);
|
|
foreach ($fileConfig['matched'] as $typematched) {
|
|
foreach ($typematched as $matched) {
|
|
if (array_key_exists($optionName, $matched['options'])) {
|
|
self::$cache_option_value[$filename . '.' . $optionName] = $matched['options'][$optionName];
|
|
break;
|
|
}
|
|
}
|
|
if (array_key_exists($filename . '.' . $optionName, self::$cache_option_value)) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return self::$cache_option_value[$filename . '.' . $optionName];
|
|
}
|
|
|
|
if ($filterFunc !== null) {
|
|
return call_user_func($filterFunc, $result);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|