The problem I have is random documents suddenly seem to have their data overwritten with another resources data. This seems to be very random as it only occasionally happens. From what i've seen it's only on resource that run a custom plugin so I'm assuming it's got to be this plugin. All the plugin does is upon publishing a doc it takes the current value for the content and the value of a TV, performs a str_replace.
Can anyone see anything in the below thats likely to cause it to overwrite the resource with another resources values.
<?php
if ($resource->get('template') == 20) {
$attachments = array('#attachment1','#attachment2','#attachment3','#attachment4','#attachment5','#attachment6');
foreach($attachments as $k => $attachment) {
$tvname = 'File'.++$k;
if ($resource->getTVValue($tvname)) {
$pattern[$k] = $attachment;
$replace[$k] = $resource->getTVValue($tvname);
}
}
unset($attachment);
$id = $modx->resource->get('id');
$pagetitle = $modx->resource->get('pagetitle');
$tv = $modx->getObject('modTemplateVar', array('name'=>'additionalSummary'));
//get content and summary text for resource
$content = $resource->getContent();
$summary = $tv->getValue($id);
//replace the link attachment strings
$newSummary = str_replace($pattern, $replace, $summary);
$newContent = str_replace($pattern, $replace, $content);
//set the new content and summary
$resource->setContent($newContent);
$tv->setValue($id,$newSummary);
$tv->save();
$resource->save();
}
Also I'd set up some logging so when this runs it outputs the current and new values for both content the TV value.