AgendaController.php 13.4 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 81
<?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)
    {
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        // $request['content'] = strip_tags($request->content);
        $request->validate([
            'title' => 'required',
            'content' => 'required|min:3',
            'event_date' => 'required',
            'keys' => 'max:100'
        ], [
            'title.required' => 'Judul wajib diisi',
            // 'title.regex' => 'Judul tidak valid! Judul hanya berupa angka dan huruf',
            // 'title.max' => 'Judul terlalu panjang',
            'content.required' => 'Konten wajib diisi',
            'content.min' => 'Konten terlalu singkat',
            'event_date.required' => 'Tanggal posting wajib diisi',
            // 'keys.regex' => 'Keyword tidak valid! Keyword hanya berupa angka dan huruf',
            'keys.max' => 'Keyword terlalu panjang'
        ]);

Bagus Pambudi committed
99
        $data = $request->except('_token');
100
        // $data['content'] = htmlspecialchars($request->content);
Bagus Pambudi committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

        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();
        $title = $trans->translate($this->SOURCE, $this->TARGET, $data['title']);

        if (strip_tags($data['content']) == null) {
            $data['content'] = 'kosong';
        }

        $content = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['content']));

        $dataEn['agenda_id'] = $agenda->id;
        $dataEn['title'] = $title;
        $dataEn['content'] = $content;

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

    private function createDe($data, $agenda)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETDE, $data['title']);

        if (strip_tags($data['content']) == null) {
            $data['content'] = 'kosong';
        }

        $content = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['content']));

        $dataDe['agenda_id'] = $agenda->id;
        $dataDe['title'] = $title;
        $dataDe['content'] = $content;

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

    private function createSa($data, $agenda)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETSA, $data['title']);

        if (strip_tags($data['content']) == null) {
            $data['content'] = 'kosong';
        }

        $content = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['content']));

        $dataSa['agenda_id'] = $agenda->id;
        $dataSa['title'] = $title;
        $dataSa['content'] = $content;

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

    private function createZh($data, $agenda)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETZH, $data['title']);

        if (strip_tags($data['content']) == null) {
            $data['content'] = 'kosong';
        }

        $content = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['content']));

        $dataZh['agenda_id'] = $agenda->id;
        $dataZh['title'] = $title;
        $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)
    {
210
        $setting = webprofilesetting();
Bagus Pambudi committed
211 212
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
        $categories = Categories::pluck('name', 'id');
213
        $manual=0;
Bagus Pambudi committed
214 215 216 217

        $data = [
            'data' => $data,
            'categories' => $categories,
218 219
            'setting' => $setting,
            'manual'=> $manual,
Bagus Pambudi committed
220 221 222
        ];

        return view('webprofile.backend.agendas.edit', $data)->withTitle(trans('feature.edit_agenda'));
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

    }
    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,
        ];
        
        return view('webprofile.backend.agendas.edit_per_bahasa', $data)->withTitle(trans('feature.edit_agenda'));
