| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php /** @noinspection PhpUnhandledExceptionInspection */
- namespace App;
- use RuntimeException;
- /**
- * @var int $id
- */
- $core = Core::getInstance();
- $core->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->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");
|