Created
July 26, 2010 14:26
-
-
Save Stanton/490603 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getNeighbours($moblog, $limit = 3, $order = 'Moblog.modified') { | |
// check for existence of order param in session, and use it if so | |
if ($this->Session->check('Moblog.order')) { | |
$order = $this->Session->read('Moblog.order'); | |
} | |
// modify the find statements based on the order param | |
switch ($order) { | |
case 'Moblog.id': | |
$prevConditions = array('Moblog.id < ' => $moblog['Moblog']['id']); | |
$nextConditions = array('Moblog.id > ' => $moblog['Moblog']['id']); | |
$prevOrder = 'Moblog.id DESC'; | |
$nextOrder = 'Moblog.id ASC'; | |
break; | |
case 'Moblog.modified': | |
$prevConditions = array('Moblog.modified < ' => $moblog['Moblog']['modified']); | |
$nextConditions = array('Moblog.modified > ' => $moblog['Moblog']['modified']); | |
$prevOrder = 'Moblog.modified DESC'; | |
$nextOrder = 'Moblog.modified DESC'; | |
break; | |
case 'moblog_comment_count': | |
$prevConditions = array( | |
'moblog_comment_count <= ' => $moblog['Moblog']['moblog_comment_count'], | |
'Moblog.id != ' . $moblog['Moblog']['id'] | |
); | |
$nextConditions = array( | |
'moblog_comment_count >= ' => $moblog['Moblog']['moblog_comment_count'], | |
'Moblog.id != ' . $moblog['Moblog']['id'] | |
); | |
$prevOrder = 'moblog_comment_count DESC'; | |
$nextOrder = 'moblog_comment_count ASC'; | |
break; | |
} | |
// find the neighbors and stick them in an array | |
$neighbours = array(); | |
$neighbours['prev'] = $this->Moblog->find('all', | |
array( | |
'conditions' => $prevConditions, | |
'limit' => $limit, | |
'order' => $prevOrder, | |
'recursive' => -1 | |
) | |
); | |
$neighbours['next'] = $this->Moblog->find('all', | |
array( | |
'conditions' => $nextConditions, | |
'limit' => $limit, | |
'order' => $nextOrder, | |
'recursive' => -1 | |
) | |
); | |
if ($order === 'moblog_comment_count') { | |
$neighbours['next'] = array_reverse($neighbours['next']); | |
} | |
return $neighbours; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment