<commit_before><?php

namespace Shopware\Components\SwagImportExport\DbAdapters;

class ArticlesImagesDbAdapter implements DataDbAdapter
{

    /**
     * Shopware\Components\Model\ModelManager
     */
    protected $manager;
    protected $articleRepository;
    protected $articleDetailRepository;
    protected $db;

    /**
     * Returns record ids
     * 
     * @param int $start
     * @param int $limit
     * @param type $filter
     * @return array
     */
    public function readRecordIds($start = null, $limit = null, $filter = null)
    {
        $manager = $this->getManager();

        $builder = $manager->createQueryBuilder();

        $builder->select('image.id');

        $builder->from('Shopware\Models\Article\Image', 'image')
                ->where('image.articleDetailId IS NULL')
                ->andWhere('image.parentId IS NULL');

        $builder->setFirstResult($start)
                ->setMaxResults($limit);

        $records = $builder->getQuery()->getResult();

        $result = array();
        if ($records) {
            foreach ($records as $value) {
                $result[] = $value['id'];
            }
        }

        return $result;
    }

    /**
     * Returns article images 
     * 
     * @param array $ids
     * @param array $columns
     * @return array
     */
    public function read($ids, $columns)
    {
        if (!$ids && empty($ids)) {
            throw new \Exception('Can not read article images without ids.');
        }

        if (!$columns && empty($columns)) {
            throw new \Exception('Can not read article images without column names.');
        }

        $manager = $this->getManager();

        $builder = $manager->createQueryBuilder();
        $builder->select($columns)
                ->from('Shopware\Models\Article\Image', 'aimage')
                ->innerJoin('aimage.article', 'article')
                ->innerJoin('article.details', 'articleDetail')
                ->leftJoin('aimage.mappings', 'im')
                ->leftJoin('im.rules', 'mr')
                ->leftJoin('mr.option', 'co')
                ->leftJoin('co.group', 'cg')
                ->where('aimage.id IN (:ids)')
                ->groupBy('aimage.id')
                ->setParameter('ids', $ids);

        $result['default'] = $builder->getQuery()->getResult();

        foreach ($result['default'] as &$image) {
            if (empty($image['relations'])) {
                continue;
            }

            $relations = explode(',', $image['relations']);
            $relations = array_unique($relations);

            $out = array();
            foreach ($relations as $rule) {
                $split = explode('|', $rule);
                $ruleId = $split[0];
                $optionId = $split[1];
                $name = $split[2];
                $groupName = $split[3];

                $out[$ruleId][] = "$groupName:$name";
            }

            $temp = array();
            foreach ($out as $group) {
                $name = $group['name'];
                $temp [] = "{" . implode(',', $group) . "}";
            }

            $image['relations'] = implode('&', $temp);
        }

        return $result;
    }

    /**
     * Returns default image columns name 
     * 
     * @return array
     */
    public function getDefaultColumns()
    {
        $request = Shopware()->Front()->Request();
        $path = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath() . '/media/image/';

        $columns = array(
            'articleDetail.number as ordernumber',
            "CONCAT('$path', aimage.path, '.', aimage.extension) as image",
            'aimage.main as main',
            'aimage.description as description',
            'aimage.position as position',
            'aimage.width as width',
            'aimage.height as height',
            "GroupConcat(im.id, '|', mr.optionId, '|' , co.name, '|', cg.name) as relations"
        );

        return $columns;
    }

