CategoryController.php 3.85 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
    private $SOURCE = 'id';
    private $TARGET = 'en';

    public function __construct(
        CategoryRepository $repo,
        EnCategoryRepository $repoEn
22
    ) {
23
        $this->repo = $repo;
24
        $this->repoEn = $repoEn;
25
    }
26

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

37
            return $this->repo->datatable($data);
38 39
        }

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

    /**
     * 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.
     *
56 57
     * @param \Illuminate\Http\Request $request
     *
58 59 60 61
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
62 63 64 65
        $data = $request->except('_token');

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

66 67 68
        $category = $this->repo->store($data);

        $this->createEn($data, $category);
69 70

        return redirect()->route('category.index');
71 72
    }

73 74 75 76 77 78 79 80 81 82 83
    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);
    }

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

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

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

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

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

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

128
        $category = $this->repo->findId($id, ['rEn']);
129 130
        $edit = $this->repo->update($data, $category);

131 132
        $this->updateEn($dataEn, $category);

133
        return redirect()->route('category.index');
134 135
    }

136 137 138 139
    public function updateEn($data, $category)
    {
        $dataEn['name'] = $data['name_en'];

140
        $this->repoEn->update($dataEn, $category->rEn);
141 142
    }

143 144 145
    /**
     * Remove the specified resource from storage.
     *
146 147
     * @param int $id
     *
148 149 150 151
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
152
        $data = $this->repo->findId($id, ['rEn']);
153 154
        $this->repo->destroy($data);

155 156 157 158
        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

159
        return response()->json(['done']);
160 161
    }
}