PostController.php 7.25 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace App\Http\Controllers\Webprofile\Backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Webprofile\Categories;
8
use App\Repositories\Webprofile\AutopostRepository;
9
use App\Repositories\Webprofile\En\PostRepository as EnPostRepository;
10
use App\Repositories\Webprofile\PostRepository;
11 12
use App\Repositories\Webprofile\SocialSharingRepository;
use Illuminate\Support\Facades\Config;
13
use Illuminate\Support\Str;
14
use Statickidz\GoogleTranslate;
15 16 17 18

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

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

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

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

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
            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.
     *
71 72
     * @param \Illuminate\Http\Request $request
     *
73 74 75 76 77 78 79 80
     * @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;
81
        $data['slug'] = Str::slug($request->input('title'));
82

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

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

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

        if (webprofilesetting()['auto_translate'] == 1) {
            // save translate
            $this->createEn($data, $save);
        }
97

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

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

103 104
    public function share($data, $request)
    {
105
        $autoPost = $this->autoPostRepo->get();
106 107 108 109 110 111 112 113

        if ($request->hasFile('thumbnail')) {
            $img = $request->file('thumbnail')->getRealPath();
        } else {
            $img = null;
        }
        $title = html_entity_decode($data['title']);

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        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 == '') {
                    $this->socialRepo->sendTextTweet($title.' '.url('post/'.$data['slug']));
                } else {
                    $this->socialRepo->sendTweetWithMedia($title.' '.url('post/'.$data['slug']), [$img]);
                }
            }
140 141 142
        }
    }

143 144 145 146
    private function createEn($data, $post)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGET, $data['title']);
147 148 149 150 151

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

152 153 154 155 156 157 158 159 160
        $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);
    }

161 162 163
    /**
     * Display the specified resource.
     *
164 165
     * @param int $id
     *
166 167 168 169 170 171 172 173 174
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    }

    /**
     * Show the form for editing the specified resource.
     *
175 176
     * @param int $id
     *
177 178 179 180
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
181
        $setting = webprofilesetting();
182
        $data = $this->repo->findId($id, ['rEn']);
183
        $categories = Categories::pluck('name', 'id');
184

185 186 187
        $data = [
            'data' => $data,
            'categories' => $categories,
188
            'setting' => $setting,
189 190
        ];

191
        return view('webprofile.backend.posts.edit', $data)->withTitle(trans('feature.edit_post'));
192 193 194 195 196
    }

    /**
     * Update the specified resource in storage.
     *
197 198 199
     * @param \Illuminate\Http\Request $request
     * @param int                      $id
     *
200 201 202 203
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
204 205
        $data = $request->except(['_token', 'id', 'title_en', 'content_en']);
        $dataEn = $request->except(['_token', 'id']);
206 207 208

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

211
        $post = $this->repo->findId($id, ['rEn']);
212 213 214 215
        $edit = $this->repo->update($data, $post);

        $tipe = 'thumbnail';
        if ($request->hasFile($tipe)) {
216
            $img[$tipe] = $post->id.'.'.$request->file($tipe)->guessClientExtension();
217 218 219 220

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

222 223
        $this->updateEn($dataEn, $post);

224 225 226
        return redirect()->route('posts.index');
    }

227 228 229 230 231
    public function updateEn($data, $post)
    {
        $dataEn['title'] = $data['title_en'];
        $dataEn['content'] = $data['content_en'];

232
        $this->repoEn->update($dataEn, $post);
233 234
    }

235 236 237
    /**
     * Remove the specified resource from storage.
     *
238 239
     * @param int $id
     *
240 241 242 243
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
244
        $data = $this->repo->findId($id, ['rEn']);
245
        $this->repo->destroy($data, 'thumbnail');
246
        $this->repo->deletefile($data, 'thumbnail');
247

248 249 250 251
        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

252 253 254
        return response()->json(['done']);
    }
}