public function action_update($id = false) { if (!is_numeric($id)) { \Response::redirect('admin/user/group/list'); } // Get group to edit if (!($item = \Sentry::group((int) $id))) { \Response::redirect('admin/user/group/list'); } \View::set_global('title', 'Edit Group'); // Update group details if (\Input::post('details', false)) { $val = $this->validate('update'); if ($val->run()) { try { $update = $item->update(array('name' => \Input::post('title'))); if ($update) { // Update discounts foreach (\Input::post('action') as $product_group_id => $value) { /** * OPTIONS */ $options[$product_group_id]['user_group_id'] = $item->id; $options[$product_group_id]['product_group_id'] = $product_group_id; //$options[$product_group_id]['action'] = $value; $options[$product_group_id]['able_to_buy'] = \Input::post('able_to_buy.' . $product_group_id); $options[$product_group_id]['able_to_view'] = \Input::post('able_to_view.' . $product_group_id); $options[$product_group_id]['sale_discount'] = \Input::post('sale_discount.' . $product_group_id); $options[$product_group_id]['apply_tier_to_sale'] = \Input::post('apply_tier_to_sale.' . $product_group_id); // Insert or update option $option = \Product\Model_Group_Options::find_by(array('user_group_id' => $item->id, 'product_group_id' => $product_group_id), null, null, 1); if ($option) { $option = $option[0]; $option->set($options[$product_group_id]); } else { $option = \Product\Model_Group_Options::forge($options[$product_group_id]); } $option->save(); /** * DISCOUNTS */ // Delete old discounts $all_discounts_id = array(); $all_discounts = \Product\Model_Group_Discounts::find_by(array('user_group_id' => $item->id, 'product_group_id' => $product_group_id), null, null, null); //if($all_discounts) foreach($all_discounts as $discount) $discount->delete(); if ($all_discounts) { foreach ($all_discounts as $discount) { $all_discounts_id[$discount->id] = $discount; } } // Update $discounts = array(); foreach (\Input::post('qty.' . $product_group_id, array()) as $key => $value) { // Ignore discounts with same qty. Only one discount per QTY number is allowed // We will insert first QTY in list. All other will be ignired if (!isset($discounts[$product_group_id][$value])) { if (isset($all_discounts_id[$key])) { unset($all_discounts_id[$key]); } $discounts[$product_group_id][$value]['id'] = $key; $discounts[$product_group_id][$value]['user_group_id'] = $item->id; $discounts[$product_group_id][$value]['product_group_id'] = $product_group_id; $discounts[$product_group_id][$value]['qty'] = $value; $discounts[$product_group_id][$value]['discount'] = \Input::post('discount.' . $product_group_id . '.' . $key); } } // Delete if ($all_discounts_id) { foreach ($all_discounts_id as $discount) { $discount->delete(); } } if (!empty($discounts)) { foreach ($discounts[$product_group_id] as $key => $value) { $id = $discounts[$product_group_id][$key]['id']; $discount = \Product\Model_Group_Discounts::find_one_by_id($id); if ($discount) { $discount->set($discounts[$product_group_id][$key]); $discount->save(); } } } // Insert $new_discounts = array(); foreach (\Input::post('new_qty.' . $product_group_id, array()) as $key => $value) { // Ignore discounts with same qty. Only one discount per QTY number is allowed // We will insert first QTY in list. All other will be ignired if (!isset($discounts[$product_group_id][$value])) { $new_discounts[$product_group_id][$value]['user_group_id'] = $item->id; $new_discounts[$product_group_id][$value]['product_group_id'] = $product_group_id; $new_discounts[$product_group_id][$value]['qty'] = $value; $new_discounts[$product_group_id][$value]['discount'] = \Input::post('new_discount.' . $product_group_id . '.' . $key); } } if (!empty($new_discounts)) { foreach ($new_discounts[$product_group_id] as $key => $value) { // Insert discount $discount = \Product\Model_Group_Discounts::forge($new_discounts[$product_group_id][$key]); $discount->save(); } } } // group was updated \Messages::success('Group successfully updated.'); \Response::redirect(\Input::post('exit', false) ? \Uri::create('admin/user/group/list/') : \Uri::admin('current')); } else { // show validation errors \Messages::error('<strong>There was an error while trying to update group</strong>'); \Messages::error('Please try again.'); } } catch (\Sentry\SentryGroupException $e) { // show validation errors \Messages::error('<strong>There was an error while trying to update group</strong>'); // Uncomment lines below to show database errors $errors = $e->getMessage(); \Messages::error($errors); } } else { if ($val->error() != array()) { // show validation errors \Messages::error('<strong>There was an error while trying to update group</strong>'); foreach ($val->error() as $e) { \Messages::error($e->get_message()); } } } } $group = \Sentry::group((int) $id); // Find all product groups and set usergroup id if ($group->product_groups = \Product\Model_Group::find_by_type('pricing')) { foreach ($group->product_groups as $product_group) { $product_group->set_user_group_id($group->id); } } \Theme::instance()->set_partial('content', $this->view_dir . 'update')->set('group', $group); }