/** * Retrieves a product, its ingredients and all ingredients available to * the product category from the database. Counts the price of the product * and displays the form for editing it. * * @param int $id id of the product to edit */ public static function edit_form($id) { $product = Product::findOne($id); $product_ingredients = Product::findIngredients($id); foreach ($product_ingredients as $product_ingredient) { $product->ingredients[] = $product_ingredient->ingredient_name; } if ($product->category != 'Juoma') { $product->price = Ingredient::count_total_price($product_ingredients); } $ingredients = Ingredient::findByCategory($product->category); View::make('product/edit.html', array('ingredients' => $ingredients, 'product' => $product)); }
/** * Retrieves the item to add from the database. If the product is * customizable, adds the selected ingredients to an array. Saves the * product to the 'cart items' session array as a JSON string. Adds the * price of the product to the total cart price. Redirects to the current * page. * * @param int $id id of the product to add */ public static function add($id) { $product = Product::findOne($id); $params = $_POST; if ($product->customizable) { $product = new Product(array('id' => $id, 'product_name' => $product->product_name, 'category' => $product->category, 'customizable' => $product->customizable, 'description' => $product->description, 'ingredients' => array())); if (isset($params['ingredients'])) { self::set_product_ingredients($product, $params['ingredients']); } if (count($product->ingredients) == 0) { Redirect::to($params['redirect'], array('message' => 'Valitse ainekset!')); } } else { $product->ingredients = Product::findIngredients($product->id); } if ($product->category != 'Juoma') { $product->price = Ingredient::count_total_price($product->ingredients); } $_SESSION['cart']['items'][] = json_encode($product); $_SESSION['cart']['price'] += $product->price; Redirect::to($params['redirect'], array('message' => $product->product_name . ' lisätty koriin!')); }