Skip to content
Snippets Groups Projects
Forked from Joshua Bezaleel Abednego / templatePPL2
36 commits ahead of the upstream repository.
TpsController.php 3.23 KiB
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Tps;
use App\User;
use App\Http\Requests;
use Illuminate\Support\Facades\Input;

class TpsController extends Controller
{
    protected $user_roles = [];

    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('tps', ['except' => [
            'store',
            'create',
        ]]);
        $this->middleware('tps.admin', ['only' => [
            'store',
            'create',
        ]]);

        if(Auth::user())
        $this->user_roles = Auth::user()->roles()->get()->toArray();
    }

    public function has_role($role_name)
    {
        foreach ($this->user_roles as $role) 
        {
            if ($role['name'] == $role_name)
            {
                return true;
            }
        }

        return false;
    }
    
    public function index()
    {
        $arr = [];
        if ($this->has_role('all_tps'))
        {
            $all = Tps::all();
        } 
        else 
        {
            $all = Tps::where('id_manager', Auth::user()->id)->get();
        }
        \Event::fire(new \App\Events\TableAccessed($all));
        foreach ($all as $tps) {
            $tmp = [];
            $tmp = $tps;
            $tmp->managerName = $tps->manager()->get(['name'])[0]->name;
            array_push($arr,$tmp);
        }
        return view('menu.tps', ['data' => $arr]);
    }

    public function store()
    {
        $tps = new Tps;
        $tps->capacity_now = Input::get('capacity_now', 0);
        $tps->capacity_full = Input::get('capacity_full', 0);
        $tps->nama = Input::get('name','');
        $tps->is_full = Input::get('is_full', 0);
        $tps->id_manager = Input::get('idPengelola', 0);
        $tps->created_by = Auth::user()->id;
        $tps->id = 0;

        $tps->latitude = Input::get('latitude', 0);
        $tps->longitude = Input::get('longitude', 0);

        $tps->save();
        //$tps = Tps::create(Input::all());
        return redirect('/tps');
    }

    public function create()
    {
        // view create doang
        return view('menu.insertTps', [
            'dataManager' => User::all(['id', 'name'])
        ]);
    }

    public function show($id)
    {
        // viewnya pake get
        return Tps::find($id);
    }

    public function edit($id)
    {
        // viewnya pake form edit
        return view('menu.editTps', [
            'id' => $id, 
            'data' => Tps::find($id),
            'dataManager' => User::all(['id', 'name'])
        ]);
    }

    public function update($id)
    {
        $tps = Tps::find($id);
        $tps->capacity_now = Input::get('capacity_now', 0);
        $tps->capacity_full = Input::get('capacity_full', 0);
        $tps->nama = Input::get('name', '');
        $tps->is_full = Input::get('is_full', 0);
        $tps->id_manager = Input::get('idPengelola', 0);
        
        $tps->latitude = Input::get('latitude', 0);
        $tps->longitude = Input::get('longitude', 0);

        $tps->save();
        \Event::fire(new \App\Events\TableModified($tps));
        return redirect('/tps');
    }

    public function destroy($id)
    {
        Tps::find($id)->delete();
        return redirect('/tps');
    }
}