Files
interblue.pl/modules/pshowimporter/classes/PShow_Import_AttributeGroup.php
2024-10-25 14:16:28 +02:00

207 lines
6.4 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_Import_AttributeGroup extends PShow_Import_Object_Abstract
{
/**
* getNewObject()
*
* get new or existing object
*/
public function getNewObject($id = null)
{
$obj = parent::getNewObject($id);
if (!$obj) {
return false;
}
$obj->attribute_group_type = 'select';
return $obj;
}
public function exists()
{
if (array_key_exists('attribute_group_name', $this->data)) {
$key = pSQL($this->data['attribute_group_name'][0]);
if (!empty($key)) {
$rows = Db::getInstance()->executeS('
SELECT p.`id_attribute_group`
FROM `' . _DB_PREFIX_ . 'attribute_group_lang` p
WHERE p.name = \'' . $key . '\'');
$objects = array();
foreach ($rows as $row) {
$objects[] = new AttributeGroup((int) $row['id_attribute_group'], null, PShow_Import::getInstance()->id_shop);
}
if (count($objects) > 0) {
return $objects;
}
}
}
return false;
}
protected function _import($key, &$_value)
{
$classname = $this->getObjectName();
$thisObject = &PShow_Import::$objects[$classname];
if (!parent::_import($key, $_value)) {
return false;
}
$value = $_value;
switch ($key) {
case 'attribute_values_separated':
if (is_array($value))
$value = reset($value);
$values = explode(PShow_Import::getInstance()->config[0]['attribute_separator'], $value);
foreach ($values as $value) {
$this->_import('attribute_value', $value);
}
break;
case 'attribute_color':
// imported in 'attribute_value' case
break;
case 'attribute_texture':
// imported in 'attribute_value' case
break;
case 'attribute_value':
if (is_array($value))
$value = reset($value);
if (!is_string($value) || empty($value)) {
return;
}
$q = "SELECT al.`id_attribute` "
. "FROM `" . _DB_PREFIX_ . "attribute_group_lang` agl "
. "JOIN `" . _DB_PREFIX_ . "attribute` a "
. "ON (a.`id_attribute_group` = agl.`id_attribute_group`) "
. "JOIN `" . _DB_PREFIX_ . "attribute_lang` al "
. "ON (al.`id_attribute` = a.`id_attribute`) "
. "WHERE agl.`id_attribute_group` = " . (int) PShow_Import::$objects[$classname]->id . " "
. "AND al.`name` = '" . pSQL($value) . "' "
. "LIMIT 1";
$rows = Db::getInstance()->executeS($q);
if (is_array($rows) && count($rows) > 0) {
return;
}
$attr = new Attribute(null, null, PShow_Import::getInstance()->id_shop);
$attr->id_attribute_group = (int) PShow_Import::$objects[$classname]->id;
$this->prepareLangField($attr->name, $value);
if (array_key_exists('attribute_color', $this->data)) {
$color = $this->data['attribute_color'];
if (Validate::isColor($color)) {
$attr->color = $color;
}
}
if (array_key_exists('attribute_texture', $this->data)) {
$texture = $this->data['attribute_texture'];
if (filter_var($img_path, FILTER_VALIDATE_URL)) {
$headers = PShow_File::get_headers($img_path);
if ($headers && PShow_File::isImage($img_path)) {
$attr->add();
$_img = PShow_File::resize_image($texture, 500, 500);
imagejpeg($_img, _PS_ROOT_DIR_ . '/img/co/' . (int) $attr->id . '.jpg', 75);
imagedestroy($_img);
} else {
PShow_Log::add(pathinfo(PShow_Import::getInstance()->filepath, PATHINFO_FILENAME) . ".log", "Error in importing image from url: " . $img_path);
}
}
}
if (Validate::isColor($value) && empty($attr->color)) {
$attr->color = $value;
}
try {
$attr->save();
} catch (PrestaShopException $e) {
PShow_Log::addExceptionLog($e);
}
break;
case 'attribute_group_type':
if (is_array($value))
$value = reset($value);
if (!in_array($value, array('select', 'radio', 'color')) || ($value == 'color' && !Validate::isColor($value))) {
$value = 'select';
}
PShow_Import::$objects[$classname]->group_type = $value;
break;
case 'attribute_group_name':
case 'attribute_group_public_name':
if (is_array($value))
$value = reset($value);
$_key = str_replace('attribute_group_', '', $key);
$this->prepareLangField(PShow_Import::$objects[$classname]->{$_key}, $value);
if (!array_key_exists('attribute_group_public_name', $this->data))
$this->prepareLangField(PShow_Import::$objects[$classname]->public_name, $value);
break;
default:
if (is_array($value))
$value = reset($value);
if (array_key_exists('lang', $classname::$definition['fields'][$key])) {
$this->prepareLangField(PShow_Import::$objects[$classname]->{$key}, $value);
} else {
PShow_Import::$objects[$classname]->$key = $value;
}
break;
}
}
}