Skip to content

Commit

Permalink
Merge pull request #406 from OpenSID/pengaturan-menu-presisi
Browse files Browse the repository at this point in the history
pengaturan menu presisi
  • Loading branch information
vickyrolanda authored Oct 29, 2024
2 parents be4ba51 + f94d361 commit 8d679d5
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 44 deletions.
112 changes: 112 additions & 0 deletions app/Helpers/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use App\Models\Config;
use App\Models\SettingAplikasi;
use App\Models\Bantuan;
use App\Models\Enums\SasaranEnum;
use Carbon\Carbon;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
Expand Down Expand Up @@ -269,6 +271,39 @@ function generateMenu($tree, $parentId = null)
}
}

if (! function_exists('generateMenuPresisi')) {
function generateMenuPresisi($tree, $parentId = null)
{
$result = '';
foreach ($tree as $item) {
// Mengambil ikon jika ada, dan membuat tag HTML ikon
$icon = isset($item->icon) ? "<i class='{$item->icon}'></i> " : '';

if ($item->children->count() > 0) {
$result .= "<div class='parent-dropdown-menu dropdown bg-white pl-3 pr-2'>
<button class='parent-menu btn bg-white p-2 dropdown-toggle text-muted rounded-0' type='button' id='{$item->id}Dropdown' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>
{$icon}{$item->text}
</button>
<div class='dropdown-menu' aria-labelledby='{$item->id}Dropdown'>";
$result .= generateMenuPresisi($item->children, $item->id);
$result .= '
</div>
</div>';
} else {
if ($parentId) {
$result .= "<a class='item-menu dropdown-item' href='{$item->href}'>{$icon}{$item->text}</a>";
} else {
$result .= "<a type='button' class='item-menu btn bg-white p-2 text-muted' href='{$item->href}'>{$icon}{$item->text}</a>";
}
}
}

return $result;
}
}