Bagus Pambudi committed
240 241 242 243 244 245 246 247 248 249 250
    }

    /**
     * Update the specified resource in storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
        // $request['content'] = strip_tags($request->content);
        // $request['content_en'] = strip_tags($request->content_en);
        // $request['content_de'] = strip_tags($request->content_de);
        // $request['content_sa'] = strip_tags($request->content_sa);
        // $request['content_zh'] = strip_tags($request->content_zh);

        $request->validate([
            'title' => 'required',
            'content' => 'required|min:3',
            'event_date' => 'required',
            'keys' => 'max:100'
        ], [
            'title.required' => 'Judul wajib diisi',
            // 'title.regex' => 'Judul tidak valid! Judul hanya berupa angka dan huruf',
            // 'title.max' => 'Judul terlalu panjang',
            'content.required' => 'Konten wajib diisi',
            'content.min' => 'Konten terlalu singkat',
            'event_date.required' => 'Tanggal posting wajib diisi',
            // 'keys.regex' => 'Keyword tidak valid! Keyword hanya berupa angka dan huruf',
            'keys.max' => 'Keyword terlalu panjang'
        ]);

273
        $data = $request->except(['_token', 'manual', 'id', 'title_en', 'content_en', 'title_de', 'content_de', 'title_sa', 'content_sa', 'title_zh', 'content_zh']);
274 275 276
        // $data['title'] = htmlspecialchars($request->title);
        // $data['content'] = htmlspecialchars($request->content);
        
277
        $dataEn = $request->except(['_token', 'id', 'manual']);
278 279 280 281 282 283 284 285 286 287 288
        // $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);
        // $dataEn['content_de'] = htmlspecialchars($request->content_de); 
        // $dataEn['content_sa'] = htmlspecialchars($request->content_sa); 
        // $dataEn['content_zh'] = htmlspecialchars($request->content_zh);
Bagus Pambudi committed
289 290 291 292 293 294

        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']);

295 296 297 298
        $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
299

300
        $this->repo->update($data, $agenda);
Bagus Pambudi committed
301 302 303
        return redirect()->route('agendas.index');
    }

304
    public function updateEn($data, $agenda, $manual)
Bagus Pambudi committed
305
    {
306
        if($manual==1) {
Bagus Pambudi committed
307 308
        $dataEn['title'] = $data['title_en'];
        $dataEn['content'] = $data['content_en'];
309 310 311 312 313
        }
        else{
            if (strip_tags($data['content']) == null) {
                $data['content'] = 'kosong';
        }
Bagus Pambudi committed
314

315 316 317 318 319 320 321 322
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGET, $data['title']);
        $content = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['content']));
        
        $dataEn['title'] = $title;
        $dataEn['content'] = $content;
        
    }
Bagus Pambudi committed
323 324 325
        $this->repoEn->update($dataEn, $agenda);
    }

326
    public function updateDe($data, $agenda, $manual)
Bagus Pambudi committed
327
    {
328
        if($manual==1) {
Bagus Pambudi committed
329 330
        $dataDe['title'] = $data['title_de'];
        $dataDe['content'] = $data['content_de'];
331 332
        }
        else{
Bagus Pambudi committed
333

334 335 336 337 338 339 340 341 342 343 344
            if (strip_tags($data['content']) == null) {
                $data['content'] = 'kosong';
            }
    
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETDE, $data['title']);
        $content = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['content']));
        $dataDe['title'] = $title;
        $dataDe['content'] = $content;
        
    }
Bagus Pambudi committed
345 346 347
        $this->repoDe->update($dataDe, $agenda);
    }

348
    public function updateSa($data, $agenda, $manual)
Bagus Pambudi committed
349
    {
350
        if($manual==1) {
Bagus Pambudi committed
351 352
        $dataSa['title'] = $data['title_sa'];
        $dataSa['content'] = $data['content_sa'];
353 354
        }
        else{
Bagus Pambudi committed
355

356 357 358 359 360 361 362 363 364 365 366 367
            if (strip_tags($data['content']) == null) {
                $data['content'] = 'kosong';
            }
    
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETSA, $data['title']);
        $content = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['content']));
        
        $dataSa['title'] = $title;
        $dataSa['content'] = $content;
        
    }
Bagus Pambudi committed
368 369 370
        $this->repoSa->update($dataSa, $agenda);
    }

371
    public function updateZh($data, $agenda, $manual)
Bagus Pambudi committed
372
    {
373
        if($manual==1) {
Bagus Pambudi committed
374 375
        $dataZh['title'] = $data['title_zh'];
        $dataZh['content'] = $data['content_zh'];
376 377
        }
        else{
Bagus Pambudi committed
378

379 380 381 382 383 384 385 386 387 388 389 390
            if (strip_tags($data['content']) == null) {
                $data['content'] = 'kosong';
            }
    
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETZH, $data['title']);
        $content = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['content']));
        
        $dataZh['title'] = $title;
        $dataZh['content'] = $content;
        
    }
Bagus Pambudi committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
        $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']);
    }
}