Example #1
0
 public function action_update($id = false)
 {
     if (!is_numeric($id)) {
         \Response::redirect('admin/product/group/list');
     }
     // Get group to edit
     if (!($item = Model_Group::find_one_by_id($id))) {
         \Response::redirect('admin/product/group/list');
     }
     \View::set_global('title', 'Edit Group');
     $i_selected_customer_group = 0;
     // Update group details
     if (\Input::post('details', false)) {
         $i_selected_customer_group = \Input::post('rdo_group');
         foreach (\Input::post('action') as $user_group_id => $value) {
             /**
              * OPTIONS
              */
             $options[$id]['user_group_id'] = $user_group_id;
             $options[$id]['product_group_id'] = $id;
             $options[$id]['apply_tier_to_sale'] = $_POST['apply_tier_to_sale'][$user_group_id];
             $options[$id]['sale_discount'] = $_POST['sale_discount'][$user_group_id];
             $options[$id]['able_to_view'] = $_POST['able_to_view'][$user_group_id];
             if ($user_group_id == $i_selected_customer_group) {
                 $options[$id]['able_to_buy'] = $_POST['able_to_buy'][$user_group_id];
                 $options[$id]['active'] = 1;
                 // NRB-Gem: Update user_group_id of products under this Pricing Group
                 // used to display correct sale price in admin/product/price/update/{product_id}
                 \DB::update('product_attribute_price')->value('user_group_id', $user_group_id)->where('pricing_group_id', '=', $id)->execute();
             } else {
                 $options[$id]['able_to_buy'] = 0;
                 $options[$id]['active'] = 0;
             }
             // Insert or update option
             $option = Model_Group_Options::find_by(array('user_group_id' => $user_group_id, 'product_group_id' => $id), null, null, 1);
             if ($option) {
                 $option = $option[0];
                 $option->set($options[$id]);
             } else {
                 $option = Model_Group_Options::forge($options[$id]);
             }
             $option->save();
             /**
              * DISCOUNTS
              */
             // Delete old discounts
             $all_discounts_id = array();
             $all_discounts = Model_Group_Discounts::find_by(array('user_group_id' => $user_group_id, 'product_group_id' => $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 ($_POST['qty'][$user_group_id] 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[$user_group_id][$value])) {
                     if (isset($all_discounts_id[$key])) {
                         unset($all_discounts_id[$key]);
                     }
                     $discounts[$user_group_id][$value]['id'] = $key;
                     $discounts[$user_group_id][$value]['user_group_id'] = $user_group_id;
                     $discounts[$user_group_id][$value]['product_group_id'] = $id;
                     $discounts[$user_group_id][$value]['qty'] = $value;
                     $discounts[$user_group_id][$value]['discount'] = $_POST['discount'][$user_group_id][$key];
                 }
             }
             // Delete
             if ($all_discounts_id) {
                 foreach ($all_discounts_id as $discount) {
                     $discount->delete();
                 }
             }
             if (!empty($discounts)) {
                 foreach ($discounts[$user_group_id] as $key => $value) {
                     $id = $discounts[$user_group_id][$key]['id'];
                     $discount = Model_Group_Discounts::find_one_by_id($id);
                     if ($discount) {
                         $discount->set($discounts[$user_group_id][$key]);
                         $discount->save();
                     }
                 }
             }
             // Insert
             $new_discounts = array();
             foreach ($_POST['new_qty'][$user_group_id] 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[$user_group_id][$value])) {
                     $new_discounts[$user_group_id][$value]['user_group_id'] = $user_group_id;
                     $new_discounts[$user_group_id][$value]['product_group_id'] = $id;
                     $new_discounts[$user_group_id][$value]['qty'] = $value;
                     $new_discounts[$user_group_id][$value]['discount'] = $_POST['new_discount'][$user_group_id][$key];
                 }
             }
             if (!empty($new_discounts)) {
                 foreach ($new_discounts[$user_group_id] as $key => $value) {
                     // Insert discount
                     $discount = Model_Group_Discounts::forge($new_discounts[$user_group_id][$key]);
                     $discount->save();
                 }
             }
         }
         //end
         $val = Model_Group::validate('update');
         // Upload image and display errors if there are any
         $image = $this->upload_image();
         if (!$image['exists'] && \Config::get('details.image.required', false) && empty($item->images)) {
             // No previous images and image is not selected and it is required
             \Messages::error('<strong>There was an error while trying to upload group image</strong>');
             \Messages::error('You have to select image');
         } elseif ($image['errors']) {
             \Messages::error('<strong>There was an error while trying to upload group image</strong>');
             foreach ($image['errors'] as $error) {
                 \Messages::error($error);
             }
         }
         if ($val->run() && $image['is_valid'] && !(!$image['exists'] && \Config::get('details.image.required', false) && empty($item->images))) {
             /** IMAGES **/
             // Get all alt texts to update if there is no image change
             foreach (\Arr::filter_prefixed(\Input::post(), 'alt_text_') as $image_id => $alt_text) {
                 if (strpos($image_id, 'new_') === false) {
                     $item_images[$image_id] = array('id' => $image_id, 'data' => array('alt_text' => \Input::post('alt_text_' . $image_id, '')));
                 }
             }
             // Save images if new files are submitted
             if (isset($this->_image_data)) {
                 foreach ($this->_image_data as $image_data) {
                     $cover_count = count($item->images);
                     if (strpos($image_data['field'], 'new_') === false) {
                         // Update existing image
                         if (str_replace('image_', '', $image_data['field']) != 0) {
                             $image_id = (int) str_replace('image_', '', $image_data['field']);
                             $cover_count--;
                             $item_images[$image_id] = array('id' => $image_id, 'data' => array('content_id' => $item->id, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_' . $image_id, '')));
                             $this->delete_image(\Input::post('image_db_' . $image_id, ''));
                         }
                     } else {
                         // Save new image
                         $image_tmp = str_replace('image_new_', '', $image_data['field']);
                         $item_images[0] = array('id' => 0, 'data' => array('content_id' => $item->id, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_new_' . $image_tmp, ''), 'cover' => $cover_count == 0 ? 1 : 0, 'sort' => $cover_count + 1));
                     }
                 }
             }
             $item_images = isset($item_images) ? $item_images : array();
             Model_Group::bind_images($item_images);
             /** END OF IMAGES **/
             // Get POST values
             $insert = \Input::post();
             $insert['type'] = 'pricing';
             $item->set($insert);
             try {
                 $item->save();
                 \Messages::success('Group successfully updated.');
                 \Response::redirect(\Input::post('exit', false) ? \Uri::create('admin/product/group/list/pricing') : \Uri::admin('current'));
             } catch (\Database_Exception $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 {
             // Delete uploaded images if there is product saving error
             if (isset($this->_image_data)) {
                 foreach ($this->_image_data as $image_data) {
                     $this->delete_image($image_data['saved_as']);
                 }
             }
             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 = Model_Group::find_one_by_id($id);
     $products = Model_Group_Options::find(array('where' => array(array('product_group_id', '=', $item->id)), 'order_by' => array('id' => 'asc')));
     $rrp_discounts = Model_Group_Discounts::find_by(array(array('product_group_id', '=', $item->id)));
     $cust_groups = \DB::select()->from('groups')->where('is_admin', 0)->order_by('id')->as_object()->execute();
     $a_cust = array();
     $a_rrp_discounts = array();
     foreach ($cust_groups as $cust) {
         $a_cust[$cust->id] = $cust->name;
         if ($rrp_discounts) {
             foreach ($rrp_discounts as $disc) {
                 if ($disc->user_group_id == $cust->id) {
                     $a_rrp_discounts[$cust->id] = $disc->discount;
                 }
             }
         }
     }
     //outer loop
     $total_user_groups = count($a_cust);
     $a_groups = array();
     $a_has = array();
     $i_ctr = 0;
     if ($products) {
         foreach ($products as $product) {
             if (isset($a_cust[$product->user_group_id])) {
                 $a_groups[$i_ctr]['user_group_id'] = $product->user_group_id;
                 $a_groups[$i_ctr]['user_group'] = isset($a_cust[$product->user_group_id]) ? $a_cust[$product->user_group_id] : '';
                 $a_groups[$i_ctr]['id'] = $product->product_group_id;
                 $a_groups[$i_ctr]['active'] = $product->active;
                 $a_groups[$i_ctr]['able_to_buy'] = $product->able_to_buy;
                 $a_groups[$i_ctr]['able_to_view'] = $product->able_to_view;
                 $a_groups[$i_ctr]['sale_discount'] = $product->sale_discount;
                 $a_groups[$i_ctr]['rrp_discount'] = isset($a_rrp_discounts[$product->user_group_id]) ? $a_rrp_discounts[$product->user_group_id] : 0;
                 $a_groupd[$i_ctr]['discounts'] = $rrp_discounts;
                 $a_groups[$i_ctr]['apply_tier_to_sale'] = $product->apply_tier_to_sale;
                 $a_has[] = $product->user_group_id;
                 $i_ctr++;
             }
         }
         //loop
     }
     //if
     if ($total_user_groups > count($a_has)) {
         foreach ($a_cust as $s_key => $c_group) {
             if (!in_array($s_key, $a_has)) {
                 $a_groups[$i_ctr]['user_group_id'] = $s_key;
                 $a_groups[$i_ctr]['user_group'] = $c_group;
                 $a_groups[$i_ctr]['id'] = 0;
                 $a_groups[$i_ctr]['active'] = 0;
                 $a_groups[$i_ctr]['able_to_buy'] = 0;
                 $a_groups[$i_ctr]['able_to_view'] = 0;
                 $a_groups[$i_ctr]['sale_discount'] = 0;
                 $a_groups[$i_ctr]['rrp_discount'] = 0;
                 $a_groupd[$i_ctr]['discounts'] = 0;
                 $a_groups[$i_ctr]['apply_tier_to_sale'] = 0;
                 $i_ctr++;
             }
         }
         //loop
     }
     // disable customer groups which already belongs to another pricing group
     if ($i_selected_customer_group == 0) {
         $assigned_groups = Model_Group_Options::find_by(array('active' => 1, array('product_group_id', '!=', $id)));
         foreach ($assigned_groups as $result) {
             foreach ($a_groups as $key => $value) {
                 if ($a_groups[$key]['user_group_id'] == $result->user_group_id) {
                     $a_groups[$key]['disabled'] = true;
                 }
             }
         }
     }
     \Theme::instance()->set_partial('content', $this->view_dir . 'update')->set('group', $group)->set('a_groups', $a_groups);
 }
Example #2
0
 /**
  * Get additional content data selected
  * 
  * @param $result = Query result
  */
 public static function post_find($result)
 {
     if ($result !== null) {
         if (is_array($result)) {
             foreach ($result as $item) {
                 // It will first check if we already have result in temporary result,
                 // and only execute query if we dont. That way we dont have duplicate queries
                 // Get related products
                 $item->get_products = static::lazy_load(function () use($item) {
                     $products = Model_Product_To_Groups::find(function ($query) use($item) {
                         return $query->where('group_id', $item->id);
                     }, 'product_id');
                     if (!empty($products)) {
                         $products = '(' . implode(',', array_keys($products)) . ')';
                         return Model_Product::find(function ($query) use($products, $item) {
                             $query->where('id', 'IN', \DB::expr($products));
                             $query->order_by('sort', 'asc');
                             return $query;
                         }, 'id');
                     }
                     return array();
                 }, $item->id, 'products');
                 // Get content children
                 $item->get_children = static::lazy_load(function () use($item) {
                     return Model_Group::find(array('where' => array('parent_id' => $item->id), 'order_by' => array('sort' => 'asc')));
                 }, $item->id, 'children');
                 // Get group images
                 $item->get_images = static::lazy_load(function () use($item) {
                     return Model_Group_Image::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc')));
                 }, $item->id, 'images');
                 // Get group discount options
                 // Use one more item property (user_group_id)
                 // It can be set using methods (set_user_group_id and get_user_group_id)
                 $item->get_discount_options = static::lazy_load(function () use($item) {
                     $options = Model_Group_Options::find(array('where' => array('user_group_id' => $item->get_user_group_id(), 'product_group_id' => $item->id)));
                     return isset($options[0]) ? $options[0] : array();
                 }, $item->id, 'discount_options', 'object');
                 // Get group discount options
                 // Use one more item property (user_group_id)
                 // It can be set using methods (set_user_group_id and get_user_group_id)
                 $item->get_discounts = static::lazy_load(function () use($item) {
                     return Model_Group_Discounts::find(array('where' => array('user_group_id' => $item->get_user_group_id(), 'product_group_id' => $item->id), 'order_by' => array('qty' => 'asc')), 'qty');
                 }, $item->id, 'discounts');
                 // DO NOT CHANGE THIS TWO FUNCTIONS AS THEY ARE NEEDED
                 $item->set_user_group_id = function ($user_group_id = NULL) {
                     \Product\Model_Group::$user_group_id = $user_group_id;
                 };
                 $item->get_user_group_id = function () {
                     return \Product\Model_Group::$user_group_id;
                 };
             }
         }
     }
     // return the result
     return $result;
 }