PostController.php 5.34 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
use Illuminate\Support\Str;
12 13 14 15

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

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

    public function __construct(
        PostRepository $repo,
        EnPostRepository $repoEn
    ){
25
        $this->repo = $repo;
26
        $this->repoEn = $repoEn;
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 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.
     *
     * @param  \Illuminate\Http\Request  $request
     * @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;
71
        $data['slug'] = str_slug($request->input('title'));
72

73 74 75 76 77 78 79 80 81
        $save = $this->repo->store($data);

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

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

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

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

91 92 93 94 95 96 97 98 99 100 101 102 103
    private function createEn($data, $post)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGET, $data['title']);
        $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);
    }

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

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

133
        return view('webprofile.backend.posts.edit', $data)->withTitle(trans('feature.edit_post'));
134 135 136 137 138 139 140 141 142 143 144
    }

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

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

152
        $post = $this->repo->findId($id, ['rEn']);
153 154 155 156 157 158 159 160 161
        $edit = $this->repo->update($data, $post);

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

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

163 164
        $this->updateEn($dataEn, $post);

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

168 169 170 171 172
    public function updateEn($data, $post)
    {
        $dataEn['title'] = $data['title_en'];
        $dataEn['content'] = $data['content_en'];

173
        $this->repoEn->update($dataEn, $post);
174 175
    }

176 177 178 179 180 181 182 183
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
184
        $data = $this->repo->findId($id, ['rEn']);
185
        $this->repo->destroy($data, 'thumbnail');
186
        $this->repo->deletefile($data, 'thumbnail');
187

188 189 190 191
        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

192 193 194
        return response()->json(['done']);
    }
}