LayoutController.php 7.98 KB
Newer Older
Aan Choesni Herlingga committed
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\Repositories\Webprofile\DesignRepository;
Bagus Pambudi committed
8 9 10 11 12
use App\Repositories\Webprofile\De\DesignRepository as DeDesignRepository;
use App\Repositories\Webprofile\En\DesignRepository as EnDesignRepository;
use App\Repositories\Webprofile\Sa\DesignRepository as SaDesignRepository;
use App\Repositories\Webprofile\Zh\DesignRepository as ZhDesignRepository;
use Statickidz\GoogleTranslate;
Aan Choesni Herlingga committed
13 14 15 16 17
use Crypt;

class LayoutController extends Controller
{
    private $repo;
Bagus Pambudi committed
18 19 20 21
    private $repoEn;
    private $repoDe;
    private $repoSa;
    private $repoZh;
Aan Choesni Herlingga committed
22

Bagus Pambudi committed
23 24 25 26 27 28 29 30 31 32 33 34 35
    private $SOURCE = 'id';
    private $TARGET = 'en';
    private $TARGETDE = 'de';
    private $TARGETSA = 'ar';
    private $TARGETZH = 'zh';

    public function __construct(
        DesignRepository $repo,
        EnDesignRepository $repoEn,
        DeDesignRepository $repoDe,
        SaDesignRepository $repoSa,
        ZhDesignRepository $repoZh
        )
Aan Choesni Herlingga committed
36 37
    {
        $this->repo = $repo;
Bagus Pambudi committed
38 39 40 41
        $this->repoEn = $repoEn;
        $this->repoDe = $repoDe;
        $this->repoSa = $repoSa;
        $this->repoZh = $repoZh;
Aan Choesni Herlingga committed
42 43 44 45 46 47 48 49 50 51
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $layouts = $this->repo->get();
Bagus Pambudi committed
52
        
Aan Choesni Herlingga committed
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

        $data = [
            'layouts' => $layouts,
        ];

        return view('webprofile.backend.design.index', $data)->withTitle(trans('feature.layout'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return redirect()->route('layouts.index');
    }

    /**
     * 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('title_show', $data) ? $data['title_show'] = 1 : $data['title_show'] = 0;

Bagus Pambudi committed
82 83 84 85 86 87 88 89 90
        $save=$this->repo->store($data);

        if (webprofilesetting()['auto_translate'] == 1) {
            // save translate
            $this->createEn($data, $save);
            $this->createDe($data, $save);
            $this->createSa($data, $save);
            $this->createZh($data, $save);
        }
Aan Choesni Herlingga committed
91 92 93 94

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

Bagus Pambudi committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    private function createEn($data, $layouts)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGET, $data['title_design']);

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

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

        $dataEn['design_id'] = $layouts->id;
        $dataEn['title_design'] = $title;
        $dataEn['value_design'] = $content;

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

    private function createDe($data, $layouts)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETDE, $data['title_design']);

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

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

        $dataDe['design_id'] = $layouts->id;
        $dataDe['title_design'] = $title;
        $dataDe['value_design'] = $content;

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

    private function createSa($data, $layouts)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETSA, $data['title_design']);

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

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

        $dataSa['design_id'] = $layouts->id;
        $dataSa['title_design'] = $title;
        $dataSa['value_design'] = $content;

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

    private function createZh($data, $layouts)
    {
        $trans = new GoogleTranslate();
        $title = $trans->translate($this->SOURCE, $this->TARGETZH, $data['title_design']);

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

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

        $dataZh['design_id'] = $layouts->id;
        $dataZh['title_design'] = $title;
        $dataZh['value_design'] = $content;

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

Aan Choesni Herlingga committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = [
            'position' => $id,
        ];

        return view('webprofile.backend.design.create', $data)->withTitle(trans('label.create') . ' ' . trans('feature.' . $id));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
Bagus Pambudi committed
190
        $layout = $this->repo->findId(Crypt::decrypt($id), ['rEn', 'rDe', 'rSa', 'rZh']);
Aan Choesni Herlingga committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

        $data = [
            'data' => $layout,
        ];

        return view('webprofile.backend.design.edit', $data)->withTitle(trans('label.edit') . ' ' . trans('feature.' . $layout->name_design));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
Bagus Pambudi committed
208
        $data = $request->except(['_token', 'id', 'title_design_en', 'content_en', 'title_design_de', 'content_de', 'title_design_sa', 'content_sa', 'title_design_zh', 'content_zh']);
Aan Choesni Herlingga committed
209
        array_key_exists('title_show', $data) ? $data['title_show'] = 1 : $data['title_show'] = 0;
Bagus Pambudi committed
210
        $dataEn = $request->except(['_token', 'id']);
Aan Choesni Herlingga committed
211 212 213 214

        $layout = $this->repo->findId($id);
        $edit = $this->repo->update($data, $layout);

Bagus Pambudi committed
215 216 217 218 219
        $this->updateEn($dataEn, $layout);
        $this->updateDe($dataEn, $layout);
        $this->updateSa($dataEn, $layout);
        $this->updateZh($dataEn, $layout);

Aan Choesni Herlingga committed
220 221
        return redirect()->route('layouts.index');
    }
Bagus Pambudi committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    public function updateEn($data, $layout)
    {
        $dataEn['title_design'] = $data['title_design_en'];
        $dataEn['value_design'] = $data['content_en'];

        $this->repoEn->update($dataEn, $layout);
    }

    public function updateDe($data, $layout)
    {
        $dataDe['title_design'] = $data['title_design_de'];
        $dataDe['value_design'] = $data['content_de'];

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

    public function updateSa($data, $layout)
    {
        $dataSa['title_design'] = $data['title_design_sa'];
        $dataSa['value_design'] = $data['content_sa'];

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

    public function updateZh($data, $layout)
    {
        $dataZh['title_design'] = $data['title_design_zh'];
        $dataZh['value_design'] = $data['content_zh'];

        $this->repoZh->update($dataZh, $layout);
    }
Aan Choesni Herlingga committed
253 254 255 256 257 258 259 260 261

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
Bagus Pambudi committed
262
        $data = $this->repo->findId(Crypt::decrypt($id),['rEn', 'rDe', 'rSa', 'rZh']);
Aan Choesni Herlingga committed
263
        $this->repo->destroy($data);
Bagus Pambudi committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
        if ($data->rEn) {
            $this->repoEn->destroy($data->rEn);
        }

        if ($data->rDe) {
            $this->repoDe->destroy($data->rDe);
        }

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

        if ($data->rZh) {
            $this->repoZh->destroy($data->rZh);
        }
Aan Choesni Herlingga committed
279 280 281 282

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