InformationController.php 8.12 KB
Newer Older
1 2 3 4 5 6
<?php

namespace App\Http\Controllers\Webprofile\Backend;

use App\Http\Controllers\Controller;
use App\Models\Webprofile\Categories;
7
use App\Repositories\Webprofile\De\InformationRepository as DeInformationRepository;
8
use App\Repositories\Webprofile\En\InformationRepository as EnInformationRepository;
9
use App\Repositories\Webprofile\InformationRepository;
10
use App\Repositories\Webprofile\Sa\InformationRepository as SaInformationRepository;
11
use App\Repositories\Webprofile\Zh\InformationRepository as ZhInformationRepository;
12
use Illuminate\Http\Request;
13
use Statickidz\GoogleTranslate;
14 15 16 17

class InformationController extends Controller
{
    private $repo;
18
    private $repoEn;
19
    private $repoDe;
20
    private $repoSa;
21
    private $repoZh;
22

23 24
    private $SOURCE = 'id';
    private $TARGET = 'en';
25
    private $TARGETDE = 'de';
26
    private $TARGETSA = 'ar';
27
    private $TARGETZH = 'zh';
28 29 30

    public function __construct(
        InformationRepository $repo,
31
        EnInformationRepository $repoEn,
32
        DeInformationRepository $repoDe,
33 34
        SaInformationRepository $repoSa,
        ZhInformationRepository $repoZh
35
    ) {
36
        $this->repo = $repo;
37
        $this->repoEn = $repoEn;
38
        $this->repoDe = $repoDe;
39
        $this->repoSa = $repoSa;
40
        $this->repoZh = $repoZh;
41
    }
42

43 44 45 46 47 48 49 50
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
Aan Choesni Herlingga committed
51
            $data = $this->repo->get(['rEn']);
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
            return $this->repo->datatable($data);
        }

        return view('webprofile.backend.informations.index')->withTitle(trans('feature.information'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except('_token');

        array_key_exists('info_status', $data) ? $data['info_status'] = 1 : $data['info_status'] = 0;
85
        $data['slug'] = str_slug($request->input('title'));
86

87 88
        $save = $this->repo->store($data);

89 90 91
        if (webprofilesetting()['auto_translate'] == 1) {
            // save translate
            $this->createEn($data, $save);
92
            $this->createDe($data, $save);
93
            $this->createSa($data, $save);
94
            $this->createZh($data, $save);
95
        }
96 97 98 99

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

100 101 102 103
    private function createEn($data, $information)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGET, $data['title']);
104 105 106 107 108

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

109 110 111 112 113 114 115 116 117
        $content = $trans->translate($this->SOURCE, $this->TARGET, strip_tags($data['content']));

        $dataEn['information_id'] = $information->id;
        $dataEn['title'] = $title;
        $dataEn['content'] = $content;

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

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    private function createDe($data, $information)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETDE, $data['title']);

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

        $content = $trans->translate($this->SOURCE, $this->TARGETDE, strip_tags($data['content']));

        $dataDe['information_id'] = $information->id;
        $dataDe['title'] = $title;
        $dataDe['content'] = $content;

        $this->repoDe->store($dataDe);
    }

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    private function createSa($data, $information)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETSA, $data['title']);

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

        $content = $trans->translate($this->SOURCE, $this->TARGETSA, strip_tags($data['content']));

        $dataSa['information_id'] = $information->id;
        $dataSa['title'] = $title;
        $dataSa['content'] = $content;

        $this->repoSa->store($dataSa);
    }

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    private function createZh($data, $information)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETZH, $data['title']);

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

        $content = $trans->translate($this->SOURCE, $this->TARGETZH, strip_tags($data['content']));

        $dataZh['information_id'] = $information->id;
        $dataZh['title'] = $title;
        $dataZh['content'] = $content;

        $this->repoZh->store($dataZh);
    }

172 173 174
    /**
     * Display the specified resource.
     *
175 176
     * @param int $id
     *
177 178 179 180 181 182 183 184 185
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    }

    /**
     * Show the form for editing the specified resource.
     *
186 187
     * @param int $id
     *
188 189 190 191
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
192
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
193
        $categories = Categories::pluck('name', 'id');
194

195 196 197 198 199
        $data = [
            'data' => $data,
            'categories' => $categories,
        ];

200
        return view('webprofile.backend.informations.edit', $data)->withTitle(trans('feature.edit_information'));
201 202 203 204 205
    }

    /**
     * Update the specified resource in storage.
     *
206
     * @param int $id
207
     *
208 209 210 211
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
212
        $data = $request->except(['_token', 'id', 'title_en', 'content_en', 'title_de', 'content_de', 'title_sa', 'content_sa', 'title_zh', 'content_zh']);
213
        $dataEn = $request->except(['_token', 'id']);
214 215

        array_key_exists('info_status', $data) ? $data['info_status'] = 1 : $data['info_status'] = 0;
216
        $data['slug'] = str_slug($request->input('title'));
217

218
        $information = $this->repo->findId($id, ['rEn', 'rDe', 'rSa','rZh']);
219 220
        $edit = $this->repo->update($data, $information);

221
        $this->updateEn($dataEn, $information);
222
        $this->updateDe($dataEn, $information);
223
        $this->updateSa($dataEn, $information);
224
        $this->updateZh($dataEn, $information);
225

226 227 228
        return redirect()->route('informations.index');
    }

229 230 231 232 233
    public function updateEn($data, $information)
    {
        $dataEn['title'] = $data['title_en'];
        $dataEn['content'] = $data['content_en'];

234
        $this->repoEn->update($dataEn, $information);
235 236
    }

237 238 239 240 241 242 243 244
    public function updateDe($data, $information)
    {
        $dataDe['title'] = $data['title_de'];
        $dataDe['content'] = $data['content_de'];

        $this->repoDe->update($dataDe, $information);
    }

245 246 247 248 249 250 251 252
    public function updateSa($data, $information)
    {
        $dataSa['title'] = $data['title_sa'];
        $dataSa['content'] = $data['content_sa'];

        $this->repoSa->update($dataSa, $information);
    }

253 254 255 256 257 258 259 260
    public function updateZh($data, $information)
    {
        $dataZh['title'] = $data['title_zh'];
        $dataZh['content'] = $data['content_zh'];

        $this->repoZh->update($dataZh, $information);
    }

261 262 263
    /**
     * Remove the specified resource from storage.
     *
264 265
     * @param int $id
     *
266 267 268 269
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
270
        $data = $this->repo->findId($id, ['rEn', 'rDe', 'rSa', 'rZh']);
271 272
        $this->repo->destroy($data);

273 274 275 276
        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

277 278 279 280 281 282 283 284
        if ($data->rDe) {
            $this->repoDe->destroy($data->rDe);
        }

        if ($data->rSa) {
            $this->repoSa->destroy($data->rSa);
        }

285 286 287 288
        if ($data->rZh) {
            $this->repoZh->destroy($data->rZh);
        }

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