示例#1
0
 /**
  * Edit product infotabs
  * 
  * @param $product_id	= Product ID
  * @param $infotab_id	= Infotab ID
  * @param $hotspot_id	= Hotspot ID
  * 
  */
 public function action_infotab_edit($produt_id = false, $infotab_id = false, $hotspot_id = false)
 {
     // Check for product
     if (!is_numeric($produt_id)) {
         \Response::redirect('admin/product/list');
     }
     // Get news item to edit
     if (!($product = Model_Product::find_one_by_id($produt_id))) {
         \Response::redirect('admin/product/list');
     }
     // Check for infotab
     if (!is_numeric($infotab_id)) {
         \Response::redirect('admin/product/list');
     }
     // Get news item to edit
     if (!($item = Model_Product_To_Infotabs::find_by_pk($infotab_id))) {
         \Response::redirect('admin/product/list');
     }
     // Get hotspot is exist
     if (is_numeric($hotspot_id)) {
         if (!($hotspot = Model_Infotab_Image::find_by_pk($hotspot_id))) {
             unset($hotspot);
         }
     }
     if (\Input::post()) {
         $val = Model_Product_To_Infotabs::validate('update', $item->type);
         // Upload image and display errors if there are any
         $image = $this->upload_infotab_image();
         if (!$image['exists'] && \Config::get('infotab.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 infotab image</strong>');
             \Messages::error('You have to select image');
         } elseif ($image['errors']) {
             \Messages::error('<strong>There was an error while trying to upload infotab image</strong>');
             foreach ($image['errors'] as $error) {
                 \Messages::error($error);
             }
         }
         if ($val->run() && $image['is_valid'] && !(!$image['exists'] && \Config::get('infotab.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->_infotab_image_data)) {
                 foreach ($this->_infotab_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('infotab_id' => $item->unique_id, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_' . $image_id, '')));
                             $this->delete_infotab_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('infotab_id' => $item->unique_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));
                     }
                 }
             }
             Model_Infotab::bind_images($item_images);
             /** END OF IMAGES **/
             // Get POST values
             $insert = \Input::post();
             // Prep some values
             $insert['description'] = trim($insert['description']);
             $insert['active'] = !empty($insert['description']) ? 1 : 0;
             $item->set($insert);
             try {
                 $item->save();
                 \Messages::success('Infotab successfully updated.');
                 \Response::redirect(\Input::post('exit', false) ? \Uri::create('admin/product/infotab_list/' . $product->id) : \Uri::admin('current'));
             } catch (\Database_Exception $e) {
                 // show validation errors
                 \Messages::error('<strong>There was an error while trying to update infotab</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->_infotab_image_data)) {
                 foreach ($this->_infotab_image_data as $image_data) {
                     $this->delete_infotab_image($image_data['saved_as']);
                 }
             }
             if ($val->error() != array()) {
                 // show validation errors
                 \Messages::error('<strong>There was an error while trying to update infotab</strong>');
                 foreach ($val->error() as $e) {
                     \Messages::error($e->get_message());
                 }
             }
         }
     }
     $item = Model_Product_To_Infotabs::find_by_pk($infotab_id);
     $theme_partial = \Theme::instance()->set_partial('content', $this->view_dir . 'infotabs_edit_' . strtolower($item->type))->set('product', $product)->set('infotab', $item);
     if (isset($hotspot)) {
         $theme_partial->set('hotspot', $hotspot);
     }
 }
示例#2
0
 /**
  * Fetch a item from the HTTP request headers
  *
  * @return  array
  */
 public static function headers($index = null, $default = null)
 {
     static $headers = null;
     // do we need to fetch the headers?
     if ($headers === null) {
         // deal with fcgi or nginx installs
         if (!function_exists('getallheaders')) {
             $server = \Arr::filter_prefixed(static::server(), 'HTTP_', true);
             foreach ($server as $key => $value) {
                 $key = join('-', array_map('ucfirst', explode('_', strtolower($key))));
                 $headers[$key] = $value;
             }
             $value = static::server('Content_Type', static::server('Content-Type')) and $headers['Content-Type'] = $value;
             $value = static::server('Content_Length', static::server('Content-Length')) and $headers['Content-Length'] = $value;
         } else {
             $headers = getallheaders();
         }
     }
     return empty($headers) ? $default : (func_num_args() === 0 ? $headers : \Arr::get($headers, $index, $default));
 }
示例#3
0
 public function action_update($id = false)
 {
     if (!is_numeric($id)) {
         throw new \HttpNotFoundException();
     }
     // Get accordion item to edit
     if (!($item = Model_Accordion::find_one_by_id($id))) {
         throw new \HttpNotFoundException();
     }
     // Accordion from home page?
     if ($item->parent_id == 1) {
         \Config::load('page::accordion_banner', 'details', true, true);
     }
     \View::set_global('title', 'Edit Accordion');
     if (\Input::post()) {
         $val = Model_Accordion::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 accordion image</strong>');
             \Messages::error('You have to select image');
         } elseif ($image['errors']) {
             \Messages::error('<strong>There was an error while trying to upload accordion 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));
                     }
                 }
             }
             Model_Accordion::bind_images($item_images);
             /** END OF IMAGES **/
             // Get POST values
             $insert = \Input::post();
             // Prepare some values
             $insert['active_from'] = !empty($insert['active_from']) ? strtotime($insert['active_from']) : NULL;
             $insert['active_to'] = !empty($insert['active_to']) ? strtotime($insert['active_to']) : NULL;
             if ($insert['status'] != 2) {
                 unset($insert['active_from']);
                 unset($insert['active_to']);
             }
             $item->set($insert);
             try {
                 $item->save();
                 \Messages::success('Accordion successfully updated.');
                 \Response::redirect(\Input::post('exit', false) ? \Uri::create('admin/page/accordion/list/' . $item->parent_id) : \Uri::admin('current'));
             } catch (\Database_Exception $e) {
                 // show validation errors
                 \Messages::error('<strong>There was an error while trying to update accordion</strong>');
                 // Uncomment lines below to show database errors
                 //$errors = $e->getMessage();
                 //\Messages::error($errors);
             }
         } else {
             // Delete uploaded images if there is page 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 accordion</strong>');
                 foreach ($val->error() as $e) {
                     \Messages::error($e->get_message());
                 }
             }
         }
     }
     $accordion = Model_Accordion::find_one_by_id($id);
     $page = Model_Page::find_one_by_id($accordion->parent_id);
     \Theme::instance()->set_partial('content', $this->view_dir . 'update')->set('page', $page)->set('accordion', $accordion);
 }
示例#4
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);
 }
示例#5
0
 /**
  * Tests Arr::element()
  *
  * @test
  */
 public function test_filter_prefixed()
 {
     $arr = array('foo' => 'baz', 'prefix_bar' => 'yay');
     $output = Arr::filter_prefixed($arr);
     $this->assertEquals(array('bar' => 'yay'), $output);
 }
示例#6
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);
 }
 protected function save_order()
 {
     if (!$this->check_logged()) {
         \Messages::error('You must be logged in if you want to continue with your order.');
         \Response::redirect(\Uri::create('order/checkout/address'));
     }
     // Save order
     $user = false;
     $order = false;
     $items = \Cart::items();
     if (\Sentry::check()) {
         $user = \Sentry::user();
     }
     if (\Input::post() && $items && $user) {
         $group_id = $user['groups'][0]['id'];
         $item_with_discount = array();
         foreach ($items as $item) {
             $id = $item->get('id');
             $product_groups = \Product\Model_Product_To_Groups::find_by_product_id($item->get('id'));
             foreach ($product_groups as $group) {
                 $all_discounts = \Product\Model_Group_Discounts::find_by(array('user_group_id' => $group_id, 'product_group_id' => $group->group_id), null, null, null);
                 foreach ($all_discounts as $discount) {
                     $discount = (int) $item_with_discount[$id]['discount'] + $discount->discount;
                     $sub_total = $item->totalPrice(true) - (int) $discount / $item->totalPrice(true) * 100;
                     $item_with_discount[$id] = array('product_group_id' => $group->product_id, 'user_group_id' => $group->group_id, 'discount' => $discount, 'sub_total' => $sub_total);
                 }
             }
             $item_with_discount['total_discount'] = (int) $item_with_discount['total_discount'] + (int) $item_with_discount[$id]['total_discount'];
             $item_with_discount['total_price'] = (double) $item_with_discount['total_price'] + (double) $item_with_discount[$id]['sub_total'];
         }
         // check for a valid CSRF token
         if (!\Security::check_token()) {
             \Messages::error('CSRF attack or expired CSRF token.');
             \Response::redirect(\Input::referrer(\Uri::create('order/checkout/cost')));
         }
         try {
             // Update or create order
             if (is_numeric(\Session::get('order.id'))) {
                 $order = \Order\Model_Order::find_one_by_id(\Session::get('order.id'));
             }
             if (!$order) {
                 $order = \Order\Model_Order::forge();
             }
             $shipping_price = $order->shipping_price(null, null, true);
             $metadata = $user['metadata'];
             if ($billing = \Arr::filter_prefixed($metadata, 'shipping_')) {
                 foreach ($billing as $key => $value) {
                     $order->{$key} = $metadata[$key];
                     unset($metadata[$key]);
                 }
             }
             foreach ($metadata as $key => $value) {
                 $order->{$key} = $value;
             }
             $order->email = $user->get('email');
             $order->user_id = $user->get('id');
             $order->status = 'Pending';
             $order->discount_amount = $item_with_discount['total_discount'];
             //\Cart::getTotal('price');
             $order->total_price = $item_with_discount['total_price'];
             //\Cart::getTotal('price');
             $order->finished = 1;
             $order->guest = $metadata['guest'] ? 1 : 0;
             $order->accepted = $metadata['master'] == 1 ? 1 : 0;
             $order->credit_account = $metadata['credit_account'] == 1 ? 1 : 0;
             $order->shipping_price = $shipping_price;
             // Save order, add products to order products
             if ($order->save()) {
                 foreach ($items as $item) {
                     $product_data = null;
                     if ($product = \Product\Model_Product::find_one_by_id($item->get('id'))) {
                         $product_data = \Product\Model_Product::product_data($product, $item->get('attributes'));
                     }
                     if ($product_data) {
                         $order_products = \Order\Model_Products::forge();
                         $order_products->order_id = $order->id;
                         $order_products->title = $product->title;
                         $order_products->code = $product_data['code'];
                         $order_products->price = $item->singlePrice(true);
                         $order_products->price_type = $product_data['price_type'];
                         $order_products->quantity = $item->get('quantity');
                         $order_products->product_id = $product->id;
                         $order_products->artwork_required = $product->artwork_required;
                         $order_products->artwork_free_over = $product->artwork_free_over;
                         $order_products->subtotal = $item_with_discount[$item->get('id')]['sub_total'];
                         //$item->totalPrice(true);
                         $order_products->attributes = json_encode(\Product\Model_Attribute::get_combination($item->get('attributes')));
                         if (!empty($product->categories)) {
                             $categories = array();
                             foreach ($product->categories as $category) {
                                 $categories[] = $category->title;
                             }
                             if ($categories) {
                                 $order_products->product_category = implode(',', $categories);
                             }
                         }
                         $order_products->save();
                         // Find artworks
                         if ($unique_id = $item->get('unique_id')) {
                             if ($artworks = \Order\Model_Artwork::find(array('where' => array('unique_id' => $unique_id, 'order_id' => $order->id)))) {
                                 $ysi = \Yousendit\Base::forge();
                                 // Artworks (update, delete)
                                 foreach ($artworks as $artwork) {
                                     // Remove deleted artwork
                                     if ($artwork->deleted_at > 0) {
                                         $ysi->delete_artwork($artwork->file_id);
                                         $artwork->delete();
                                     } else {
                                         $artwork->order_product_id = $order_products->id;
                                         $artwork->save();
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             if ($order) {
                 return $order;
             } else {
                 return false;
             }
         } catch (\Database_Exception $e) {
             // show validation errors
             \Messages::error('There was an error while trying to save your order.');
             // Uncomment lines below to show database errors
             $errors = $e->getMessage();
             \Messages::error($errors);
             \Response::redirect(\Uri::create('order/checkout/cost'));
         }
         return false;
     }
 }
示例#8
0
 protected function save_order()
 {
     if (!$this->check_logged()) {
         \Messages::error('You must be logged in if you want to continue with your order.');
         \Response::redirect(\Uri::create('order/checkout/address'));
     }
     // Save order
     $user = false;
     $order = false;
     $items = \Cart::items();
     if (\Sentry::check()) {
         $user = \Sentry::user();
     }
     if (\Input::post() && $items && $user) {
         // check for a valid CSRF token
         if (!\Security::check_token()) {
             \Messages::error('CSRF attack or expired CSRF token.');
             \Response::redirect(\Input::referrer(\Uri::create('order/checkout/cost')));
         }
         try {
             // Update or create order
             if (is_numeric(\Session::get('order.id'))) {
                 $order = \Order\Model_Order::find_one_by_id(\Session::get('order.id'));
             }
             if (!$order) {
                 $order = \Order\Model_Order::forge();
             }
             $cart_total = 0;
             foreach ($items as $item) {
                 $product_data = null;
                 if ($product = \Product\Model_Product::find_one_by_id($item->get('id'))) {
                     $product_data = \Product\Model_Product::product_data($product, $item->get('attributes'));
                 }
                 $total_price = $item->totalDiscountedPrice(true);
                 if (isset($product_data["price"]) && $product_data["price"] != 0) {
                     $total_price = $product_data["price"] * $item->get('quantity');
                 }
                 $cart_total += $total_price;
             }
             $method = \Input::post('delivery') == 'pickup' ? false : true;
             $shipping_price = $order->shipping_price(null, null, true, $method);
             $metadata = $user['metadata'];
             $data = array('email' => $user->get('email'), 'user_id' => $user->get('id'), 'status' => 'Pending', 'total_price' => $cart_total, 'finished' => 1, 'guest' => $metadata['guest'] ? 1 : 0, 'accepted' => $metadata['master'] == 1 ? 1 : 0, 'credit_account' => $metadata['credit_account'] == 1 ? 1 : 0, 'shipping_method' => \Input::post('delivery'), 'shipping_price' => $shipping_price, 'gst_price' => ($cart_total + $shipping_price) * 0.1);
             // $order->discount_amount = $item_with_discount['total_discount'];//\Cart::getTotal('price');
             //\Cart::getDiscountedTotal('price');//\Cart::getTotal('price');
             if ($billing = \Arr::filter_prefixed($metadata, 'shipping_')) {
                 foreach ($billing as $key => $value) {
                     $data[$key] = $metadata[$key];
                     unset($metadata[$key]);
                 }
             }
             foreach ($metadata as $key => $value) {
                 $data[$key] = $value;
             }
             $order->set($data);
             // Save order, add products to order products
             if ($order->save()) {
                 foreach ($items as $item) {
                     $product_data = null;
                     if ($product = \Product\Model_Product::find_one_by_id($item->get('id'))) {
                         $product_data = \Product\Model_Product::product_data($product, $item->get('attributes'));
                     }
                     $item_exists = \Order\Model_Products::find(array('where' => array('product_id' => $product->id, 'order_id' => $order->id)));
                     if ($product_data && !$item_exists) {
                         $order_products = \Order\Model_Products::forge(array('order_id' => $order->id, 'title' => $product->title, 'code' => $product_data['code'], 'price' => $product_data['price'], 'price_type' => $product_data['price_type'], 'quantity' => $item->get('quantity'), 'product_id' => $product->id, 'artwork_required' => $product->artwork_required, 'artwork_free_over' => $product->artwork_free_over, 'subtotal' => $product_data['price'] * $item->get('quantity'), 'attributes' => json_encode(\Product\Model_Attribute::get_combination($item->get('attributes'))), 'attributes_id' => $item->get('attributes')));
                         //$item->singleDiscountedPrice(true);//$item->singlePrice(true);
                         //$item->totalDiscountedPrice(true);//$item->totalPrice(true);
                         if (!empty($product->categories)) {
                             $categories = array();
                             foreach ($product->categories as $category) {
                                 $categories[] = $category->title;
                             }
                             if ($categories) {
                                 $order_products->product_category = implode(',', $categories);
                             }
                         }
                         // update stock quantity
                         \Product\Model_Attribute::update_stock_quantity($item->get('attributes'), $item->get('quantity'));
                         $order_products->save();
                         // Find artworks
                         if ($unique_id = $item->get('unique_id')) {
                             if ($artworks = \Order\Model_Artwork::find(array('where' => array('unique_id' => $unique_id, 'order_id' => $order->id)))) {
                                 $ysi = \Yousendit\Base::forge();
                                 // Artworks (update, delete)
                                 foreach ($artworks as $artwork) {
                                     // Remove deleted artwork
                                     if ($artwork->deleted_at > 0) {
                                         $ysi->delete_artwork($artwork->file_id);
                                         $artwork->delete();
                                     } else {
                                         $artwork->set(array('order_product_id' => $order_products->id));
                                         $artwork->save();
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             if ($order) {
                 return $order;
             } else {
                 return false;
             }
         } catch (\Database_Exception $e) {
             // show validation errors
             \Messages::error('There was an error while trying to save your order.');
             // Uncomment lines below to show database errors
             $errors = $e->getMessage();
             \Messages::error($errors);
             \Response::redirect(\Uri::create('order/checkout/cost'));
         }
         return false;
     }
 }
示例#9
0
 public function action_update_files($id = false)
 {
     if (!is_numeric($id)) {
         \Response::redirect('admin/application/list');
     }
     // Get news item to edit
     if (!($item = Model_Application::find_one_by_id($id))) {
         \Response::redirect('admin/application/list');
     }
     \View::set_global('title', 'Edit Application Files');
     if (\Input::post()) {
         if (\Input::post('file_upload', false)) {
             // Upload files and display errors if there are any
             $file = $this->upload_file();
             if (!$file['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 content file</strong>');
                 \Messages::error('You have to select image');
             } elseif ($file['errors']) {
                 \Messages::error('<strong>There was an error while trying to upload content file</strong>');
                 foreach ($file['errors'] as $error) {
                     \Messages::error($error);
                 }
             }
             if ($file['is_valid'] && !(!$file['exists'] && \Config::get('details.file.required', false) && empty($item->files))) {
                 /** FILES **/
                 // Get all alt texts to update if there is no image change
                 foreach (\Arr::filter_prefixed(\Input::post(), 'title_') as $file_id => $title) {
                     if (strpos($file_id, 'new_') === false) {
                         $item_files[$file_id] = array('id' => $file_id, 'data' => array('title' => \Input::post('title_' . $file_id, '')));
                     }
                 }
                 // Save files if new ones are submitted
                 if (isset($this->_file_data)) {
                     foreach ($this->_file_data as $file_data) {
                         $cover_count = count($item->files);
                         if (strpos($file_data['field'], 'new_') === false) {
                             // Update existing file
                             if (str_replace('file_', '', $file_data['field']) != 0) {
                                 $file_id = (int) str_replace('file_', '', $file_data['field']);
                                 $cover_count--;
                                 $item_files[$file_id] = array('id' => $file_id, 'data' => array('content_id' => $item->id, 'file' => $file_data['saved_as'], 'title' => \Input::post('title_' . $file_id, '')));
                                 $this->delete_file(\Input::post('file_db_' . $file_id, ''));
                             }
                         } else {
                             // Save new file
                             $file_tmp = str_replace('file_new_', '', $file_data['field']);
                             $item_files[0] = array('id' => 0, 'data' => array('content_id' => $item->id, 'file' => $file_data['saved_as'], 'title' => \Input::post('title_new_' . $file_tmp, ''), 'cover' => $cover_count == 0 ? 1 : 0, 'sort' => $cover_count + 1));
                         }
                     }
                 }
                 Model_Application::bind_files($item_files);
                 \Messages::success('Application documents successfully updated.');
                 \Response::redirect(\Uri::admin('current'));
                 /** END OF FILES **/
             } else {
                 // Delete uploaded files if there is application saving error
                 if (isset($this->_file_data)) {
                     foreach ($this->_file_data as $file_data) {
                         $this->delete_file($file_data['saved_as']);
                     }
                 }
                 \Messages::error('<strong>There was an error while trying to update application documents</strong>');
             }
         }
         if (\Input::post('video_upload', false)) {
             // Upload image and display errors if there are any
             $image = $this->upload_image('video');
             if ($image['errors']) {
                 \Messages::error('<strong>There was an error while trying to upload content image</strong>');
                 foreach ($image['errors'] as $error) {
                     \Messages::error($error);
                 }
             }
             if ($image['is_valid'] && !(\Config::get('details.video.required', false) && empty($item->videos))) {
                 /** VIDEOS **/
                 // Get all video urls to update if there is no image change
                 foreach (\Arr::filter_prefixed(\Input::post(), 'video_url_') as $video_id => $url) {
                     $not_updated[] = $video_id;
                     if (strpos($video_id, 'new_') === false) {
                         $video_url = \Input::post('video_url_' . $video_id, false);
                         if ($video_url) {
                             // Check video
                             $youtube = \App\Youtube::forge();
                             $video = $youtube->parse(\Input::post('video_url_' . $video_id, ''))->get();
                             if ($video) {
                                 // Update existing videos
                                 $item_videos[$video_id] = array('id' => $video_id, 'data' => array('content_id' => $item->id, 'url' => \Input::post('video_url_' . $video_id, ''), 'title' => \Input::post('video_title_' . $video_id, '')));
                                 // Remove video ID as it is updated correctly
                                 array_pop($not_updated);
                                 // Delete image and use default one
                                 if (\Input::post('video_delete_image_' . $video_id, false)) {
                                     $item_videos[$video_id]['data']['thumbnail'] = '';
                                     $this->delete_image(\Input::post('video_file_db_' . $video_id, ''), 'video');
                                 }
                             } else {
                                 \Messages::error('"' . \Input::post('video_url_' . $video_id, '') . '" is invalid video URL. Video not updated.');
                             }
                         } else {
                             \Messages::error('Video URL can\'t be empty. Video not updated.');
                         }
                     } else {
                         $video_url = \Input::post('video_url_' . $video_id, false);
                         if ($video_url) {
                             // Check video
                             $youtube = \App\Youtube::forge();
                             $video = $youtube->parse(\Input::post('video_url_' . $video_id, ''))->get();
                             if ($video) {
                                 // Add new videos
                                 $item_videos[0] = array('id' => 0, 'data' => array('content_id' => $item->id, 'url' => \Input::post('video_url_' . $video_id, ''), 'title' => \Input::post('video_title_' . $video_id, ''), 'sort' => count($item->videos) + 1));
                                 // Remove video ID as it is updated correctly
                                 array_pop($not_updated);
                             } else {
                                 \Messages::error('"' . \Input::post('video_url_' . $video_id, '') . '" is invalid video URL. Video not updated.');
                             }
                         }
                     }
                 }
                 // Save images if new files are submitted
                 if (isset($this->_image_data)) {
                     foreach ($this->_image_data as $image_data) {
                         if (!in_array(str_replace('video_file_', '', $image_data['field']), $not_updated)) {
                             if (strpos($image_data['field'], 'new_') === false) {
                                 // Update existing image
                                 if (str_replace('video_file_', '', $image_data['field']) != 0) {
                                     $video_id = (int) str_replace('video_file_', '', $image_data['field']);
                                     $item_videos[$video_id]['data']['thumbnail'] = $image_data['saved_as'];
                                     $this->delete_image(\Input::post('video_file_db_' . $video_id, ''), 'video');
                                 }
                             } else {
                                 // Save new image
                                 $image_tmp = str_replace('video_file_new_', '', $image_data['field']);
                                 $item_videos[0]['data']['thumbnail'] = $image_data['saved_as'];
                             }
                         } else {
                             // That video was not updated so we remove its image if there is one
                             $this->delete_image($image_data['saved_as'], 'video');
                         }
                     }
                 }
                 Model_Application::bind_videos($item_videos);
                 \Messages::success('Application videos successfully updated.');
                 \Response::redirect(\Uri::admin('current'));
                 /** END OF VIDEOS **/
             } else {
                 // Delete uploaded images if there is application saving error
                 if (isset($this->_image_data)) {
                     foreach ($this->_image_data as $image_data) {
                         $this->delete_image($image_data['saved_as'], 'video');
                     }
                 }
                 \Messages::error('<strong>There was an error while trying to update application videos</strong>');
             }
         }
     }
     \Theme::instance()->set_partial('content', $this->view_dir . 'update_files')->set('application', Model_Application::find_one_by_id($id));
 }