public function update_product($product_id = NULL) { if (is_null($product_id)) { add_error_flash_message('Produkt sa nenašiel.'); redirect(site_url('products')); } $this->db->trans_begin(); $product = new Product(); $product->get_by_id((int) $product_id); if (!$product->exists()) { $this->db->trans_rollback(); add_error_flash_message('Produkt sa nenašiel.'); redirect(site_url('products')); } build_validator_from_form($this->get_product_form()); if ($this->form_validation->run()) { $product_data = $this->input->post('product'); $product->from_array($product_data, array('title', 'price')); if ($product->save() && $this->db->trans_status()) { $this->db->trans_commit(); add_success_flash_message('Produkt s ID <strong>' . $product->id . '</strong> bol úspešne upravený.'); redirect(site_url('products')); } else { $this->db->trans_rollback(); add_error_flash_message('Produk s ID <strong>' . $product->id . '</strong> sa nepodarilo upraviť.'); redirect(site_url('products/edit_product/' . (int) $product->id)); } } else { $this->db->trans_rollback(); $this->edit_product($product_id); } }