AgendaController.php 14.8 KB
Newer Older
Bagus Pambudi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
<?php

namespace App\Http\Controllers\Webprofile\Backend;

use App\Http\Controllers\Controller;
use App\Models\Webprofile\Categories;
use App\Repositories\Webprofile\De\AgendaRepository as DeAgendaRepository;
use App\Repositories\Webprofile\En\AgendaRepository as EnAgendaRepository;
use App\Repositories\Webprofile\AgendaRepository;
use App\Repositories\Webprofile\Sa\AgendaRepository as SaAgendaRepository;
use App\Repositories\Webprofile\Zh\AgendaRepository as ZhAgendaRepository;
use Illuminate\Http\Request;
use Statickidz\GoogleTranslate;

class AgendaController extends Controller
{
    private $repo;
    private $repoEn;
    private $repoDe;
    private $repoSa;
    private $repoZh;

    private $SOURCE = 'id';
    private $TARGET = 'en';
    private $TARGETDE = 'de';
    private $TARGETSA = 'ar';
    private $TARGETZH = 'zh';

    public function __construct(
        AgendaRepository $repo,
        EnAgendaRepository $repoEn,
        DeAgendaRepository $repoDe,
        SaAgendaRepository $repoSa,
        ZhAgendaRepository $repoZh
    ) {
        $this->repo = $repo;
        $this->repoEn = $repoEn;
        $this->repoDe = $repoDe;
        $this->repoSa = $repoSa;
        $this->repoZh = $repoZh;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $data = $this->repo->get(['rEn']);
            return $this->repo->datatable($data);
        }

        return view('webprofile.backend.agendas.index')->withTitle(trans('feature.agenda'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categories = Categories::pluck('name', 'id');

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

        return view('webprofile.backend.agendas.create', $data)->withTitle(trans('feature.create_agenda'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
81
        $request['content'] = $request->content;
82
        $request['title'] = strip_tags($request->title);
83 84 85 86
        $request->validate([
            'title' => 'required',
            'content' => 'required|min:3',
            'event_date' => 'required',
87
            'keys' => 'required|max:100',
88 89 90 91 92
        ], [
            'title.required' => 'Judul wajib diisi',
            'content.required' => 'Konten wajib diisi',
            'content.min' => 'Konten terlalu singkat',
            'event_date.required' => 'Tanggal posting wajib diisi',
93
            'keys.required' => 'Keyword wajib diisi',
94 95 96
            'keys.max' => 'Keyword terlalu panjang'
        ]);

Bagus Pambudi committed
97
        $data = $request->except('_token');
98
        // $data['content'] = htmlspecialchars($request->content);
Bagus Pambudi committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

        array_key_exists('info_status', $data) ? $data['info_status'] = 1 : $data['info_status'] = 0;
        $data['slug'] = str_slug($request->input('title'));

        $save = $this->repo->store($data);

        if (webprofilesetting()['auto_translate'] == 1) {
            // save translate
            $this->createEn($data, $save);
            $this->createDe($data, $save);
            $this->createSa($data, $save);
            $this->createZh($data, $save);
        }

        return redirect()->route('agendas.index');
    }

    private function createEn($data, $agenda)
    {
        $trans = new GoogleTranslate();
119
        $title = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['title']));
120
        $keys = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['keys']));
Bagus Pambudi committed
121

122
        if ($data['content'] == null) {
Bagus Pambudi committed
123 124 125
            $data['content'] = 'kosong';
        }

126
        $content = $trans->translate($this->SOURCE, $this->TARGET, $data['content']);
Bagus Pambudi committed
127 128 129

        $dataEn['agenda_id'] = $agenda->id;
        $dataEn['title'] = $title;
130
        $dataEn['keys'] = $keys;
Bagus Pambudi committed
131 132 133 134 135 136 137 138
        $dataEn['content'] = $content;

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

    private function createDe($data, $agenda)
    {
        $trans = new GoogleTranslate();
139
        $title = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['title']));
140
        $keys = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['keys']));
Bagus Pambudi committed
141

142
        if ($data['content'] == null) {
Bagus Pambudi committed
143 144 145
            $data['content'] = 'kosong';
        }

146
        $content = $trans->translate($this->SOURCE, $this->TARGETDE, $data['content']);
Bagus Pambudi committed
147 148 149

        $dataDe['agenda_id'] = $agenda->id;
        $dataDe['title'] = $title;
150
        $dataDe['keys'] = $keys;
Bagus Pambudi committed
151 152 153 154 155 156 157 158
        $dataDe['content'] = $content;

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

    private function createSa($data, $agenda)
    {
        $trans = new GoogleTranslate();
159
        $title = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['title']));
160
        $keys = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['keys']));
Bagus Pambudi committed
161

162
        if ($data['content'] == null) {
Bagus Pambudi committed
163 164 165
            $data['content'] = 'kosong';
        }

166
        $content = $trans->translate($this->SOURCE, $this->TARGETSA, $data['content']);
Bagus Pambudi committed
167 168 169

        $dataSa['agenda_id'] = $agenda->id;
        $dataSa['title'] = $title;
170
        $dataSa['keys'] = $keys;
Bagus Pambudi committed
171 172 173 174 175 176 177 178
        $dataSa['content'] = $content;

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

    private function createZh($data, $agenda)
    {
        $trans = new GoogleTranslate();
179
        $title = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['title']));
180
        $keys = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['keys']));
Bagus Pambudi committed
181

182
        if ($data['content'] == null) {
Bagus Pambudi committed
183 184 185
            $data['content'] = 'kosong';
        }

186
        $content = $trans->translate($this->SOURCE, $this->TARGETZH, $data['content']);
Bagus Pambudi committed
187 188 189

        $dataZh['agenda_id'] = $agenda->id;
        $dataZh['title'] = $title;
190
        $dataZh['keys'] = $keys;
Bagus Pambudi committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
        $dataZh['content'] = $content;

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

    /**
     * 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)
    {
216
        $setting = webprofilesetting();
Bagus Pambudi committed
217 218
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
        $categories = Categories::pluck('name', 'id');
219
        $manual=0;
Bagus Pambudi committed
220 221 222 223

        $data = [
            'data' => $data,
            'categories' => $categories,
224 225
            'setting' => $setting,
            'manual'=> $manual,
Bagus Pambudi committed
226 227 228
        ];

        return view('webprofile.backend.agendas.edit', $data)->withTitle(trans('feature.edit_agenda'));
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

    }
    public function editPerBahasa($id)
    {
        $setting = webprofilesetting();
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
        $categories = Categories::pluck('name', 'id');
        $manual=1;

        $data = [
            'data' => $data,
            'categories' => $categories,
            'setting' => $setting,
            'manual' => $manual,
        ];
244

245
        return view('webprofile.backend.agendas.edit_per_bahasa', $data)->withTitle(trans('feature.edit_agenda'));
Bagus Pambudi committed
246 247 248 249 250 251 252 253 254 255 256
    }

    /**
     * Update the specified resource in storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
257 258 259 260 261
        $request['content'] = $request->content;
        $request['content_en'] = $request->content_en;
        $request['content_de'] = $request->content_de;
        $request['content_sa'] = $request->content_sa;
        $request['content_zh'] = $request->content_zh;
262 263 264 265 266
        $request['title'] = strip_tags($request->title);
        $request['title_en'] = strip_tags($request->title_en);
        $request['title_de'] = strip_tags($request->title_de);
        $request['title_sa'] = strip_tags($request->title_sa);
        $request['title_zh'] = strip_tags($request->title_zh);
267 268 269 270 271
        $request['keys'] = strip_tags($request->keys);
        $request['keys_en'] = strip_tags($request->keys_en);
        $request['keys_de'] = strip_tags($request->keys_de);
        $request['keys_sa'] = strip_tags($request->keys_sa);
        $request['keys_zh'] = strip_tags($request->keys_zh);
272 273

        $request->validate([
274
            'title' => 'required',
275 276
            'content' => 'required|min:3',
            'event_date' => 'required',
277
            'keys' => 'required|max:100'
278
        ], [
279
            'title.required' => 'Judul wajib diisi',
280 281 282
            'content.required' => 'Konten wajib diisi',
            'content.min' => 'Konten terlalu singkat',
            'event_date.required' => 'Tanggal posting wajib diisi',
283
            'keys.required' => 'Keyword wajib diisi',
284 285 286
            'keys.max' => 'Keyword terlalu panjang'
        ]);

287
        $data = $request->except(['_token', 'manual', 'id', 'title_en', 'keys_en', 'content_en', 'title_de', 'keys_de', 'content_de', 'title_sa', 'keys_sa', 'content_sa', 'title_zh', 'keys_zh', 'content_zh']);
288 289
        // $data['title'] = htmlspecialchars($request->title);
        // $data['content'] = htmlspecialchars($request->content);
290

291
        $dataEn = $request->except(['_token', 'id', 'manual']);
292 293 294 295 296 297 298 299
        // $dataEn = $request->except(['_token', 'id', 'manual', 'title_en', 'content_en', 'title_de', 'content_de', 'title_sa', 'content_sa', 'title_zh', 'content_zh']);
        // $dataEn['title'] = htmlspecialchars($request->title);
        // $dataEn['title_en'] = htmlspecialchars($request->title_en);
        // $dataEn['title_de'] = htmlspecialchars($request->title_de);
        // $dataEn['title_sa'] = htmlspecialchars($request->title_sa);
        // $dataEn['title_zh'] = htmlspecialchars($request->title_zh);
        // $dataEn['content'] = htmlspecialchars($request->content);
        // $dataEn['content_en'] = htmlspecialchars($request->content_en);
300 301
        // $dataEn['content_de'] = htmlspecialchars($request->content_de);
        // $dataEn['content_sa'] = htmlspecialchars($request->content_sa);
302
        // $dataEn['content_zh'] = htmlspecialchars($request->content_zh);
Bagus Pambudi committed
303 304 305 306 307 308

        array_key_exists('info_status', $data) ? $data['info_status'] = 1 : $data['info_status'] = 0;
        $data['slug'] = str_slug($request->input('title'));

        $agenda = $this->repo->findId($id, ['rEn', 'rDe', 'rSa','rZh']);

309 310 311 312
        $this->updateEn($dataEn, $agenda, $request->manual);
        $this->updateDe($dataEn, $agenda, $request->manual);
        $this->updateSa($dataEn, $agenda, $request->manual);
        $this->updateZh($dataEn, $agenda, $request->manual);
Bagus Pambudi committed
313

314
        $this->repo->update($data, $agenda);
Bagus Pambudi committed
315 316 317
        return redirect()->route('agendas.index');
    }

318
    public function updateEn($data, $agenda, $manual)
Bagus Pambudi committed
319
    {
320
        if($manual==1) {
321
        $dataEn['title'] = strip_tags($data['title_en']);
322
        $dataEn['keys'] = strip_tags($data['keys_en']);
323
        $dataEn['content'] = $data['content_en'];
324 325
        }
        else{
326
            if ($data['content'] == null) {
327 328
                $data['content'] = 'kosong';
        }
Bagus Pambudi committed
329

330
        $trans = new GoogleTranslate();
331
        $title = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['title']));
332
        $keys = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['keys']));
333
        $content = $trans->translate($this->SOURCE, $this->TARGET, $data['content']);
334

335
        $dataEn['title'] = $title;
336
        $dataEn['keys'] = $keys;
337
        $dataEn['content'] = $content;
338

339
    }
Bagus Pambudi committed
340 341 342
        $this->repoEn->update($dataEn, $agenda);
    }

343
    public function updateDe($data, $agenda, $manual)
Bagus Pambudi committed
344
    {
345
        if($manual==1) {
346
        $dataDe['title'] = strip_tags($data['title_de']);
347
        $dataDe['keys'] = strip_tags($data['keys_de']);
348
        $dataDe['content'] = $data['content_de'];
349 350
        }
        else{
Bagus Pambudi committed
351

352
            if ($data['content'] == null) {
353 354
                $data['content'] = 'kosong';
            }
355

356
        $trans = new GoogleTranslate();
357
        $title = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['title']));
358
        $keys = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['keys']));
359
        $content = $trans->translate($this->SOURCE, $this->TARGETDE, $data['content']);
360
        
361
        $dataDe['title'] = $title;
362
        $dataDe['keys'] = $keys;
363
        $dataDe['content'] = $content;
364

365
    }
Bagus Pambudi committed
366 367 368
        $this->repoDe->update($dataDe, $agenda);
    }

369
    public function updateSa($data, $agenda, $manual)
Bagus Pambudi committed
370
    {
371
        if($manual==1) {
372
        $dataSa['title'] = strip_tags($data['title_sa']);
373
        $dataSa['keys'] = strip_tags($data['keys_sa']);
374
        $dataSa['content'] = $data['content_sa'];
375 376
        }
        else{
Bagus Pambudi committed
377

378
            if ($data['content'] == null) {
379 380
                $data['content'] = 'kosong';
            }
381

382
        $trans = new GoogleTranslate();
383
        $title = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['title']));
384
        $keys = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['keys']));
385
        $content = $trans->translate($this->SOURCE, $this->TARGETSA, $data['content']);
386

387
        $dataSa['title'] = $title;
388
        $dataSa['keys'] = $keys;
389
        $dataSa['content'] = $content;
390

391
    }
Bagus Pambudi committed
392 393 394
        $this->repoSa->update($dataSa, $agenda);
    }

395
    public function updateZh($data, $agenda, $manual)
Bagus Pambudi committed
396
    {
397
        if($manual==1) {
398
        $dataZh['title'] = strip_tags($data['title_zh']);
399
        $dataZh['keys'] = strip_tags($data['keys_zh']);
400
        $dataZh['content'] = $data['content_zh'];
401 402
        }
        else{
Bagus Pambudi committed
403

404
            if ($data['content'] == null) {
405 406
                $data['content'] = 'kosong';
            }
407

408
        $trans = new GoogleTranslate();
409
        $title = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['title']));
410
        $keys = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['keys']));
411
        $content = $trans->translate($this->SOURCE, $this->TARGETZH, $data['content']);
412

413
        $dataZh['title'] = $title;
414
        $dataZh['keys'] = $keys;
415
        $dataZh['content'] = $content;
416

417
    }
Bagus Pambudi committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
        $this->repoZh->update($dataZh, $agenda);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
        $this->repo->destroy($data);

        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

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

        if ($data->rSa) {
            $this->repoSa->destroy($data->rSa);
        }

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

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