Created
February 24, 2014 17:32
-
-
Save xamedow/9192857 to your computer and use it in GitHub Desktop.
Simple menu class
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 | |
class menu | |
{ | |
private $data_arr = array(); | |
private $page_name = ''; | |
private $default_module = ''; | |
/** | |
* @param array $data_arr | |
* @param string $page_name | |
* @param string $default_module | |
*/ | |
public function __construct($data_arr, $page_name, $default_module = 'content') | |
{ | |
$this->data_arr = $data_arr; | |
$this->page_name = $page_name; | |
$this->default_module = $default_module; | |
} | |
/** | |
* @param string $redirect | |
* @param string $anchor | |
* @param string $module | |
* @return string | |
*/ | |
protected function get_href($redirect, $anchor, $module = 'content') | |
{ | |
return ($redirect) ? $redirect . '/' : "/$module/$anchor.html"; | |
} | |
/** | |
* @return string | |
*/ | |
public function getDefaultModule() | |
{ | |
return $this->default_module; | |
} | |
/** | |
* @return array | |
*/ | |
protected function getDataArr() | |
{ | |
return $this->data_arr; | |
} | |
/** | |
* @return string | |
*/ | |
protected function getPageName() | |
{ | |
return $this->page_name; | |
} | |
/** | |
* @return bool|string | |
*/ | |
public function get_li() | |
{ | |
$data_arr = $this->data_arr; | |
if (is_array($data_arr)) { | |
$list_items = ''; | |
foreach ($data_arr as $row) { | |
if (is_array($row) && !empty($row['anchor'])) { | |
if ($row['anchor'] === $this->getPageName()) { | |
$list_items .= "<li><span>{$row['name']}</span></li>"; | |
} else { | |
$redirect = (isset($row['redirect'])) ? $row['redirect'] : ''; | |
$href = $this->get_href($redirect, $row['anchor']); | |
$list_items .= "<li><a href=\"$href\">{$row['name']}</a></li>"; | |
} | |
} else { | |
continue; | |
} | |
} | |
return $list_items; | |
} | |
return false; | |
} | |
} | |
class nestedMenu extends menu | |
{ | |
public function get_li($nest_level = 0, $module = '') | |
{ | |
if(!$module) { | |
$module = parent::getDefaultModule(); | |
} | |
$data_arr = parent::getDataArr(); | |
if (is_array($data_arr)) { | |
$list_items = ''; | |
foreach ($data_arr as $row) { | |
if ( | |
is_array($row) && | |
( !empty($row['anchor']) || !empty($row['redirect']) ) && | |
$row['parent'] == $nest_level | |
) { | |
if ($row['anchor'] === parent::getPageName()) { | |
$list_items .= "<li><span>{$row['name']}</span></li>"; | |
} else { | |
$redirect = (!empty($row['redirect'])) ? $row['redirect'] : ''; | |
$href = parent::get_href($redirect, $row['anchor'], $module); | |
$parent = ($row['parent']) ? $row['parent'] : 0; | |
$id = ($row['id']); | |
$nested_ul = ''; | |
if (!$parent) { | |
$nested_ul = $this->get_li($id, $redirect); | |
if ($nested_ul) { | |
$nested_ul = "<ul class='nested'>$nested_ul</ul>"; | |
} | |
} | |
$list_items .= "<li><a href=\"$href\">{$row['name']}</a>$nested_ul</li>"; | |
} | |
} else { | |
continue; | |
} | |
} | |
return $list_items; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment