Exemplo n.º 1
0
 public function action_create()
 {
     \View::set_global('title', 'Add New Attribute');
     if (\Input::post()) {
         $val = Model_Attribute::validate('create');
         if ($val->run()) {
             // Get POST values
             $insert = \Input::post();
             $item = Model_Attribute::forge($insert);
             try {
                 $item->save();
                 \Messages::success('Attribute successfully created.');
                 \Response::redirect(\Input::post('update', false) ? \Uri::create('admin/attribute/update/' . $item->id) : \Uri::admin('current'));
             } catch (\Database_Exception $e) {
                 // show validation errors
                 \Messages::error('<strong>There was an error while trying to create attribute</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 create attribute</strong>');
                 foreach ($val->error() as $e) {
                     \Messages::error($e->get_message());
                 }
             }
         }
     }
     // Keep existing messages
     \Messages::instance()->shutdown();
     \Response::redirect('admin/attribute/list');
 }
Exemplo n.º 2
0
 /**
  * Update product price
  * 
  * @access  public
  * @return  Response
  */
 public function action_update($id = false)
 {
     $flag = true;
     if (!is_numeric($id)) {
         \Response::redirect('admin/product/list');
     }
     // Get product item to edit
     if (!($item = Model_Product::find_one_by_id($id))) {
         \Response::redirect('admin/product/list');
     }
     // Redirect to attribute group
     if (\Input::get('attribute_group', false) === false && !empty($item->attributes)) {
         foreach ($item->attributes as $attr_obj) {
             if ($attr_obj->attribute_group_id > 0) {
                 \Response::redirect(\Uri::create(\Uri::admin(), array(), array('attribute_group' => $attr_obj->attribute_group_id)));
             }
         }
     }
     // NRB-Gem: Comment out; Logic does not apply anymore
     // if(\Input::get('user_group', false) === false)
     // {
     //     \Response::redirect(\Uri::create(\Uri::admin(), array(), array('user_group' => 3) + \Input::get()));
     // }
     \View::set_global('title', 'Edit Product Price');
     \View::set_global('quick_menu_save', 'form.main_form');
     // Update
     if (\Input::post()) {
         $post = \Input::post();
         $return_to = is_numeric(\Input::post('return_to')) ? '#' . \Input::post('return_to') : '';
         //$val = Model_Attribute::validate();
         try {
             if (isset($post[$post['price_type']])) {
                 foreach ($post[$post['price_type']] as $key => $price) {
                     if ($update_price = Model_Attribute_Price::find_one_by_id($key)) {
                         if (isset($post['active'][$key])) {
                             $update_price->active = $post['active'][$key];
                         }
                         if (isset($post['product_group_discount_id'][$key])) {
                             $update_price->product_group_discount_id = $post['product_group_discount_id'][$key];
                         }
                         $update_price->save();
                     }
                 }
             }
             $multiple_images = array();
             $image_new = false;
             foreach ($post['attributes'] as $key => $attribute) {
                 if (!empty($post['delete_items'])) {
                     $delete_items = explode(',', $post['delete_items']);
                     $result1 = \DB::delete('product_attributes')->where('id', 'in', $delete_items)->execute();
                     $result2 = \DB::delete('product_attribute_price')->where('product_attribute_id', 'in', $delete_items)->execute();
                 }
                 // Check existing product attribute group
                 $existing_attribute_group = Model_Attribute::find(function ($query) use($post) {
                     $query->where('product_id', $post['product_id']);
                     $query->and_where_open();
                     $query->where('attribute_group_id', null, \DB::expr('IS NOT NULL'));
                     // $query->and_where('attribute_group_id', '!=' , 0);
                     $query->and_where('attribute_group_id', '!=', $post['attribute_group_id']);
                     $query->and_where_close();
                 });
                 //if($existing_attribute_group && $post['attribute_group_id'] != 0)
                 if ($existing_attribute_group) {
                     foreach ($existing_attribute_group as $item) {
                         $delete_attribute = $item->id;
                         $item->delete();
                         $attribute_option = Model_Attribute_Price::find_one_by_product_attribute_id($delete_attribute);
                         if ($attribute_option) {
                             $attribute_option->delete();
                         }
                     }
                 }
                 // Update
                 if (isset($post['update_items'][$key])) {
                     // Lightmedia - michael: check if product attribute code is exists on the product
                     if ($this->check_attr_code_exists($id, $post['update_items'][$key], $post['product_code'][$key])) {
                         $flag = false;
                         \Messages::error($post['product_code'][$key] . ' code already used');
                         continue;
                     }
                     $update = Model_Attribute::find_one_by_id($post['update_items'][$key]);
                     if ($update) {
                         $item_images = array();
                         $data = array('product_id' => $post['product_id'], 'attribute_group_id' => $post['attribute_group_id'], 'attributes' => $attribute, 'product_code' => $post['product_code'][$key], 'retail_price' => $post['retail_price'][$key], 'sale_price' => $post['sale_price'][$key], 'stock_quantity' => $post['stock_quantity'][$key], 'active' => isset($post['active']) && $post['active'][$key] ? $post['active'][$key] : $post['active_new'][$key]);
                         // Default radio
                         $data['default'] = 0;
                         if (isset($post['default']) && $post['default'] == $key) {
                             $this->reset_default($item->id);
                             $data['default'] = 1;
                         }
                         $update->set($data);
                         $update->save();
                         $attr_id = $update->id;
                         // Get combinations for multiple images
                         if (isset($post['apply_image']) && isset($post['action'])) {
                             if (in_array($attr_id, $post['action'])) {
                                 $multiple_images['action'][$attr_id] = $attr_id;
                             }
                         }
                         if (isset($_FILES['image_new_' . $attr_id])) {
                             // Upload image and display errors if there are any
                             $image = $this->upload_image('image');
                             if ($image['errors'] && $image['exists']) {
                                 \Messages::error('<strong>There was an error while trying to upload product attribute image</strong>');
                                 foreach ($image['errors'] as $error) {
                                     \Messages::error($error);
                                 }
                             }
                             // if($image['is_valid'] && !(!$image['exists'] && \Config::get('details.image.required', false) && empty($item->images)))
                             if ($image['is_valid'] && !(!$image['exists'] && \Config::get('details.image.required', false))) {
                                 /** 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, '')));
                                         if (!empty($item_images)) {
                                             Model_Attribute::bind_images($item_images);
                                         }
                                     }
                                 }
                                 // Save images if new files are submitted
                                 if (isset($this->_image_data) && $image['exists'] !== false) {
                                     foreach ($this->_image_data as $image_data) {
                                         $cover_count = count($update->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' => $attr_id, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_' . $image_id, '')));
                                                 //$this->delete_image(\Input::post('image_db_' . $image_id, ''));
                                             }
                                             if (!empty($item_images)) {
                                                 Model_Attribute::bind_images($item_images);
                                             }
                                         } else {
                                             // Save new image
                                             $image_tmp = str_replace('image_new_', '', $image_data['field']);
                                             $image_new = $item_images[0] = array('id' => 0, 'data' => array('content_id' => $attr_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));
                                             if (!empty($item_images)) {
                                                 Model_Attribute::bind_images($item_images);
                                             }
                                             // Multiple images
                                             if (isset($post['apply_image']) && isset($post['action']) && false) {
                                                 foreach ($post['action'] as $action_value) {
                                                     if ($action_value == $attr_id) {
                                                         continue;
                                                     }
                                                     $item_images = array();
                                                     if ($action_value > 0) {
                                                         $image_new[0] = array('id' => 0, 'data' => array('content_id' => $action_value, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_new_' . $image_tmp, ''), 'cover' => $cover_count == 0 ? 1 : 0, 'sort' => $cover_count + 1));
                                                     }
                                                     if (!empty($item_images)) {
                                                         Model_Attribute::bind_images($item_images);
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                                 /** END OF IMAGES **/
                             }
                         }
                         if (isset($post['active_new'][$key])) {
                             //NRB-Gem: insert attributes to existing Products added to Pricing Groups
                             $this->add_attribute_price($post, array('attr_id' => $attr_id, 'key' => $key));
                         }
                     }
                 } else {
                     if ($this->check_attr_code_exists($post['product_id'], false, $post['product_code_new'][$key])) {
                         $flag = false;
                         \Messages::error($post['product_code_new'][$key] . ' code already used');
                         continue;
                     }
                     $item_images = array();
                     $insert = Model_Attribute::forge(array('product_id' => $post['product_id'], 'attribute_group_id' => $post['attribute_group_id'], 'attributes' => $attribute, 'product_code' => $post['product_code_new'][$key], 'retail_price' => $post['retail_price_new'][$key], 'active' => $post['active_new'][$key], 'sale_price' => $post['sale_price_new'][$key], 'stock_quantity' => $post['stock_quantity'][$key]));
                     // Default radio
                     $insert->default = 0;
                     if (isset($post['default']) && $post['default'] == $key) {
                         $this->reset_default($item->id);
                         $insert->default = 1;
                     }
                     $insert->save();
                     $attr_id = $insert->id;
                     // Get combinations for multiple images
                     if (isset($post['apply_image']) && isset($post['action_new'][$key]) && $post['action_new'][$key] == 1) {
                         $multiple_images['action_new'][$attr_id] = $attr_id;
                     }
                     if (isset($_FILES['_image_new_' . $key])) {
                         // Upload image and display errors if there are any
                         $image = $this->upload_image('image');
                         if ($image['errors'] && $image['exists']) {
                             \Messages::error('<strong>There was an error while trying to upload product attribute image</strong>');
                             foreach ($image['errors'] as $error) {
                                 \Messages::error($error);
                             }
                         }
                         // if($image['is_valid'] && !(!$image['exists'] && \Config::get('details.image.required', false) && empty($item->images)))
                         if ($image['is_valid'] && !(!$image['exists'] && \Config::get('details.image.required', false))) {
                             /** IMAGES **/
                             // Save images if new files are submitted
                             if (isset($this->_image_data) && $image['exists'] !== false) {
                                 foreach ($this->_image_data as $image_data) {
                                     $cover_count = 0;
                                     // Save new image
                                     $image_tmp = str_replace('_image_new_', '', $image_data['field']);
                                     $image_new = $item_images[0] = array('id' => 0, 'data' => array('content_id' => $attr_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));
                                     if (!empty($item_images)) {
                                         Model_Attribute::bind_images($item_images);
                                     }
                                     // Multiple images
                                     if (isset($post['apply_image']) && isset($post['action_new']) && false) {
                                         foreach ($post['action_new'] as $action_key => $action_value) {
                                             if ($action_value == $attr_id) {
                                                 continue;
                                             }
                                             $item_images = array();
                                             if ($action_value > 0) {
                                                 $image_new[0] = array('id' => 0, 'data' => array('content_id' => $action_value, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_new_' . $image_tmp, ''), 'cover' => $cover_count == 0 ? 1 : 0, 'sort' => $cover_count + 1));
                                             }
                                             if (!empty($item_images)) {
                                                 Model_Attribute::bind_images($item_images);
                                             }
                                         }
                                     }
                                 }
                             }
                             /** END OF IMAGES **/
                         }
                     }
                     if (isset($post[$post['price_type'] . '_new'][$key])) {
                         //NRB-Gem: insert attributes to existing Products added to Pricing Groups
                         $this->add_attribute_price($post, array('attr_id' => $attr_id, 'key' => $key));
                     }
                 }
             }
             // Update/insert multiple images
             if (!empty($multiple_images) && !empty($image_new) && isset($image_new['data'])) {
                 $content_id = $image_new['data']['content_id'];
                 if (isset($multiple_images['action']) && !empty($multiple_images['action'])) {
                     foreach ($multiple_images['action'] as $value) {
                         $item_images = array();
                         if ($content_id == $value) {
                             continue;
                         }
                         $image_new['data']['content_id'] = $value;
                         $item_images[0] = $image_new;
                         Model_Attribute::bind_images($item_images);
                     }
                 }
                 if (isset($multiple_images['action_new']) && !empty($multiple_images['action_new'])) {
                     foreach ($multiple_images['action_new'] as $value) {
                         $item_images = array();
                         if ($content_id == $value) {
                             continue;
                         }
                         $image_new['data']['content_id'] = $value;
                         $item_images[0] = $image_new;
                         Model_Attribute::bind_images($item_images);
                     }
                 }
             }
             if ($flag) {
                 \Messages::success('Product successfully updated.');
             }
         } catch (\Database_Exception $e) {
             // show validation errors
             \Messages::error('<strong>There was an error while trying to update product attributes</strong>');
             // Uncomment lines below to show database errors
             // $errors = $e->getMessage();
             // \Messages::error($errors);
             \Response::redirect(\Uri::create(\Uri::admin('current'), array(), \Input::get()) . $return_to);
         }
         \Response::redirect(\Uri::create(\Uri::admin('current'), array(), \Input::get()) . $return_to);
     }
     // Get current product
     $product = Model_Product::find_one_by_id($id);
     // Create array for attribute group select
     $attribute_groups_select = \Attribute\Model_Attribute_Group::fetch_pair('id', 'title', array(), array('0' => '- No Attributes -'));
     // Set user group for lazy load
     if (\Input::get('user_group')) {
         Model_Attribute::set_user_group();
     }
     // Set tier price group for lazy load
     if (\Input::get('tier_price')) {
         Model_Attribute::set_tier_price();
     }
     $param = \Input::get();
     // Find attributes for current product
     $items_db = Model_Attribute::find(function ($query) use($id, $param) {
         $query->select('product_attributes.*');
         $query->join('attribute_groups', 'LEFT')->on('attribute_groups.id', '=', 'product_attributes.attribute_group_id');
         $query->where('product_id', $id);
         if (isset($param['attribute_group']) && is_numeric($param['attribute_group'])) {
             $query->and_where('product_attributes.attribute_group_id', $param['attribute_group']);
         }
         $query->order_by('attribute_groups.sort', 'asc');
     }, 'id');
     // Decode attribute json
     $combinations_db_arr = $this->decode_attribute_json($items_db);
     // Find current attribute group
     $attribute_group = array();
     if (\Input::get('attribute_group')) {
         $attribute_group = \Attribute\Model_Attribute_Group::find_one_by_id(\Input::get('attribute_group'));
     }
     // Set vars
     $combinations = array();
     $combinations_data = array();
     $combinations_tmp = array();
     $attributes_order = array();
     $compared = array();
     $update_items = array();
     $delete_items = '';
     // Create attribute combinations
     if ($attribute_group && $attribute_group->attributes) {
         $i = 0;
         foreach ($attribute_group->attributes as $attribute) {
             $attributes_order[] = $attribute->id;
             if ($attribute->options) {
                 foreach ($attribute->options as $option) {
                     $combinations[$i][] = $attribute->title . ': ' . $option->title;
                     //$combinations[$i][] =  array($attribute->title => $option->title);
                     $combinations_tmp[$i][] = array($attribute->id => $option->id);
                 }
             }
             $i++;
         }
         // Create combinations from current attributes
         $combinations = $this->combos($combinations);
         $combinations_tmp = $this->combos($combinations_tmp);
         // Sort product atributes from database
         if ($combinations_db_arr) {
             $sorted_db_arr = $this->sort_array(array_keys($attribute_group->attributes), $combinations_db_arr);
             if ($sorted_db_arr) {
                 $compared = $this->compare_array($combinations_tmp, $sorted_db_arr, $combinations_db_arr);
             }
         }
     }
     // Something crazy
     if (!empty($compared) && !empty($items_db)) {
         if (!empty($compared['not_exist_in1'])) {
             $delete_items = array_keys($compared['not_exist_in1']);
             $delete_items = implode(',', $delete_items);
         }
         if (!empty($compared['exist_id'])) {
             $update_items = $compared['exist_id'];
         }
     }
     // Sort array asc
     $combinations_sorted = $this->sort_array_asc($combinations_tmp);
     foreach ($combinations_sorted as $key => $value) {
         $combinations_data[$key] = json_encode($value);
     }
     // Select user groups
     $user_groups = \Sentry::group()->all('front');
     if (!empty($user_groups)) {
         $user_groups = \Model_Base::fetch_pair('id', 'name', array(), false, $user_groups);
     } else {
         $user_groups = array();
     }
     $price_select = array(3 => 'Sale Price');
     $price_select_fields = array(3 => 'sale_price');
     // Set default user price type or use selected
     $price_field = 'sale_price';
     if (isset($price_select_fields[\Input::get('special')])) {
         $price_field = $price_select_fields[\Input::get('special')];
     }
     $listing = true;
     if (\Input::get('special') == 2 && !\Input::get('tier_price')) {
         $listing = false;
     }
     // Product pricing group
     if ($item->pricing_group) {
         $pricing_group = $item->pricing_group->id;
     } else {
         $pricing_group = false;
     }
     // NRB-Gem: removed logic
     // Find and select tier price
     // $tier_price = array();
     // if(\Input::get('user_group') && is_numeric(\Input::get('user_group')) && $pricing_group)
     // {
     //     $tier_price = \Product\Model_Group_Discounts::find(array(
     //         'where' => array(
     //             'user_group_id' => \Input::get('user_group'),
     //             'product_group_id' => $pricing_group,
     //         ),
     //         'order_by' => array(
     //             'discount' => 'asc'
     //         )
     //     ));
     // }
     // Reset to empty array if there are no result found by query
     if (is_null($combinations)) {
         $combinations = array();
     }
     if (is_null($combinations_data)) {
         $combinations_data = array();
     }
     $number_of_combinations = count($combinations);
     // Pagination per page default
     $per_page = \Input::get('per_page', 'all') == 'all' ? 9999 : \Input::get('per_page');
     $show_all = $per_page > $number_of_combinations ? true : false;
     // Initiate pagination
     $pagination = \Hybrid\Pagination::make(array('total_items' => count($combinations), 'per_page' => $per_page, 'uri_segment' => null));
     // Remove unwanted items, and show only required ones
     $combinations = array_slice($combinations, $pagination->offset, $pagination->per_page, true);
     $combinations_data = array_slice($combinations_data, $pagination->offset, $pagination->per_page, true);
     \Theme::instance()->set_partial('content', $this->view_dir . 'attributes')->set('product', $product)->set('price_select', $price_select, false)->set('combinations', $combinations, false)->set('combinations_data', $combinations_data, false)->set('attribute_groups_select', $attribute_groups_select, false)->set('delete_items', $delete_items, false)->set('update_items', $update_items, false)->set('items_db', $items_db, false)->set('price_field', $price_field)->set('listing', $listing)->set('pagination', $pagination, false)->set('number_of_combinations', $number_of_combinations)->set('show_all', $show_all);
 }