PostController.php 17.9 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)
    {
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        // dd($request);
        // $request['content']  = strip_tags($request->content);
        $request->validate([
            'title' => 'required',
            'categories' => 'required',
            'content' => 'required|min:3',
            'post_date' => 'required',
            'keys' => 'max:100'
        ], [
            'title.required' => 'Judul wajib diisi',
            // 'title.regex' => 'Judul tidak valid! Judul hanya berupa angka dan huruf',
            // 'title.max' => 'Judul terlalu panjang',
            'categories.required' => 'Kategori wajib diisi',
            'content.required' => 'Konten wajib diisi',
            'content.min' => 'Konten terlalu singkat',
            'post_date.required' => 'Tanggal posting wajib diisi',
            // 'keys.regex' => 'Keyword tidak valid! Keyword hanya berupa angka dan huruf',
            'keys.max' => 'Keyword terlalu panjang'
        ]);

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

        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;
116
        $data['slug'] = Str::slug($request->input('title'));
117

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

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

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

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

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

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

143 144 145
    public function shorterUrl($long_url)
    {
        $client = new Client();
146
        $URI = 'http://unesa.me/create.php';
147 148 149 150 151 152 153 154 155
        $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;
    }

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

Bagus Pambudi committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
        $client = new Client([
            'headers' => [
                'Accept' => 'application/json', 
                // '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);
               
        // $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);
188

Bagus Pambudi committed
189
        // foreach ($autoPost as $value) {
190 191 192 193 194 195 196 197 198 199
            //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']);

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
            // 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
215 216 217



Bagus Pambudi committed
218
            
Bagus Pambudi committed
219 220
    

Bagus Pambudi committed
221
        // }
222 223
    }

224 225 226 227
    private function createEn($data, $post)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGET, $data['title']);
228 229 230 231 232

        if (strip_tags($data['content']) == null) {
            $data['content'] = 'kosong';
        }

233 234 235 236 237 238 239 240 241
        $content = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['content']));

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

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

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    private function createDe($data, $post)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETDE, $data['title']);

        if (strip_tags($data['content']) == null) {
            $data['content'] = 'kosong';
        }

        $content = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['content']));

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

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

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    private function createSa($data, $post)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETSA, $data['title']);

        if (strip_tags($data['content']) == null) {
            $data['content'] = 'kosong';
        }

        $content = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['content']));

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

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

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    private function createZh($data, $post)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETZH, $data['title']);

        if (strip_tags($data['content']) == null) {
            $data['content'] = 'kosong';
        }

        $content = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['content']));

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

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

