<commit_before><?php

namespace Shopware\Components\SwagImportExport\DbAdapters;

use Shopware\Components\SwagImportExport\Utils\SnippetsHelper as SnippetsHelper;
use Shopware\Components\SwagImportExport\Exception\AdapterException;

class TranslationsDbAdapter implements DataDbAdapter
{

    protected $manager;

    protected $configuratorGroupRepo;
    protected $configuratorOptionRepo;

    protected $propertyGroupRepo;
    protected $propertyOptionRepo;
    protected $propertyValueRepo;

    protected $logMessages;

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

        $builder = $manager->createQueryBuilder();

        $builder->select('t.id');
        $builder->from('Shopware\Models\Translation\Translation', 't')
            ->where("t.type = 'propertyvalue'")
            ->orWhere("t.type = 'propertyvalue'")
            ->orWhere("t.type = 'propertyoption'")
            ->orWhere("t.type = 'propertygroup'")
            ->orWhere("t.type = 'configuratoroption'")
            ->orWhere("t.type = 'configuratorgroup'");

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

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

        $result = array_map(
            function($item) {
                return $item['id'];
            }, $records
        );

        return $result;
    }

    public function read($ids, $columns)
    {
        if (!$ids && empty($ids)) {
            $message = SnippetsHelper::getNamespace()
                ->get('adapters/translations/no_ids', 'Can not read translations without ids.');
            throw new \Exception($message);
        }

        if (!$columns && empty($columns)) {
            $message = SnippetsHelper::getNamespace()
                ->get('adapters/translations/no_column_names', 'Can not read translations without column names.');
            throw new \Exception($message);
        }

        $translations = $this->getTranslations($ids);

        $result['default'] = $this->prepareTranslations($translations);

        return $result;
    }

    public function prepareTranslations($translations)
    {
        $mapper = $this->getElemenetMapper();

        $result = array();
        foreach ($translations as $index => $translation){

            $data = unserialize($translation['objectdata']);

            //key for different translation types
            $key = $mapper[$translation['objecttype']];

            $result[] = array(
                'objectKey' => $translation['objectkey'],
                'objectType' => $translation['objecttype'],
                'baseName' => $translation['baseName'],
                'name' => $data[$key],
                'description' => $data['description'],
                'languageId' => $translation['languageId'],
            );
        }


        return $result;
    }

    public function getDefaultColumns()
    {
        $translation = array(
            'objectKey',
            'objectType',
            'baseName',
            'name',
            'description',
            'languageId',
        );

        return $translation;
    }

    /**
     * @return array
     */
    public function getSections()
    {
        return array(
            array('id' => 'default', 'name' => 'default ')
        );
    }

    /**
     * @param string $section
     * @return mix
     */
    public function getColumns($section)
    {
        $method = 'get' . ucfirst($section) . 'Columns';

        if (method_exists($this, $method)) {
            return $this->{$method}();
        }

        return false;
    }

    public function write($records)
    {
        if ($records['default'] == null) {
            return;
        }

        $records = Shopware()->Events()->filter(
            'Shopware_Components_SwagImportExport_DbAdapters_TranslationsDbAdapter_Write',
            $records,
            array('subject' => $this)
        );

        $importMapper = $this->getElemenetMapper();

        $translationWriter = new \Shopware_Components_Translation();

        foreach ($records['default'] as $index => $record) {
            try {

                if (!isset($record['objectType']) || empty($record['objectType'])) {
                    $message = SnippetsHelper::getNamespace()
                        ->get('adapters/translations/object_type_not_found', 'Object type is required.');
                    throw new AdapterException($message);
                }

                if (isset($record['languageId'])) {
                    $shop = $this->getManager()->find('Shopware\Models\Shop\Shop', $record['languageId']);
                }

                if (!$shop) {
                    $message = SnippetsHelper::getNamespace()
                        ->get('adapters/translations/lang_id_not_found', 'Language with id %s does not exists');
                    throw new \AdapterException(sprintf($message, $record['languageId']));
                }

                $repository = $this->getRepository($record['objectType']);

                if (!isset($record['objectKey']) && !empty($record['objectKey'])){
                    $element = $repository->findOneBy(array('id' => (int) $record['objectKey']));

                    if(!$element){
                        $message = SnippetsHelper::getNamespace()
                            ->get('adapters/translations/element_id_not_found', '%s element not found with ID %s');
                        throw new AdapterException(sprintf($message, $record['objectType'], $record['objectKey']));
                    }<commit_msg>PT-3920 removed backslash<commit_after><?php

namespace Shopware\Components\SwagImportExport\DbAdapters;

use Shopware\Components\SwagImportExport\Utils\SnippetsHelper as SnippetsHelper;
use Shopware\Components\SwagImportExport\Exception\AdapterException;

class TranslationsDbAdapter implements DataDbAdapter
{

    protected $manager;

    protected $configuratorGroupRepo;
    protected $configuratorOptionRepo;

    protected $propertyGroupRepo;
    protected $propertyOptionRepo;
    protected $propertyValueRepo;

    protected $logMessages;

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

        $builder = $manager->createQueryBuilder();

        $builder->select('t.id');
        $builder->from('Shopware\Models\Translation\Translation', 't')
            ->where("t.type = 'propertyvalue'")
            ->orWhere("t.type = 'propertyvalue'")
            ->orWhere("t.type = 'propertyoption'")
            ->orWhere("t.type = 'propertygroup'")
            ->orWhere("t.type = 'configuratoroption'")
            ->orWhere("t.type = 'configuratorgroup'");

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

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

        $result = array_map(
            function($item) {
                return $item['id'];
            }, $records
        );

        return $result;
    }

    public function read($ids, $columns)
    {
        if (!$ids && empty($ids)) {
            $message = SnippetsHelper::getNamespace()
                ->get('adapters/translations/no_ids', 'Can not read translations without ids.');
            throw new \Exception($message);
        }

        if (!$columns && empty($columns)) {
            $message = SnippetsHelper::getNamespace()
                ->get('adapters/translations/no_column_names', 'Can not read translations without column names.');
            throw new \Exception($message);
        }

        $translations = $this->getTranslations($ids);

        $result['default'] = $this->prepareTranslations($translations);

        return $result;
    }

    public function prepareTranslations($translations)
    {
        $mapper = $this->getElemenetMapper();

        $result = array();
        foreach ($translations as $index => $translation){

            $data = unserialize($translation['objectdata']);

            //key for different translation types
            $key = $mapper[$translation['objecttype']];

            $result[] = array(
                'objectKey' => $translation['objectkey'],
                'objectType' => $translation['objecttype'],
                'baseName' => $translation['baseName'],
                'name' => $data[$key],
                'description' => $data['description'],
                'languageId' => $translation['languageId'],
            );
        }


        return $result;
    }

    public function getDefaultColumns()
    {
        $translation = array(
            'objectKey',
            'objectType',
            'baseName',
            'name',
            'description',
            'languageId',
        );

        return $translation;
    }

    /**
     * @return array
     */
    public function getSections()
    {
        return array(
            array('id' => 'default', 'name' => 'default ')
        );
    }

    /**
     * @param string $section
     * @return mix
     */
    public function getColumns($section)
    {
        $method = 'get' . ucfirst($section) . 'Columns';

        if (method_exists($this, $method)) {
            return $this->{$method}();
        }

        return false;
    }

    public function write($records)
    {
        if ($records['default'] == null) {
            return;
        }

        $records = Shopware()->Events()->filter(
            'Shopware_Components_SwagImportExport_DbAdapters_TranslationsDbAdapter_Write',
            $records,
            array('subject' => $this)
        );

        $importMapper = $this->getElemenetMapper();

        $translationWriter = new \Shopware_Components_Translation();

        foreach ($records['default'] as $index => $record) {
            try {

                if (!isset($record['objectType']) || empty($record['objectType'])) {
                    $message = SnippetsHelper::getNamespace()
                        ->get('adapters/translations/object_type_not_found', 'Object type is required.');
                    throw new AdapterException($message);
                }

                if (isset($record['languageId'])) {
                    $shop = $this->getManager()->find('Shopware\Models\Shop\Shop', $record['languageId']);
                }

                if (!$shop) {
                    $message = SnippetsHelper::getNamespace()
                        ->get('adapters/translations/lang_id_not_found', 'Language with id %s does not exists');
                    throw new AdapterException(sprintf($message, $record['languageId']));
                }

                $repository = $this->getRepository($record['objectType']);

                if (!isset($record['objectKey']) && !empty($record['objectKey'])){
                    $element = $repository->findOneBy(array('id' => (int) $record['objectKey']));

                    if(!$element){
                        $message = SnippetsHelper::getNamespace()
                            ->get('adapters/translations/element_id_not_found', '%s element not found with ID %s');
                        throw new AdapterException(sprintf($message, $record['objectType'], $record['objectKey']));
                    }
{'fraction_non_alphanumeric': 0.14132050721469175, 'fraction_numerical': 0.00034980323567993004, 'mean_word_length': 2.0166183065154315, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 87, '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': '4669392', 'n_tokens_mistral': 3043, 'n_tokens_neox': 2893, 'n_words': 552}