Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.73 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers\API;
  4.  
  5. use App\Helper\Traits\ApiAuthHelper;
  6. use Illuminate\Http\Request;
  7. use App\Http\Controllers\Controller;
  8. use App\Kecamatan;
  9. use App\Kelurahan;
  10. use App\Aktifitas;
  11. use Validator;
  12. use Illuminate\Support\Str;
  13. use File;
  14. use Image;
  15.  
  16. class AktifitasController extends Controller
  17. {
  18.     use ApiAuthHelper;
  19.  
  20.     public function getKecamatan()
  21.     {
  22.         $check = $this->authCheck();
  23.         if ($check['success'] == true) {
  24.             $user = $check['user'];
  25.             unset($check['user']);
  26.             $data = Kecamatan::where('kota_id', '6105')->get();
  27.             if (!empty($data)) {
  28.                 $res['success']     = true;
  29.                 $res['msg']         = "Berhasil mengambil data!";
  30.                 $res['kecamatan']   = $data;
  31.             } else {
  32.                 $res['success']     = false;
  33.                 $res['msg']         = "Gagal mengambil data!";
  34.             }
  35.             $code = 200;
  36.         } else {
  37.             $res = $check;
  38.             $code = $res['code'];
  39.             unset($res['code']);
  40.         }
  41.         return response()->json($res, $code);
  42.     }
  43.  
  44.     public function getKelurahan($id)
  45.     {
  46.         $check = $this->authCheck();
  47.         if ($check['success'] == true) {
  48.             $user = $check['user'];
  49.             unset($check['user']);
  50.             $data = Kelurahan::where('kecamatan_id', $id)->get();
  51.             if (!empty($data)) {
  52.                 $res['success']     = true;
  53.                 $res['msg']         = "Berhasil mengambil data!";
  54.                 $res['kelurahan']   = $data;
  55.             } else {
  56.                 $res['success']     = false;
  57.                 $res['msg']         = "Gagal mengambil data!";
  58.             }
  59.             $code = 200;
  60.         } else {
  61.             $res = $check;
  62.             $code = $res['code'];
  63.             unset($res['code']);
  64.         }
  65.         return response()->json($res, $code);
  66.     }
  67.  
  68.     public function store(Request $request)
  69.     {
  70.         $check = $this->authCheck();
  71.         if ($check['success'] == true) {
  72.             $user = $check['user'];
  73.             unset($check['user']);
  74.             $limit = [
  75.                 'kecamatan_id' => 'required',
  76.                 'kelurahan_id' => 'required',
  77.                 'alamat' => 'required',
  78.                 'rt' => 'required',
  79.                 'rw' => 'required',
  80.                 'wadah' => 'required',
  81.                 'jumlah_wadah' => 'required',
  82.                 'tempat_tinggal' => 'required',
  83.                 'tipe_tempat' => 'required',
  84.                 'isJentik' => 'required',
  85.                 'photo_sebelum' => 'required|max:5000|mimes:jpeg,jpg,bmp,png',
  86.                 'photo_sesudah' => 'required|max:5000|mimes:jpeg,jpg,bmp,png',
  87.                 'jenis_jentik' => 'required',
  88.             ];
  89.             $data = $request->only(array_keys($limit));
  90.             // dd($data);
  91.             $validator = Validator($data, $limit);
  92.             if ($validator->fails()){
  93.                 $res['success'] = false;
  94.                 $res['msg']     = $validator->messages()->first();
  95.             } else {
  96.                 $data['user_id'] = $user->id;
  97.                 $data['photo_sesudah'] = $this->uploadImage(
  98.                     Image::make($data['photo_sesudah']),
  99.                     'after_'
  100.                 );
  101.                 $data['photo_sebelum'] = $this->uploadImage(
  102.                     Image::make($data['photo_sebelum']),
  103.                     'before_'
  104.                 );
  105.                 $create = Aktifitas::create($data);
  106.                 if (isset($create->id)) {
  107.                     $res['success'] = true;
  108.                     $res['msg']     = "Berhasil menambah data!";
  109.                 } else {
  110.                     $res['success'] = false;
  111.                     $res['msg']     = "Gagal menambah data!";
  112.                 }
  113.             }
  114.             $code = 200;
  115.         } else {
  116.             $res = $check;
  117.             $code = $res['code'];
  118.             unset($res['code']);
  119.         }
  120.         return response()->json($res, $code);
  121.     }
  122.  
  123.     public function getRiwayat(Request $request)
  124.     {
  125.         $check = $this->authCheck();
  126.         if ($check['success'] == true) {
  127.             $user = $check['user'];
  128.             unset($check['user']);
  129.             $page = $request->get('page') ?: 1;
  130.             $take = 10;
  131.             $getData = Aktifitas::where('user_id', $user->id)
  132.             ->orderBy('created_at', 'DESC')
  133.             ->limit($take)
  134.             ->offset(($page - 1)*$take);
  135.             foreach ($getData->get() as $val) {
  136.                 $val['kecamatan_name'] = $val->kecamatan->name;
  137.                 $val['kelurahan_name'] = $val->kelurahan->name;
  138.                 $data[] = $val;
  139.             }
  140.             if (!empty($data)) {
  141.                 $res['success'] = true;
  142.                 $res['msg']     = "Berhasil mengambil data!";
  143.                 $res['riwayat'] = $data;
  144.                 $res['page']    = $page;
  145.                 $total_page     = (int) ceil(Aktifitas::where('user_id', $user->id)->count()/$take);
  146.                 if ($page < $total_page) {
  147.                     $res['next_page'] = $page+1;
  148.                 }
  149.                 if ($page > 1) {
  150.                     $res['prev_page'] = $page-1;
  151.                 }
  152.             } else {
  153.                 $res['success'] = false;
  154.                 $res['msg'] = "Tidak ada data!";
  155.             }
  156.         } else {
  157.             $res = $check;
  158.         }
  159.         return response()->json($res);
  160.     }
  161.  
  162.     public function uploadImage($image, $custom_name = null)
  163.     {
  164.         $filename = $custom_name.Str::random()."_".time().".jpg";
  165.         if ($image->width() > 1024) {
  166.             $image->resize(1024, null, function ($constraint) {
  167.                 $constraint->aspectRatio();
  168.             });
  169.         }
  170.         if ($image->height() > 768) {
  171.             $image->resize(null, 768, function ($constraint) {
  172.                 $constraint->aspectRatio();
  173.             });
  174.         }
  175.         $image->encode(null, 80);
  176.         $path = public_path('uploads/aktivitas');
  177.  
  178.         if (!file_exists($path)) {
  179.             File::makeDirectory($path, 0775, true);
  180.         }
  181.         $image->save($path.'/'.$filename);
  182.         return $filename;
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement