Skip to content

Instantly share code, notes, and snippets.

@enminc
Created March 5, 2013 20:26
Show Gist options
  • Save enminc/5093966 to your computer and use it in GitHub Desktop.
Save enminc/5093966 to your computer and use it in GitHub Desktop.
<?php
/**
* Example of how to fetch your own db table records cached, without using the snippet cached.
*
* Useful when the user can filter the data, like in a seach engine
*
* @package yourpackage
*/
$yourpackage = $modx->getService('yourpackage','yourpackage',$modx->getOption('yourpackage.core_path',null,$modx->getOption('core_path').'components/yourpackage/').'model/yourpackage/',$scriptProperties);
if (!($yourpackage instanceof yourpackage)) return '';
//properties
$tpl = $modx->getOption('tpl',$scriptProperties,'yourTplChunk'); //name of the "template" chunk
$sortBy = $modx->getOption('sortBy',$scriptProperties,'createdon');
$sortDir = $modx->getOption('sortDir',$scriptProperties,'DESC');
$outputSeparator = $modx->getOption('outputSeparator',$scriptProperties,"\n");
$className = $modx->getOption('className',$scriptProperties,'newsItem'); //class name to select objects of
//filter settings (user can set this using a form on the front end)
$fltrKeywords = $modx->getOption('keywords',$_GET,'');
//getPage setings
$limit = $modx->getOption('limit', $scriptProperties, 10);
$offset = $modx->getOption('offset', $scriptProperties, 0);
$totalVar = $modx->getOption('totalVar', $scriptProperties, 'total');
$page = $modx->getOption('page', $scriptProperties, 1);
//cache settings
$cacheOptions = array(xPDO::OPT_CACHE_KEY => 'customcmps/yourpackage');
$cacheLifetime = 0; //forever; use a plugin to delete the cache when needed or set a life time here to expire the cache automatically
$cacheKeyPage = 'resource-'.$modx->resource->get('id').'-limit-'.$limit.'-page-'.$page.'-filters-'.sha1($fltrKeywords).'.list';
$cacheKeyCount = 'resource-'.$modx->resource->get('id').'-filters-'.sha1($fltrKeywords.$fltrCategory.$fltrStartDate.$fltrEndDate).'.count';
//get cached output
$output = $modx->cacheManager->get($cacheKeyPage, $cacheOptions);
$count = $modx->cacheManager->get($cacheKeyCount, $cacheOptions);
//no cached result, so get from DB and store cache file
if(!$output || !$count) {
/* build query */
$c = $modx->newQuery($className);
//filters
$conditions = array('published' => 1);
if(!empty($fltrKeywords)) {
$fltrKeywords = explode(' ', $fltrKeywords);
$kwConditions = array();
foreach ($fltrKeywords as $keyword) {
$c->orCondition(array('name:LIKE' => '%'.trim($keyword).'%'), null, 0);
$c->orCondition(array('description:LIKE' => '%'.trim($keyword).'%'), null, 0);
}
}
$c->andCondition($conditions);
$count = $modx->getCount($className, $c);
$c->sortby($sortBy,$sortDir);
$c->limit($limit, $offset);
$items = $modx->getCollection($className,$c);
$idx = 0; //index
/* iterate through items */
$list = array();
foreach ($items as $item) {
$itemArray = $item->toArray();
$itemArray['idx'] = $idx;
$list[] = $yourpackage->getChunk($tpl,$itemArray);
$idx++;
}
/* output */
$output = implode($outputSeparator,$list);
//store cached output and count
$modx->cacheManager->set($cacheKeyPage, $output, $cacheLifetime, $cacheOptions);
$modx->cacheManager->set($cacheKeyCount, $count, $cacheLifetime, $cacheOptions);
}
//set total placeholder for getPage
$modx->setPlaceholder($totalVar, $count);
if(empty($output)) {
return '<p>No results. Try to limit your search.</p>';
}
/* if using a placeholder, no output and set output to specified placeholder */
$toPlaceholder = $modx->getOption('toPlaceholder',$scriptProperties,false);
if (!empty($toPlaceholder)) {
$modx->setPlaceholder($toPlaceholder,$output);
return '';
}
/* by default just return output */
return $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment