Skip to content

Instantly share code, notes, and snippets.

@digibutt
Created September 20, 2011 11:55
Show Gist options
  • Save digibutt/1228932 to your computer and use it in GitHub Desktop.
Save digibutt/1228932 to your computer and use it in GitHub Desktop.
Log a hit in a plugin in MODX
$tv = $modx->getObject('modTemplateVar', array('name' => 'hitcount'));
$criteria = array(
'tmplvarid' => $tv->get('id'),
'contentid' => $modx->resourceIdentifier
);
if(!$tvResource = $modx->getObject('modTemplateVarResource', $criteria)){
$tvResource = $modx->newObject('modTemplateVarResource');
$tvResource->set('value', 1);
$tvResource->set('tmplvarid', $tv->get('id'));
$tvResource->set('contentid', $modx->resourceIdentifier);
$tvResource->save();
} else {
$value = (int)$tvResource->get('value') + 1;
$tvResource->set('value', $value);
$tvResource->save();
}
@digibutt
Copy link
Author

On review I think $tv->setValue($modx->resourceIdentifier, $value); would have been a little more elegant... =P

@Mark-H
Copy link

Mark-H commented Sep 20, 2011

hehe, perhaps!

Thanks for sharing ^^

I suppose this is either onWebPreRender / OnWebInit and that it really doesn't matter much as long as it only triggers one on page review and has a resource. :P

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