Created
February 11, 2012 16:13
-
-
Save Aurielle/1801560 to your computer and use it in GitHub Desktop.
Nekonečně hluboké zanoření - routy
This file contains 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 | |
$catFilterIn = function($slug, $params, $origParams) use($site) { | |
if(!isset($site->languages[$params['lang']])) | |
return NULL; | |
$lang = $site->languages[$params['lang']]; | |
$temp = explode('/', trim($slug, '/')); | |
$path = array(); | |
$slugs = array(); | |
$result = Category::findAll() | |
->where('[slug] IN %l', $temp) | |
->and('[language] = %i', $lang->id) | |
->orderBy('[lft]') | |
->fetchAll(); | |
// Ensure that no other categories with same slug as any child category are selected before the root level category | |
reset($result); | |
while(list($key, $val) = each($result)) { | |
if($temp[0] !== $val->slug) { | |
unset($result[$key]); | |
continue; | |
} | |
break; | |
} | |
// Limit the array to only count($temp) elements - no other will be needed | |
$result = array_slice($result, 0, count($temp)); | |
foreach($result as $val) { | |
$slugs[$val->slug] = $val; | |
} | |
foreach($temp as $part) { | |
// Match found | |
if(isset($slugs[$part])) { | |
$part2 = $slugs[$part]; | |
if($part2->parent_id === Category::ROOT) { | |
$path[] = $part2; | |
} | |
else { | |
$lastPart = end($path); | |
if($part2->parent_id === $lastPart->id) { | |
$path[] = $part2; | |
} | |
} | |
} else { | |
return NULL; | |
} | |
} | |
return end($path)->id; | |
}; | |
$catFilterOut = function($id, $params, $origParams) { | |
$ch = Avalon\Tools::getSiteCache(); | |
if(!isset($ch[Category::CACHE_KEY_ALL])) { | |
$cache = Category::fillCache(); | |
} else { | |
$cache = $ch[Category::CACHE_KEY_ALL]; | |
} | |
if($id === Category::ROOT) { // ROOT - no linking | |
return NULL; | |
} | |
$curId = $id; | |
$return = ''; | |
do { | |
if(!isset($cache[$curId])) { | |
return NULL; | |
} | |
$return = $cache[$curId]->slug . '/' . $return; | |
if($cache[$curId]->parent_id === Category::ROOT) // ROOT ID | |
break; | |
$curId = $cache[$curId]->parent_id; | |
} | |
while(TRUE); | |
$return = trim($return, '/'); | |
return $return; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment