PostController.php 11.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 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

299 300 301
        $data = [
            'data' => $data,
            'categories' => $categories,
302
            'setting' => $setting,
303 304
        ];

305
        return view('webprofile.backend.posts.edit', $data)->withTitle(trans('feature.edit_post'));
306 307 308 309 310
    }

    /**
     * Update the specified resource in storage.
     *
311
     * @param int $id
312
     *
313 314 315 316
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
317
        $data = $request->except(['_token', 'id', 'title_en', 'content_en', 'title_de', 'content_de', 'title_sa', 'content_sa', 'title_zh', 'content_zh']);
318
        $dataEn = $request->except(['_token', 'id']);
319 320 321

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

324
        $post = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
325 326 327 328
        $edit = $this->repo->update($data, $post);

        $tipe = 'thumbnail';
        if ($request->hasFile($tipe)) {
329
            $img[$tipe] = $post->id.'.'.$request->file($tipe)->guessClientExtension();
330 331 332 333

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

335
        $this->updateEn($dataEn, $post);
336
        $this->updateDe($dataEn, $post);
337
        $this->updateSa($dataEn, $post);
338
        $this->updateZh($dataEn, $post);
339

340 341 342
        return redirect()->route('posts.index');
    }

343 344 345 346 347
    public function updateEn($data, $post)
    {
        $dataEn['title'] = $data['title_en'];
        $dataEn['content'] = $data['content_en'];

348
        $this->repoEn->update($dataEn, $post);
349 350
    }

351 352 353 354 355 356 357 358
    public function updateDe($data, $post)
    {
        $dataDe['title'] = $data['title_de'];
        $dataDe['content'] = $data['content_de'];

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

359 360 361 362 363 364 365 366
    public function updateSa($data, $post)
    {
        $dataSa['title'] = $data['title_sa'];
        $dataSa['content'] = $data['content_sa'];

        $this->repoSa->update($dataSa, $post);
    }

367 368 369 370 371 372 373 374
    public function updateZh($data, $post)
    {
        $dataZh['title'] = $data['title_zh'];
        $dataZh['content'] = $data['content_zh'];

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

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

388 389 390 391
        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

392 393 394 395 396 397 398 399
        if ($data->rDe) {
            $this->repoDe->destroy($data->rDe);
        }

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

400 401 402 403
        if ($data->rZh) {
            $this->repoZh->destroy($data->rZh);
        }

404 405 406
        return response()->json(['done']);
    }
}