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

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

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

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

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

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

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

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

Bagus Pambudi committed
124
        $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','section']));
125 126 127 128 129 130 131 132

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

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

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

Siti Aisah committed
138
        if ($data['content'] == null) {
139 140 141
            $data['content'] = 'kosong';
        }

Bagus Pambudi committed
142
        $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','section']));
143 144 145 146 147 148 149 150

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

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

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

Siti Aisah committed
156
        if ($data['content'] == null) {
157 158 159
            $data['content'] = 'kosong';
        }

Bagus Pambudi committed
160
        $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','section']));
161 162 163 164 165 166 167 168

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

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

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

Siti Aisah committed
174
        if ($data['content'] == null) {
175 176 177
            $data['content'] = 'kosong';
        }

Bagus Pambudi committed
178
        $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','section']));
179 180 181 182 183 184 185 186

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

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

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

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

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

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

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

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

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

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

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

264 265
        $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']);
266 267 268 269 270 271 272 273 274 275 276
        // $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);
277 278
        // $dataEn['content_de'] = htmlspecialchars($request->content_de);
        // $dataEn['content_sa'] = htmlspecialchars($request->content_sa);
279
        // $dataEn['content_zh'] = htmlspecialchars($request->content_zh);
280

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

284 285 286 287 288
        $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);
289

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

293
    public function updateEn($data, $page, $manual)
294
    {
295
        if ($manual==1){
296
            $dataEn['title'] = strip_tags($data['title_en']);
Bagus Pambudi committed
297
            $dataEn['content'] = strip_tags($data['content_en'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section']);
298 299
        }
        else{
Siti Aisah committed
300
            if ($data['content'] == null) {
301 302 303 304
                $data['content'] = 'kosong';
            }

            $trans = new GoogleTranslate();
305
            $title = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['title']));
Bagus Pambudi committed
306
            $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','section']));
307 308 309 310

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

312
        $this->repoEn->update($dataEn, $page);
313 314
    }

315
    public function updateDe($data, $page, $manual)
316
    {
317
        if($manual==1){
318
            $dataDe['title'] = strip_tags($data['title_de']);
Bagus Pambudi committed
319
            $dataDe['content'] = strip_tags($data['content_de'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section']);
320 321
        }
        else{
Siti Aisah committed
322
            if($data['content']==null){
323 324 325
                $data['content'] = 'kosong';
            }
            $trans = new GoogleTranslate();
326
            $title = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['title']));
Bagus Pambudi committed
327
            $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','section']));
328 329 330 331

            $dataDe['title'] = $title;
            $dataDe['content'] = $content;
        }
332 333 334 335

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

336
    public function updateSa($data, $page, $manual)
337
    {
338
        if($manual==1){
339
            $dataSa['title'] = strip_tags($data['title_sa']);
Bagus Pambudi committed
340
            $dataSa['content'] = strip_tags($data['content_sa'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section']);
341 342
        }
        else{
Siti Aisah committed
343
            if($data['content']==null){
344 345
                $data['content'] = 'kosong';
            }
346

347
            $trans = new GoogleTranslate();
348
            $title = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['title']));
Bagus Pambudi committed
349
            $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','section']));
350 351 352 353

            $dataSa['title'] = $title;
            $dataSa['content'] = $content;
        }
354

355 356 357
        $this->repoSa->update($dataSa, $page);
    }

358
    public function updateZh($data, $page, $manual)
359
    {
360
        if($manual==1){
361
            $dataZh['title'] = strip_tags($data['title_zh']);
Bagus Pambudi committed
362
            $dataZh['content'] = strip_tags($data['content_zh'],['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'td', 'th', 'tr', 'iframe','section']);
363 364
        }
        else{
Siti Aisah committed
365
            if($data['content']==null){
366 367 368 369
                $data['content'] = 'kosong';
            }

            $trans = new GoogleTranslate();
370
            $title = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['title']));
Bagus Pambudi committed
371
            $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','section']));
372 373 374 375

            $dataZh['title'] = $title;
            $dataZh['content'] = $content;
        }
376 377 378 379

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

380 381 382
    /**
     * Remove the specified resource from storage.
     *
383 384
     * @param int $id
     *
385 386 387 388
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
389
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa','rZh']);
390 391
        $this->repo->destroy($data);

392 393 394 395
        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

396 397 398 399 400 401 402 403
        if ($data->rDe) {
            $this->repoDe->destroy($data->rDe);
        }

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

404 405 406 407
        if ($data->rZh) {
            $this->repoZh->destroy($data->rZh);
        }

408 409 410
        return response()->json(['done']);
    }
}