    /**
     * Insert/Update data into db
     * 
     * @param array $records
     */
    public function write($records)
    {
        $configuratorGroupRepository = $this->getManager()->getRepository('Shopware\Models\Article\Configurator\Group');
        $configuratorOptionRepository = $this->getManager()->getRepository('Shopware\Models\Article\Configurator\Option');

        foreach ($records['default'] as $record) {
            if (empty($record['ordernumber']) || empty($record['image'])) {
                throw new \Exception('Ordernumber and image are required');
            }
<commit_msg>Fixed ordernumber in article image export<commit_after><?php

namespace Shopware\Components\SwagImportExport\DbAdapters;

class ArticlesImagesDbAdapter implements DataDbAdapter
{

    /**
     * Shopware\Components\Model\ModelManager
     */
    protected $manager;
    protected $articleRepository;
    protected $articleDetailRepository;
    protected $db;

    /**
     * Returns record ids
     * 
     * @param int $start
     * @param int $limit
     * @param type $filter
     * @return array
     */
    public function readRecordIds($start = null, $limit = null, $filter = null)
    {
        $manager = $this->getManager();

        $builder = $manager->createQueryBuilder();

        $builder->select('image.id');

        $builder->from('Shopware\Models\Article\Image', 'image')
                ->where('image.articleDetailId IS NULL')
                ->andWhere('image.parentId IS NULL');

        $builder->setFirstResult($start)
                ->setMaxResults($limit);

        $records = $builder->getQuery()->getResult();

        $result = array();
        if ($records) {
            foreach ($records as $value) {
                $result[] = $value['id'];
            }
        }

        return $result;
    }

    /**
     * Returns article images 
     * 
     * @param array $ids
     * @param array $columns
     * @return array
     */
    public function read($ids, $columns)
    {
        if (!$ids && empty($ids)) {
            throw new \Exception('Can not read article images without ids.');
        }

        if (!$columns && empty($columns)) {
            throw new \Exception('Can not read article images without column names.');
        }

        $manager = $this->getManager();

        $builder = $manager->createQueryBuilder();
        $builder->select($columns)
                ->from('Shopware\Models\Article\Image', 'aimage')
                ->innerJoin('aimage.article', 'article')
                ->innerJoin('article.details', 'articleDetail')
                ->leftJoin('Shopware\Models\Article\Detail', 'mv', \Doctrine\ORM\Query\Expr\Join::WITH, 'mv.articleId=article.id AND mv.kind=1')
                ->leftJoin('aimage.mappings', 'im')
                ->leftJoin('im.rules', 'mr')
                ->leftJoin('mr.option', 'co')
                ->leftJoin('co.group', 'cg')
                ->where('aimage.id IN (:ids)')
                ->groupBy('aimage.id')
                ->setParameter('ids', $ids);

        $result['default'] = $builder->getQuery()->getResult();

        foreach ($result['default'] as &$image) {
            if (empty($image['relations'])) {
                continue;
            }

            $relations = explode(',', $image['relations']);
            $relations = array_unique($relations);

            $out = array();
            foreach ($relations as $rule) {
                $split = explode('|', $rule);
                $ruleId = $split[0];
                $optionId = $split[1];
                $name = $split[2];
                $groupName = $split[3];

                $out[$ruleId][] = "$groupName:$name";
            }

            $temp = array();
            foreach ($out as $group) {
                $name = $group['name'];
                $temp [] = "{" . implode(',', $group) . "}";
            }

            $image['relations'] = implode('&', $temp);
        }

        return $result;
    }

    /**
     * Returns default image columns name 
     * 
     * @return array
     */
    public function getDefaultColumns()
    {
        $request = Shopware()->Front()->Request();
        $path = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath() . '/media/image/';

        $columns = array(
            'mv.number as ordernumber',
            "CONCAT('$path', aimage.path, '.', aimage.extension) as image",
            'aimage.main as main',
            'aimage.description as description',
            'aimage.position as position',
            'aimage.width as width',
            'aimage.height as height',
            "GroupConcat(im.id, '|', mr.optionId, '|' , co.name, '|', cg.name) as relations"
        );

        return $columns;
    }

    /**
     * Insert/Update data into db
     * 
     * @param array $records
     */
    public function write($records)
    {
        $configuratorGroupRepository = $this->getManager()->getRepository('Shopware\Models\Article\Configurator\Group');
        $configuratorOptionRepository = $this->getManager()->getRepository('Shopware\Models\Article\Configurator\Option');

        foreach ($records['default'] as $record) {
            if (empty($record['ordernumber']) || empty($record['image'])) {
                throw new \Exception('Ordernumber and image are required');
            }

{'fraction_non_alphanumeric': 0.15795090715048027, 'fraction_numerical': 0.00096051227321238, 'mean_word_length': 1.8710171568627452, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5527405', 'n_tokens_mistral': 2633, 'n_tokens_neox': 2492, 'n_words': 529}