Commit a97be435 by Aan Choesni Herlingga

optmized query backend menu

parent 72ab1c1a
......@@ -2,8 +2,6 @@
namespace App\Helpers;
use App\Models\Webprofile\Menu;
class InseoHelper
{
public static function tglbulanindo2($waktu, $tipe = '')
......@@ -18,13 +16,13 @@ class InseoHelper
switch ($tipe) {
case 1:
$full = $tgl . ' ' . $bulan[(int) $bln - 1] . ' ' . $thn;
$full = $tgl.' '.$bulan[(int) $bln - 1].' '.$thn;
break;
case 2:
$full = $tgl . '/' . $bln . '/' . $thn;
$full = $tgl.'/'.$bln.'/'.$thn;
break;
default:
$full = "$tgl " . $bulan[(int) $bln - 1] . " $thn";
$full = "$tgl ".$bulan[(int) $bln - 1]." $thn";
}
return $full;
......@@ -36,22 +34,8 @@ class InseoHelper
$bln = substr($waktu, 5, 2);
$thn = substr($waktu, 0, 4);
$full = $tgl . ' - ' . $bln . ' - ' . $thn;
$full = $tgl.' - '.$bln.' - '.$thn;
return $full;
}
public static function maxmenu($lvl, $parentid)
{
$hnewmenu = Menu::where('parent', $parentid)->where('level', $lvl)->max('urutan');
return $hnewmenu;
}
public static function jumchild($lvl, $parentid)
{
$hnewmenu = Menu::where('parent', $parentid)->where('level', $lvl)->count();
return $hnewmenu;
}
}
......@@ -41,24 +41,9 @@ class MenuController extends Controller
public function build_menu()
{
$data = Menu::select('id', 'parent', 'name', 'url', 'level', 'urutan')->where('level', '1')->orderby('urutan', 'asc')->get();
$menu = [];
$data = Menu::select('id', 'parent', 'name', 'url', 'level', 'urutan')->where('level', '1')->orderby('urutan', 'asc')->with('rEn', 'rChild')->get()->toArray();
$i = 0;
foreach ($data as $item) {
$menu[$i]['id'] = $item->id;
$menu[$i]['parent'] = $item->parent;
$menu[$i]['name'] = $item->name;
$menu[$i]['url'] = $item->url;
$menu[$i]['level'] = $item->level;
$menu[$i]['urutan'] = $item->urutan;
if ($this->menu_has_child($item->parent)) {
$menu[$i]['child'] = $this->menu_get_child($item->id);
}
++$i;
}
return $menu;
return $data;
}
public function menu_has_child($parentid)
......@@ -202,10 +187,14 @@ class MenuController extends Controller
}
}
private function createEn($data, $menu)
private function createEn($data, $menu, $translate = 1)
{
if ($translate == 1) {
$trans = new GoogleTranslate();
$name = $trans->translate($this->SOURCE, $this->TARGET, $data['name']);
} else {
$name = $data['name'];
}
$dataEn['menu_id'] = $menu->id;
$dataEn['name'] = $name;
......@@ -236,9 +225,9 @@ class MenuController extends Controller
$urutan = Menu::where('level', $level)->where('parent', $request->input('parentpage'))->max('urutan');
$page = Pages::where('id', $request->input('page'))->first();
$page = Pages::with(['rEn'])->where('id', $request->input('page'))->first();
$data['name'] = $page->title;
$data['name'] = $page->rEn->title;
$data['level'] = $level;
$data['parentlevel'] = $parentlevel;
$data['urutan'] = $urutan + 1;
......@@ -250,7 +239,7 @@ class MenuController extends Controller
$save = Menu::create($data);
$this->createEn($data, $save);
$this->createEn($data, $save, 'notTranslate');
Alert::success('Data berhasil disimpan')->persistent('Ok');
......
......@@ -29,4 +29,9 @@ class Menu extends Model
{
return $this->hasOne(EnMenu::class, 'menu_id', 'id');
}
public function rChild()
{
return $this->hasMany(Menu::class, 'parent', 'id')->orderBy('urutan', 'asc')->with(['rChild', 'rEn']);
}
}
......@@ -120,16 +120,15 @@
<td>
<a href="{!! url((string)$value['url']) !!}" target="_blank">{!! $value['name'] !!}</a>
</td>
{{-- <td style="text-align: center;">{!! $value['level'] !!}</td> --}}
<td style="text-align:center; vertical-align: middle;">
@if(InseoHelper::jumchild(1, $value['parent']) != 1)
@if(count($value['r_child']) != 1)
@if($value['urutan'] == 1)
<a href="{{ route('menu_down', ['data'=>Crypt::encrypt($value['id'])]) }}" class="btn btn-warning btn-xs">
<i class="fa fa-arrow-down"></i>
</a>
@endif
@if($value['urutan'] > 1)
@if($value['urutan'] == InseoHelper::maxmenu(1, $value['parent']))
@if($value['urutan'] == count($arr))
<a href="{{ route('menu_up', ['data'=>Crypt::encrypt($value['id'])]) }}" class="btn btn-warning btn-xs">
<i class="fa fa-arrow-up"></i>
</a>
......@@ -152,22 +151,22 @@
{{ Form::close() }}
</td>
</tr>
@if (!empty($value['child']))
@foreach ($value['child'] as $ckey => $val)
@if(!empty($value['r_child']))
@foreach ($value['r_child'] as $ckey => $val)
<tr>
<td>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="{!! url((string)$val['url']) !!}">{!! $val['name'] !!}</a>
</td>
{{-- <td style="text-align: center;">{!! $val['level'] !!}</td> --}}
<td style="text-align:center; vertical-align: middle;">
@if(InseoHelper::jumchild(2, $val['parent']) != 1)
@if(count($value['r_child']) != 1)
@if($val['urutan'] == 1)
<a href="{{ route('menu_down', ['data'=>Crypt::encrypt($val['id'])]) }}" class="btn btn-warning btn-xs">
<i class="fa fa-arrow-down"></i>
</a>
@endif
@if($val['urutan'] > 1)
@if($val['urutan'] == InseoHelper::maxmenu(2, $val['parent']))
@if($val['urutan'] == count($value['r_child']))
<a href="{{ route('menu_up', ['data'=>Crypt::encrypt($val['id'])]) }}" class="btn btn-warning btn-xs">
<i class="fa fa-arrow-up"></i>
</a>
......@@ -175,7 +174,7 @@
<a href="{{ route('menu_up', ['data'=>Crypt::encrypt($val['id'])]) }}" class="btn btn-warning btn-xs">
<i class="fa fa-arrow-up"></i>
</a>
@if($ckey+1 < count($value['child']))
@if($ckey+1 < count($value['r_child']))
<a href="{{ route('menu_down', ['data'=>Crypt::encrypt($val['id'])]) }}" class="btn btn-warning btn-xs">
<i class="fa fa-arrow-down"></i>
</a>
......@@ -190,22 +189,22 @@
{{ Form::close() }}
</td>
</tr>
@if (!empty($val['child']))
@foreach ($val['child'] as $ckey2 => $val2)
@if (!empty($val['r_child']))
@foreach ($val['r_child'] as $ckey2 => $val2)
<tr>
<td>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="{!! url((string)$val2['url']) !!}">{!! $val2['name'] !!}</a>
</td>
{{-- <td style="text-align: center;">{!! $val2['level'] !!}</td> --}}
<td style="text-align:center; vertical-align: middle;">
@if(InseoHelper::jumchild(3, $val2['parent']) != 1)
@if(count($val['r_child']) != 1)
@if($val2['urutan'] == 1)
<a href="{{ route('menu_down', ['data'=>Crypt::encrypt($val2['id'])]) }}" class="btn btn-warning btn-xs">
<i class="fa fa-arrow-down"></i>
</a>
@endif
@if($val2['urutan'] > 1)
@if($val2['urutan'] == InseoHelper::maxmenu(3, $val2['parent']))
@if($val2['urutan'] == count($val['r_child']))
<a href="{{ route('menu_up', ['data'=>Crypt::encrypt($val2['id'])]) }}" class="btn btn-warning btn-xs">
<i class="fa fa-arrow-up"></i>
</a>
......@@ -213,7 +212,7 @@
<a href="{{ route('menu_up', ['data'=>Crypt::encrypt($val2['id'])]) }}" class="btn btn-warning btn-xs">
<i class="fa fa-arrow-up"></i>
</a>
@if($ckey2+1 < count($val['child']))
@if($ckey2+1 < count($val['r_child']))
<a href="{{ route('menu_down', ['data'=>Crypt::encrypt($val2['id'])]) }}" class="btn btn-warning btn-xs">
<i class="fa fa-arrow-down"></i>
</a>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment