CategoryController.php 3.72 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace App\Http\Controllers\Webprofile\Backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\Webprofile\CategoryRepository;
8 9
use App\Repositories\Webprofile\En\CategoryRepository as EnCategoryRepository;
use Statickidz\GoogleTranslate;
10 11 12 13

class CategoryController extends Controller
{
    private $repo;
14
    private $repoEn;
15

16 17 18 19 20 21 22
    private $SOURCE = 'id';
    private $TARGET = 'en';

    public function __construct(
        CategoryRepository $repo,
        EnCategoryRepository $repoEn
    ){
23
        $this->repo = $repo;
24
        $this->repoEn = $repoEn;
25 26 27 28 29 30 31 32 33
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
Aan Choesni Herlingga committed
34
            $data = $this->repo->get(['rEn']);
35
            return $this->repo->datatable($data);
36 37
        }

38
        return view('webprofile.backend.categories.index')->withTitle(trans('feature.category'));
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('webprofile.backend.categories.create')->withTitle(trans('feature.create_category'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
59 60 61 62
        $data = $request->except('_token');

        array_key_exists('is_active', $data) ? $data['is_active'] = 1 : $data['is_active'] = 0;

63 64 65
        $category = $this->repo->store($data);

        $this->createEn($data, $category);
66 67

        return redirect()->route('category.index');
68 69
    }

70 71 72 73 74 75 76 77 78 79 80
    private function createEn($data, $category)
    {
        $trans = new GoogleTranslate();
        $name = $trans->translate($this->SOURCE, $this->TARGET, $data['name']);

        $dataEn['category_id'] = $category->id;
        $dataEn['name'] = $name;

        $this->repoEn->store($dataEn);
    }

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
100 101 102 103 104 105
        $data = $this->repo->findId($id);

        $data = [
            'data' => $data,
        ];

106
        return view('webprofile.backend.categories.edit', $data)->withTitle(trans('feature.edit_category'));
107 108 109 110 111 112 113 114 115 116 117
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
118 119
        $data = $request->except(['_token', 'id', 'name_en']);
        $dataEn = $request->except(['_token', 'id']);
120 121 122

        array_key_exists('is_active', $data) ? $data['is_active'] = 1 : $data['is_active'] = 0;

123
        $category = $this->repo->findId($id, ['rEn']);
124 125
        $edit = $this->repo->update($data, $category);

126 127
        $this->updateEn($dataEn, $category);

128
        return redirect()->route('category.index');
129 130
    }

131 132 133 134
    public function updateEn($data, $category)
    {
        $dataEn['name'] = $data['name_en'];

135
        $this->repoEn->update($dataEn, $category->rEn);
136 137
    }

138 139 140 141 142 143 144 145
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
146 147 148 149
        $data = $this->repo->findId($id);
        $this->repo->destroy($data);

        return response()->json(['done']);
150 151
    }
}