2.0.4.2 Study addDropdown (ok)
Last updated
Last updated
C:\xampp82\htdocs\testvn\app\MenuBuilder\MenuBuilder.php
<?php
namespace App\MenuBuilder;
class MenuBuilder
{
private $menu = [];
private $dropdown = false;
private $dropdownDeep = 0;
private function addRegularLink($id, $name, $slug, $icon, $iconType, $sequence = 0)
{
$hasIcon = ($icon === false || strlen($icon) === 0) ? false : true;
if ($hasIcon) {
array_push($this->menu, [
'id' => $id,
'target' => 'link',
'name' => $name,
'slug' => $slug,
'hasIcon' => $hasIcon,
'iconType' => $iconType,
'sequence' => $sequence
]);
} else {
array_push($this->menu, [
'id' => $id,
'target' => 'link',
'name' => $name,
'slug' => $slug,
'hasIcon' => $hasIcon,
'iconType' => $iconType,
'sequence' => $sequence
]);
}
}
public function addLink($id, $name, $slug, $icon = false, $iconType = 'coreui', $sequence = 0)
{
if ($this->dropdown) {
$this->addDropdownLink($id, $name, $slug, $icon, $iconType, $sequence);
} else {
$this->addRegularLink($id, $name, $slug, $icon, $iconType, $sequence);
}
}
public function addTitle($id, $name, $icon = false, $iconType = 'coreui', $sequence = 0)
{
$hasIcon = ($icon === false || strlen($icon) === 0) ? false : true;
if ($hasIcon) {
array_push($this->menu, [
'id' => $id,
'target' => 'title',
'name' => $name,
'hasIcon' => $hasIcon,
'iconType' => $iconType,
'sequence' => $sequence
]);
} else {
array_push($this->menu, [
'id' => $id,
'target' => 'title',
'name' => $name,
'hasIcon' => $hasIcon,
'iconType' => $iconType,
'sequence' => $sequence
]);
}
}
public function beginDropdown($id, $name, $icon, $iconType = 'coreui', $sequence = 0)
{
$hasIcon = ($icon === false || strlen($icon) === 0) ? false : true;
$this->dropdown = true;
$this->dropdownDeep++;
if ($this->dropdownDeep === 1) {
if ($hasIcon) {
array_push($this->menu, array(
'id' => $id,
'name' => $name,
'target' => "dropdown",
'hasIcon' => $hasIcon,
'icon' => $icon,
'iconType' => $iconType,
'elements' => array(),
'sequence' => $sequence,
));
} else {
array_push($this->menu, array(
'id' => $id,
'target' => "dropdown",
'name' => $name,
'hasIcon' => $hasIcon,
'elements' => array(),
'sequence' => $sequence,
));
}
} else {
if ($hasIcon) {
$this->addElementToMenuLastPostion(array(
'id' => $id,
'slug' => 'dropdown',
'name' => $name,
'hasIcon' => $hasIcon,
'icon' => $icon,
'iconType' => $iconType,
'elements' => array(),
'sequence' => $sequence,
), 1);
} else {
$this->addElementToMenuLastPostion(array(
'id' => $id,
'slug' => 'dropdown',
'name' => $name,
'hasIcon' => $hasIcon,
'elements' => array(),
'sequence' => $sequence,
), 1);
}
}
}
public function endDropdown()
{
$this->dropdownDeep--;
if ($this->dropdownDeep < 0) {
$this->dropdownDeep = 0;
}
if ($this->dropdownDeep == 0) {
$this->dropdown = false;
}
}
private function innerAddElementToMenuLastPostion(&$menu, $element, $offset)
{
$z = 1;
$result = false;
$menu = &$menu[count($menu) - 1];
while (is_array($menu)) {
if ($z == $this->dropdownDeep - $offset) {
array_push($menu['elements'], $element);
$result = true;
break;
}
$z++;
}
return $result;
}
private function addElementToMenuLastPostion($element, $offset = 0)
{
$this->innerAddElementToMenuLastPostion($this->menu, $element, $offset);
}
public function addDropdownLink($id, $name, $slug, $icon, $iconType, $sequence = 0)
{
$hasIcon = ($icon == false || strlen($icon) === 0) ? false : true;
if ($hasIcon) {
$this->addElementToMenuLastPostion([
"id" => $id,
"name" => $name,
"slug" => $slug,
"target" => "link",
"icon" => $icon,
"iconType" => $iconType,
"hasIcon" => $hasIcon,
"sequence" => $sequence
]);
} else {
$this->addElementToMenuLastPostion([
"id" => $id,
"name" => $name,
"slug" => $slug,
"target" => "link",
"hasIcon" => $hasIcon,
"sequence" => $sequence
]);
}
}
public function getResult()
{
return $this->menu;
}
}
C:\xampp82\htdocs\testvn\app\MenuBuilder\RenderFromDatabaseData.php
<?php
namespace App\MenuBuilder;
class RenderFromDatabaseData
{
private $mb;
private $data;
public function __construct()
{
$this->mb = new MenuBuilder();
}
private function addLink($data)
{
if ($data['parent_id'] === NULL) {
$this->mb->addLink($data["id"], $data['name'], $data['slug'], false, 'coreui', $data['sequence']);
}
}
private function addTitle($data)
{
$this->mb->addTitle($data['id'], $data['name'], false, 'coreui', $data['sequence']);
}
private function loopMain()
{
for ($i = 0; $i < count($this->data); $i++) {
switch ($this->data[$i]['target']) {
case 'link':
$this->addLink($this->data[$i]);
break;
case 'text':
$this->addTitle($this->data[$i]);
break;
case 'dropdown':
if ($this->data[$i]['parent_id'] == NULL) {
$this->addDropdown($this->data[$i]);
}
break;
}
}
}
private function dropdownLoop($id)
{
for ($i = 0; $i < count($this->data); $i++) {
if ($this->data[$i]['parent_id'] == $id) {
if ($this->data[$i]['target'] === 'link') {
$this->mb->addLink($this->data[$i]['id'], $this->data[$i]['name'], $this->data[$i]['slug'], $this->data[$i]['icon'], 'coreui', $this->data[$i]['sequence']);
} else {
$this->addTitle($this->data[$i]);
}
}
}
}
private function addDropdown($data)
{
$this->mb->beginDropdown($data['id'], $data['name'], false, 'coreui', $data['sequence']);
$this->dropdownLoop($data['id']);
$this->mb->endDropdown();
}
public function render($data)
{
if (!empty($data)) {
$this->data = $data;
$this->loopMain();
}
return $this->mb->getResult();
}
}