InformationController.php 15 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)
    {
Triyah Fatmawati committed
82
        $request['content'] = strip_tags($request->content, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'span']);
83
        $request['title'] = strip_tags($request->title);
84 85 86 87 88 89 90 91 92 93
        $request->validate([
            'title' => 'required',
            'content' => 'required|min:3',
            'event_date' => 'required',
            'keys' => 'max:100'
        ], [
            'title.required' => 'Judul wajib diisi',
            'content.required' => 'Konten wajib diisi',
            'content.min' => 'Konten terlalu singkat',
            'event_date.required' => 'Tanggal posting wajib diisi',
94
            'keys.max' => 'Keyword terlalu panjang'
95 96
        ]);

97
        $data = $request->except('_token');
98
        // $data['content'] = htmlspecialchars($request->content);
99 100

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

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

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

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

116 117 118
    private function createEn($data, $information)
    {
        $trans = new GoogleTranslate();
119
        $title = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['title']));
120

121
        if ($data['content'] == null) {
122 123 124
            $data['content'] = 'kosong';
        }

Triyah Fatmawati committed
125
        $content = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'span']));
126 127 128 129 130 131 132 133

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

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

134 135 136
    private function createDe($data, $information)
    {
        $trans = new GoogleTranslate();
137
        $title = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['title']));
138

139
        if ($data['content'] == null) {
140 141 142
            $data['content'] = 'kosong';
        }

Triyah Fatmawati committed
143
        $content = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'span']));
144 145 146 147 148 149 150 151

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

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

152 153 154
    private function createSa($data, $information)
    {
        $trans = new GoogleTranslate();
155
        $title = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['title']));
156

157
        if ($data['content'] == null) {
158 159 160
            $data['content'] = 'kosong';
        }

Triyah Fatmawati committed
161
        $content = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'span']));
162 163 164 165 166 167 168 169

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

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

170 171 172
    private function createZh($data, $information)
    {
        $trans = new GoogleTranslate();
173
        $title = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['title']));
174

175
        if ($data['content'] == null) {
176 177 178
            $data['content'] = 'kosong';
        }

Triyah Fatmawati committed
179
        $content = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'span']));
180 181 182 183 184 185 186 187

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

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

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

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

212 213 214
        $data = [
            'data' => $data,
            'categories' => $categories,
215
            'manual'=> $manual,
216 217
        ];

218
        return view('webprofile.backend.informations.edit', $data)->withTitle(trans('feature.edit_information'));
219
    }
220 221 222 223 224 225 226 227 228 229 230 231 232
    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,
        ];
233

Siti Aisah committed
234
        return view('webprofile.backend.informations.edit_per_bahasa', $data)->withTitle(trans('feature.edit_information'));
235 236
    }

