<?php namespace App\Http\Controllers;

		use Session;
		use Request;
		use DB;
		use CRUDBooster;

		class ApiGetNavigasiController extends \crocodicstudio\crudbooster\controllers\ApiController {

		    function __construct() {    
				$this->table       = "navigasi";        
				$this->permalink   = "get_navigasi";    
				$this->method_type = "get";    
		    }
		

		    public function hook_before(&$postdata) {
		        //This method will be execute before run the main process

		    }

		    public function hook_query(&$query) {
		        //This method is to customize the sql query
                $query->where('parent_id',NULL)->orderBy('id','ASC');
		    }

		    public function hook_after($postdata,&$result) {
		        //This method will be execute after run the main process
                if($result['api_status']==1){
                    foreach($result['data'] as $data){
                        if($data->id==6){
                           $data->child = DB::table('tipe_dokumen')->where('parent_id','<>',0)->orderBy('id','ASC')->get();
                           foreach($data->child as $data2){
                                $data2->caption = $data2->nama;
                                $data2->url = "/jdihdprd/kategori/".$data2->id;
                                $data2->child =[];
                           }
                           
                        }else{
                           $data->child = DB::table('navigasi')->where('parent_id',$data->id)->orderBy('id','ASC')->get();  
                        }
                        if($data->id!=6){
                            foreach($data->child as $data1){
                                $data1->child = DB::table('navigasi')->where('parent_id',$data1->id)->get();
                            }
                        }
                    }
                }
		    }

		}