PostController.php 4.36 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 23
    private $SOURCE = 'id';
    private $TARGET = 'en';

    public function __construct(
        PostRepository $repo,
        EnPostRepository $repoEn
    ){
24
        $this->repo = $repo;
25
        $this->repoEn = $repoEn;
26 27 28 29 30 31 32 33 34
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
35
            $data = $this->repo->get(['rEn']);
36 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 72 73 74 75
        
        $save = $this->repo->store($data);
        
        // save translate
        $this->createEn($data, $save);
76 77 78 79

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

80 81 82 83 84 85 86 87 88 89 90 91 92
    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);
    }

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    /**
     * 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)
    {
112
        $data = $this->repo->findId($id, ['rEn']);
113 114 115 116 117 118 119
        $categories = Categories::pluck('name', 'id');
        
        $data = [
            'data' => $data,
            'categories' => $categories,
        ];

120
        return view('webprofile.backend.posts.edit', $data)->withTitle(trans('feature.edit_post'));
121 122 123 124 125 126 127 128 129 130 131
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
132 133
        $data = $request->except(['_token', 'id', 'title_en', 'content_en']);
        $dataEn = $request->except(['_token', 'id']);
134 135 136 137

        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;

138
        $post = $this->repo->findId($id, ['rEn']);
139 140
        $edit = $this->repo->update($data, $post);

141 142
        $this->updateEn($dataEn, $post);

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

146 147 148 149 150 151 152 153
    public function updateEn($data, $post)
    {
        $dataEn['title'] = $data['title_en'];
        $dataEn['content'] = $data['content_en'];

        $this->repo->update($dataEn, $post->rEn);
    }

154 155 156 157 158 159 160 161 162 163 164 165 166 167
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = $this->repo->findId($id);
        $this->repo->destroy($data);

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