<commit_before><?php
/**
 * Attributes.php
 *
 * @package PrestashopImporterKit
 * @author  <NAME> <<EMAIL>>
 * @link    http://www.michael-pratt.com/
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Attribute Importer
 */
class Pik_Importer_Attributes extends Pik_Importer_Adapter implements Pik_Importer_Interface
{
    protected $groups = array();
    protected $groupAttributes = array();
    protected $attributes = array();


    public function __construct(array $config = array(), array $filters = array())
    {
        parent::__construct($config, $filters);
        foreach ((array) AttributeGroup::getAttributesGroups($this->config['id_lang']) as $group) {
            $this->groups[$group['name']] = (int) $group['id_attribute_group'];
        }

        foreach ((array) Attribute::getAttributes($this->config['id_lang']) as $attribute) {
            $this->attributes[$attribute['attribute_group'].'_'.$attribute['name']] = (int) $attribute['id_attribute'];
        }
    }

    /** inline {@inheritdoc} */
    public function import(array $data)
    {
        try {
            if (!$id = $this->getProductId($data)) {
                throw new InvalidArgumentException('Attr: The product doesnt exist - ' . print_r($data, true));
            }

            if (!empty($data['features'])) {
                $this->addFeatures($id, $data['features']);
                unset($data['features']);
            }

            if (!empty($data['categories'])) {
                $cats = array_map('intval', $data['categories']);
                $product = $this->getProduct($id);
                $product->id_category = array((int) min($cats));
                $product->id_category_default = (int) min($cats);
                $product->addToCategories($cats);
                $product->save();
                //sleep(1);
                $product->save();
                unset($data['categories']);
            }

            $this->addSingleQuantity($id, $data);
            if (!empty($data['value']) && !empty($data['attribute'])) {
                $this->importAttributes($id, $data);
            }
        } catch (Exception $e) {
            $this->errors[] = $e->getMessage();
        }

    }

    protected function addFeatures($idProduct, $features)
    {
        $array = explode(',', $features);
        foreach ($array as $feat)
        {
            if (empty($feat)) {
                continue;
            }

            //$tab = explode(':', utf8_decode($feat));
            $tab = explode(':', $feat);
            $featName = isset($tab[0]) ? trim($tab[0]) : '';
            $featValue = isset($tab[1]) ? trim($tab[1]) : '';
            $position = isset($tab[2]) ? (int)$tab[2] : false;
            $custom = isset($tab[3]) ? (int)$tab[3] : false;

            if(!empty($featName) && !empty($featValue))
            {
                $idFeature = (int)Feature::addFeatureImport($featName, $position);
                $idFeatureValue = (int)FeatureValue::addFeatureValueImport($idFeature, $featValue, $idProduct, $this->config['id_lang'], $custom);
                Product::addFeatureProductImport($idProduct, $idFeature, $idFeatureValue);
            }
        }

        Feature::cleanPositions();
    }

    protected function addSingleQuantity($id, array &$data = array())
    {
        if (!empty($data['quantity']) && empty($data['value']) && empty($data['attribute'])) {
            StockAvailable::setQuantity($id, null, (int) $data['quantity']);
            unset($data['quantity']);
        }
    }

