yj-t
12/24/2015 - 1:16 AM

Sets Resource properties OnDocFormSave. To be used with getResourceProps.snippet.php

Sets Resource properties OnDocFormSave. To be used with getResourceProps.snippet.php

<?php
/**
 * 
 * @author @sepiariver
 *
 **/
if ($modx->context->get('key') !== 'mgr' || $modx->event->name !== 'OnDocFormSave') return;
if (!($resource instanceof modResource)) {
    $modx->log(modX::LOG_LEVEL_ERROR, 'setResourceProps Plugin did not have access to a valid resource object on line: ' . __LINE__);
    return;
}

// set default namespace for legacy content from modx.com import
$namespace = $modx->getOption('namespace', $scriptProperties, 'customprops');

// This is custom and would be modified per-implementation
// Set previously published and next article
$pC = $modx->newQuery('modResource');
$pC->select('id');
$pC->where(array(
    'parent:=' => $resource->get('parent'),
    'template:=' => $resource->get('template'),
    'publishedon:<' => $resource->publishedon,
    'deleted' => 0,
));
$pC->sortby('publishedon', 'DESC');
$pC->limit(1);

$nC = $modx->newQuery('modResource');
$nC->select('id');
$nC->where(array(
    'parent:=' => $resource->get('parent'),
    'template:=' => $resource->get('template'),
    'publishedon:>' => $resource->publishedon,
    'deleted' => 0,
));
$nC->sortby('publishedon', 'ASC');
$nC->limit(1);

$props = array(
	'prev' => $modx->getValue($pC->prepare()),
	'next' => $modx->getValue($nC->prepare()),
);


$resource->setProperties($props, $namespace);
$resource->save();