public function delete_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->include_related_count('product_quantity', 'product_quantity_count'); $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')); } if ((int) $product->product_quantity_count > 0) { $this->db->trans_rollback(); add_error_flash_message('Produkt nie je možné vymazať, keď už obsahuje záznamy o množstve.'); redirect(site_url('products')); } $success_message = 'Produkt <strong>' . $product->title . '</strong> s ID <strong>' . $product->id . '</strong> bol úspešne vymazaný.'; $error_message = 'Produkt <strong>' . $product->title . '</strong> s ID <strong>' . $product->id . '</strong> sa nepodarilo vymazať.'; if ($product->delete() && $this->db->trans_status()) { $this->db->trans_commit(); add_success_flash_message($success_message); redirect(site_url('products')); } else { $this->db->trans_rollback(); add_error_flash_message($error_message); redirect(site_url('products')); } }