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 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.pages.index')->withTitle(trans('feature.page'));
    }

    /**
     * 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.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
        // dd($request);
Bagus Pambudi committed
83
        $request['content']  = strip_tags($request->content, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe']);
84
        $request['title']  = strip_tags($request->title);
85 86 87 88 89 90 91 92 93 94 95 96 97 98
        $request->validate([
            'title' => 'required',
            'content' => 'required|min:3',
            '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',
            // 'keys.regex' => 'Keyword tidak valid! Keyword hanya berupa angka dan huruf',
            'keys.max' => 'Keyword terlalu panjang'
        ]);

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

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

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

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

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

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

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

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

        $dataEn['page_id'] = $page->id;
        $dataEn['title'] = $title;
        $dataEn['content'] = $content;

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

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

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

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

        $dataDe['page_id'] = $page->id;
        $dataDe['title'] = $title;
        $dataDe['content'] = $content;

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

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

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

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

        $dataSa['page_id'] = $page->id;
        $dataSa['title'] = $title;
        $dataSa['content'] = $content;

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

173 174 175
    private function createZh($data, $page)
    {
        $trans = new GoogleTranslate();
176
        $title = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['title']));
177

Siti Aisah committed
178
        if ($data['content'] == null) {
179 180 181
            $data['content'] = 'kosong';
        }

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

        $dataZh['page_id'] = $page->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.pages.edit', $data)->withTitle(trans('feature.edit_page'));
222 223
    }

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

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

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

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

        $request->validate([
261
            // 'title' => 'required',
262 263 264 265 266 267 268 269 270 271 272 273
            'content' => 'required|min:3',
            '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',
            // 'keys.regex' => 'Keyword tidak valid! Keyword hanya berupa angka dan huruf',
            'keys.max' => 'Keyword terlalu panjang'
        ]);

274 275
        $data = $request->except(['_token', 'id', 'title_en', 'content_en', 'title_de', 'content_de', 'title_sa', 'content_sa', 'title_zh', 'content_zh', 'manual']);
        $dataEn = $request->except(['_token', 'id', 'manual']);
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
        // $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);
        
291
        $page = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
292
        // $edit = $this->repo->update($data, $page);
293

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

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

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

            $trans = new GoogleTranslate();
315
            $title = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['title']));
Bagus Pambudi committed
316
            $content = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe']));
317 318 319 320

            $dataEn['title'] = $title;
            $dataEn['content'] = $content;
        }
321

322
        $this->repoEn->update($dataEn, $page);
323 324
    }

325
    public function updateDe($data, $page, $manual)
326
    {
327 328 329 330 331
        if($manual==1){
            $dataDe['title'] = $data['title_de'];
            $dataDe['content'] = $data['content_de'];
        }
        else{
Siti Aisah committed
332
            if($data['content']==null){
333 334 335
                $data['content'] = 'kosong';
            }
            $trans = new GoogleTranslate();
336
            $title = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['title']));
Bagus Pambudi committed
337
            $content =  $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe']));
338 339 340 341

            $dataDe['title'] = $title;
            $dataDe['content'] = $content;
        }
342 343 344 345

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

346
    public function updateSa($data, $page, $manual)
347
    {
348 349 350 351 352
        if($manual==1){
            $dataSa['title'] = $data['title_sa'];
            $dataSa['content'] = $data['content_sa'];
        }
        else{
Siti Aisah committed
353
            if($data['content']==null){
354 355
                $data['content'] = 'kosong';
            }
356

357
            $trans = new GoogleTranslate();
358
            $title = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['title']));
Bagus Pambudi committed
359
            $content = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe']));
360 361 362 363 364

            $dataSa['title'] = $title;
            $dataSa['content'] = $content;
        }
        
365 366 367
        $this->repoSa->update($dataSa, $page);
    }

368
    public function updateZh($data, $page, $manual)
369
    {
370 371 372 373 374
        if($manual==1){
            $dataZh['title'] = $data['title_zh'];
            $dataZh['content'] = $data['content_zh'];
        }
        else{
Siti Aisah committed
375
            if($data['content']==null){
376 377 378 379
                $data['content'] = 'kosong';
            }

            $trans = new GoogleTranslate();
380
            $title = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['title']));
Bagus Pambudi committed
381
            $content = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['content'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe']));
382 383 384 385

            $dataZh['title'] = $title;
            $dataZh['content'] = $content;
        }
386 387 388 389

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

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

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

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

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

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

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