diff --git a/app/Http/Livewire/Admin/Menu/MenuParent.php b/app/Http/Livewire/Admin/Menu/MenuParent.php index 58ba93b..bd2667b 100644 --- a/app/Http/Livewire/Admin/Menu/MenuParent.php +++ b/app/Http/Livewire/Admin/Menu/MenuParent.php @@ -19,7 +19,7 @@ class MenuParent extends Component public string $pageHeader = 'Menu'; public array $menu, $guards, $routes, $parentData; public ?int $rowID = null, $menuID = null; - public $menus, $menuRecord = null; + public $menuRecord = null; /* INITIALIZATION AT START */ public function mount() @@ -32,9 +32,9 @@ public function resetForm() $this->menu= $this->Repository(name: 'resetInputs'); $this->resetErrorBag(); $this->resetValidation(); - } + /* VALIDATION MESSAGES */ protected array $messages = [ 'menu.name.required' => 'Menu name is required.', @@ -45,9 +45,10 @@ public function resetForm() 'menu.guard.required' => 'Guard is required.', 'menu.guard.integer' => ':attribute must be integer.', 'menu.guard.gt' => ':attribute must be positive integer.', + 'menu.svg.unique' => ':attribute already exists', 'menu.route.required' => 'Select a route for this menu.', - 'menuItem.route.unique' => ':attribute already exists.', - 'menuItem.route.regex' => ':attribute must lower case. Allowed ' . ' only', + 'menu.route.unique' => ':attribute already exists.', + 'menu.route.regex' => ':attribute is invalid', 'menu.sort.required' => 'Give te order for this menu', 'menu.sort.integer' => ':attribute must be integer.', 'menu.sort.gt' => ':attribute must be positive integer.', @@ -60,7 +61,8 @@ protected function rules(): array 'menu.name' => 'required|min:4|unique:menus,name,' . $this->rowID, 'menu.menuID' => 'numeric|gt:0|nullable', 'menu.guard' => 'required|numeric|gt:0', - 'menu.route' => 'required|min:4|regex:/^[a-z,\.-]+$/|unique:menus,route,' . $this->rowID, +// 'menu.svg' => 'unique:menus,svg,' . $this->rowID, +// 'menu.route' => '|unique:menus,route,' . $this->rowID, 'menu.sort' => 'required|numeric|gt:0' ]; } @@ -70,7 +72,9 @@ protected function validationAttributes(): array { return [ 'menu.name' => $this->menu['name'], + 'menu.route' => 'Route', 'menu.menuID' => $this->menu['menuID'], + 'menu.svg' => $this->menu['svg'], ]; } @@ -156,7 +160,7 @@ public function render() if (count($this->guards) === 1) $this->menu['guard'] = array_key_first($this->guards); } - $this->menus = Menu::with('childMenus')->where('parent_id', '=', null)->get(); - return view('livewire.admin.menu.menu-parent', ['menus' => $this->menus]); + $menus = Menu::with('childMenus')->where('parent_id', '=', null)->orderBy('sort','ASC')->get(); + return view('livewire.admin.menu.menu-parent', ['menus' => $menus]); } } diff --git a/app/Http/Livewire/Layout/Sidebar.php b/app/Http/Livewire/Layout/Sidebar.php index 2f050f3..0923a82 100644 --- a/app/Http/Livewire/Layout/Sidebar.php +++ b/app/Http/Livewire/Layout/Sidebar.php @@ -10,15 +10,24 @@ class Sidebar extends Component { protected $listeners=['refreshSidebar'=>'$refresh']; +public array $routes=['hello']; - - public function mount() + public function getRoutes($menu):array { + $array=[]; + array_push($array,$menu->route); + foreach ($menu->childMenus as $childMenu1){ + array_push($array,$childMenu1->route); + foreach ($childMenu1->childMenus as $childMenu2){ + array_push($array,$childMenu2->route); + } + } + return $array; } public function render() { - $sideBar=Menu::with('childMenus')->where('parent_id',null)->get(); + $sideBar=Menu::with('childMenus')->where('parent_id',null)->orderBy('sort','ASC')->get(); return view('livewire.layout.sidebar', ['sideBar' => $sideBar]); } } diff --git a/app/Repositories/MenuRepository.php b/app/Repositories/MenuRepository.php index d6988b1..232ee67 100644 --- a/app/Repositories/MenuRepository.php +++ b/app/Repositories/MenuRepository.php @@ -13,8 +13,9 @@ class MenuRepository implements MenuRepositoryInterface use Data; /* RESET FORM FIELDS */ - public function resetInputs(): array{ - return ['name' => null,'svg' => null, 'menuID' => null, 'guard' => null, 'route' => null, 'sort' => null]; + public function resetInputs(): array + { + return ['name' => null, 'svg' => null, 'menuID' => null, 'guard' => null, 'route' => null, 'sort' => null]; } /* GET RECORD */ @@ -63,8 +64,8 @@ public function getParentData($type, $record, $level): array case 'update': return match ($level) { - 'l1' => $this->get_array_for_select_input($this->getRecordByField(['id', 'name'], 'parent_id', null)), - 'l2' => $this->get_second_array_for_select_input($this->getRecordByField(['id', 'name'], 'parent_id', null)), + 'l1' => $this->get_all_parent_array_for_select_input($this->getRecordByField(['id', 'name'], 'parent_id', null),$record->id), + 'l2' => $this->get_all_parent_array_for_select_input($this->getRecordByField(['id', 'name'], 'parent_id', null)), default => $this->get_array_for_select_input($this->getRecordByField(['id', 'name'], 'parent_id', null)), }; @@ -77,51 +78,66 @@ public function getParentData($type, $record, $level): array /* GET ROUTE DATA ARRAY */ public function getRoutes($type, $record): array { - return match ($type) { - 'create' => Data::get_routes_array_for_select_input(), - 'update' => Data::get_routes_array_for_select_input([$record->route]), - default => [], - }; + + switch ($type) { + case 'create': + $arr = Data::get_routes_array_for_select_input(); + return (array_merge(["none" => "None"], $arr)); + case 'update': + $arr = Data::get_routes_array_for_select_input([$record->route]); + return (array_merge(["none" => "None"], $arr)); + default: + return []; + + } +// $arr = Data::get_routes_array_for_select_input([$record->route]); +// $arr = (array_merge(["none" => "None"], $arr)); +// return match ($type) { +// 'create' => Data::get_routes_array_for_select_input(), +// 'update' => $arr, +// default => [], +// }; + } /* ASSIGN DATA FOR SUBMIT */ public function assignData($record, $formInputs): Menu { $record->name = ucfirst($formInputs['name']); - $record->svg = strtolower($formInputs['svg']); + $record->svg = ($formInputs['svg'] === '') ? null : strtolower($formInputs['svg']); $record->parent_id = $formInputs['menuID']; - $record->route = $formInputs['route']; + $record->route = ($formInputs['route'] === 'none') ? 'none' : $formInputs['route']; $record->guards_id = $formInputs['guard']; $record->sort = $formInputs['sort']; return $record; } /* SAVE DATA FOR SUBMIT */ - public function saveData($record,$formInputs):array + public function saveData($record, $formInputs): array { try { - $success= DB::transaction(function () use ($record,$formInputs) { - $this->assignData($record,$formInputs)->save(); - return [true, $record]; + $success = DB::transaction(function () use ($record, $formInputs) { + $this->assignData($record, $formInputs)->save(); + return [true, $record]; }); } catch (\Exception $e) { DB::rollback(); - return [false, $e->getMessage()]; + return [false, $e->getMessage()]; } - return $success; + return $success; } /* DELETE DATA FOR SUBMIT */ public function deleteData($record): array { try { - $success= DB::transaction(function () use ($record) { + $success = DB::transaction(function () use ($record) { $record->delete(); - return [true, $record]; + return [true, $record]; }); } catch (\Exception $e) { DB::rollback(); - return [false, $e->getMessage()]; + return [false, $e->getMessage()]; } return $success; } diff --git a/app/Traits/Data.php b/app/Traits/Data.php index 476b6b5..5a0975b 100644 --- a/app/Traits/Data.php +++ b/app/Traits/Data.php @@ -34,7 +34,22 @@ public function get_array_for_select_input($record): array { $array = []; foreach ($record as $data) { - $array[$data->id] = $data->name; + $array[$data->id] = $data->name.' (Parent)'; + } + return $array; + } + + /* GET ALL PARENTS FOR SELECT OPTIONS */ + public function get_all_parent_array_for_select_input($record,$exclude=null): array + { + $array = []; + foreach ($record as $data) { + $array[$data->id] = $data->name.' (Parent)'; + foreach ($data->childMenus as $childData) { + if($childData->id !== $exclude) { + $array[$childData->id] = $childData->name . ' (Child)'; + } + } } return $array; } diff --git a/database/migrations/2022_11_09_085026_create_menus_table.php b/database/migrations/2022_11_09_085026_create_menus_table.php index 521de03..7dfcbe9 100644 --- a/database/migrations/2022_11_09_085026_create_menus_table.php +++ b/database/migrations/2022_11_09_085026_create_menus_table.php @@ -17,8 +17,8 @@ public function up() $table->id(); $table->string('name')->unique(); $table->unsignedBigInteger('guards_id'); - $table->string('route')->unique(); - $table->string('svg')->unique()->nullable(); + $table->string('route')->nullable(); + $table->string('svg')->nullable(); $table->integer('sort'); $table->unsignedBigInteger('permissions_id')->nullable(); $table->unsignedBigInteger('parent_id')->nullable(); diff --git a/resources/views/components/svg/cog.blade.php b/resources/views/components/svg/cog.blade.php new file mode 100644 index 0000000..799fdb4 --- /dev/null +++ b/resources/views/components/svg/cog.blade.php @@ -0,0 +1,4 @@ +merge(['class' => 'flex-none']) }} xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"> + + + diff --git a/resources/views/components/svg/gear.blade.php b/resources/views/components/svg/gear.blade.php index b347f64..9003ced 100644 --- a/resources/views/components/svg/gear.blade.php +++ b/resources/views/components/svg/gear.blade.php @@ -1,3 +1,3 @@ -merge(['class' => 'flex-none']) }} xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6"> +merge(['class' => 'flex-none']) }} xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"> diff --git a/resources/views/components/svg/home.blade.php b/resources/views/components/svg/home.blade.php index 7a53887..1ef1151 100644 --- a/resources/views/components/svg/home.blade.php +++ b/resources/views/components/svg/home.blade.php @@ -1,4 +1,4 @@ -merge(['class' => 'flex-none']) }} xmlns="http://www.w3.org/2000/svg" width="16" height="16" +merge(['class' => 'flex-none']) }} xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 16 16"> diff --git a/resources/views/components/svg/select.blade.php b/resources/views/components/svg/select.blade.php index e323d33..87c4b9e 100644 --- a/resources/views/components/svg/select.blade.php +++ b/resources/views/components/svg/select.blade.php @@ -1,4 +1,4 @@ -@props(['type'=>null,'class'=>null]) +@props(['type'=>null]) @switch ($type) @case('add') merge() }}> @@ -12,6 +12,10 @@ merge() }}> @break + @case('cog') + merge() }}> + @break + @case('cross-circle') merge() }}> @break @@ -40,6 +44,10 @@ merge() }}> @break + @case('three-dots-horizontal') + merge() }}> + @break + @case('menu') merge() }}> @break @@ -64,7 +72,7 @@ merge() }} > @break - @case('user') + @case('users') merge() }} > @break diff --git a/resources/views/components/svg/three-dots-horizontal.blade.php b/resources/views/components/svg/three-dots-horizontal.blade.php new file mode 100644 index 0000000..5450637 --- /dev/null +++ b/resources/views/components/svg/three-dots-horizontal.blade.php @@ -0,0 +1,3 @@ +merge(['class' => 'flex-none']) }} xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"> + + diff --git a/resources/views/components/svg/three-dots-verticle.blade.php b/resources/views/components/svg/three-dots-verticle.blade.php new file mode 100644 index 0000000..53d0d4a --- /dev/null +++ b/resources/views/components/svg/three-dots-verticle.blade.php @@ -0,0 +1,3 @@ +merge(['class' => 'flex-none']) }} xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" > + + diff --git a/resources/views/livewire/admin/menu/menu-parent.blade.php b/resources/views/livewire/admin/menu/menu-parent.blade.php index 6b2da22..d7e2f21 100644 --- a/resources/views/livewire/admin/menu/menu-parent.blade.php +++ b/resources/views/livewire/admin/menu/menu-parent.blade.php @@ -18,7 +18,7 @@ class="!block w-auto dark:bg-green-700 dark:hover:bg-green-800 "> placeholder="Enter a name for menu "> + placeholder="Enter a icon (`home`, `users`, `menu` ... ) "> diff --git a/resources/views/livewire/layout/sidebar.blade.php b/resources/views/livewire/layout/sidebar.blade.php index 69a4bcc..a12d8ab 100644 --- a/resources/views/livewire/layout/sidebar.blade.php +++ b/resources/views/livewire/layout/sidebar.blade.php @@ -20,50 +20,53 @@ class="flex flex-col justify-center items-center px-6 xs:!h-[128px] lg:!h-[146px {{ __('Task Manager') }} - - @foreach($sideBar as $menu) -
  • - childMenus) > 0) "javascript:;" @else"{{route($menu->route)}}" @endif> - - {{$menu->name}} - @if(count($menu->childMenus) > 0) @endif - - - +
  • + @endforeach