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

namespace App\Http\Controllers\Webprofile\Backend;

use App\Http\Controllers\Controller;
use App\Repositories\Webprofile\CategoryRepository;
7
use App\Repositories\Webprofile\De\CategoryRepository as DeCategoryRepository;
8
use App\Repositories\Webprofile\En\CategoryRepository as EnCategoryRepository;
9
use App\Repositories\Webprofile\Sa\CategoryRepository as SaCategoryRepository;
10
use App\Repositories\Webprofile\Zh\CategoryRepository as ZhCategoryRepository;
11
use Illuminate\Http\Request;
12
use Statickidz\GoogleTranslate;
13 14 15 16

class CategoryController extends Controller
{
    private $repo;
17
    private $repoEn;
18
    private $repoDe;
19
    private $repoSa;
20
    private $repoZh;
21

22 23
    private $SOURCE = 'id';
    private $TARGET = 'en';
24
    private $TARGETDE = 'de';
25
    private $TARGETSA = 'ar';
26
    private $TARGETZH = 'zh';
27 28 29

    public function __construct(
        CategoryRepository $repo,
30
        EnCategoryRepository $repoEn,
31
        DeCategoryRepository $repoDe,
32 33
        SaCategoryRepository $repoSa,
        ZhCategoryRepository $repoZh
34
    ) {
35
        $this->repo = $repo;
36
        $this->repoEn = $repoEn;
37
        $this->repoDe = $repoDe;
38
        $this->repoSa = $repoSa;
39
        $this->repoZh = $repoZh;
40
    }
41

42 43 44 45 46 47 48 49
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
Aan Choesni Herlingga committed
50
            $data = $this->repo->get(['rEn']);
51

52
            return $this->repo->datatable($data);
53 54
        }

55
        return view('webprofile.backend.categories.index')->withTitle(trans('feature.category'));
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    }

    /**
     * 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.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
75 76 77 78
        $data = $request->except('_token');

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

79 80 81
        $category = $this->repo->store($data);

        $this->createEn($data, $category);
82
        $this->createDe($data, $category);
83
        $this->createSa($data, $category);
84
        $this->createZh($data, $category);
85 86

        return redirect()->route('category.index');
87 88
    }

89 90 91 92 93 94 95 96 97 98 99
    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);
    }

100 101 102 103 104 105 106 107 108 109 110
    private function createDe($data, $category)
    {
        $trans = new GoogleTranslate();
        $name = $trans->translate($this->SOURCE, $this->TARGETDE, $data['name']);

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

        $this->repoDe->store($dataDe);
    }

111 112 113 114 115 116 117 118 119 120 121
    private function createSa($data, $category)
    {
        $trans = new GoogleTranslate();
        $name = $trans->translate($this->SOURCE, $this->TARGETSA, $data['name']);

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

        $this->repoSa->store($dataSa);
    }

122 123 124 125 126 127 128 129 130 131 132
    private function createZh($data, $category)
    {
        $trans = new GoogleTranslate();
        $name = $trans->translate($this->SOURCE, $this->TARGETZH, $data['name']);

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

        $this->repoZh->store($dataZh);
    }

133 134 135
    /**
     * Display the specified resource.
     *
136 137
     * @param int $id
     *
138 139 140 141 142 143 144 145 146
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    }

    /**
     * Show the form for editing the specified resource.
     *
147 148
     * @param int $id
     *
149 150 151 152
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
153
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
154 155 156 157 158

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

159
        return view('webprofile.backend.categories.edit', $data)->withTitle(trans('feature.edit_category'));
160 161 162 163 164
    }

    /**
     * Update the specified resource in storage.
     *
165
     * @param int $id
166
     *
167 168 169 170
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
171
        $data = $request->except(['_token', 'id', 'name_en', 'name_de', 'name_sa', 'name_zh']);
172 173
        $dataEn = $request->except(['_token', 'id', 'name_de']);
        $dataDe = $request->except(['_token', 'id', 'name', 'name_en']);
174 175 176

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

177
        $category = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
178 179
        $edit = $this->repo->update($data, $category);

180
        $this->updateEn($dataEn, $category);
181
        $this->updateDe($dataDe, $category);
182
        $this->updateSa($dataEn, $category);
183
        $this->updateZh($dataEn, $category);
184

185
        return redirect()->route('category.index');
186 187
    }

188 189 190 191
    public function updateEn($data, $category)
    {
        $dataEn['name'] = $data['name_en'];

192 193 194 195 196 197 198 199
        if ($category->rEn) {
            $this->repoDe->update($dataEn, $category->rEn);
        } else {
            $dataEn['category_id'] = $category->id;
            $dataEn['name'] = $data['name_en'];

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

202 203 204 205
    public function updateDe($data, $category)
    {
        $dataDe['name'] = $data['name_de'];

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
        if ($category->rDe) {
            $this->repoDe->update($dataDe, $category->rDe);
        } else {
            $dataDe['category_id'] = $category->id;
            $dataDe['name'] = $data['name_de'];

            $this->repoDe->store($dataDe);
        }
    }

    public function updateSa($data, $category)
    {
        $dataSa['name'] = $data['name_sa'];

        if ($category->rSa) {
            $this->repoSa->update($dataSa, $category->rSa);
        } else {
            $dataSa['category_id'] = $category->id;
            $dataSa['name'] = $data['name_sa'];

            $this->repoSa->store($dataSa);
        }
228 229
    }

230 231 232 233 234 235 236 237 238 239 240 241 242 243
    public function updateZh($data, $category)
    {
        $dataZh['name'] = $data['name_zh'];

        if ($category->rZh) {
            $this->repoZh->update($dataZh, $category->rZh);
        } else {
            $dataZh['category_id'] = $category->id;
            $dataZh['name'] = $data['name_zh'];

            $this->repoZh->store($dataZh);
        }
    }

244 245 246
    /**
     * Remove the specified resource from storage.
     *
247 248
     * @param int $id
     *
249 250 251 252
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
253
        $data = $this->repo->findId($id, ['rEn', 'berita']);
254
        if (count($data->berita) <= 0) {
255
            $this->repo->destroy($data);
256

257 258 259
            if ($data->rEn) {
                $this->repoEn->destroy($data->rEn);
            }
260 261 262 263 264 265 266 267

            if ($data->rDe) {
                $this->repoDe->destroy($data->rDe);
            }

            if ($data->rSa) {
                $this->repoSa->destroy($data->rSa);
            }
268 269 270 271

            if ($data->rZh) {
                $this->repoZh->destroy($data->rZh);
            }
272 273
        }

274
        return response()->json(['done']);
275 276
    }
}