InformationController.php 15.1 KB
Newer Older
1 2 3 4 5 6
<?php

namespace App\Http\Controllers\Webprofile\Backend;

use App\Http\Controllers\Controller;
use App\Models\Webprofile\Categories;
7
use App\Repositories\Webprofile\De\InformationRepository as DeInformationRepository;
8
use App\Repositories\Webprofile\En\InformationRepository as EnInformationRepository;
9
use App\Repositories\Webprofile\InformationRepository;
10
use App\Repositories\Webprofile\Sa\InformationRepository as SaInformationRepository;
11
use App\Repositories\Webprofile\Zh\InformationRepository as ZhInformationRepository;
12
use Illuminate\Http\Request;
13
use Statickidz\GoogleTranslate;
14 15 16 17

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

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

    public function __construct(
        InformationRepository $repo,
31
        EnInformationRepository $repoEn,
32
        DeInformationRepository $repoDe,
33 34
        SaInformationRepository $repoSa,
        ZhInformationRepository $repoZh
35
    ) {
36
        $this->repo = $repo;
37
        $this->repoEn = $repoEn;
38
        $this->repoDe = $repoDe;
39
        $this->repoSa = $repoSa;
40
        $this->repoZh = $repoZh;
41
    }
42

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

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

    /**
     * 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.informations.create', $data)->withTitle(trans('feature.create_information'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
82 83
        $request['content'] = strip_tags($request->content, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img']);
        $request['title'] = strip_tags($request->title, ['b', 'i', 'u']);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        $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'
        ]);

100
        $data = $request->except('_token');
101
        // $data['content'] = htmlspecialchars($request->content);
102 103

        array_key_exists('info_status', $data) ? $data['info_status'] = 1 : $data['info_status'] = 0;
104
        $data['slug'] = str_slug($request->input('title'));
105
        // dd($request);
106 107
        $save = $this->repo->store($data);

108 109 110
        if (webprofilesetting()['auto_translate'] == 1) {
            // save translate
            $this->createEn($data, $save);
111
            $this->createDe($data, $save);
112
            $this->createSa($data, $save);
113
            $this->createZh($data, $save);
114
        }
115 116 117 118

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

119 120 121
    private function createEn($data, $information)
    {
        $trans = new GoogleTranslate();
122
        $title = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['title'], ['b', 'i', 'u']));
123

124
        if ($data['content'] == null) {
125 126 127
            $data['content'] = 'kosong';
        }

128
        $content = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img']));
129 130 131 132 133 134 135 136

        $dataEn['information_id'] = $information->id;
        $dataEn['title'] = $title;
        $dataEn['content'] = $content;

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

137 138 139
    private function createDe($data, $information)
    {
        $trans = new GoogleTranslate();
140
        $title = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['title'], ['b', 'i', 'u']));
141

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

146
        $content = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img']));
147 148 149 150 151 152 153 154

        $dataDe['information_id'] = $information->id;
        $dataDe['title'] = $title;
        $dataDe['content'] = $content;

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

155 156 157
    private function createSa($data, $information)
    {
        $trans = new GoogleTranslate();
158
        $title = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['title'], ['b', 'i', 'u']));
159

160
        if ($data['content'] == null) {
161 162 163
            $data['content'] = 'kosong';
        }

164
        $content = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img']));
165 166 167 168 169 170 171 172

        $dataSa['information_id'] = $information->id;
        $dataSa['title'] = $title;
        $dataSa['content'] = $content;

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

173 174 175
    private function createZh($data, $information)
    {
        $trans = new GoogleTranslate();
176
        $title = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['title'], ['b', 'i', 'u']));
177 178 179 180 181

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

182
        $content = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img']));
183 184 185 186 187 188 189 190

        $dataZh['information_id'] = $information->id;
        $dataZh['title'] = $title;
        $dataZh['content'] = $content;

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

191 192 193
    /**
     * Display the specified resource.
     *
194 195
     * @param int $id
     *
196 197 198 199 200 201 202 203 204
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    }

    /**
     * Show the form for editing the specified resource.
     *
205 206
     * @param int $id
     *
207 208 209 210
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
211
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
212
        $categories = Categories::pluck('name', 'id');
213
        $manual=0;
214

215 216 217
        $data = [
            'data' => $data,
            'categories' => $categories,
218
            'manual'=> $manual,
219 220
        ];

221
        return view('webprofile.backend.informations.edit', $data)->withTitle(trans('feature.edit_information'));
222
    }
223 224 225 226 227 228 229 230 231 232 233 234 235 236
    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,
        ];
        
Siti Aisah committed
237
        return view('webprofile.backend.informations.edit_per_bahasa', $data)->withTitle(trans('feature.edit_information'));
238 239
    }

240 241 242 243

    /**
     * Update the specified resource in storage.
     *
244
     * @param int $id
245
     *
246 247 248 249
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
250 251 252 253 254 255 256 257 258 259
        $request['content'] = strip_tags($request->content, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img']);
        $request['content_en'] = strip_tags($request->content_en, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img']);
        $request['content_de'] = strip_tags($request->content_de, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img']);
        $request['content_sa'] = strip_tags($request->content_sa, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img']);
        $request['content_zh'] = strip_tags($request->content_zh, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img']);
        $request['title'] = strip_tags($request->title, ['b', 'i', 'u']);
        $request['title_en'] = strip_tags($request->title_en, ['b', 'i', 'u']);
        $request['title_de'] = strip_tags($request->title_de, ['b', 'i', 'u']);
        $request['title_sa'] = strip_tags($request->title_sa, ['b', 'i', 'u']);
        $request['title_zh'] = strip_tags($request->title_zh, ['b', 'i', 'u']);
260 261

        $request->validate([
262
            // 'title' => 'required',
263 264 265 266 267 268 269 270 271 272 273 274 275 276
            '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'
        ]);

277 278
        $data = $request->except(['_token', 'manual', 'id', 'title_en', 'content_en', 'title_de', 'content_de', 'title_sa', 'content_sa', 'title_zh', 'content_zh']);
        $dataEn = $request->except(['_token', 'id', 'manual']);
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
        // $data['title'] = htmlspecialchars($request->title);
        // $data['content'] = htmlspecialchars($request->content);
        
        // $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);
        
294
        array_key_exists('info_status', $data) ? $data['info_status'] = 1 : $data['info_status'] = 0;
295
        $data['slug'] = str_slug($request->input('title'));
296
        // dd($request, $dataEn);
297
        $information = $this->repo->findId($id, ['rEn', 'rDe', 'rSa','rZh']);
298

299 300 301 302
        $this->updateEn($dataEn, $information, $request->manual);
        $this->updateDe($dataEn, $information, $request->manual);
        $this->updateSa($dataEn, $information, $request->manual);
        $this->updateZh($dataEn, $information, $request->manual);
303

304
        $this->repo->update($data, $information);
305 306 307
        return redirect()->route('informations.index');
    }

308
    public function updateEn($data, $information, $manual)
309
    {
310
        if($manual==1) {
311 312
        $dataEn['title'] = $data['title_en'];
        $dataEn['content'] = $data['content_en'];
313 314 315 316 317
        }
        else{
            if (strip_tags($data['content']) == null) {
                $data['content'] = 'kosong';
        }
318

319
        $trans = new GoogleTranslate();
320 321
        $title = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['title'], ['b', 'i', 'u']));
        $content = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img']));
322 323 324 325 326
        
        $dataEn['title'] = $title;
        $dataEn['content'] = $content;
    
    }
327
        $this->repoEn->update($dataEn, $information);
328 329
    }

330
    public function updateDe($data, $information, $manual)
331
    {
332
        if($manual==1) {
333 334
        $dataDe['title'] = $data['title_de'];
        $dataDe['content'] = $data['content_de'];
335 336 337 338 339
        }
        else{
            if (strip_tags($data['content']) == null) {
                $data['content'] = 'kosong';
            }
340

341
            $trans = new GoogleTranslate();
342 343
            $title = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['title'], ['b', 'i', 'u']));
            $content = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img']));
344 345 346 347
            $dataDe['title'] = $title;
            $dataDe['content'] = $content;

        }
348 349 350
        $this->repoDe->update($dataDe, $information);
    }

351
    public function updateSa($data, $information, $manual)
352
    {
353
        if($manual==1) {
354 355
        $dataSa['title'] = $data['title_sa'];
        $dataSa['content'] = $data['content_sa'];
356 357 358 359 360 361 362 363
        }
        else{

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

            $trans = new GoogleTranslate();
364 365
            $title = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['title'], ['b', 'i', 'u']));
            $content = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img']));
366 367 368 369
            
            $dataSa['title'] = $title;
            $dataSa['content'] = $content;
        }
370 371 372 373

        $this->repoSa->update($dataSa, $information);
    }

374
    public function updateZh($data, $information, $manual)
375
    {
376
        if($manual==1) {
377 378
        $dataZh['title'] = $data['title_zh'];
        $dataZh['content'] = $data['content_zh'];
379 380 381 382 383 384 385 386
        }
        else{
            
            if (strip_tags($data['content']) == null) {
                $data['content'] = 'kosong';
            }
    
            $trans = new GoogleTranslate();
387 388
            $title = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['title'], ['b', 'i', 'u']));
            $content = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img']));
389 390 391 392
            
            $dataZh['title'] = $title;
            $dataZh['content'] = $content;
        }
393 394 395 396

        $this->repoZh->update($dataZh, $information);
    }

397 398 399
    /**
     * Remove the specified resource from storage.
     *
400 401
     * @param int $id
     *
402 403 404 405
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
406
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
407 408
        $this->repo->destroy($data);

409 410 411 412
        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

413 414 415 416 417 418 419 420
        if ($data->rDe) {
            $this->repoDe->destroy($data->rDe);
        }

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

421 422 423 424
        if ($data->rZh) {
            $this->repoZh->destroy($data->rZh);
        }

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