addBreadcrumb("/products", __("Products")); $user = $core->getUser(); if(isset($id)) { $product = Product::get($id); if(!$product) { throw new RuntimeException(__("Product not found.")); } $product->dts_updated = Database::dts(); $title = __("Update Product"); } else { $product = new Product(); $product->dts_created = Database::dts(); $title = __("Add Product"); } $core->addBreadcrumb($_SERVER["REQUEST_URI"], $title); $request = Request::fromPost(); $submit = $request->int("submit"); if($submit) { try { $user->requireValidCsrfToken(); $supplier_id = $request->int("supplier_id"); $supplier = Supplier::get($supplier_id); if(!$supplier) { throw new RuntimeException(__("Invalid supplier selected.")); } $product->supplier_id = $supplier_id; $product->name = $request->string("name"); $product->sku = $request->string("sku"); $product->type = $request->int("type"); $product->price = $request->decimal("price", 2); if(!$product->name) { throw new RuntimeException(__("Name is required.")); } if(!$product->type) { throw new RuntimeException(__("Type is required.")); } $product->commit(); Session::addAlert("success", __("Product saved successfully.")); Http::redirect($product->getUrl()); } catch(RuntimeException $e) { Session::addAlert("danger", $e->getMessage()); } } View::include("header"); View::include("product_form", [ "title" => $title, "product" => $product, ]); View::include("footer");