PostController.php 10.2 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 14
use GuzzleHttp\Client;
use Illuminate\Http\Request;
15
use Illuminate\Support\Facades\Config;
16
use Illuminate\Support\Str;
17
use Statickidz\GoogleTranslate;
18 19 20 21

class PostController extends Controller
{
    private $repo;
22
    private $repoEn;
23
    private $repoDe;
24
    private $repoSa;
25

26 27
    private $SOURCE = 'id';
    private $TARGET = 'en';
28
    private $TARGETDE = 'de';
29
    private $TARGETSA = 'ar';
30 31 32

    public function __construct(
        PostRepository $repo,
33
        EnPostRepository $repoEn,
34
        DePostRepository $repoDe,
35
        SaPostRepository $repoSa,
36 37
        SocialSharingRepository $socialRepo,
        AutopostRepository $autoPostRepo
38
    ) {
39
        $this->repo = $repo;
40
        $this->repoEn = $repoEn;
41
        $this->socialRepo = $socialRepo;
42
        $this->autoPostRepo = $autoPostRepo;
43
        $this->repoDe = $repoDe;
44
        $this->repoSa = $repoSa;
45
    }
46

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

57 58 59 60 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
            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;
90
        $data['slug'] = Str::slug($request->input('title'));
91

92 93 94 95
        $save = $this->repo->store($data);

        $tipe = 'thumbnail';
        if ($request->hasFile($tipe)) {
96
            $img[$tipe] = $save->id.'.'.$request->file($tipe)->guessClientExtension();
97 98 99 100

            $this->repo->upload($img[$tipe], $request, $tipe);
            $this->repo->update($img, $save);
        }
101 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
            }
109
        }
110

111 112
        $this->share($data, $request);

113 114 115
        return redirect()->route('posts.index');
    }

116 117 118
    public function shorterUrl($long_url)
    {
        $client = new Client();
119
        $URI = 'http://unesa.me/create.php';
120 121 122 123 124 125 126 127 128
        $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;
    }

129 130
    public function share($data, $request)
    {
131
        $setting = webprofilesetting();
132
        $autoPost = $this->autoPostRepo->get();
133 134 135 136 137 138 139

        if ($request->hasFile('thumbnail')) {
            $img = $request->file('thumbnail')->getRealPath();
        } else {
            $img = null;
        }
        $title = html_entity_decode($data['title']);
140 141 142 143 144 145 146 147 148
        $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).' ';
        }
149

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        foreach ($autoPost as $value) {
            //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']);

            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 == '') {
171
                    $this->socialRepo->sendTextTweet($setting['header_admin'].'-'.$title."\n\n".$url."\n\n".$hashtag);
172
                } else {
173
                    $this->socialRepo->sendTweetWithMedia($setting['header_admin'].'-'.$title."\n\n".$url."\n\n".$hashtag, [$img]);
174 175
                }
            }
176 177 178
        }
    }

179 180 181 182
    private function createEn($data, $post)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGET, $data['title']);
183 184 185 186 187

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

188 189 190 191 192 193 194 195 196
        $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);
    }

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    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);
    }

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    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);
    }

233 234 235
    /**
     * Display the specified resource.
     *
236 237
     * @param int $id
     *
238 239 240 241 242 243 244 245 246
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    }

    /**
     * Show the form for editing the specified resource.
     *
247 248
     * @param int $id
     *
249 250 251 252
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
253
        $setting = webprofilesetting();
254
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa']);
255
        $categories = Categories::pluck('name', 'id');
256

257 258 259
        $data = [
            'data' => $data,
            'categories' => $categories,
260
            'setting' => $setting,
261 262
        ];

263
        return view('webprofile.backend.posts.edit', $data)->withTitle(trans('feature.edit_post'));
264 265 266 267 268
    }

    /**
     * Update the specified resource in storage.
     *
269
     * @param int $id
270
     *
271 272 273 274
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
275
        $data = $request->except(['_token', 'id', 'title_en', 'content_en', 'title_de', 'content_de', 'title_sa', 'content_sa']);
276
        $dataEn = $request->except(['_token', 'id']);
277 278 279

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

282
        $post = $this->repo->findId($id, ['rEn']);
283 284 285 286
        $edit = $this->repo->update($data, $post);

        $tipe = 'thumbnail';
        if ($request->hasFile($tipe)) {
287
            $img[$tipe] = $post->id.'.'.$request->file($tipe)->guessClientExtension();
288 289 290 291

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

293
        $this->updateEn($dataEn, $post);
294
        $this->updateDe($dataEn, $post);
295
        $this->updateSa($dataEn, $post);
296

297 298 299
        return redirect()->route('posts.index');
    }

300 301 302 303 304
    public function updateEn($data, $post)
    {
        $dataEn['title'] = $data['title_en'];
        $dataEn['content'] = $data['content_en'];

305
        $this->repoEn->update($dataEn, $post);
306 307
    }

308 309 310 311 312 313 314 315
    public function updateDe($data, $post)
    {
        $dataDe['title'] = $data['title_de'];
        $dataDe['content'] = $data['content_de'];

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

316 317 318 319 320 321 322 323
    public function updateSa($data, $post)
    {
        $dataSa['title'] = $data['title_sa'];
        $dataSa['content'] = $data['content_sa'];

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

324 325 326
    /**
     * Remove the specified resource from storage.
     *
327 328
     * @param int $id
     *
329 330 331 332
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
333
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa']);
334
        $this->repo->destroy($data, 'thumbnail');
335
        $this->repo->deletefile($data, 'thumbnail');
336

337 338 339 340
        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

341 342 343 344 345 346 347 348
        if ($data->rDe) {
            $this->repoDe->destroy($data->rDe);
        }

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

349 350 351
        return response()->json(['done']);
    }
}