PostController.php 19.8 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\AutopostRepository;
8
use App\Repositories\Webprofile\De\PostRepository as DePostRepository;
9
use App\Repositories\Webprofile\En\PostRepository as EnPostRepository;
10
use App\Repositories\Webprofile\PostRepository;
11
use App\Repositories\Webprofile\Sa\PostRepository as SaPostRepository;
12
use App\Repositories\Webprofile\SocialSharingRepository;
13
use App\Repositories\Webprofile\Zh\PostRepository as ZhPostRepository;
14 15
use GuzzleHttp\Client;
use Illuminate\Http\Request;
16
use Illuminate\Support\Facades\Config;
17
use Illuminate\Support\Str;
18
use Statickidz\GoogleTranslate;
19 20 21 22

class PostController extends Controller
{
    private $repo;
23
    private $repoEn;
24
    private $repoDe;
25
    private $repoSa;
26
    private $repoZh;
27

28 29
    private $SOURCE = 'id';
    private $TARGET = 'en';
30
    private $TARGETDE = 'de';
31
    private $TARGETSA = 'ar';
32
    private $TARGETZH = 'zh';
33 34 35

    public function __construct(
        PostRepository $repo,
36
        EnPostRepository $repoEn,
37
        DePostRepository $repoDe,
38
        SaPostRepository $repoSa,
39
        ZhPostRepository $repoZh,
40 41
        SocialSharingRepository $socialRepo,
        AutopostRepository $autoPostRepo
42
    ) {
43
        $this->repo = $repo;
44
        $this->repoEn = $repoEn;
45
        $this->socialRepo = $socialRepo;
46
        $this->autoPostRepo = $autoPostRepo;
47
        $this->repoDe = $repoDe;
48
        $this->repoSa = $repoSa;
49
        $this->repoZh = $repoZh;
50
    }
51

52 53 54 55 56 57 58 59
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
60
            $data = $this->repo->get(['rEn']);
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
            return $this->repo->datatable($data);
        }

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

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

    /**
     * Store a newly created resource in storage.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
Triyah Fatmawati committed
91
        $request['content'] = strip_tags($request->content, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'iframe','section', 'span']);
92
        $request['title'] = strip_tags($request->title);
93 94 95 96 97
        $request->validate([
            'title' => 'required',
            'categories' => 'required',
            'content' => 'required|min:3',
            'post_date' => 'required',
98
            'keys' => 'max:100',
99 100
            'thumbnail' => 'mimes:jpg,jpeg,png|max : 3072'

101 102 103 104 105 106 107 108 109
        ], [
            'title.required' => 'Judul wajib diisi',
            'categories.required' => 'Kategori wajib diisi',
            'content.required' => 'Konten wajib diisi',
            'content.min' => 'Konten terlalu singkat',
            'post_date.required' => 'Tanggal posting wajib diisi',
            'keys.max' => 'Keyword terlalu panjang'
        ]);

110
        $data = $request->except('_token');
111
        // $data['content'] = htmlspecialchars($request->content);
112 113 114

        array_key_exists('post_status', $data) ? $data['post_status'] = 1 : $data['post_status'] = 0;
        array_key_exists('cover_status', $data) ? $data['cover_status'] = 1 : $data['cover_status'] = 0;
115
        $data['slug'] = Str::slug($request->input('title'));
116

117 118 119 120
        $save = $this->repo->store($data);

        $tipe = 'thumbnail';
        if ($request->hasFile($tipe)) {
121
            $img[$tipe] = $save->id.'.'.$request->file($tipe)->guessClientExtension();
122 123 124 125

            $this->repo->upload($img[$tipe], $request, $tipe);
            $this->repo->update($img, $save);
        }
126 127 128

        if (webprofilesetting()['auto_translate'] == 1) {
            // save translate
Aan Choesni Herlingga committed
129
            if (strlen($data['content']) < 5000) {
130
                $this->createEn($data, $save);
131
                $this->createDe($data, $save);
132
                $this->createSa($data, $save);
133
                $this->createZh($data, $save);
134
            }
135
        }
136

137 138
        $this->share($data, $request);

139 140 141
        return redirect()->route('posts.index');
    }

142 143 144
    public function shorterUrl($long_url)
    {
        $client = new Client();
145
        $URI = 'http://unesa.me/create.php';
146 147 148 149 150 151 152 153 154
        $params['form_params'] = ['token' => env('TOKEN_URL', 'forge'), 'url_value' => $long_url];
        $response = $client->post($URI, $params);
        $responseJson = json_decode($response->getBody(), true);

        $url = $responseJson['url'];

        return $url;
    }

155 156 157
    public function share($data, $request)
    {

Bagus Pambudi committed
158 159
        $client = new Client([
            'headers' => [
160
                'Accept' => 'application/json',
Bagus Pambudi committed
161 162 163 164 165 166
                // 'Authorization' => 'Bearer ' . session('token')
            ],
            'form_params' => ['url' => url('post/'.$data['slug'])]
        ]);
        $result1      = $client->post('https://alia.unesa.ac.id/api/posts');
        $value  = json_decode($result1->getBody()->getContents(), true);
167

Bagus Pambudi committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
        // $setting = webprofilesetting();
        // $autoPost = $this->autoPostRepo->get();

        // if ($request->hasFile('thumbnail')) {
        //     $img = $request->file('thumbnail')->getRealPath();
        // } else {
        //     $img = null;
        // }
        // $title = html_entity_decode($data['title']);
        // $url = $this->shorterUrl(url('post/'.$data['slug']));
        // $keys = $data['keys'];

        // $olhashtag = explode(', ', str_replace([', ', ','], ', ', $keys));

        // $hashtag = '#unesa #unesaterkini ';
        // foreach ($olhashtag as $value) {
        //     $hashtag .= '#'.trim($value).' ';
        // }
        // dd($autoPost);
187

Bagus Pambudi committed
188
        // foreach ($autoPost as $value) {
189 190 191 192 193 194 195 196 197 198
            //Social Share Facebook
            // Config::set('larasap.facebook', [
            //     'app_id' => $setting['facebook_app_id'],
            //     'app_secret' => $setting['facebook_app_secret'],
            //     'default_graph_version' => null,
            //     'page_access_token' => null,
            // ]);

            // $this->socialRepo->sendLinkToFacebook(url('post/'.$data['slug']), $data['content']);

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
            // if ($value->type == 'twitter') {
            //     //Social Share Twitter
            //     Config::set('larasap.twitter', [
            //         'consurmer_key' => $value->key_1,
            //         'consurmer_secret' => $value->key_2,
            //         'access_token' => $value->key_3,
            //         'access_token_secret' => $value->key_4,
            //     ]);

            //     if ($img == '') {
            //         $this->socialRepo->sendTextTweet($setting['header_admin'].'-'.$title."\n\n".$url."\n\n".$hashtag);
            //     } else {
            //         $this->socialRepo->sendTweetWithMedia($setting['header_admin'].'-'.$title."\n\n".$url."\n\n".$hashtag, [$img]);
            //     }
            // }
Bagus Pambudi committed
214
        // }
215 216
    }

217 218 219
    private function createEn($data, $post)
    {
        $trans = new GoogleTranslate();
220
        $title = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['title']));
221

Siti Aisah committed
222
        if ($data['content'] == null) {
223 224 225
            $data['content'] = 'kosong';
        }

Triyah Fatmawati committed
226
        $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', 'iframe','section', 'span']));
227 228 229 230 231 232 233 234

        $dataEn['post_id'] = $post->id;
        $dataEn['title'] = $title;
        $dataEn['content'] = $content;

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

235 236 237
    private function createDe($data, $post)
    {
        $trans = new GoogleTranslate();
238
        $title = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['title']));
239

Siti Aisah committed
240
        if ($data['content'] == null) {
241 242 243
            $data['content'] = 'kosong';
        }

Triyah Fatmawati committed
244
        $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', 'iframe','section', 'span']));
245 246 247 248 249 250 251 252

        $dataDe['post_id'] = $post->id;
        $dataDe['title'] = $title;
        $dataDe['content'] = $content;

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

253 254 255
    private function createSa($data, $post)
    {
        $trans = new GoogleTranslate();
256
        $title = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['title']));
257

Siti Aisah committed
258
        if ($data['content'] == null) {
259 260 261
            $data['content'] = 'kosong';
        }

Triyah Fatmawati committed
262
        $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', 'iframe','section', 'span']));
263 264 265 266 267 268 269 270

        $dataSa['post_id'] = $post->id;
        $dataSa['title'] = $title;
        $dataSa['content'] = $content;

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

271 272 273
    private function createZh($data, $post)
    {
        $trans = new GoogleTranslate();
274
        $title = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['title']));
275

Siti Aisah committed
276
        if ($data['content'] == null) {
277 278 279
            $data['content'] = 'kosong';
        }

Triyah Fatmawati committed
280
        $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', 'iframe','section','span']));
281 282 283 284 285 286 287 288

        $dataZh['post_id'] = $post->id;
        $dataZh['title'] = $title;
        $dataZh['content'] = $content;

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

289 290 291
    /**
     * Display the specified resource.
     *
292 293
     * @param int $id
     *
294 295 296 297 298 299 300 301 302
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    }

    /**
     * Show the form for editing the specified resource.
     *
303 304
     * @param int $id
     *
305 306 307 308
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
309
        $setting = webprofilesetting();
310
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
311
        $categories = Categories::pluck('name', 'id');
312
        $manual=0;
313

314 315 316
        $data = [
            'data' => $data,
            'categories' => $categories,
317
            'setting' => $setting,
318
            'manual'=> $manual,
319
        ];
320

321
        return view('webprofile.backend.posts.edit', $data)->withTitle(trans('feature.edit_post'));
322
    }
323 324 325 326 327 328 329 330 331 332 333 334 335
    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,
        ];
336

337 338
        return view('webprofile.backend.posts.edit_per_bahasa', $data)->withTitle(trans('feature.edit_post'));
    }
339 340 341 342

    /**
     * Update the specified resource in storage.
     *
343
     * @param int $id
344
     *
345 346 347 348
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
Triyah Fatmawati committed
349 350 351 352 353
        $request['content'] = strip_tags($request->content, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'iframe','section', 'span']);
        $request['content_en'] = strip_tags($request->content_en, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'iframe','section', 'span']);
        $request['content_de'] = strip_tags($request->content_de, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'iframe','section', 'span']);
        $request['content_sa'] = strip_tags($request->content_sa, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'iframe','section', 'span']);
        $request['content_zh'] = strip_tags($request->content_zh, ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'iframe','section', 'span']);
354 355 356 357 358
        $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);
359 360 361 362 363

        $request->validate([
            'categories' => 'required',
            'content' => 'required|min:3',
            'post_date' => 'required',
364
            'keys' => 'max:100',
Triyah Fatmawati committed
365
            'thumbnail' => 'mimes:jpg,jpeg,png|max : 3072'
366 367 368 369 370 371 372 373 374
        ], [
            'categories.required' => 'Kategori wajib diisi',
            'content.required' => 'Konten wajib diisi',
            'content.min' => 'Konten terlalu singkat',
            'post_date.required' => 'Tanggal posting wajib diisi',
            'keys.max' => 'Keyword terlalu panjang'
        ]);


375 376
        $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']);
377 378 379 380 381 382
        // $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);
383
        // $dataEn['title_de'] = htmlspecialchars($request->title_de);
384 385 386 387
        // $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);
388 389 390 391
        // $dataEn['content_de'] = htmlspecialchars($request->content_de);
        // $dataEn['content_sa'] = htmlspecialchars($request->content_sa);
        // $dataEn['content_zh'] = htmlspecialchars($request->content_zh);

392 393
        array_key_exists('post_status', $data) ? $data['post_status'] = 1 : $data['post_status'] = 0;
        array_key_exists('cover_status', $data) ? $data['cover_status'] = 1 : $data['cover_status'] = 0;
394
        $data['slug'] = Str::slug($request->input('title'));
395

396
        $post = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
397
        $edit = $this->repo->update($data, $post);
398

399 400
        $tipe = 'thumbnail';
        if ($request->hasFile($tipe)) {
401
            $img[$tipe] = $post->id.'.'.$request->file($tipe)->guessClientExtension();
402 403 404 405

            $this->repo->upload($img[$tipe], $request, $tipe);
            $this->repo->update($img, $post);
        }
406

407 408 409 410
        $this->updateEn($dataEn, $post, $request->manual);
        $this->updateDe($dataEn, $post, $request->manual);
        $this->updateSa($dataEn, $post, $request->manual);
        $this->updateZh($dataEn, $post, $request->manual);
411

412 413 414
        return redirect()->route('posts.index');
    }

415
    public function updateEn($data, $post, $manual)
416
    {
417
        if($manual==1){
418
            $dataEn['title'] = strip_tags($data['title_en']);
Triyah Fatmawati committed
419
            $dataEn['content'] = strip_tags($data['content_en'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'iframe','section', 'span']);
420 421
        }
        else{
422

Siti Aisah committed
423
            if ($data['content'] == null) {
424 425
                $data['content'] = 'kosong';
            }
426

427
        $trans = new GoogleTranslate();
428
        $title = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['title']));
429
        if (strlen($data['content']) < 5000) {
Triyah Fatmawati committed
430
        $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', 'iframe','section', 'span']));
431
        $dataEn['content'] = $content;
432 433
        }
        $dataEn['title'] = $title;
434
    }
435
        $this->repoEn->update($dataEn, $post);
436 437
    }

438
    public function updateDe($data, $post, $manual)
439
    {
440
        if($manual==1){
441
            $dataDe['title'] = strip_tags($data['title_de']);
Triyah Fatmawati committed
442
            $dataDe['content'] = strip_tags($data['content_de'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'iframe','section', 'span']);
443 444
        }
        else{
445

446 447
            if ($data['content'] == null) {
                $data['content'] = 'kosong';
448
            }
449 450 451

            $trans = new GoogleTranslate();
            $title = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['title']));
452
            if (strlen($data['content']) < 5000) {
Triyah Fatmawati committed
453
            $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', 'iframe','section', 'span']));
454
            $dataDe['content'] = $content;
455 456
            }
            $dataDe['title'] = $title;
457
        }
458 459 460
        $this->repoDe->update($dataDe, $post);
    }

461
    public function updateSa($data, $post, $manual)
462
    {
463
        if($manual==1){
464
            $dataSa['title'] = strip_tags($data['title_sa']);
Triyah Fatmawati committed
465
            $dataSa['content'] = strip_tags($data['content_sa'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'iframe','section', 'span']);
466 467
        }
        else{
468

469 470
            if ($data['content'] == null) {
                $data['content'] = 'kosong';
471
            }
472 473 474

            $trans = new GoogleTranslate();
            $title = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['title']));
475
            if (strlen($data['content']) < 5000) {
Triyah Fatmawati committed
476
            $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', 'iframe','section', 'span']));
477
            $dataSa['content'] = $content;
478 479
            }
            $dataSa['title'] = $title;
480
        }
481 482 483
        $this->repoSa->update($dataSa, $post);
    }

484
    public function updateZh($data, $post, $manual)
485
    {
486
        if($manual==1){
487
            $dataZh['title'] = strip_tags($data['title_zh']);
Triyah Fatmawati committed
488
            $dataZh['content'] = strip_tags($data['content_zh'], ['a', 'br', 'p', 'b', 'i', 'u', 'ul', 'li', 'ol', 'img', 'table', 'tr', 'td', 'th', 'iframe','section', 'span']);
489 490 491 492
        }
        else{
            if ($data['content'] == null) {
                $data['content'] = 'kosong';
493
            }
494 495 496

            $trans = new GoogleTranslate();
            $title = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['title']));
497
            if (strlen($data['content']) < 5000) {
Triyah Fatmawati committed
498
            $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', 'iframe','section', 'span']));
499
            $dataZh['content'] = $content;
500 501
            }
            $dataZh['title'] = $title;
502
        }
503 504 505
        $this->repoZh->update($dataZh, $post);
    }

506 507 508
    /**
     * Remove the specified resource from storage.
     *
509 510
     * @param int $id
     *
511 512 513 514
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
515
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
516
        $this->repo->destroy($data, 'thumbnail');
517
        $this->repo->deletefile($data, 'thumbnail');
518

519 520 521 522
        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

523 524 525 526 527 528 529 530
        if ($data->rDe) {
            $this->repoDe->destroy($data->rDe);
        }

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

531 532 533 534
        if ($data->rZh) {
            $this->repoZh->destroy($data->rZh);
        }

535 536 537
        return response()->json(['done']);
    }
}