if (! function_exists('angka_lokal')) {
/**
* Menampilkan nilai persentase.
Expand Down Expand Up @@ -724,3 +759,80 @@ function persen3($number, $total, $precision = 2)

return round(($number / $total) * 100, $precision).'%';
}

if (! function_exists('getStatistikLabel')) {
/**
* Mendapatkan label statistik berdasarkan kode laporan.
*
* @param mixed $lap
* @param mixed $stat
* @param mixed $namaDesa
*
* @return array
*/
function getStatistikLabel($lap, $stat, $namaDesa)
{
$akhiran = ' di ' . ucwords(setting('sebutan_desa') . ' ' . $namaDesa) . ', ' . date('Y');
$kategori = 'Penduduk';
$label = 'Jumlah dan Persentase Penduduk Berdasarkan ' . $stat . $akhiran;

if ((int) $lap > 50) {
// Untuk program bantuan, $lap berbentuk '50<program_id>'
$program_id = substr($lap, 2);
$program = Bantuan::select(['nama', 'sasaran'])->find($program_id)->toArray();
$program['judul_sasaran'] = SasaranEnum::valueOf($program['sasaran']);
$kategori = 'Bantuan';
$label = 'Jumlah dan Persentase Peserta ' . $program['nama'] . $akhiran;
} elseif ((int) $lap > 20 || $lap === 'kelas_sosial') {
$kategori = 'Keluarga';
$label = 'Jumlah dan Persentase Keluarga Berdasarkan ' . $stat . $akhiran;
} else {
switch ($lap) {
case 'bantuan_keluarga':
$kategori = 'Bantuan';
$label = 'Jumlah dan Persentase ' . $stat . $akhiran;
break;

case 'bdt':
$kategori = 'RTM';
$label = 'Jumlah dan Persentase Rumah Tangga Berdasarkan ' . $stat . $akhiran;
break;

case '1':
$label = 'Jumlah dan Persentase Penduduk Berdasarkan Aktivitas atau Jenis Pekerjaannya ' . $akhiran;
break;

case '0':
case '14':
$label = 'Jumlah dan Persentase Penduduk Berdasarkan ' . $stat . ' yang Dicatat dalam Kartu Keluarga ' . $akhiran;
break;

case '13':
case '15':
$label = 'Jumlah dan Persentase Penduduk Menurut Kelompok ' . $stat . $akhiran;
break;

case '16':
$label = 'Jumlah dan Persentase Penduduk Menurut Penggunaan Alat Keluarga Berencana dan Jenis Kelamin ' . $akhiran;
break;

case '13':
$label = 'Jumlah Keluarga dan Penduduk Berdasarkan Wilayah RT ' . $akhiran;
break;

case '4':
$label = 'Jumlah Penduduk yang Memiliki Hak Suara ' . $stat . $akhiran;
break;

case 'hamil':
$label = 'Jumlah dan Persentase Penduduk Perempuan Berdasarkan ' . $stat . $akhiran;
break;
}
}

return [
'kategori' => $kategori,
'label' => $label,
];
}
}
17 changes: 15 additions & 2 deletions app/Http/Controllers/CMS/MenuController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public function __construct(MenuRepository $menuRepo)
*/
public function index()
{
$type = 1;
if (request('type') != null){
$type = request('type');
}
$sourceItem = [
'Halaman' => Page::activePublished()->get()->pluck('title', 'link')->toArray(),
'Kategori' => Category::all()->pluck('name', 'link')->toArray(),
Expand Down Expand Up @@ -82,11 +86,20 @@ public function store(UpdateMenuRequest $request)
{
$input = $request->all();

$menu_type = $input["menu_type"] == null ? 1 : $input["menu_type"];

$menu = $this->menuRepository->create($input);

Session::flash('success', 'Menu berhasil disimpan.');

return redirect(route('menus.index'));
if ($menu_type == 1){
return redirect(route('menus.index'));
}
else if ($menu_type == 2){
return redirect(route('menus.index') . "?type=" .$menu_type);
}
else{
return redirect(route('menus.index'));
}
}

protected function getOptionItems($id = null)
Expand Down
43 changes: 31 additions & 12 deletions app/Http/Repository/CMS/MenuRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
class MenuRepository extends BaseRepository
{
protected $fieldSearchable = [
'icon',
'menu_type',
'name',
'url',
'sequence',
Expand All @@ -29,22 +31,35 @@ public function model(): string

public function treeJson(): string
{
$menus = $this->tree()->toArray();
$type = 1;
if (request('type') != null){
$type = request('type');
}
$menus = $this->tree($type)->toArray();

$this->removeEmptyChildren($menus);

return json_encode($menus);
}

public function tree(): Collection|null
public function tree($menu_type): Collection|null
{
return $this->model->selectRaw("id, parent_id , name as text, url as href, 'fas fa-list' as icon")
->whereNull('parent_id')
if($menu_type == null){
$menu_type = request('type');
}
$menus = $this->model->selectRaw("id, parent_id , name as text, url as href, icon");
if($menu_type != null){
$menus = $menus->where('menu_type', $menu_type);
}
else{
$menus = $menus->where('menu_type', 1);
}
$menus = $menus->whereNull('parent_id')
->with(['children' => function ($q) {
$q->selectRaw("id, parent_id , name as text, url as href, 'fas fa-list' as icon");
$q->selectRaw("id, parent_id , name as text, url as href, icon");
}])
->orderBy('sequence')
->get();
->orderBy('sequence');
return $menus->get();
}

private function removeEmptyChildren(&$array)
Expand All @@ -63,30 +78,34 @@ private function removeEmptyChildren(&$array)
*/
public function create(array $input): Model
{
$this->model->whereNotNull('id')->delete();
$menu_type = $input['menu_type'] != null ? $input['menu_type'] : 1;

$this->model->where('menu_type', $menu_type)->whereNotNull('id')->delete();

try {
// hapus data lama lalu buat lagi
$json = json_decode($input['json_menu'], 1);

return $this->loopTree($json);
return $this->loopTree($json, $menu_type);
} catch (\Throwable $th) {
throw $th;
}
}

private function loopTree(array $elements, $parentId = null)
private function loopTree(array $elements, $menu_type, $parentId = null)
{
$sequence = 1;
foreach ($elements as $element) {
$input = [
'menu_type' => $menu_type,
'name' => $element['text'],
'url' => $element['href'],
'icon' => $element['icon'],
'sequence' => $sequence,
'parent_id' => $parentId,
];
$model = parent::create($input);
if (isset($element['children'])) {
$this->loopTree($element['children'], $model->id);
$this->loopTree($element['children'], $menu_type, $model->id);
}
$sequence++;
}
Expand Down
3 changes: 3 additions & 0 deletions app/Models/CMS/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Menu extends Model
public $table = 'menus';

public $fillable = [
'icon',
'menu_type',
'name',
'url',
'sequence',
Expand All @@ -26,6 +28,7 @@ class Menu extends Model
public static array $rules = [
'name' => 'required|string|max:255',
'url' => 'nullable|url|max:255',
'icon' => 'nullable|url|max:255',
'text' => 'nullable|string|max:255',
'href' => 'nullable|url|max:255',
'sequence' => 'nullable',
Expand Down
3 changes: 2 additions & 1 deletion catatan_rilis.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Di rilis v2410.0.0 berisi penambahan fitur dan perbaikan lain sesuai dengan pela
1. [#362](https://github.com/OpenSID/OpenKab/issues/362) Penambahan halaman statistik untuk stunting pada halaman public.
2. [#372](https://github.com/OpenSID/OpenKab/issues/372) Penyesuaian tampilan baru dasbor presisi.
3. [#385](https://github.com/OpenSID/OpenKab/issues/385) Penambahan fungsi group kabupaten pada API dan tampilkan pada filter wilayah sehingga kabupaten yang ada dapat di pilih.
4. [#380](https://github.com/OpenSID/OpenKab/issues/380) Tambahkan pengaturan menu statistik.
4. [#380](https://github.com/OpenSID/OpenKab/issues/380) Penambahan pengaturan menu statistik.
5. [#398](https://github.com/OpenSID/OpenKab/issues/398) Penambahan pengaturan menu presisi.

#### Perbaikan BUG

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('menus', function ($table) {
// $table->integer('menu_type')->default(1)->change();
$table->string('menu_type')->default(1)->after('id');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('menus', function($table) {
$table->dropColumn('menu_type');
});
}
};
32 changes: 32 additions & 0 deletions database/migrations/2024_10_28_081758_icon-menu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('menus', function ($table) {
$table->string('icon')->nullable()->after('name');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('menus', function($table) {
$table->dropColumn('icon');
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// Insert data baru tanpa tag <i> di kolom name
DB::table("menus")->insert([
["id" => "192", "menu_type" => "2", "name" => "Demografi", "icon" => "fa-solid fa-chart-column", "url" => "/presisi", "sequence" => "2", "position" => "top", "parent_id" => NULL, "created_at" => "2023-09-27 17:39:33"],
["id" => "193", "menu_type" => "2", "name" => "Sosial", "icon" => "fas fa-table", "url" => "/presisi/bantuan", "sequence" => "3", "position" => "top", "parent_id" => NULL, "created_at" => "2023-09-27 17:39:33"],
["id" => "194", "menu_type" => "2", "name" => "Data Kemiskinan", "icon" => NULL, "url" => "/presisi/sosial", "sequence" => "1", "position" => "top", "parent_id" => "193", "created_at" => "2023-09-27 17:39:33"],
["id" => "195", "menu_type" => "2", "name" => "Program Bantuan", "icon" => NULL, "url" => "/presisi/bantuan", "sequence" => "2", "position" => "top", "parent_id" => "193", "created_at" => "2023-09-27 17:39:33"],
["id" => "196", "menu_type" => "2", "name" => "Kependudukan", "icon" => "fas fa-book", "url" => "/presisi/kependudukan", "sequence" => "4", "position" => "top", "parent_id" => NULL, "created_at" => "2023-09-27 17:39:33"],
["id" => "197", "menu_type" => "2", "name" => "Statistik Penduduk", "icon" => NULL, "url" => "/presisi/kependudukan", "sequence" => "1", "position" => "top", "parent_id" => "196", "created_at" => "2023-09-27 17:39:33"],
["id" => "198", "menu_type" => "2", "name" => "Statistik Keluarga", "icon" => NULL, "url" => "#", "sequence" => "2", "position" => "top", "parent_id" => "196", "created_at" => "2023-09-27 17:39:33"],
["id" => "199", "menu_type" => "2", "name" => "Statistik RTM", "icon" => NULL, "url" => "#", "sequence" => "3", "position" => "top", "parent_id" => "196", "created_at" => "2023-09-27 17:39:33"],
["id" => "200", "menu_type" => "2", "name" => "Ekonomi", "icon" => "fa fa-solid fa-briefcase", "url" => "/presisi/ekonomi", "sequence" => "5", "position" => "top", "parent_id" => NULL, "created_at" => "2023-09-27 17:39:33"],
["id" => "201", "menu_type" => "2", "name" => "E-Stunting", "icon" => "fa fa-solid fa-camera-retro pl-1", "url" => "/presisi/kesehatan", "sequence" => "6", "position" => "top", "parent_id" => NULL, "created_at" => "2023-09-27 17:39:33"],
["id" => "202", "menu_type" => "2", "name" => "Geo Spasial", "icon" => "fas fa-map", "url" => "#", "sequence" => "7", "position" => "top", "parent_id" => NULL, "created_at" => "2023-09-27 17:39:33"],
]);
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{

// Menghapus data berdasarkan ID untuk rollback
DB::table("menus")->whereIn('id', [192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202])->delete();
}
};
Loading

0 comments on commit 8d679d5

Please sign in to comment.