Compare commits

..
10 Commits
Author SHA1 Message Date
Artur Welp 0cf40d4462 Atualizar 'composer.json' 2023-02-07 13:24:46 +00:00
ahwelp 277c9d4201 Adding defaults to methods 2023-01-24 13:35:51 +00:00
ahwelp 25d673f84d Update 'composer.json' 2019-04-05 23:49:28 +00:00
ahwelp 8edb1efb78 Update 'index.php' 2019-04-05 23:11:35 +00:00
ahwelp 24edef32ce Change name 2019-04-05 20:10:34 -03:00
ahwelp 16dc9dd2cb Name migration 2019-03-14 11:07:28 -03:00
Artur a8051dda4d Now, if a submenu is empty, it will not be rendered 2019-01-31 23:53:19 -02:00
Artur 8cb8e98154 Release 2.0 2019-01-25 02:21:35 -02:00
ahwelp 9b78ca484c Update 'src/MenuBuilder/HtmlBuilder.php' 2018-08-14 22:49:31 +00:00
ahwelp df2ea4f5db Removing IDE files 2018-08-14 19:36:55 -03:00
11 changed files with 259 additions and 216 deletions
-8
View File
@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
-5
View File
@@ -1,5 +0,0 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="PROJECT_PROFILE" />
</settings>
</component>
-6
View File
@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>
-8
View File
@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/MenuBuilder.iml" filepath="$PROJECT_DIR$/.idea/MenuBuilder.iml" />
</modules>
</component>
</project>
-10
View File
@@ -1,10 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="NodePackageJsonFileManager">
<packageJsonPaths />
</component>
<component name="PropertiesComponent">
<property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
<property name="nodejs_npm_path_reset_for_default_project" value="true" />
</component>
</project>
Regular → Executable
+5 -6
View File
@@ -1,17 +1,16 @@
{ {
"name" : "ahwelp/menubuilder", "name" : "inforsistemas/menus",
"description" : "Different Menus", "description" : "Different Menus",
"type" : "library", "type" : "library",
"authors" : [ "authors" : [
{ {
"name" : "ahwelp", "name" : "Artur Welp",
"email" : "ahwelp@ahwelp.com" "email" : "ahwelp@universo.univates.br"
} }
], ],
"autoload" : { "autoload" : {
"psr-4" : { "psr-4" : {
"MenuBuilder\\" : "src/MenuBuilder/" "Menus\\" : "src/Menus/"
}
} }
},
"require" : { }
} }
Executable
+23
View File
@@ -0,0 +1,23 @@
<?php
use Menus\Menu as Menu;
use Menus\MenuItem as MenuItem;
include_once './src/Menus/HtmlBuilder.php';
include_once './src/Menus/Menu.php';
include_once './src/Menus/MenuItem.php';
$sidebar = new Menu( array('id' => 'side-menu', 'class' => "nav") );
$sidebar->create_submenu('basico', 'Basico', '', Array(), 2);
$sidebar->add_on_new('basico', '', 'Usuarios');
$sidebar->create_submenu('administrativo', 'Administrativo');
$sidebar->add_on_new('basico', '', 'Geografico', '', Array(), -1);
$sidebar->create_submenu('teste', 'Teste');
//$sidebar->add_on_new('teste', '', 'Elemento');
echo $sidebar->render();
-146
View File
@@ -1,146 +0,0 @@
<?php
namespace MenuBuilder;
class MenuItem {
/**
* HTML builder instance.
*
* @var Menu\HtmlBuilder
*/
protected $html;
/**
* Menu item title.
*
* @var string
*/
protected $title;
/**
* Menu item link.
*
* @var string
*/
protected $link;
/**
* Nested menu.
*
* @var Menu\Menu
*/
protected $nested;
/**
* Array of attributes.
*
* @var array
*/
protected $attributes = [];
/**
* Create a new menu item instance.
*
*/
public $icon = '';
public $weight = 0;
public function __construct()
{
$this->html = new HtmlBuilder;
}
/**
* Set the menu item link.
*
* @param string $link
* @return Menu\MenuItem
*/
public function link($link)
{
$this->link = $link;
return $this;
}
/**
* Set the menu item title.
*
* @param string $link
* @return Menu\MenuItem
*/
public function title($title)
{
$this->title = $title;
return $this;
}
public function weight($weight = 0){
$this->weight = $weight;
return $this;
}
public function icon($icon = ''){
$this->icon = $icon;
return $this;
}
/**
* Set the menu item attributes.
*
* @param array $attributes
* @return Menu\MenuItem
*/
public function attributes(array $attributes)
{
$this->attributes = $attributes;
return $this;
}
/**
* Nest another menu.
*
* @param Menu\Menu $menu
* @return Menu\MenuItem
*/
public function nest(Menu $menu)
{
$this->nested = $menu;
return $this;
}
/**
* Render the menu item.
*
* @return string
*/
public function render()
{
return '<li>'.$this->html->link($this->link, $this->title, $this->icon, $this->attributes).$this->renderNestedMenu().'</li>';
}
/**
* Render the nested menu.
*
* @return string
*/
public function renderNestedMenu()
{
if ($this->nested)
{
return $this->nested->render();
}
}
/**
* Render the menu item.
*
* @return string
*/
public function __toString()
{
return $this->render();
}
public static function newMenu($link = '', $title = '', $icon = '' ){
$menu = new MenuItem();
$menu->link($link);
$menu->title($title);
$menu->icon = $icon;
return $menu;
}
}
+2
View File
@@ -1,5 +1,7 @@
<?php <?php
namespace Menus;
class HtmlBuilder { class HtmlBuilder {
/** /**
* Create an HTML link. * Create an HTML link.
+39 -17
View File
@@ -1,6 +1,6 @@
<?php <?php
namespace MenuBuilder; namespace Menus;
class Menu { class Menu {
@@ -25,7 +25,7 @@ class Menu {
*/ */
protected $items = []; protected $items = [];
public function items(){ public function items() {
return $this->items; return $this->items;
} }
@@ -40,11 +40,46 @@ class Menu {
$this->html = new HtmlBuilder; $this->html = new HtmlBuilder;
} }
public function addDirect($menu){ public function addDirect($menu) {
$this->items[] = $menu; $this->items[] = $menu;
return $this; return $this;
} }
public function create_submenu($name, $title, $icon = '', $attributes = [], $weight = 0) {
$menu_item = false;
foreach ($this->items as $item) {
if ($item->name == $name) {
$menu_item = $item;
break;
}
}
if (!$menu_item) {
$menu = new MenuItem();
$menu = $menu->title($title)->icon($icon)->attributes($attributes)->weight($weight)->name($name)->link('#');
$menu = $menu->nest(new Menu(Array('class' => 'nav nav-ident')));
$menu_item = $menu->nested();
$this->items[] = $menu;
} else {
if (!$title == null) {
$menu_item->title($title)->icon($icon)->attributes($attributes)->weight($weight)->name($name);
$menu_item = $menu_item->nested();
}else{
$menu_item = $menu_item->nested();
}
}
return $menu_item;
}
public function add_on($name, MenuItem $item) {
$this->create_submenu($name)->items[] = $item;
}
public function add_on_new($name, $link = '#', $title, $icon = '', $attributes = [], $weight = 0) {
$menu_item = $this->create_submenu($name, null);
$menu = new MenuItem();
$menu_item->items[] = $menu->link($link)->title($title)->icon($icon)->attributes($attributes)->weight($weight)->name($name);
}
/** /**
* Add a menu item. * Add a menu item.
* *
@@ -94,7 +129,7 @@ class Menu {
return implode(PHP_EOL, $menu); return implode(PHP_EOL, $menu);
} }
public function renderRaw(){ public function renderRaw() {
$menu[] = ""; $menu[] = "";
foreach ($this->items as $item) { foreach ($this->items as $item) {
$menu[] = $item->renderRaw(); $menu[] = $item->renderRaw();
@@ -113,19 +148,6 @@ class Menu {
}); });
} }
/*private function merge(){
$new_array = Array();
foreach($items as $key => $item){
foreach($items as $item_){
if( $item->title == $item_->title ){
}
}
}
}*/
/** /**
* Render the menu. * Render the menu.
* *
+180
View File
@@ -0,0 +1,180 @@
<?php
namespace Menus;
class MenuItem {
/**
* HTML builder instance.
*
* @var Menu\HtmlBuilder
*/
protected $html;
/**
* Menu item title.
*
* @var string
*/
protected $title;
/**
* Menu item link.
*
* @var string
*/
protected $link;
/**
* Menu item name.
*
* @var string
*/
public $name;
/**
* Nested menu.
*
* @var Menu\Menu
*/
protected $nested;
/**
* Array of attributes.
*
* @var array
*/
protected $attributes = [];
/**
* Create a new menu item instance.
*
*/
public $icon = '';
public $weight = 0;
public function __construct() {
$this->html = new HtmlBuilder;
}
/**
* Set the menu item link.
*
* @param string $link
* @return Menu\MenuItem
*/
public function link($link) {
$this->link = $link;
return $this;
}
/**
* Set the menu item name.
*
* @param string $name
* @return Menu\MenuItem
*/
public function name($name) {
$this->name = $name;
return $this;
}
/**
* Set the menu item title.
*
* @param string $link
* @return Menu\MenuItem
*/
public function title($title) {
$this->title = $title;
return $this;
}
public function weight($weight = 0) {
$this->weight = $weight;
return $this;
}
public function icon($icon = '') {
$this->icon = $icon;
return $this;
}
/**
* Set the menu item attributes.
*
* @param array $attributes
* @return Menu\MenuItem
*/
public function attributes(array $attributes) {
$this->attributes = $attributes;
return $this;
}
/**
* Nest another menu.
*
* @param Menu\Menu $menu
* @return Menu\MenuItem
*/
public function nest(Menu $menu) {
$this->nested = $menu;
return $this;
}
/**
* Nest another menu.
*
* @param Menu\Menu $menu
* @return Menu\MenuItem
*/
public function nested() {
return $this->nested;
}
/**
* Render the menu item.
*
* @return string
*/
public function render() {
if( empty($this->nested) ){
return '<li>' . $this->html->link($this->link, $this->title, $this->icon, $this->attributes) . '</li>';
}
if ( !empty($this->nested->items()) ) {
return '<li>' . $this->html->link($this->link, $this->title, $this->icon, $this->attributes) . $this->renderNestedMenu() . '</li>';
}
}
/**
* Render the nested menu.
*
* @return string
*/
public function renderNestedMenu() {
if ($this->nested) {
return $this->nested->render();
}
}
/**
* Render the menu item.
*
* @return string
*/
public function __toString() {
return $this->render();
}
public static function newMenu($link = '', $title = '', $icon = '') {
$menu = new MenuItem();
$menu->link($link);
$menu->title($title);
$menu->icon = $icon;
return $menu;
}
}