/** * Updates a product option. * * @param Model_Product_Option $option The product option to update. * @param array $data The data to use to update the product option. * * @return Model_Product_Option */ public static function update(Model_Product_Option $option, array $data = array()) { $option->populate($data); if (!empty($data['meta'])) { $option->metas = array(); foreach ($data['meta'] as $meta_id => $meta_option_id) { if (empty($meta_option_id)) { unset($option->metas[$meta_id]); } else { if ($meta_option = Service_Product_Meta_Option::find_one($meta_option_id)) { $option->metas[] = $meta_option; } } } } try { $option->save(); } catch (FuelException $e) { Log::error($e); return false; } Service_Event::trigger('product.option.update', $option->product->seller, $option->to_array()); return $option; }
/** * POST Edit action. * * @param int $product_id Product ID. * @param int $id Product meta ID. * * @return void */ public function post_edit($product_id = null, $id = null) { $this->get_edit($product_id, $id); $validator = Validation_Product_Meta::update(); if (!$validator->run()) { Session::set_alert('error', __('form.error')); $this->view->errors = $validator->error(); return; } $product = $this->get_product($product_id); $meta = $this->get_meta($id); $data = $validator->validated(); if (!Service_Product_Meta::update($meta, $data)) { Session::set_alert('error', 'There was an error updating the product meta.'); return; } $options = $meta->options; foreach (array_filter(Input::post('value')) as $option_id => $value) { if ($option = Arr::get($options, $option_id)) { // Update existing meta option. if (!($data = $this->validate_meta_option('update', array('value' => $value)))) { return; } $option = Service_Product_Meta_Option::update($option, $data); } else { // Create new meta option. if (!($data = $this->validate_meta_option('create', array('value' => $value)))) { return; } $option = Service_Product_Meta_Option::create($data['value'], $meta); } if (!$option) { Session::set_alert('error', 'There was an error updating the product meta option(s).'); return; } } Session::set_alert('success', 'The product meta has been updated.'); Response::redirect($product->link('metas')); }
/** * Attempts to get a product meta option from a given ID. * * @param int $id Product meta option ID. * @param \Model_Product_Meta $meta Product meta the option should belong to. * * @return \Model_Product_Meta_Option */ protected function get_option($id, \Model_Product_Meta $meta) { if (!$id) { throw new HttpNotFoundException(); } $option = \Service_Product_Meta_Option::find_one($id); if (!$option || $option->meta != $meta || $option->meta->product->seller != \Seller::active()) { throw new HttpNotFoundException(); } return $option; }