CategoryController.php 10 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
        $manual=0;
155 156 157

        $data = [
            'data' => $data,
158
            'manual' => $manual,
159 160
        ];

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

164 165 166 167 168 169 170 171 172 173 174 175 176 177
    public function editPerBahasa($id)
    {
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
        $manual=1;

        $data = [
            'data' => $data,
            'manual' => $manual,
        ];
        // dd($data);

        return view('webprofile.backend.categories.edit_per_bahasa', $data)->withTitle(trans('feature.edit_category'));
    }

178 179 180
    /**
     * Update the specified resource in storage.
     *
181
     * @param int $id
182
     *
183 184 185 186
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
187 188 189 190 191
        $data = $request->except(['_token', 'manual', 'id', 'name_en', 'name_de', 'name_sa', 'name_zh']);
        // $dataEn = $request->except(['_token', 'id', 'name_de']);
        $dataEn = $request->except(['_token','id', 'manual']);
        // $dataDe = $request->except(['_token', 'id', 'name', 'name_en']);
        // dd($dataEn);
192 193
        array_key_exists('is_active', $data) ? $data['is_active'] = 1 : $data['is_active'] = 0;

194
        $category = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
195 196 197 198 199 200 201 202 203 204
        // $edit = $this->repo->update($data, $category);
        // dd($dataEn, $category, $request->manual);
        
        
        $this->updateEn($dataEn, $category, $request->manual);
        $this->updateDe($dataEn, $category, $request->manual);
        $this->updateSa($dataEn, $category, $request->manual);
        $this->updateZh($dataEn, $category, $request->manual);
        $this->repo->update($data, $category);
        // dd($data, $category, $request->manual);
205
        return redirect()->route('category.index');
206 207
    }

208
    public function updateEn($data, $category, $manual)
209
    {
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        // $dataEn['name'] = $data['name_en'];

        // if ($category->rEn) {
        //     $this->repoDe->update($dataEn, $category->rEn);
        // } else {
        //     $dataEn['category_id'] = $category->id;
        //     $dataEn['name'] = $data['name_en'];

        //     $this->repoEn->store($dataEn);
        // }
            // dd($data, $category, $manual);
            if($manual==1){
                $dataEn['name'] = $data['name_en'];
            }
            else{
                if (strip_tags($data['name']) == null) {
                    $data['name'] = 'kosong';
                }

                $trans = new GoogleTranslate();
                $name = $trans->translate($this->SOURCE, $this->TARGET, $data['name']);
                $dataEn['name'] = $name;
                // dd($dataEn);
            }
            // dd($category);
            $this->repoEn->update($dataEn, $category);
236 237
    }

238
    public function updateDe($data, $category, $manual)
239
    {
240 241 242 243 244 245 246 247 248 249
        // $dataDe['name'] = $data['name_de'];

        // if ($category->rDe) {
        //     $this->repoDe->update($dataDe, $category->rDe);
        // } else {
        //     $dataDe['category_id'] = $category->id;
        //     $dataDe['name'] = $data['name_de'];

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

251
        if($manual==1){
252
            $dataDe['name'] = $data['name_de'];
253 254 255 256 257
        }
        else{
            if (strip_tags($data['name']) == null) {
                $data['name'] = 'kosong';
            }
258

259 260 261
            $trans = new GoogleTranslate();
            $name = $trans->translate($this->SOURCE, $this->TARGETDE, $data['name']);
            $dataDe['name'] = $name;
262
        }
263
        $this->repoDe->update($dataDe, $category);
264 265
    }

266
    public function updateSa($data, $category, $manual)
267
    {
268 269 270 271 272 273 274
        // $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'];
275

276 277 278 279
        //     $this->repoSa->store($dataSa);
        // }

        if($manual==1){
280
            $dataSa['name'] = $data['name_sa'];
281 282 283 284 285
        }
        else{
            if (strip_tags($data['name']) == null) {
                $data['name'] = 'kosong';
            }
286

287 288 289
            $trans = new GoogleTranslate();
            $name = $trans->translate($this->SOURCE, $this->TARGETSA, $data['name']);
            $dataSa['name'] = $name;
290
        }
291
        $this->repoSa->update($dataSa, $category);
292 293
    }

294
    public function updateZh($data, $category, $manual)
295
    {
296 297 298 299 300 301 302
        // $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'];
303

304 305 306 307
        //     $this->repoZh->store($dataZh);
        // }

        if($manual==1){
308
            $dataZh['name'] = $data['name_zh'];
309 310 311 312 313
        }
        else{
            if (strip_tags($data['name']) == null) {
                $data['name'] = 'kosong';
            }
314

315 316 317
            $trans = new GoogleTranslate();
            $name = $trans->translate($this->SOURCE, $this->TARGETZH, $data['name']);
            $dataZh['name'] = $name;
318
        }
319
        $this->repoZh->update($dataZh, $category);
320 321
    }

322 323 324
    /**
     * Remove the specified resource from storage.
     *
325 326
     * @param int $id
     *
327 328 329 330
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
Bagus Pambudi committed
331
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh', 'berita']);
332
        if (count($data->berita) <= 0) {
333
            $this->repo->destroy($data);
334

335 336 337
            if ($data->rEn) {
                $this->repoEn->destroy($data->rEn);
            }
338 339 340 341 342 343 344 345

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

            if ($data->rSa) {
                $this->repoSa->destroy($data->rSa);
            }
346 347 348 349

            if ($data->rZh) {
                $this->repoZh->destroy($data->rZh);
            }
350 351
        }

352
        return response()->json(['done']);
353 354
    }
}