237 238 239 240

    /**
     * Update the specified resource in storage.
     *
241
     * @param int $id
242
     *
243 244 245 246
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
Triyah Fatmawati committed
247 248 249 250 251
        $request['content'] = strip_tags($request->content, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'span']);
        $request['content_en'] = strip_tags($request->content_en, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'span']);
        $request['content_de'] = strip_tags($request->content_de, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'span']);
        $request['content_sa'] = strip_tags($request->content_sa, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'span']);
        $request['content_zh'] = strip_tags($request->content_zh, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'span']);
252 253 254 255 256
        $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);
257 258 259 260 261 262 263 264 265

        $request->validate([
            'content' => 'required|min:3',
            'event_date' => 'required',
            'keys' => 'max:100'
        ], [
            'content.required' => 'Konten wajib diisi',
            'content.min' => 'Konten terlalu singkat',
            'event_date.required' => 'Tanggal posting wajib diisi',
266
            'keys.max' => 'Keyword terlalu panjang'
267 268
        ]);

269 270
        $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']);
271 272
        // $data['title'] = htmlspecialchars($request->title);
        // $data['content'] = htmlspecialchars($request->content);
273

274 275 276 277 278 279 280 281
        // $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);
282 283
        // $dataEn['content_de'] = htmlspecialchars($request->content_de);
        // $dataEn['content_sa'] = htmlspecialchars($request->content_sa);
284
        // $dataEn['content_zh'] = htmlspecialchars($request->content_zh);
285

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

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

291 292 293 294
        $this->updateEn($dataEn, $information, $request->manual);
        $this->updateDe($dataEn, $information, $request->manual);
        $this->updateSa($dataEn, $information, $request->manual);
        $this->updateZh($dataEn, $information, $request->manual);
295

296
        $this->repo->update($data, $information);
297 298 299
        return redirect()->route('informations.index');
    }

300
    public function updateEn($data, $information, $manual)
301
    {
302
        if($manual==1) {
303
        $dataEn['title'] = strip_tags($data['title_en']);
Triyah Fatmawati committed
304
        $dataEn['content'] = strip_tags($data['content_en'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'span']);
305 306
        }
        else{
307
            if ($data['content'] == null) {
308 309
                $data['content'] = 'kosong';
        }
310

311
        $trans = new GoogleTranslate();
312
        $title = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['title']));
Triyah Fatmawati committed
313
        $content = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'span']));
314

315 316
        $dataEn['title'] = $title;
        $dataEn['content'] = $content;
317

318
    }
319
        $this->repoEn->update($dataEn, $information);
320 321
    }

322
    public function updateDe($data, $information, $manual)
323
    {
324
        if($manual==1) {
325
        $dataDe['title'] = strip_tags($data['title_de']);
Triyah Fatmawati committed
326
        $dataDe['content'] = strip_tags($data['content_de'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'span']);
327 328
        }
        else{
329
            if ($data['content'] == null) {
330 331
                $data['content'] = 'kosong';
            }
332

333
            $trans = new GoogleTranslate();
334
            $title = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['title']));
Triyah Fatmawati committed
335
            $content = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'span']));
336 337 338 339
            $dataDe['title'] = $title;
            $dataDe['content'] = $content;

        }
340 341 342
        $this->repoDe->update($dataDe, $information);
    }

343
    public function updateSa($data, $information, $manual)
344
    {
345
        if($manual==1) {
346
        $dataSa['title'] = strip_tags($data['title_sa']);
Triyah Fatmawati committed
347
        $dataSa['content'] = strip_tags($data['content_sa'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'span']);
348 349 350
        }
        else{

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

            $trans = new GoogleTranslate();
356
            $title = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['title']));
Triyah Fatmawati committed
357
            $content = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'span']));
358

359 360 361
            $dataSa['title'] = $title;
            $dataSa['content'] = $content;
        }
362 363 364 365

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

366
    public function updateZh($data, $information, $manual)
367
    {
368
        if($manual==1) {
369
        $dataZh['title'] = strip_tags($data['title_zh']);
Triyah Fatmawati committed
370
        $dataZh['content'] = strip_tags($data['content_zh'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'span']);
371 372
        }
        else{
373

374
            if ($data['content'] == null) {
375 376
                $data['content'] = 'kosong';
            }
377

378
            $trans = new GoogleTranslate();
379
            $title = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['title']));
Triyah Fatmawati committed
380
            $content = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'span']));
381

382 383 384
            $dataZh['title'] = $title;
            $dataZh['content'] = $content;
        }
385 386 387 388

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

389 390 391
    /**
     * Remove the specified resource from storage.
     *
392 393
     * @param int $id
     *
394 395 396 397
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
398
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
399 400
        $this->repo->destroy($data);

401 402 403 404
        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

405 406 407 408 409 410 411 412
        if ($data->rDe) {
            $this->repoDe->destroy($data->rDe);
        }

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

413 414 415 416
        if ($data->rZh) {
            $this->repoZh->destroy($data->rZh);
        }

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