Skip to content

Instantly share code, notes, and snippets.

@ig0r74
Created August 18, 2016 09:36
Show Gist options
  • Save ig0r74/4bf003c23ac4a79de09ed2fe54815413 to your computer and use it in GitHub Desktop.
Save ig0r74/4bf003c23ac4a79de09ed2fe54815413 to your computer and use it in GitHub Desktop.
Предыдущая и следующая новость в Smarty
{$params = [
'currid' => {$modx->resource->id}
]}
{processor action="web/prevnext/getdata" params=$params ns=modxsite assign=result}
{$pn=$result.object}
<div class="row">
<div class="col-md-4 text-center">
{if $pn.prev}
<a href="{$pn.prev.uri}">&larr; Предыдущая новость</a>
{/if}
</div>
<div class="col-md-4 text-center"><a href="[[~5]]">Читать все новости &uarr;</a></div>
<div class="col-md-4 text-center">
{if $pn.next}
<a href="{$pn.next.uri}">Следующая новость &rarr;</a>
{/if}
</div>
</div>
<?php
require_once __DIR__ . '/../resources/getdata.class.php';
class modWebPrevNextGetdataProcessor extends modWebResourcesGetdataProcessor{
public function initialize(){
$this->setDefaultProperties(array(
'includeTVs' => false,
'sort' => "{$this->classKey}.publishedon",
'dir' => 'desc',
'showhidden' => true,
'showunpublished' => false,
'limit' => 0,
'parent' => 5,
# 'currid' => null,
));
return parent::initialize();
}
public function afterIteration(array $list){
$items = parent::afterIteration($list);
$items=array_values($list);
$prev=-1;
$next=0;
$curr=$this->getProperty('currid');
for($i=0;$i<count($items);$i++){
// print $items[$i]['id']."\n";
if($items[$i]['id']==$curr){
$prev=$i-1;
$next=$i+1;
break;
}
}
$prev=($prev>=0)?$items[$prev]:null;
$next=($next>0 && $next<count($items))?$items[$next]:null;
return ['prev'=>$prev,'next'=>$next];
}
}
return 'modWebPrevNextGetdataProcessor';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment