Skip to content

gsOnExportValues

Triggered after retrieving data. Here we can modify the data, perform additional filtering, or add new data.

Available variables:

NameDescription
fieldsExport Fields
valuesCurrent values as an array
objectExport object
propertiesExport properties

modUser

Add the user ID to the username value.

php
if ($modx->event->name === 'gsOnExportValues') {
    if ($object->model_class === 'modUser') {
        $values = array_map(function($value) {
            $value['username'] = "{$value['username']} ({$value['id']})";
            return $value;
        }, $values);
    }
    $modx->event->params['values'] = $values;
}

before:

text
Array
(
    [id] => 1
    [username] => boshnik
    [email] => email@gmail.com
)

after:

text
Array
(
    [id] => 1
    [username] => boshnik (1)
    [email] => email@gmail.com
)

modResource

Get the template name instead of its ID.

php
if ($modx->event->name === 'gsOnExportValues') {
    if ($object->model_class === 'modResource') {
        $values = array_map(function($value) use ($modx) {
            $template = $modx->getObject(modTemplate::class, $value['template']);
            $value['template'] = $template ? $template->templatename : $value['template'];
            return $value;
        }, $values);
    }
    $modx->event->params['values'] = $values;
}

If there are many resources, we can optimize the code a bit:

php
if ($modx->event->name === 'gsOnExportValues') {
    if ($object->model_class === 'modResource') {
        // Get all templates
        $rows = $modx->getIterator(modTemplate::class);
        // Store the templates in an array.
        $templates = [];
        foreach ($rows as $row) {
            $templates[$row->id] = $row->templatename;
        }
        
        $values = array_map(function($value) use ($templates) {
            $value['template'] = $templates[$value['template']] ?? $value['template'];
            return $value;
        }, $values);
    }
    $modx->event->params['values'] = $values;
}

Splitting MIGX into columns.

php
if ($modx->event->name === 'gsOnExportValues') {
    if ($object->model_class === 'modResource') {
        foreach ($values as $key => &$value) {
            $migx = json_decode($value['tv.slide'], 1);
            foreach ($migx as $idx => $item) {
                $value['MIGX_id' . $idx] = implode("\n", array_intersect_key($item, array_flip(['title', 'text'])));
            }
        }
        
        // Adding appropriate headers
        unset($fields['tv.slide']);
        $migx = json_decode($values[0]['tv.slide'], 1);
        foreach ($migx as $idx => $item) {
            $fields['MIGX_id' . $idx] = 'MIGX_ID_' . $idx;
        }
        
        $modx->event->params['fields'] = $fields;
        $modx->event->params['values'] = $values;
    }
}

FormIt

Export forms that were submitted after a specific date (07.07.2024).

php
if ($modx->event->name === 'gsOnExportValues') {
    if ($object->model_class === 'FormIt') {
        $time = strtotime('07.07.2024');
        $values = array_filter($values, function($value) use ($time) {
            return $value['date'] > $time;
        });
    }
    $modx->event->params['values'] = $values;
}

msCategory

Get the parent pagetitle instead of its ID.

php
if ($modx->event->name === 'gsOnExportValues') {
    if ($object->model_class === 'msCategory') {
        $values = array_map(function($value) use ($modx) {
            $parent = $modx->getObject(modResource::class, $value['parent']);
            $value['parent'] = $parent ? $parent->pagetitle : $value['parent'];
            return $value;
        }, $values);
    }
    $modx->event->params['values'] = $values;
}

msProduct

Add 10% to the price, and save the current price as the old price.

php
if ($modx->event->name === 'gsOnExportValues') {
    if ($object->model_class === 'msProduct') {
        $values = array_map(function($value) {
            if (isset($value['old_price']) && !empty($value['price'])) {
                $value['old_price'] = $value['price'];
            }
            
            if (!empty($value['price'])) {
                $value['price'] = $value['price'] * 1.1;
            }
      
            return $value;
        }, $values);
    }
    $modx->event->params['values'] = $values;
}

msVendor

Add a default country if the manufacturer does not specify one.

php
if ($modx->event->name === 'gsOnExportValues') {
    if ($object->model_class === 'msVendor') {
        $values = array_map(function($value) {
            if (empty($value['country'])) {
	            $value['country'] = 'China';
	        }
            return $value;
        }, $values);
    }
    $modx->event->params['values'] = $values;
}

msOrder

If the order amount is greater than 10,000, add the comment: Urgent order.

php
if ($modx->event->name === 'gsOnExportValues') {
    if ($object->model_class === 'msOrder') {
        $values = array_map(function($value) {
            if (isset($value['cost']) && isset($value['order_comment'])) {
	            if ($value['cost'] > 10000) {
	                $value['order_comment'] = 'Urgent order';
	            }
	        }
            return $value;
        }, $values);
    }
    $modx->event->params['values'] = $values;
}

msOptionsPrice2

If the price is greater than 500, double it.

php
if ($modx->event->name === 'gsOnExportValues') {
    if ($object->model_class === 'msOptionsPrice2') {
        $values = array_map(function($value) {
            if (isset($value['price'])) {
	            if ($value['price'] > 500) {
	                $value['price'] = $value['price'] * 2;
	            }
	        }
            return $value;
        }, $values);
    }
    $modx->event->params['values'] = $values;
}

© GoogleSheets 2019-present