PageController.php 14.6 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\PagesRepository as DePagesRepository;
8
use App\Repositories\Webprofile\En\PagesRepository as EnPagesRepository;
9
use App\Repositories\Webprofile\PagesRepository;
10
use App\Repositories\Webprofile\Sa\PagesRepository as SaPagesRepository;
11
use App\Repositories\Webprofile\Zh\PagesRepository as ZhPagesRepository;
12
use Illuminate\Http\Request;
13
use Statickidz\GoogleTranslate;
14 15 16 17

class PageController 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(
        PagesRepository $repo,
31
        EnPagesRepository $repoEn,
32
        DePagesRepository $repoDe,
33 34
        SaPagesRepository $repoSa,
        ZhPagesRepository $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
            return $this->repo->datatable($data);
        }

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
Bagus Pambudi committed
66
        $categories = Categories::pluck('name', 'id');
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

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

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

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

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

Aan Choesni Herlingga committed
99
        $data['slug'] = str_slug($request->input('title'));
100

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

103 104
        if (webprofilesetting()['auto_translate'] == 1) {
            // save translate
Aan Choesni Herlingga committed
105
            if (strlen($data['content']) < 5000) {
106
                $this->createEn($data, $save);
107
                $this->createDe($data, $save);
108
                $this->createSa($data, $save);
109
                $this->createZh($data, $save);
110
            }
111
        }
112 113 114 115

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

116 117 118
    private function createEn($data, $page)
    {
        $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']));
121

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

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

        $dataEn['page_id'] = $page->id;
        $dataEn['title'] = $title;
130
        $dataEn['keys'] = $keys;
131 132 133 134 135
        $dataEn['content'] = $content;

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

136 137 138
    private function createDe($data, $page)
    {
        $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']));
141

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

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

        $dataDe['page_id'] = $page->id;
        $dataDe['title'] = $title;
150
        $dataDe['keys'] = $keys;
151 152 153 154 155
        $dataDe['content'] = $content;

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

156 157 158
    private function createSa($data, $page)
    {
        $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']));
161

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

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

        $dataSa['page_id'] = $page->id;
        $dataSa['title'] = $title;
170
        $dataSa['keys'] = $keys;
171 172 173 174 175
        $dataSa['content'] = $content;

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

176 177 178
    private function createZh($data, $page)
    {
        $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']));
181

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

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

        $dataZh['page_id'] = $page->id;
        $dataZh['title'] = $title;
190
        $dataZh['keys'] = $keys;
191 192 193 194 195
        $dataZh['content'] = $content;

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

196 197 198
    /**
     * Display the specified resource.
     *
199 200
     * @param int $id
     *
201 202 203 204 205 206 207 208 209
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    }

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

220 221 222
        $data = [
            'data' => $data,
            'categories' => $categories,
223
            'manual'=> $manual,
224 225
        ];

226
        return view('webprofile.backend.pages.edit', $data)->withTitle(trans('feature.edit_page'));
227 228
    }

229 230 231
    public function editPerBahasa($id)
    {
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
232
        $categories = Categories::pluck('name', 'id');
233 234 235 236 237 238 239 240 241 242 243
        $manual=1;

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

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

244 245 246
    /**
     * Update the specified resource in storage.
     *
247
     * @param int $id
248
     *
249 250 251 252
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
253 254 255 256 257
        $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;
258 259 260 261
        $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);
262
        $request['title_zh'] = strip_tags($request->title_zh);
263 264 265 266
        $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);
267 268

        $request->validate([
269
            'title' => 'required',
270
            'content' => 'required|min:3',
271
            'keys' => 'required|max:100'
272
        ], [
273
            'title.required' => 'Judul wajib diisi',
274 275
            'content.required' => 'Konten wajib diisi',
            'content.min' => 'Konten terlalu singkat',
276 277
            'keys.max' => 'Keyword terlalu panjang',
            'keys.required' => 'Keyword wajib diisi'
278 279
        ]);

280
        $data = $request->except(['_token', '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', 'manual']);
281
        $dataEn = $request->except(['_token', 'id', 'manual']);
282 283 284 285 286 287 288 289 290 291 292
        // $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);
293 294
        // $dataEn['content_de'] = htmlspecialchars($request->content_de);
        // $dataEn['content_sa'] = htmlspecialchars($request->content_sa);
295
        // $dataEn['content_zh'] = htmlspecialchars($request->content_zh);
296

297
        $page = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
298
        // $edit = $this->repo->update($data, $page);
299

300 301 302 303 304
        $this->updateEn($dataEn, $page, $request->manual);
        $this->updateDe($dataEn, $page, $request->manual);
        $this->updateSa($dataEn, $page, $request->manual);
        $this->updateZh($dataEn, $page, $request->manual);
        $this->repo->update($data, $page);
305

306 307 308
        return redirect()->route('pages.index');
    }

309
    public function updateEn($data, $page, $manual)
310
    {
311
        if ($manual==1){
312
            $dataEn['title'] = strip_tags($data['title_en']);
313
            $dataEn['keys'] = strip_tags($data['keys_en']);
314
            $dataEn['content'] = $data['content_en'];
315 316
        }
        else{
Siti Aisah committed
317
            if ($data['content'] == null) {
318 319 320 321
                $data['content'] = 'kosong';
            }

            $trans = new GoogleTranslate();
322
            $title = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['title']));
323 324
            $keys = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['keys']));

325
            if (strlen($data['content']) < 5000) {
326
            $content = $trans->translate($this->SOURCE, $this->TARGET, $data['content']);
327
            $dataEn['content'] = $content;
328
            }
329

330
            $dataEn['title'] = $title;
331
            $dataEn['keys'] = $keys;
332
        }
333

334
        $this->repoEn->update($dataEn, $page);
335 336
    }

337
    public function updateDe($data, $page, $manual)
338
    {
339
        if($manual==1){
340
            $dataDe['title'] = strip_tags($data['title_de']);
341
            $dataDe['keys'] = strip_tags($data['keys_de']);
342
            $dataDe['content'] = $data['content_de'];
343 344
        }
        else{
Siti Aisah committed
345
            if($data['content']==null){
346 347 348
                $data['content'] = 'kosong';
            }
            $trans = new GoogleTranslate();
349
            $title = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['title']));
350 351
            $keys = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['keys']));
            
352
            if (strlen($data['content']) < 5000) {
353
            $content =  $trans->translate($this->SOURCE, $this->TARGETDE, $data['content']);
354
            $dataDe['content'] = $content;
355
            }
356
            
357
            $dataDe['title'] = $title;
358
            $dataDe['keys'] = $keys;
359
        }
360 361 362 363

        $this->repoDe->update($dataDe, $page);
    }

364
    public function updateSa($data, $page, $manual)
365
    {
366
        if($manual==1){
367
            $dataSa['title'] = strip_tags($data['title_sa']);
368
            $dataSa['keys'] = strip_tags($data['keys_sa']);
369
            $dataSa['content'] = $data['content_sa'];
370 371
        }
        else{
Siti Aisah committed
372
            if($data['content']==null){
373 374
                $data['content'] = 'kosong';
            }
375

376
            $trans = new GoogleTranslate();
377
            $title = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['title']));
378 379
            $keys = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['keys']));

380
            if (strlen($data['content']) < 5000) {
381
            $content = $trans->translate($this->SOURCE, $this->TARGETSA, $data['content']);
382
            $dataSa['content'] = $content;
383
            }
384

385
            $dataSa['title'] = $title;
386
            $dataSa['keys'] = $keys;
387
        }
388

389 390 391
        $this->repoSa->update($dataSa, $page);
    }

392
    public function updateZh($data, $page, $manual)
393
    {
394
        if($manual==1){
395
            $dataZh['title'] = strip_tags($data['title_zh']);
396
            $dataZh['keys'] = strip_tags($data['keys_zh']);
397
            $dataZh['content'] = $data['content_zh'];
398 399
        }
        else{
Siti Aisah committed
400
            if($data['content']==null){
401 402 403 404
                $data['content'] = 'kosong';
            }

            $trans = new GoogleTranslate();
405
            $title = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['title']));
406 407
            $keys = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['keys']));

408
            if (strlen($data['content']) < 5000) {
409
            $content = $trans->translate($this->SOURCE, $this->TARGETZH, $data['content']);
410
            $dataZh['content'] = $content;
411
            }
412

413
            $dataZh['title'] = $title;
414
            $dataZh['keys'] = $keys;
415
        }
416 417 418 419

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

420 421 422
    /**
     * Remove the specified resource from storage.
     *
423 424
     * @param int $id
     *
425 426 427 428
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
429
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa','rZh']);
430 431
        $this->repo->destroy($data);

432 433 434 435
        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

436 437 438 439 440 441 442 443
        if ($data->rDe) {
            $this->repoDe->destroy($data->rDe);
        }

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

444 445 446 447
        if ($data->rZh) {
            $this->repoZh->destroy($data->rZh);
        }

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