PostController.php 5.44 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\En\PostRepository as EnPostRepository;
9
use App\Repositories\Webprofile\PostRepository;
10
use Statickidz\GoogleTranslate;
11 12 13 14

class PostController extends Controller
{
    private $repo;
15
    private $repoEn;
16

17 18 19 20 21 22
    private $SOURCE = 'id';
    private $TARGET = 'en';

    public function __construct(
        PostRepository $repo,
        EnPostRepository $repoEn
23
    ) {
24
        $this->repo = $repo;
25
        $this->repoEn = $repoEn;
26
    }
27

28 29 30 31 32 33 34 35
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
36
            $data = $this->repo->get(['rEn']);
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
            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.
     *
63 64
     * @param \Illuminate\Http\Request $request
     *
65 66 67 68 69 70 71 72
     * @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;
73
        $data['slug'] = str_slug($request->input('title'));
74

75 76 77 78
        $save = $this->repo->store($data);

        $tipe = 'thumbnail';
        if ($request->hasFile($tipe)) {
79
            $img[$tipe] = $save->id.'.'.$request->file($tipe)->guessClientExtension();
80 81 82 83

            $this->repo->upload($img[$tipe], $request, $tipe);
            $this->repo->update($img, $save);
        }
84 85 86 87 88

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

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

93 94 95 96
    private function createEn($data, $post)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGET, $data['title']);
97 98 99 100 101

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

102 103 104 105 106 107 108 109 110
        $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);
    }

111 112 113
    /**
     * Display the specified resource.
     *
114 115
     * @param int $id
     *
116 117 118 119 120 121 122 123 124
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    }

    /**
     * Show the form for editing the specified resource.
     *
125 126
     * @param int $id
     *
127 128 129 130
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
131
        $setting = webprofilesetting();
132
        $data = $this->repo->findId($id, ['rEn']);
133
        $categories = Categories::pluck('name', 'id');
134

135 136 137
        $data = [
            'data' => $data,
            'categories' => $categories,
138
            'setting' => $setting,
139 140
        ];

141
        return view('webprofile.backend.posts.edit', $data)->withTitle(trans('feature.edit_post'));
142 143 144 145 146
    }

    /**
     * Update the specified resource in storage.
     *
147 148 149
     * @param \Illuminate\Http\Request $request
     * @param int                      $id
     *
150 151 152 153
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
154 155
        $data = $request->except(['_token', 'id', 'title_en', 'content_en']);
        $dataEn = $request->except(['_token', 'id']);
156 157 158

        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;
159
        $data['slug'] = str_slug($request->input('title'));
160

161
        $post = $this->repo->findId($id, ['rEn']);
162 163 164 165
        $edit = $this->repo->update($data, $post);

        $tipe = 'thumbnail';
        if ($request->hasFile($tipe)) {
166
            $img[$tipe] = $post->id.'.'.$request->file($tipe)->guessClientExtension();
167 168 169 170

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

172 173
        $this->updateEn($dataEn, $post);

174 175 176
        return redirect()->route('posts.index');
    }

177 178 179 180 181
    public function updateEn($data, $post)
    {
        $dataEn['title'] = $data['title_en'];
        $dataEn['content'] = $data['content_en'];

182
        $this->repoEn->update($dataEn, $post);
183 184
    }

185 186 187
    /**
     * Remove the specified resource from storage.
     *
188 189
     * @param int $id
     *
190 191 192 193
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
194
        $data = $this->repo->findId($id, ['rEn']);
195
        $this->repo->destroy($data, 'thumbnail');
196
        $this->repo->deletefile($data, 'thumbnail');
197

198 199 200 201
        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

202 203 204
        return response()->json(['done']);
    }
}