Last active
December 19, 2015 10:49
-
-
Save motin/5943228 to your computer and use it in GitHub Desktop.
Helper files for a Yii module for app navigation
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 | |
// views/default/dashboard.php | |
$this->breadcrumbs = array( | |
'Home' => SiteMap::homeNav(), | |
'Dashboard', | |
); |
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 | |
// components/SiteMap.php | |
class SiteMap | |
{ | |
static private $uiModulePrefix = '/nav'; | |
static public $map; | |
/** | |
* Defines what menu items are returned | |
* @var type | |
*/ | |
static public $currentProductPhase = 1; | |
/** | |
* | |
* @param type $screen If specified, only the level 2 items adhering to this screen will be returned | |
* @return type | |
*/ | |
static public function menu($screen = null) | |
{ | |
$map = & self::getMap(); | |
$menu = array(); | |
if (!empty($screen)) | |
{ | |
foreach ($map as $k => $menuGroup) | |
{ | |
if ($menuGroup['screen'] == $screen && $menuGroup['phase'] <= self::$currentProductPhase) | |
{ | |
foreach ($menuGroup["items"] as $k2 => $menuitem) | |
{ | |
if ($menuitem['screen'] == $screen && $menuitem['phase'] <= self::$currentProductPhase) | |
$menu[] = $menuitem; | |
} | |
break; | |
} | |
} | |
} else | |
{ | |
$menu = & $map; | |
} | |
return $menu; | |
} | |
static public function screensMenu($screens, $labels = array(), $get = array()) | |
{ | |
if (!empty($labels)) | |
$screensAndLabels = array_combine($screens, $labels); | |
$map = & self::getMap($get); | |
$menu = array(); | |
foreach ($map as $menuGroup) | |
{ | |
if (in_array($menuGroup['screen'], $screens) && $menuGroup['level'] == 1 && $menuGroup['phase'] <= self::$currentProductPhase) | |
{ | |
if (!empty($screensAndLabels)) | |
$menuGroup["label"] = $screensAndLabels[$menuGroup['screen']]; | |
$submenu = array(); | |
foreach ($menuGroup["items"] as $k2 => $menuitem) | |
{ | |
if ($menuitem['phase'] <= self::$currentProductPhase) | |
$submenu[] = $menuitem; | |
} | |
$menuGroup["items"] = $submenu; | |
$menu[$menuGroup['screen']] = $menuGroup; | |
} | |
} | |
// Arrange in correct order | |
$orderedMenu = array(); | |
foreach ($screens as $screen) | |
{ | |
$orderedMenu[] = $menu[$screen]; | |
} | |
return $orderedMenu; | |
} | |
static public function getMap($get = array()) | |
{ | |
U::inspection(__METHOD__, func_get_args()); | |
if (empty(self::$map) || !empty($get)) | |
{ | |
if (empty($get)) | |
$get = $_GET; | |
$map = array( | |
array('screen' => 'default', 'phase' => 1, 'level' => 1, 'label' => 'Home', 'url' => $def = SiteMap::homeDashboard(), 'items' => array( | |
array('screen' => 'default', 'phase' => 1, 'level' => 2, 'label' => 'Dashboard', 'url' => $def), | |
array('screen' => 'default', 'phase' => 1, 'level' => 2, 'label' => 'Overview', 'url' => SiteMap::homeOverview()), | |
)), | |
array('screen' => 'explore', 'phase' => 1, 'level' => 1, 'label' => 'Explore', 'url' => $def = SiteMap::exploreExplorer(), 'items' => array( | |
array('screen' => 'explore', 'phase' => 1, 'level' => 2, 'label' => 'Explorer', 'url' => $def), | |
array('screen' => 'explore', 'phase' => 1, 'level' => 2, 'label' => 'Objects', 'url' => SiteMap::exploreObjects()), | |
)), | |
array('screen' => 'import', 'phase' => 1, 'level' => 1, 'label' => 'Import', 'url' => $def = SiteMap::importScheduler(), 'items' => array( | |
array('screen' => 'import', 'phase' => 1, 'level' => 2, 'label' => 'Scheduler', 'url' => $def), | |
array('screen' => 'import', 'phase' => 1, 'level' => 2, 'label' => 'Manual import', 'url' => SiteMap::importManualImport()), | |
)), | |
array('screen' => 'export', 'phase' => 1, 'level' => 1, 'label' => 'Export', 'url' => $def = SiteMap::exportScheduler(), 'items' => array( | |
array('screen' => 'export', 'phase' => 1, 'level' => 2, 'label' => 'Scheduler', 'url' => $def), | |
array('screen' => 'export', 'phase' => 1, 'level' => 2, 'label' => 'Manual export', 'url' => SiteMap::exportManualexport()), | |
)), | |
/* Some pages may be included purely to help with initial development and/or QA routines / functional testing | |
array('screen' => 'error', 'phase' => 1, 'level' => 1, 'label' => 'Error pages', 'items' => array( | |
array('screen' => 'error', 'phase' => 1, 'level' => 2, 'label' => '403 - Not authorized', 'url' => array('/site/page', 'view' => 'error-403')), | |
array('screen' => 'error', 'phase' => 1, 'level' => 2, 'label' => '404 - Page not found', 'url' => array('/site/page', 'view' => 'error-404')), | |
array('screen' => 'error', 'phase' => 1, 'level' => 2, 'label' => '500 - General error', 'url' => array('/site/page', 'view' => 'error-500')), | |
array('screen' => 'error', 'phase' => 1, 'level' => 2, 'label' => '503 - Server Overload', 'url' => array('/site/page', 'view' => 'error-503')), | |
)), | |
*/ | |
); | |
self::$map = $map; | |
} | |
return self::$map; | |
} | |
static public function url($params) | |
{ | |
$first = array_shift($params); | |
return Yii::app()->createUrl($first, $params); | |
} | |
static public function absoluteUrl($params) | |
{ | |
$first = array_shift($params); | |
return Yii::app()->createAbsoluteUrl($first, $params); | |
} | |
static public function home($page) | |
{ | |
$params = array(); | |
array_unshift($params, self::$uiModulePrefix . '/default/' . $page); | |
return $params; | |
} | |
static public function homeNav() | |
{ | |
return self::home($page = "index"); | |
} | |
static public function homeDashboard() | |
{ | |
return self::home($page = "dashboard"); | |
} | |
static public function homeOverview() | |
{ | |
return self::home($page = "overview"); | |
} | |
static public function explore($page) | |
{ | |
$params = array(); | |
array_unshift($params, self::$uiModulePrefix . '/explore/' . $page); | |
return $params; | |
} | |
static public function exploreNav() | |
{ | |
return self::explore($page = "index"); | |
} | |
static public function exploreExplorer() | |
{ | |
return self::explore($page = "explorer"); | |
} | |
static public function exploreObjects() | |
{ | |
return self::explore($page = "objects"); | |
} | |
static public function import($page) | |
{ | |
$params = array(); | |
array_unshift($params, self::$uiModulePrefix . '/import/' . $page); | |
return $params; | |
} | |
static public function importNav() | |
{ | |
return self::import($page = "index"); | |
} | |
static public function importScheduler() | |
{ | |
return self::import($page = "scheduler"); | |
} | |
static public function importManualImport() | |
{ | |
return self::import($page = "manual"); | |
} | |
static public function export($page) | |
{ | |
$params = array(); | |
array_unshift($params, self::$uiModulePrefix . '/export/' . $page); | |
return $params; | |
} | |
static public function exportNav() | |
{ | |
return self::export($page = "index"); | |
} | |
static public function exportScheduler() | |
{ | |
return self::export($page = "scheduler"); | |
} | |
static public function exportManualexport() | |
{ | |
return self::export($page = "manual"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment