PageController.php 4.29 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\PagesRepository as EnPagesRepository;
9
use App\Repositories\Webprofile\PagesRepository;
10
use Statickidz\GoogleTranslate;
11 12 13 14

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

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

    public function __construct(
        PagesRepository $repo,
        EnPagesRepository $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()) {
Aan Choesni Herlingga committed
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.pages.index')->withTitle(trans('feature.page'));
    }

    /**
     * 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.pages.create', $data)->withTitle(trans('feature.create_page'));
    }

    /**
     * Store a newly created resource in storage.
     *
63 64
     * @param \Illuminate\Http\Request $request
     *
65 66 67 68 69
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except('_token');
Aan Choesni Herlingga committed
70
        $data['slug'] = str_slug($request->input('title'));
71

72 73
        $save = $this->repo->store($data);

74 75 76 77
        if (webprofilesetting()['auto_translate'] == 1) {
            // save translate
            $this->createEn($data, $save);
        }
78 79 80 81

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

82 83 84 85
    private function createEn($data, $page)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGET, $data['title']);
86 87 88 89 90

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

91 92 93 94 95 96 97 98 99
        $content = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['content']));

        $dataEn['page_id'] = $page->id;
        $dataEn['title'] = $title;
        $dataEn['content'] = $content;

        $this->repoEn->store($dataEn);
    }

100 101 102
    /**
     * Display the specified resource.
     *
103 104
     * @param int $id
     *
105 106 107 108 109 110 111 112 113
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    }

    /**
     * Show the form for editing the specified resource.
     *
114 115
     * @param int $id
     *
116 117 118 119 120 121
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = $this->repo->findId($id);
        $categories = Categories::pluck('name', 'id');
122

123 124 125 126 127
        $data = [
            'data' => $data,
            'categories' => $categories,
        ];

128
        return view('webprofile.backend.pages.edit', $data)->withTitle(trans('feature.edit_page'));
129 130 131 132 133
    }

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

144
        $page = $this->repo->findId($id, ['rEn']);
145 146
        $edit = $this->repo->update($data, $page);

147 148
        $this->updateEn($dataEn, $page);

149 150 151
        return redirect()->route('pages.index');
    }

152 153 154 155 156
    public function updateEn($data, $page)
    {
        $dataEn['title'] = $data['title_en'];
        $dataEn['content'] = $data['content_en'];

157
        $this->repoEn->update($dataEn, $page);
158 159
    }

160 161 162
    /**
     * Remove the specified resource from storage.
     *
163 164
     * @param int $id
     *
165 166 167 168
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
169
        $data = $this->repo->findId($id, ['rEn']);
170 171
        $this->repo->destroy($data);

172 173 174 175
        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

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