Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export in different rows OneToMany, mTm or localized fields #133

Open
jkdc opened this issue Nov 18, 2023 · 1 comment
Open

Export in different rows OneToMany, mTm or localized fields #133

jkdc opened this issue Nov 18, 2023 · 1 comment

Comments

@jkdc
Copy link

jkdc commented Nov 18, 2023

Hello, I'm attempting to export data using the Data Director bundle, but I'm unsure of how to proceed. Specifically, I aim to export my product names in all available languages, following this structure:

productId, language, name
1, en, hello
1, es, hola
1, de, hallo
...

Additionally, I'm interested in exporting the relationships of my products, such as:

productId, categoryId, productName, categoryName
1, 1, pencil, cat1
1, 2, pencil, cat2
1, 3, pencil, cat3
...

I am aware that I can include this information in the same row and cell, but I am not interested in using this approach. Could you please guide me on how to accomplish this? Or is it not feasible with the Data Director bundle?

@BlackbitDevs
Copy link
Collaborator

Specifically, I aim to export my product names in all available languages

First you need to extract the data in the different languages. There are 2 ways do do that:

  1. Create separate raw data fields with data query selectors name#en, name#es and name#de.
  2. Create one raw data field with data query selector name#all
    This will result in a JSON object:
    {
      "en": "hello",
      "es": "hola",
      "de": "hallo"
    }
    This way has the advantage that you will get the name in all configured languages - so if you add a new one in Pimcore's system settings, this will automatically be present in this array, too.

To achieve a CSV output like you want it

productId, language, name
1, en, hello
1, es, hola
1, de, hallo
you will have to adjust the result callback function template for CSV. By default it exports the data of each field in a separate column. But you want it in rows. This can be achieved with a result callback function like this (I assume that you have 2 raw data fields: id and name#all):

$separator = {{ CSV separator }} ?: ';';
                    
if({{ CSV ENCODING }}) {
    $separator = iconv('UTF-8', {{ CSV ENCODING }}, $separator);
}
                    
if(!$params['response']->hasContent()) {
    $params['response']->headers->set('Content-Type', 'text/csv');
    
    $result = '';
    if(!{{ CSV ENCODING }} || strtolower({{ CSV ENCODING }}) === 'utf-8') {
        $result .= "\xEF\xBB\xBF";
    }
    
    $result .= '"productId", "language", "name"'."\n";
} else {
    $result = '';
}

$decodeValue = function($value) use ($params) {
    if(!is_scalar($value)) {
        if(is_array($value) && is_scalar(reset($value))) {
            $value = implode(', ', $value);
        } else {
            $value = json_encode($value, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE);
        }
    }
    
    if({{ CSV ENCODING }}) {
        $value = iconv('UTF-8', {{ CSV ENCODING }}, $value);
    }
    
    return '"'.str_replace('"', '""', $value).'"';
};

if($params['rawItemData']) {
    foreach({{ name#all }} as $language => $name) {
        $line = [];
        $line[] = $decodeValue({{ id }});
        $line[] = $decodeValue($language);
        $line[] = $decodeValue($name);
        $result .= implode($separator, $line)."\n";
    }
}

$params['response']->addContent($result);

Additionally, I'm interested in exporting the relationships of my products, such as:

productId, categoryId, productName, categoryName
1, 1, pencil, cat1
1, 2, pencil, cat2
1, 3, pencil, cat3

This works the same as above for the languages but this time you have to select categories:each:(id;name) to retrieve the categories which are assigned to the currently processed product.

Next time, please be so fair and write such project-specific questions to help@blackbit.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants