diff --git a/application/config/autoload.php b/application/config/autoload.php index e2039c2..c34f142 100644 --- a/application/config/autoload.php +++ b/application/config/autoload.php @@ -58,7 +58,7 @@ $autoload['packages'] = array(); | | $autoload['libraries'] = array('user_agent' => 'ua'); */ -$autoload['libraries'] = array(); +$autoload['libraries'] = array('database','session'); /* | ------------------------------------------------------------------- diff --git a/application/controllers/admin/Product.php b/application/controllers/admin/Product.php index 7d66011..729297f 100644 --- a/application/controllers/admin/Product.php +++ b/application/controllers/admin/Product.php @@ -2,14 +2,71 @@ defined('BASEPATH') OR exit('No direct script access allowed'); class Product extends CI_Controller { - + private $_tpl_path; + + public function __construct() + { + parent::__construct(); + $this->_tpl_path = 'admin/' . config_item('admin_template') . '/'; + $this->load->model("m_product"); + $this->load->library('form_validation'); + } + public function index() { - $tpl = config_item('admin_template'); - $tpl_path = 'admin/' . $tpl . '/'; + + $product = $this->m_product; + $data = array( - 'tpl_path' => $tpl_path + 'tpl_path' => $this->_tpl_path, + 'products' => $product->getAll() ); - $this->load->view($tpl_path . 'product',$data); + $this->load->view($this->_tpl_path . 'product/list',$data); } + + public function add() + { + $product = $this->m_product; + $validation = $this->form_validation; + $validation->set_rules($product->rules()); + + if ($validation->run()) { + $product->save(); + $this->session->set_flashdata('success', 'Berhasil disimpan'); + } + + $data = array( + 'tpl_path' => $this->_tpl_path, + 'content_title' => 'Tambah Produk' + ); + $this->load->view($this->_tpl_path . 'product/edit',$data); + } + + public function edit($id = null) + { + if (!isset($id)) redirect('admin/product'); + + $product = $this->m_product; + $validation = $this->form_validation; + $validation->set_rules($product->rules()); + + if ($validation->run()) { + $product->update(); + $this->session->set_flashdata('success', 'Berhasil disimpan'); + } + + $data["product"] = $product->getById($id); + if (!$data["product"]) show_404(); + + $this->load->view("admin/product/edit_form", $data); + } + + public function delete($id=null) + { + if (!isset($id)) show_404(); + + if ($this->product_model->delete($id)) { + redirect(site_url('admin/product')); + } + } } diff --git a/application/models/M_Product.php b/application/models/M_Product.php new file mode 100644 index 0000000..2acad64 --- /dev/null +++ b/application/models/M_Product.php @@ -0,0 +1,64 @@ + 'name', + 'label' => 'Name', + 'rules' => 'required'], + + ['field' => 'price', + 'label' => 'Price', + 'rules' => 'numeric'], + + ['field' => 'description', + 'label' => 'Description', + 'rules' => 'required'] + ]; + } + + public function getAll() + { + return $this->db->get($this->_table)->result(); + } + + public function getById($id) + { + return $this->db->get_where($this->_table, ["product_id" => $id])->row(); + } + + public function save() + { + $post = $this->input->post(); + $this->product_id = uniqid(); + $this->name = $post["name"]; + $this->price = $post["price"]; + $this->description = $post["description"]; + $this->db->insert($this->_table, $this); + } + + public function update() + { + $post = $this->input->post(); + $this->product_id = $post["id"]; + $this->name = $post["name"]; + $this->price = $post["price"]; + $this->description = $post["description"]; + $this->db->update($this->_table, $this, array('product_id' => $post['id'])); + } + + public function delete($id) + { + return $this->db->delete($this->_table, array("product_id" => $id)); + } +} \ No newline at end of file diff --git a/application/views/admin/sb_admin/index.php b/application/views/admin/sb_admin/index.php index 8067fc7..f82d5eb 100644 --- a/application/views/admin/sb_admin/index.php +++ b/application/views/admin/sb_admin/index.php @@ -36,154 +36,7 @@