Advertisement
jamboljack

Simpan File

Dec 4th, 2019
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.93 KB | None | 0 0
  1. public function simpanproduk_post()
  2.     {
  3.         $toko_id     = trim($this->post('toko_id'));
  4.         $nama        = trim(strtoupper($this->post('nama')));
  5.         $desc        = trim($this->post('desc'));
  6.         $spesifikasi = trim(strtoupper($this->post('spesifikasi')));
  7.         $tipe        = trim($this->post('tipe'));
  8.         $sertifikat  = trim(strtoupper($this->post('sertifikat')));
  9.         $panen       = trim(strtoupper($this->post('panen')));
  10.         $kapasitas   = $this->post('kapasitas');
  11.         $stok        = $this->post('stok');
  12.         $luas        = $this->post('luas');
  13.         $harga       = $this->post('harga');
  14.  
  15.         if ($toko_id == '') {
  16.             $response = [
  17.                 'resp_error' => true,
  18.                 'resp_msg'   => 'ID Toko Kosong.',
  19.             ];
  20.  
  21.             $this->response($response, 403);
  22.         } elseif ($nama == '') {
  23.             $response = [
  24.                 'resp_error' => true,
  25.                 'resp_msg'   => 'Nama Produk harus diisi.',
  26.             ];
  27.  
  28.             $this->response($response, 403);
  29.         } elseif ($desc == '') {
  30.             $response = [
  31.                 'resp_error' => true,
  32.                 'resp_msg'   => 'Deskripsi Produk harus diisi.',
  33.             ];
  34.  
  35.             $this->response($response, 403);
  36.         } elseif ($spesifikasi == '') {
  37.             $response = [
  38.                 'resp_error' => true,
  39.                 'resp_msg'   => 'Spesifikasi harus diisi.',
  40.             ];
  41.  
  42.             $this->response($response, 403);
  43.         } elseif ($tipe == '') {
  44.             $response = [
  45.                 'resp_error' => true,
  46.                 'resp_msg'   => 'Tipe harus dipilih.',
  47.             ];
  48.  
  49.             $this->response($response, 403);
  50.         } elseif ($sertifikat == '') {
  51.             $response = [
  52.                 'resp_error' => true,
  53.                 'resp_msg'   => 'Sertifikat harus diisi.',
  54.             ];
  55.  
  56.             $this->response($response, 403);
  57.         } elseif ($panen == '') {
  58.             $response = [
  59.                 'resp_error' => true,
  60.                 'resp_msg'   => 'Jadwal Panen harus diisi.',
  61.             ];
  62.  
  63.             $this->response($response, 403);
  64.         } elseif ($kapasitas == '') {
  65.             $response = [
  66.                 'resp_error' => true,
  67.                 'resp_msg'   => 'Kapasitas harus diisi.',
  68.             ];
  69.  
  70.             $this->response($response, 403);
  71.         } elseif ($stok == '') {
  72.             $response = [
  73.                 'resp_error' => true,
  74.                 'resp_msg'   => 'Stok harus diisi.',
  75.             ];
  76.  
  77.             $this->response($response, 403);
  78.         } elseif ($luas == '') {
  79.             $response = [
  80.                 'resp_error' => true,
  81.                 'resp_msg'   => 'Luas Lahan harus diisi.',
  82.             ];
  83.  
  84.             $this->response($response, 403);
  85.         } elseif ($harga == '') {
  86.             $response = [
  87.                 'resp_error' => true,
  88.                 'resp_msg'   => 'Harga harus diisi.',
  89.             ];
  90.  
  91.             $this->response($response, 403);
  92.         } else {
  93.             $this->load->library('image_lib');
  94.             if (!empty($_FILES['attachment']['name'])) {
  95.                 $jam                     = time();
  96.                 $name                    = seo_title($nama);
  97.                 $config['file_name']     = 'Produk_' . $name . '_' . $jam . '.jpg';
  98.                 $config['upload_path']   = './img/produk_folder/';
  99.                 $config['allowed_types'] = 'jpg|png|gif|jpeg';
  100.                 $config['overwrite']     = true;
  101.                 $config['max_size']      = 2048; // 2MB
  102.                 $config['max_width']     = 800; // 800 pixel
  103.                 $config['max_height']    = 800; // 800 pixel
  104.                 $this->load->library('upload');
  105.                 $this->upload->initialize($config);
  106.                 if (!$this->upload->do_upload('attachment')) {
  107.                     $response = [
  108.                         'resp_error' => true,
  109.                         'resp_msg'   => 'Error File',
  110.                     ];
  111.  
  112.                     $this->response($response, 403);
  113.                 } else {
  114.                     $upload      = $this->upload->do_upload('attachment');
  115.                     $upload_data = $this->upload->data();
  116.                     $config      = array(
  117.                         'file_name'      => $upload_data['file_name'],
  118.                         'source_image'   => $upload_data['full_path'], //path to the uploaded image
  119.                         'new_image'      => $this->resized_path, //path to
  120.                         'maintain_ratio' => true,
  121.                         'overwrite'      => true,
  122.                         'width'          => 800,
  123.                         'height'         => 800,
  124.                     );
  125.  
  126.                     $this->image_lib->initialize($config);
  127.                     $this->image_lib->resize();
  128.  
  129.                     $config = array(
  130.                         'source_image'   => $upload_data['full_path'],
  131.                         'new_image'      => $this->thumbs_path,
  132.                         'maintain_ratio' => true,
  133.                         'overwrite'      => true,
  134.                         'width'          => 400,
  135.                         'height'         => 400,
  136.                     );
  137.  
  138.                     $this->image_lib->initialize($config);
  139.                     $this->image_lib->resize();
  140.  
  141.                     $data = array(
  142.                         'toko_id'           => $toko_id,
  143.                         'produk_nama'       => $nama,
  144.                         'produk_seo'        => seo_title($nama),
  145.                         'produk_desc'       => $desc,
  146.                         'produk_spek'       => $spesifikasi,
  147.                         'produk_tipe'       => $tipe,
  148.                         'produk_sertifikat' => $sertifikat,
  149.                         'produk_panen'      => $panen,
  150.                         'produk_kapasitas'  => intval(str_replace(",", "", $kapasitas)),
  151.                         'produk_stok'       => intval(str_replace(",", "", $stok)),
  152.                         'produk_luas'       => intval(str_replace(",", "", $luas)),
  153.                         'produk_harga'      => intval(str_replace(",", "", $harga)),
  154.                         'produk_gambar'     => $this->upload->file_name,
  155.                         'produk_update'     => date('Y-m-d H:i:s'),
  156.                     );
  157.  
  158.                     $this->db->insert('simastani_produk', $data);
  159.  
  160.                     $response = [
  161.                         'resp_error' => false,
  162.                         'resp_msg'   => 'Simpan Data Berhasil',
  163.                     ];
  164.  
  165.                     $this->response($response, 200);
  166.                 }
  167.             } else {
  168.                 $response = [
  169.                     'resp_error' => false,
  170.                     'resp_msg'   => 'Gambar Produk Kosong',
  171.                 ];
  172.  
  173.                 $this->response($response, 403);
  174.             }
  175.         }
  176.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement