PostController.php 8.01 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\En\PostRepository as EnPostRepository;
9
use App\Repositories\Webprofile\PostRepository;
10
use App\Repositories\Webprofile\SocialSharingRepository;
11 12
use GuzzleHttp\Client;
use Illuminate\Http\Request;
13
use Illuminate\Support\Facades\Config;
14
use Illuminate\Support\Str;
15
use Statickidz\GoogleTranslate;
16 17 18 19

class PostController extends Controller
{
    private $repo;
20
    private $repoEn;
21

22 23 24 25 26
    private $SOURCE = 'id';
    private $TARGET = 'en';

    public function __construct(
        PostRepository $repo,
27
        EnPostRepository $repoEn,
28 29
        SocialSharingRepository $socialRepo,
        AutopostRepository $autoPostRepo
30
    ) {
31
        $this->repo = $repo;
32
        $this->repoEn = $repoEn;
33
        $this->socialRepo = $socialRepo;
34
        $this->autoPostRepo = $autoPostRepo;
35
    }
36

37 38 39 40 41 42 43 44
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
45
            $data = $this->repo->get(['rEn']);
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
            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;
80
        $data['slug'] = Str::slug($request->input('title'));
81

82 83 84 85
        $save = $this->repo->store($data);

        $tipe = 'thumbnail';
        if ($request->hasFile($tipe)) {
86
            $img[$tipe] = $save->id.'.'.$request->file($tipe)->guessClientExtension();
87 88 89 90

            $this->repo->upload($img[$tipe], $request, $tipe);
            $this->repo->update($img, $save);
        }
91 92 93

        if (webprofilesetting()['auto_translate'] == 1) {
            // save translate
Aan Choesni Herlingga committed
94
            if (strlen($data['content']) < 5000) {
95 96
                $this->createEn($data, $save);
            }
97
        }
98

99 100
        $this->share($data, $request);

101 102 103
        return redirect()->route('posts.index');
    }

104 105 106
    public function shorterUrl($long_url)
    {
        $client = new Client();
107
        $URI = 'http://unesa.me/create.php';
108 109 110 111 112 113 114 115 116
        $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;
    }

117 118
    public function share($data, $request)
    {
119
        $setting = webprofilesetting();
120
        $autoPost = $this->autoPostRepo->get();
121 122 123 124 125 126 127

        if ($request->hasFile('thumbnail')) {
            $img = $request->file('thumbnail')->getRealPath();
        } else {
            $img = null;
        }
        $title = html_entity_decode($data['title']);
128 129 130 131 132 133 134 135 136
        $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).' ';
        }
137

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
        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 == '') {
159
                    $this->socialRepo->sendTextTweet($setting['header_admin'].'-'.$title."\n\n".$url."\n\n".$hashtag);
160
                } else {
161
                    $this->socialRepo->sendTweetWithMedia($setting['header_admin'].'-'.$title."\n\n".$url."\n\n".$hashtag, [$img]);
162 163
                }
            }
164 165 166
        }
    }

167 168 169 170
    private function createEn($data, $post)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGET, $data['title']);
171 172 173 174 175

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

176 177 178 179 180 181 182 183 184
        $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);
    }

185 186 187
    /**
     * Display the specified resource.
     *
188 189
     * @param int $id
     *
190 191 192 193 194 195 196 197 198
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    }

    /**
     * Show the form for editing the specified resource.
     *
199 200
     * @param int $id
     *
201 202 203 204
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
205
        $setting = webprofilesetting();
206
        $data = $this->repo->findId($id, ['rEn']);
207
        $categories = Categories::pluck('name', 'id');
208

209 210 211
        $data = [
            'data' => $data,
            'categories' => $categories,
212
            'setting' => $setting,
213 214
        ];

215
        return view('webprofile.backend.posts.edit', $data)->withTitle(trans('feature.edit_post'));
216 217 218 219 220
    }

    /**
     * Update the specified resource in storage.
     *
221
     * @param int $id
222
     *
223 224 225 226
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
227 228
        $data = $request->except(['_token', 'id', 'title_en', 'content_en']);
        $dataEn = $request->except(['_token', 'id']);
229 230 231

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

234
        $post = $this->repo->findId($id, ['rEn']);
235 236 237 238
        $edit = $this->repo->update($data, $post);

        $tipe = 'thumbnail';
        if ($request->hasFile($tipe)) {
239
            $img[$tipe] = $post->id.'.'.$request->file($tipe)->guessClientExtension();
240 241 242 243

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

245 246
        $this->updateEn($dataEn, $post);

247 248 249
        return redirect()->route('posts.index');
    }

250 251 252 253 254
    public function updateEn($data, $post)
    {
        $dataEn['title'] = $data['title_en'];
        $dataEn['content'] = $data['content_en'];

255
        $this->repoEn->update($dataEn, $post);
256 257
    }

258 259 260
    /**
     * Remove the specified resource from storage.
     *
261 262
     * @param int $id
     *
263 264 265 266
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
267
        $data = $this->repo->findId($id, ['rEn']);
268
        $this->repo->destroy($data, 'thumbnail');
269
        $this->repo->deletefile($data, 'thumbnail');
270

271 272 273 274
        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

275 276 277
        return response()->json(['done']);
    }
}