public function getContent($textquery, $parameters = '', &$pager = [], $whereparameters = [])
{
// Start the 'stopwatch' for the profiler.
$this->app['stopwatch']->start('bolt.getcontent', 'doctrine');
// $whereparameters is passed if called from a compiled template. If present, merge it with $parameters.
if (!empty($whereparameters)) {
$parameters = array_merge((array) $parameters, (array) $whereparameters);
}
$logNotFound = false;
if (isset($parameters['log_not_found'])) {
$logNotFound = $parameters['log_not_found'];
unset($parameters['log_not_found']);
}
// Decode this textquery
$decoded = $this->decodeContentQuery($textquery, $parameters);
if ($decoded === false) {
$this->app['logger.system']->error("Not a valid query: '{$textquery}'", ['event' => 'storage']);
$this->app['stopwatch']->stop('bolt.getcontent');
return false;
}
// Run table checks
if (!$this->runContentTypeTableChecks($decoded['contenttypes'])) {
$this->app['stopwatch']->stop('bolt.getcontent');
return false;
}
// Run the actual queries
list($results, $totalResults) = call_user_func($decoded['queries_callback'], $decoded, $parameters);
// Perform post hydration ordering
if ($decoded['order_callback'] !== false) {
if (is_scalar($decoded['order_callback']) && $decoded['order_callback'] == 'RANDOM') {
shuffle($results);
} else {
uasort($results, $decoded['order_callback']);
}
}
// Perform pagination if necessary, but never paginate when 'returnsingle' is used.
$offset = 0;
$limit = false;
if ($decoded['self_paginated'] === false && isset($decoded['parameters']['page']) && !$decoded['return_single']) {
$offset = ($decoded['parameters']['page'] - 1) * $decoded['parameters']['limit'];
$limit = $decoded['parameters']['limit'];
}
if ($limit !== false) {
$results = array_slice($results, $offset, $limit);
}
// Return content
if ($decoded['return_single']) {
if (count($results) > 0) {
$this->app['stopwatch']->stop('bolt.getcontent');
return reset($results);
}
if ($logNotFound) {
$msg = sprintf("Requested specific query '%s', not found.", $textquery);
$this->app['logger.system']->error($msg, ['event' => 'storage']);
}
$this->app['stopwatch']->stop('bolt.getcontent');
return false;
}
// Set up the $pager array with relevant values, but only if we requested paging.
if (isset($decoded['parameters']['paging'])) {
$pagerName = implode('_', $decoded['contenttypes']);
/** @var \Bolt\Pager\PagerManager $manager */
$manager = $this->app['pager'];
$pager = $manager->createPager($pagerName)->setCount($totalResults)->setTotalpages(ceil($totalResults / $decoded['parameters']['limit']))->setCurrent($decoded['parameters']['page'])->setShowingFrom(($decoded['parameters']['page'] - 1) * $decoded['parameters']['limit'] + 1)->setShowingTo(($decoded['parameters']['page'] - 1) * $decoded['parameters']['limit'] + count($results));
$this->app['twig']->addGlobal('pager', $pager);
}
$this->app['stopwatch']->stop('bolt.getcontent');
return $results;
}