PostController.php 14.7 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 91 92 93 94
            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)
    {
        $data = $request->except('_token');

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

97 98 99 100
        $save = $this->repo->store($data);

        $tipe = 'thumbnail';
        if ($request->hasFile($tipe)) {
101
            $img[$tipe] = $save->id.'.'.$request->file($tipe)->guessClientExtension();
102 103 104 105

            $this->repo->upload($img[$tipe], $request, $tipe);
            $this->repo->update($img, $save);
        }
106 107 108

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

117 118
        $this->share($data, $request);

119 120 121
        return redirect()->route('posts.index');
    }

122 123 124
    public function shorterUrl($long_url)
    {
        $client = new Client();
125
        $URI = 'http://unesa.me/create.php';
126 127 128 129 130 131 132 133 134
        $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;
    }

135 136 137
    public function share($data, $request)
    {

Bagus Pambudi committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        $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);
167

Bagus Pambudi committed
168
        // foreach ($autoPost as $value) {
169 170 171 172 173 174 175 176 177 178
            //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']);

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
            // 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
194 195 196



Bagus Pambudi committed
197
            
Bagus Pambudi committed
198 199
    

Bagus Pambudi committed
200
        // }
201 202
    }

203 204 205 206
    private function createEn($data, $post)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGET, $data['title']);
207 208 209 210 211

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

212 213 214 215 216 217 218 219 220
        $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);
    }

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    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);
    }

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    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);
    }

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    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);
    }

275 276 277
    /**
     * Display the specified resource.
     *
278 279
     * @param int $id
     *
280 281 282 283 284 285 286 287 288
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    }

    /**
     * Show the form for editing the specified resource.
     *
289 290
     * @param int $id
     *
291 292 293 294
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
295
        $setting = webprofilesetting();
296
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
297
        $categories = Categories::pluck('name', 'id');
298
        $manual=0;
299

300 301 302
        $data = [
            'data' => $data,
            'categories' => $categories,
303
            'setting' => $setting,
304
            'manual'=> $manual,
305
        ];
306
        
307
        return view('webprofile.backend.posts.edit', $data)->withTitle(trans('feature.edit_post'));
308
    }
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    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'));
    }
325 326 327 328

    /**
     * Update the specified resource in storage.
     *
329
     * @param int $id
330
     *
331 332 333 334
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
335 336
        $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']);
337 338 339

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

342
        $post = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
343
        $edit = $this->repo->update($data, $post);
344
        // dd($post);
345 346
        $tipe = 'thumbnail';
        if ($request->hasFile($tipe)) {
347
            $img[$tipe] = $post->id.'.'.$request->file($tipe)->guessClientExtension();
348 349 350 351

            $this->repo->upload($img[$tipe], $request, $tipe);
            $this->repo->update($img, $post);
        }
352
        // dd($data);
353 354 355 356
        $this->updateEn($dataEn, $post, $request->manual);
        $this->updateDe($dataEn, $post, $request->manual);
        $this->updateSa($dataEn, $post, $request->manual);
        $this->updateZh($dataEn, $post, $request->manual);
357

358 359 360
        return redirect()->route('posts.index');
    }

361
    public function updateEn($data, $post, $manual)
362
    {
363 364 365
        // dd($manual);
        
        if($manual==1){
366 367 368 369
        $dataEn['title'] = $data['title_en'];
        $dataEn['content'] = $data['content_en'];
        }
        else{
370 371 372 373 374

            if (strip_tags($data['content']) == null) {
                $data['content'] = 'kosong';
            }
    
375 376 377 378 379 380 381
        $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']);
382
    }
383
        $this->repoEn->update($dataEn, $post);
384 385
    }

386
    public function updateDe($data, $post, $manual)
387
    {
388
        if($manual==1){
389 390 391 392
            $dataDe['title'] = $data['title_de'];
            $dataDe['content'] = $data['content_de'];
            }
            else{
393 394 395 396 397

                if (strip_tags($data['content']) == null) {
                    $data['content'] = 'kosong';
                }
        
398 399 400 401 402
        $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;
403
            }
404 405 406
        $this->repoDe->update($dataDe, $post);
    }

407
    public function updateSa($data, $post, $manual)
408
    {
409
        if($manual==1){
410 411 412 413
            $dataSa['title'] = $data['title_sa'];
            $dataSa['content'] = $data['content_sa'];
            }
            else{
414 415 416 417 418

                if (strip_tags($data['content']) == null) {
                    $data['content'] = 'kosong';
                }
        
419 420 421 422 423
        $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;
424
            }
425 426 427
        $this->repoSa->update($dataSa, $post);
    }

428
    public function updateZh($data, $post, $manual)
429
    {
430
        if($manual==1){
431 432 433 434
            $dataZh['title'] = $data['title_zh'];
            $dataZh['content'] = $data['content_zh'];
            }
            else{
435 436 437 438
                if (strip_tags($data['content']) == null) {
                    $data['content'] = 'kosong';
                }
                
439 440 441 442 443
        $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;
444
            }
445 446 447
        $this->repoZh->update($dataZh, $post);
    }

448 449 450
    /**
     * Remove the specified resource from storage.
     *
451 452
     * @param int $id
     *
453 454 455 456
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
457
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
458
        $this->repo->destroy($data, 'thumbnail');
459
        $this->repo->deletefile($data, 'thumbnail');
460

461 462 463 464
        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

465 466 467 468 469 470 471 472
        if ($data->rDe) {
            $this->repoDe->destroy($data->rDe);
        }

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

473 474 475 476
        if ($data->rZh) {
            $this->repoZh->destroy($data->rZh);
        }

477 478 479
        return response()->json(['done']);
    }
}