    protected function importAttributes($id, array $data)
    {
        $idShopList = $this->getIdShopList($data);
        $this->sortGroups($data, $idShopList);

        // inits attribute
        $id_image = null;
        $id_product_attribute = 0;
        $id_product_attribute_update = false;
        $attributes_to_add = array();

        // for each attribute
        $ref = implode('-', $data['value']);
        foreach ($data['value'] as $key => $attribute)
        {
            if (empty($attribute)) {
                continue;
            }

            $tabAttr = explode(':', $attribute);<commit_msg>Minor bugfixes on the Attributes importer - Allow strings as $features in the Attributes::addFeatures method - Attributes::addSingleQuantity() now allows "0" as a valid quantity<commit_after><?php
/**
 * Attributes.php
 *
 * @package PrestashopImporterKit
 * @author  <NAME> <<EMAIL>>
 * @link    http://www.michael-pratt.com/
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Attribute Importer
 */
class Pik_Importer_Attributes extends Pik_Importer_Adapter implements Pik_Importer_Interface
{
    protected $groups = array();
    protected $groupAttributes = array();
    protected $attributes = array();


    public function __construct(array $config = array(), array $filters = array())
    {
        parent::__construct($config, $filters);
        foreach ((array) AttributeGroup::getAttributesGroups($this->config['id_lang']) as $group) {
            $this->groups[$group['name']] = (int) $group['id_attribute_group'];
        }

        foreach ((array) Attribute::getAttributes($this->config['id_lang']) as $attribute) {
            $this->attributes[$attribute['attribute_group'].'_'.$attribute['name']] = (int) $attribute['id_attribute'];
        }
    }

    /** inline {@inheritdoc} */
    public function import(array $data)
    {
        try {
            if (!$id = $this->getProductId($data)) {
                throw new InvalidArgumentException('Attr: The product doesnt exist - ' . print_r($data, true));
            }

            if (!empty($data['features'])) {
                $this->addFeatures($id, $data['features']);
                unset($data['features']);
            }

            if (!empty($data['categories'])) {
                $cats = array_map('intval', (array) $data['categories']);
                $product = $this->getProduct($id);
                $product->id_category = array((int) min($cats));
                $product->id_category_default = (int) min($cats);
                $product->addToCategories($cats);
                $product->save();
                $product->save();
                unset($data['categories']);
            }

            $this->addSingleQuantity($id, $data);
            if (!empty($data['value']) && !empty($data['attribute'])) {
                $this->importAttributes($id, $data);
            }
        } catch (Exception $e) {
            $this->errors[] = $e->getMessage();
        }

    }

    protected function addFeatures($idProduct, $features)
    {
        if (!is_array($features)) {
            $features = explode(',', $features);
        }

        foreach ($features as $feat)
        {
            if (empty($feat)) {
                continue;
            }

            //$tab = explode(':', utf8_decode($feat));
            $tab = explode(':', $feat);
            $featName = isset($tab[0]) ? trim($tab[0]) : '';
            $featValue = isset($tab[1]) ? trim($tab[1]) : '';
            $position = isset($tab[2]) ? (int)$tab[2] : false;
            $custom = isset($tab[3]) ? (int)$tab[3] : false;

            if(!empty($featName) && !empty($featValue))
            {
                $idFeature = (int)Feature::addFeatureImport($featName, $position);
                $idFeatureValue = (int)FeatureValue::addFeatureValueImport($idFeature, $featValue, $idProduct, $this->config['id_lang'], $custom);
                Product::addFeatureProductImport($idProduct, $idFeature, $idFeatureValue);
            }
        }

        Feature::cleanPositions();
    }

    protected function addSingleQuantity($id, array &$data = array())
    {
        if (array_key_exists('quantity', $data) && empty($data['value']) && empty($data['attribute'])) {
            StockAvailable::setQuantity($id, null, (int) $data['quantity']);
            unset($data['quantity']);
        }
    }

    protected function importAttributes($id, array $data)
    {
        $idShopList = $this->getIdShopList($data);
        $this->sortGroups($data, $idShopList);

        // inits attribute
        $id_image = null;
        $id_product_attribute = 0;
        $id_product_attribute_update = false;
        $attributes_to_add = array();

        // for each attribute
        $ref = implode('-', $data['value']);
        foreach ($data['value'] as $key => $attribute)
        {
            if (empty($attribute)) {
                continue;
            }

            $tabAttr = explode(':', $attribute);
{'fraction_non_alphanumeric': 0.16591979777088361, 'fraction_numerical': 0.0025278639549580605, 'mean_word_length': 2.282051282051282, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4416910', 'n_tokens_mistral': 2428, 'n_tokens_neox': 2348, 'n_words': 473}