296 297 298
    /**
     * Display the specified resource.
     *
299 300
     * @param int $id
     *
301 302 303 304 305 306 307 308 309
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    }

    /**
     * Show the form for editing the specified resource.
     *
310 311
     * @param int $id
     *
312 313 314 315
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
316
        $setting = webprofilesetting();
317
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
318
        $categories = Categories::pluck('name', 'id');
319
        $manual=0;
320

321 322 323
        $data = [
            'data' => $data,
            'categories' => $categories,
324
            'setting' => $setting,
325
            'manual'=> $manual,
326
        ];
327
        
328
        return view('webprofile.backend.posts.edit', $data)->withTitle(trans('feature.edit_post'));
329
    }
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    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,
        ];
        
        return view('webprofile.backend.posts.edit_per_bahasa', $data)->withTitle(trans('feature.edit_post'));
    }
346 347 348 349

    /**
     * Update the specified resource in storage.
     *
350
     * @param int $id
351
     *
352 353 354 355
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
        // $request['content'] = strip_tags($request->content);
        // $request['content_en'] = strip_tags($request->content_en);
        // $request['content_de'] = strip_tags($request->content_de);
        // $request['content_sa'] = strip_tags($request->content_sa);
        // $request['content_zh'] = strip_tags($request->content_zh);

        $request->validate([
            'title' => 'required',
            'categories' => 'required',
            'content' => 'required|min:3',
            'post_date' => 'required',
            'keys' => 'max:100'
        ], [
            'title.required' => 'Judul wajib diisi',
            // 'title.regex' => 'Judul tidak valid! Judul hanya berupa angka dan huruf',
            // 'title.max' => 'Judul terlalu panjang',
            'categories.required' => 'Kategori wajib diisi',
            'content.required' => 'Konten wajib diisi',
            'content.min' => 'Konten terlalu singkat',
            'post_date.required' => 'Tanggal posting wajib diisi',
            // 'keys.regex' => 'Keyword tidak valid! Keyword hanya berupa angka dan huruf',
            'keys.max' => 'Keyword terlalu panjang'
        ]);


381 382
        $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']);
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
        // $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); 
        
398 399
        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;
400
        $data['slug'] = Str::slug($request->input('title'));
401
        // dd($dataEn);
402
        $post = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
403
        $edit = $this->repo->update($data, $post);
404
        // dd($post);
405 406
        $tipe = 'thumbnail';
        if ($request->hasFile($tipe)) {
407
            $img[$tipe] = $post->id.'.'.$request->file($tipe)->guessClientExtension();
408 409 410 411

            $this->repo->upload($img[$tipe], $request, $tipe);
            $this->repo->update($img, $post);
        }
412
        // dd($dataEn);
413 414 415 416
        $this->updateEn($dataEn, $post, $request->manual);
        $this->updateDe($dataEn, $post, $request->manual);
        $this->updateSa($dataEn, $post, $request->manual);
        $this->updateZh($dataEn, $post, $request->manual);
417

418 419 420
        return redirect()->route('posts.index');
    }

421
    public function updateEn($data, $post, $manual)
422
    {
423 424 425
        // dd($manual);
        
        if($manual==1){
426 427 428 429
        $dataEn['title'] = $data['title_en'];
        $dataEn['content'] = $data['content_en'];
        }
        else{
430 431 432 433 434

            if (strip_tags($data['content']) == null) {
                $data['content'] = 'kosong';
            }
    
435 436 437 438 439 440 441
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGET, $data['title']);
        $content = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['content']));
        // dd($title);
        $dataEn['title'] = $title;
        $dataEn['content'] = $content;
        // dd($dataEn['title']);
442
    }
443
        $this->repoEn->update($dataEn, $post);
444 445
    }

446
    public function updateDe($data, $post, $manual)
447
    {
448
        if($manual==1){
449 450 451 452
            $dataDe['title'] = $data['title_de'];
            $dataDe['content'] = $data['content_de'];
            }
            else{
453 454 455 456 457

                if (strip_tags($data['content']) == null) {
                    $data['content'] = 'kosong';
                }
        
458 459 460 461 462
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETDE, $data['title']);
        $content = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['content']));
        $dataDe['title'] = $title;
        $dataDe['content'] = $content;
463
            }
464 465 466
        $this->repoDe->update($dataDe, $post);
    }

467
    public function updateSa($data, $post, $manual)
468
    {
469
        if($manual==1){
470 471 472 473
            $dataSa['title'] = $data['title_sa'];
            $dataSa['content'] = $data['content_sa'];
            }
            else{
474 475 476 477 478

                if (strip_tags($data['content']) == null) {
                    $data['content'] = 'kosong';
                }
        
479 480 481 482 483
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETSA, $data['title']);
        $content = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['content']));
        $dataSa['title'] = $title;
        $dataSa['content'] = $content;
484
            }
485 486 487
        $this->repoSa->update($dataSa, $post);
    }

488
    public function updateZh($data, $post, $manual)
489
    {
490
        if($manual==1){
491 492 493 494
            $dataZh['title'] = $data['title_zh'];
            $dataZh['content'] = $data['content_zh'];
            }
            else{
495 496 497 498
                if (strip_tags($data['content']) == null) {
                    $data['content'] = 'kosong';
                }
                
499 500 501 502 503
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETZH, $data['title']);
        $content = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['content']));
        $dataZh['title'] = $title;
        $dataZh['content'] = $content;
504
            }
505 506 507
        $this->repoZh->update($dataZh, $post);
    }

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

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

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

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

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

537 538 539
        return response()->json(['done']);
    }
}