Commit a97be435 by Aan Choesni Herlingga

optmized query backend menu

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