Skip to content
This repository has been archived by the owner on Sep 28, 2020. It is now read-only.

Commit

Permalink
Ordenation to the menus pages
Browse files Browse the repository at this point in the history
  • Loading branch information
marcobraghim committed Nov 14, 2018
1 parent eb31bef commit 3576f45
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions plugins/MenusPages.php
Expand Up @@ -63,6 +63,12 @@ public function preRun() {
}
}

foreach ($menus as $menuName => $menuItems) {
$newItems = $menuItems;
uksort($newItems, array('MenusPages', 'sortArray'));
$menus[$menuName] = $newItems;
}

$this->addViewVar('pages', $pages);
$this->addViewVar('menus', $menus);

Expand Down Expand Up @@ -141,6 +147,42 @@ private function renderSubLayout(array $route) {

$this->addViewVar('content', $content);
}

/**
* Make the ordenation trying to compare with created date in the URL.
*
* /blog/yyyy-mm-dd/something.html <-- Ordenation BY DATE OK
* /blog/yy-mm-dd/something.html <-- Ordenation BY DATE OK
* /blog/dd-mm-yyyy/something.html <-- Ordenation BY DATE FALSE
* /blog/dd-mm-yy/something.html <-- Ordenation BY DATE FALSE
* /blog/yyyy-mm-dd-something.html <-- Ordenation BY DATE FALSE
*
* @param string $a
* @param string $b
* @return int
*/
public static function sortArray($a, $b) {
if ($a == $b) { return 0; }

$tmspA = $a;
$itemsA = explode('/', $a);
foreach ($itemsA as $aItem) {
if (strtotime($aItem) !== false) {
$tmspA = strtotime($aItem);
}
}

$tmspB = $b;
$itemsB = explode('/', $b);
foreach ($itemsB as $bItem) {
if (strtotime($bItem) !== false) {
$tmspB = strtotime($bItem);
}
}

return ($tmspA<$tmspB);
}

}


Expand Down

0 comments on commit 3576f45

Please sign in to comment.