Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adding the following config_options entries to your MODX Revolution config file will allow you to track changes made to any xPDOObject class via the `xPDOObject::save()`, `xPDOObject::remove()`, `xPDO::updateCollection()`, or `xPDO::removeCollection()` methods. This example records changes into a database register called `changes` under a config…
<?php
$config_options = array (
'callback_excludes' => array(
'modSession',
'modManagerLog',
'modActiveUser',
'modDbRegisterQueue',
'modDbRegisterTopic',
'modDbRegisterMessage',
'modUser',
'modUserProfile',
'modUserGroupMember',
'ReviseResourceDraft',
'ReviseResourceHistory',
'vxChunk',
'vxPlugin',
'vxResource',
'vxSnippet',
'vxTemplate',
'vxTemplateVar',
),
'changeSet' => 'prod',
'callback_on_save' => function($data) {
if (!isset($data['className'])) return;
/** @var modX $modx */
$modx =& $GLOBALS['modx'];
if ($modx->getOption(xPDO::OPT_SETUP, null, false)) return;
$object = null;
$payload = null;
$class = $data['className'];
if (!in_array($class, $modx->getOption('callback_excludes', null, array('modActiveUser', 'modDbRegisterQueue', 'modDbRegisterTopic', 'modDbRegisterMessage', 'modManagerLog', 'modSession')))) {
if (isset($data['object'])) {
/** @var xPDOObject $object */
$object = $data['object'];
if ($object instanceof xPDOObject) {
$pk = $object->getPrimaryKey();
$payload = array('action' => 'save', 'class' => $class, 'pk' => $pk, 'object' => $object->toArray('', true));
if (is_array($pk)) $pk = implode('-', array_values($pk));
$changeSet = $object->getOption('changeSet', null, 'global');
}
} elseif (isset($data['criteria'])) {
/** @var xPDOQuery $object */
$object = $data['criteria'];
if ($object instanceof xPDOQuery && $object->query['command'] === 'UPDATE') {
$payload = array(
'action' => 'updateCollection',
'class' => $class,
'criteria' => array(
'query' => $object->query,
'alias' => $object->getAlias()
)
);
$changeSet = $object->xpdo->getOption('changeSet', null, 'global');
}
}
if ($payload !== null && $object !== null) {
/** @var modRegistry $registry */
$registry = $object->xpdo->getService('registry', 'registry.modRegistry');
$registry->getRegister('changes', 'registry.modDbRegister', array('directory' => 'changes'));
$topic = "/{$changeSet}/";
$registry->changes->subscribe($topic);
$registry->changes->connect();
$payload = $object->xpdo->toJSON($payload);
$registry->changes->send($topic, $payload);
}
}
},
'callback_on_remove' => function($data) {
if (!isset($data['className'])) return;
/** @var modX $modx */
$modx =& $GLOBALS['modx'];
if ($modx->getOption(xPDO::OPT_SETUP, null, false)) return;
$object = null;
$payload = null;
$class = $data['className'];
if (!in_array($class, $modx->getOption('callback_excludes', null, array('modActiveUser', 'modDbRegisterQueue', 'modDbRegisterTopic', 'modDbRegisterMessage', 'modManagerLog', 'modSession')))) {
if (isset($data['object'])) {
/** @var xPDOObject $object */
$object = $data['object'];
if ($object instanceof xPDOObject && !in_array($class, $object->getOption('callback_excludes', null, array('modActiveUser', 'modDbRegisterQueue', 'modDbRegisterTopic', 'modDbRegisterMessage', 'modManagerLog', 'modSession')))) {
$pk = $object->getPrimaryKey();
$payload = array('action' => 'remove', 'class' => $class, 'pk' => $pk);
if (is_array($pk)) $pk = implode('-', array_values($pk));
$changeSet = $object->getOption('changeSet', null, 'global');
}
} elseif (isset($data['criteria'])) {
/** @var xPDOQuery $object */
$object = $data['criteria'];
if ($object instanceof xPDOQuery && $object->query['command'] === 'DELETE') {
$payload = array(
'action' => 'removeCollection',
'class' => $class,
'criteria' => array(
'query' => $object->query,
'alias' => $object->getAlias()
)
);
$changeSet = $object->xpdo->getOption('changeSet', null, 'global');
}
}
}
if ($payload !== null && $object !== null) {
/** @var modRegistry $registry */
$registry = $object->xpdo->getService('registry', 'registry.modRegistry');
$registry->getRegister('changes', 'registry.modDbRegister', array('directory' => 'changes'));
$topic = "/{$changeSet}/";
$registry->changes->subscribe($topic);
$registry->changes->connect();
$payload = $object->xpdo->toJSON($payload);
$registry->changes->send($topic, $payload);
}
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment