예제 #1
0
 public function download($fileId, $orderId)
 {
     //get the order.
     $order = \CI::db()->where('orders.id', $orderId)->join('customers', 'customers.id = orders.customer_id')->get('orders')->row();
     $file = \CI::db()->where('order_item_files.id', $fileId)->join('digital_products', 'digital_products.id = order_item_files.file_id')->get('order_item_files')->row();
     if ($order && $file) {
         if ($order->is_guest || $order->customer_id == $this->customer->id) {
             if ($file->max_downloads == 0 || $file->downloads_used < $file->max_downloads) {
                 \CI::DigitalProducts()->touchDownload($fileId);
                 \CI::DigitalProducts()->downloadFile($file->filename);
             }
         } else {
             //send to login page
             if (\CI::Login()->isLoggedIn(false, false)) {
                 redirect('login');
             } else {
                 throw_404();
             }
         }
     } else {
         //move along nothing to see here
         throw_404();
     }
 }
예제 #2
0
 public function download($link)
 {
     $filedata = \CI::DigitalProducts()->get_file_info_by_link($link);
     // missing file (bad link)
     if (!$filedata) {
         show_404();
     }
     // validate download counter
     if ($filedata->max_downloads > 0) {
         if (intval($filedata->downloads) >= intval($filedata->max_downloads)) {
             show_404();
         }
     }
     // increment downloads counter
     \CI::DigitalProducts()->touch_download($link);
     // Deliver file
     \CI::load()->helper('download');
     force_download('uploads/digital_uploads/', $filedata->filename);
 }
예제 #3
0
 public function insertItem($data = [])
 {
     $product = false;
     $quantity = 1;
     $postedOptions = false;
     $downloads = false;
     $combine = false;
     //is this an item from a separate cart being combined?
     extract($data);
     if (is_int($product)) {
         $product = \CI::Products()->getProduct($product);
         if (!$product) {
             return json_encode(['error' => lang('error_product_not_found')]);
         }
         //Clean up the product for the orderItems database
         $product = $this->cleanProduct($product);
         //get downloadable files
         $downloads = \CI::DigitalProducts()->getAssociationsByProduct($product->product_id);
     }
     $update = false;
     if (empty($product->hash)) {
         $product->hash = md5(json_encode($product) . json_encode($postedOptions));
         //set defaults for new items
         $product->coupon_discount = 0;
         $product->coupon_discount_quantity = 0;
         $product->coupon_code = '';
     } else {
         if (!$combine) {
             //this is an update
             $update = true;
         }
     }
     $product->order_id = $this->cart->id;
     //loop through the products in the cart and make sure we don't have this in there already. If we do get those quantities as well
     $qty_count = $quantity;
     $this->getCartItems();
     // refresh the cart items
     foreach ($this->items as $item) {
         if (intval($item->product_id) == intval($product->product_id)) {
             if ($item->hash != $product->hash) {
                 $qty_count = $qty_count + $item->quantity;
             }
         }
         if ($item->hash == $product->hash && !$update) {
             //if the item is already in the cart, send back a message
             return json_encode(['message' => lang('item_already_added')]);
         }
     }
     if (!config_item('allow_os_purchase') && (bool) $product->track_stock) {
         $stock = \CI::Products()->getProduct($product->product_id);
         if ($stock->quantity < $qty_count) {
             return json_encode(['error' => sprintf(lang('not_enough_stock'), $stock->name, $stock->quantity)]);
         }
     }
     if (!$quantity || $quantity <= 0 || $product->fixed_quantity == 1) {
         $product->quantity = 1;
     } else {
         $product->quantity = $quantity;
     }
     //create save options array here for use later.
     $saveOptions = [];
     if (!$update && $product->product_id) {
         //set the base "total_price"
         if ($product->saleprice > 0) {
             $product->total_price = $product->saleprice;
         } else {
             $product->total_price = $product->price;
         }
         //set base "total_weight"
         $product->total_weight = $product->weight;
         $productOptions = \CI::ProductOptions()->getProductOptions($product->product_id);
         //option error vars
         $optionError = false;
         $optionErrorMessage = lang('option_error') . '<br/>';
         //lets validate the options
         foreach ($productOptions as $productOption) {
             // are we missing any required values?
             $optionValue = false;
             if (!empty($postedOptions[$productOption->id])) {
                 $optionValue = $postedOptions[$productOption->id];
             }
             if ((int) $productOption->required && !$optionValue) {
                 $optionError = true;
                 $optionErrorMessage .= "- " . $productOption->name . '<br/>';
                 continue;
                 // don't bother processing this particular option any further
             }
             //create options to save to the database in case we get past the errors
             if ($productOption->type == 'checklist') {
                 if (is_array($optionValue)) {
                     foreach ($optionValue as $ov) {
                         foreach ($productOption->values as $productOptionValue) {
                             if ($productOptionValue->id == $ov) {
                                 $saveOptions[] = ['option_name' => $productOption->name, 'value' => $productOptionValue->value, 'price' => $productOptionValue->price, 'weight' => $productOptionValue->weight];
                                 $product->total_weight += $productOptionValue->weight;
                                 $product->total_price += $productOptionValue->price;
                             }
                         }
                     }
                 }
             } else {
                 $saveOption = [];
                 if ($productOption->type == 'textfield' || $productOption->type == 'textarea') {
                     $saveOption['value'] = $optionValue;
                     $productOptionValue = $productOption->values[0];
                 } else {
                     foreach ($productOption->values as $ov) {
                         if ($ov->id == $optionValue) {
                             $productOptionValue = $ov;
                             break;
                         }
                     }
                     $saveOption['value'] = $optionValue;
                 }
                 if (isset($productOptionValue)) {
                     $saveOption['option_name'] = $productOption->name;
                     $saveOption['price'] = $productOptionValue->price;
                     $saveOption['weight'] = $productOptionValue->weight;
                     //add it to the array;
                     $saveOptions[] = $saveOption;
                     //update the total weight and price
                     $product->total_weight += $productOptionValue->weight;
                     $product->total_price += $productOptionValue->price;
                 }
             }
         }
         if ($optionError) {
             return json_encode(['error' => $optionErrorMessage]);
         }
     }
     //save the product
     $product_id = \CI::Orders()->saveItem((array) $product);
     //save the options if we have them
     foreach ($saveOptions as $saveOption) {
         $saveOption['order_item_id'] = $product_id;
         $saveOption['order_id'] = $this->cart->id;
         \CI::Orders()->saveItemOption($saveOption);
     }
     if ($update) {
         foreach ($this->items as $key => $item) {
             if ($item->id == $product_id) {
                 $this->items[$key] = $product;
             }
         }
     } else {
         $product->id = $product_id;
         $this->items[] = $product;
         //update file downloads
         if ($downloads) {
             foreach ($downloads as $file) {
                 \CI::Orders()->saveOrderItemFile(['order_id' => $this->cart->id, 'order_item_id' => $product->id, 'file_id' => $file->file_id]);
             }
         }
     }
     //get current item count
     $itemCount = $this->totalItems();
     if ($update) {
         return json_encode(['message' => lang('cart_updated'), 'itemCount' => $itemCount]);
     } else {
         return json_encode(['message' => lang('item_added_to_cart'), 'itemCount' => $itemCount]);
     }
 }
예제 #4
0
 public function form($id = false, $duplicate = false)
 {
     $this->product_id = $id;
     \CI::load()->library('form_validation');
     \CI::load()->model(array('ProductOptions', 'Categories', 'DigitalProducts', 'Customers'));
     \CI::lang()->load('digital_products');
     \CI::form_validation()->set_error_delimiters('<div class="error">', '</div>');
     $data['groups'] = \CI::Customers()->get_groups();
     $data['categories'] = \CI::Categories()->get_categories_tiered();
     $data['file_list'] = \CI::DigitalProducts()->getList();
     $data['page_title'] = lang('product_form');
     //default values are empty if the product is new
     $data['id'] = '';
     $data['sku'] = '';
     $data['primary_category'] = '';
     $data['name'] = '';
     $data['slug'] = '';
     $data['description'] = '';
     $data['excerpt'] = '';
     $data['weight'] = '';
     $data['track_stock'] = '';
     $data['seo_title'] = '';
     $data['meta'] = '';
     $data['shippable'] = '';
     $data['taxable'] = '';
     $data['fixed_quantity'] = '';
     $data['quantity'] = '';
     $data['enabled'] = '';
     $data['related_products'] = [];
     $data['product_categories'] = [];
     $data['images'] = [];
     $data['product_files'] = [];
     $data['productOptions'] = [];
     foreach ($data['groups'] as $group) {
         $data['enabled_' . $group->id] = '';
         $data['price_' . $group->id] = '';
         $data['saleprice_' . $group->id] = '';
     }
     //create the photos array for later use
     $data['photos'] = [];
     if ($id) {
         // get the existing file associations and create a format we can read from the form to set the checkboxes
         $pr_files = \CI::DigitalProducts()->getAssociationsByProduct($id);
         foreach ($pr_files as $f) {
             $data['product_files'][] = $f->file_id;
         }
         // get product & options data
         $data['productOptions'] = \CI::ProductOptions()->getProductOptions($id);
         $product = \CI::Products()->find($id, true);
         //if the product does not exist, redirect them to the product list with an error
         if (!$product) {
             \CI::session()->set_flashdata('error', lang('error_not_found'));
             redirect('admin/products');
         }
         //helps us with the slug generation
         $this->product_name = \CI::input()->post('slug', $product->slug);
         //set values to db values
         $data['id'] = $id;
         $data['sku'] = $product->sku;
         $data['primary_category'] = $product->primary_category;
         $data['name'] = $product->name;
         $data['seo_title'] = $product->seo_title;
         $data['meta'] = $product->meta;
         $data['slug'] = $product->slug;
         $data['description'] = $product->description;
         $data['excerpt'] = $product->excerpt;
         $data['weight'] = $product->weight;
         $data['track_stock'] = $product->track_stock;
         $data['shippable'] = $product->shippable;
         $data['quantity'] = $product->quantity;
         $data['taxable'] = $product->taxable;
         $data['fixed_quantity'] = $product->fixed_quantity;
         foreach ($data['groups'] as $group) {
             $data['enabled_' . $group->id] = $product->{'enabled_' . $group->id};
             $data['price_' . $group->id] = $product->{'price_' . $group->id};
             $data['saleprice_' . $group->id] = $product->{'saleprice_' . $group->id};
         }
         //make sure we haven't submitted the form yet before we pull in the images/related products from the database
         if (!\CI::input()->post('submit')) {
             $data['product_categories'] = [];
             foreach ($product->categories as $product_category) {
                 $data['product_categories'][] = $product_category->id;
             }
             $data['related_products'] = $product->related_products;
             $data['images'] = (array) json_decode($product->images);
         }
     }
     //if $data['related_products'] is not an array, make it one.
     if (!is_array($data['related_products'])) {
         $data['related_products'] = [];
     }
     if (!is_array($data['product_categories'])) {
         $data['product_categories'] = [];
     }
     //no error checking on these
     \CI::form_validation()->set_rules('caption', 'Caption');
     \CI::form_validation()->set_rules('primary_photo', 'Primary');
     \CI::form_validation()->set_rules('sku', 'lang:sku', 'trim');
     \CI::form_validation()->set_rules('seo_title', 'lang:seo_title', 'trim');
     \CI::form_validation()->set_rules('meta', 'lang:meta_data', 'trim');
     \CI::form_validation()->set_rules('name', 'lang:name', 'trim|required|max_length[64]');
     \CI::form_validation()->set_rules('slug', 'lang:slug', 'trim');
     \CI::form_validation()->set_rules('description', 'lang:description', 'trim');
     \CI::form_validation()->set_rules('excerpt', 'lang:excerpt', 'trim');
     \CI::form_validation()->set_rules('weight', 'lang:weight', 'trim|numeric|floatval');
     \CI::form_validation()->set_rules('track_stock', 'lang:track_stock', 'trim|numeric');
     \CI::form_validation()->set_rules('quantity', 'lang:quantity', 'trim|numeric');
     \CI::form_validation()->set_rules('shippable', 'lang:shippable', 'trim|numeric');
     \CI::form_validation()->set_rules('taxable', 'lang:taxable', 'trim|numeric');
     \CI::form_validation()->set_rules('fixed_quantity', 'lang:fixed_quantity', 'trim|numeric');
     foreach ($data['groups'] as $group) {
         \CI::form_validation()->set_rules('enabled_' . $group->id, lang('enabled') . '(' . $group->name . ')', 'trim|numeric');
         \CI::form_validation()->set_rules('price_' . $group->id, lang('price') . '(' . $group->name . ')', 'trim|numeric|floatval');
         \CI::form_validation()->set_rules('saleprice_' . $group->id, lang('saleprice') . '(' . $group->name . ')', 'trim|numeric|floatval');
     }
     /*
     if we've posted already, get the photo stuff and organize it
     if validation comes back negative, we feed this info back into the system
     if it comes back good, then we send it with the save item
     
     submit button has a value, so we can see when it's posted
     */
     if ($duplicate) {
         $data['id'] = false;
     }
     if (\CI::input()->post('submit')) {
         //reset the product options that were submitted in the post
         $data['ProductOptions'] = \CI::input()->post('option');
         $data['related_products'] = \CI::input()->post('related_products');
         $data['product_categories'] = \CI::input()->post('categories');
         $data['images'] = \CI::input()->post('images');
         $data['product_files'] = \CI::input()->post('downloads');
     }
     if (\CI::form_validation()->run() == FALSE) {
         $this->view('product_form', $data);
     } else {
         \CI::load()->helper('text');
         //first check the slug field
         $slug = \CI::input()->post('slug');
         //if it's empty assign the name field
         if (empty($slug) || $slug == '') {
             $slug = \CI::input()->post('name');
         }
         $slug = url_title(convert_accented_characters($slug), '-', TRUE);
         //validate the slug
         $slug = $id ? \CI::Products()->validate_slug($slug, $product->id) : \CI::Products()->validate_slug($slug);
         $save['id'] = $id;
         $save['sku'] = \CI::input()->post('sku');
         $save['name'] = \CI::input()->post('name');
         $save['seo_title'] = \CI::input()->post('seo_title');
         $save['meta'] = \CI::input()->post('meta');
         $save['description'] = \CI::input()->post('description');
         $save['excerpt'] = \CI::input()->post('excerpt');
         $save['weight'] = \CI::input()->post('weight');
         $save['track_stock'] = \CI::input()->post('track_stock');
         $save['fixed_quantity'] = \CI::input()->post('fixed_quantity');
         $save['quantity'] = \CI::input()->post('quantity');
         $save['shippable'] = \CI::input()->post('shippable');
         $save['taxable'] = \CI::input()->post('taxable');
         $post_images = \CI::input()->post('images');
         foreach ($data['groups'] as $group) {
             $save['enabled_' . $group->id] = \CI::input()->post('enabled_' . $group->id);
             $save['price_' . $group->id] = \CI::input()->post('price_' . $group->id);
             $save['saleprice_' . $group->id] = \CI::input()->post('saleprice_' . $group->id);
         }
         $save['slug'] = $slug;
         if ($primary = \CI::input()->post('primary_image')) {
             if ($post_images) {
                 foreach ($post_images as $key => &$pi) {
                     if ($primary == $key) {
                         $pi['primary'] = true;
                         continue;
                     }
                 }
             }
         }
         $save['images'] = json_encode($post_images);
         if (\CI::input()->post('related_products')) {
             $save['related_products'] = json_encode(\CI::input()->post('related_products'));
         } else {
             $save['related_products'] = '';
         }
         //save categories
         $categories = \CI::input()->post('categories');
         if (!$categories) {
             $categories = [];
         }
         //(\CI::input()->post('primary_category')) ? \CI::input()->post('primary_category') : 0;
         if (!\CI::input()->post('primary_category') && $categories) {
             $save['primary_category'] = $categories[0];
         } elseif (!\CI::input()->post('primary_category') && !$categories) {
             $save['primary_category'] = 0;
         } else {
             $save['primary_category'] = \CI::input()->post('primary_category');
         }
         // format options
         $options = [];
         if (\CI::input()->post('option')) {
             foreach (\CI::input()->post('option') as $option) {
                 $options[] = $option;
             }
         }
         // save product
         $product_id = \CI::Products()->save($save, $options, $categories);
         // add file associations
         // clear existsing
         \CI::DigitalProducts()->disassociate(false, $product_id);
         // save new
         $downloads = \CI::input()->post('downloads');
         if (is_array($downloads)) {
             foreach ($downloads as $d) {
                 \CI::DigitalProducts()->associate($d, $product_id);
             }
         }
         \CI::session()->set_flashdata('message', lang('message_saved_product'));
         //go back to the product list
         redirect('admin/products');
     }
 }
예제 #5
0
 public function form($id = false, $duplicate = false)
 {
     $this->product_id = $id;
     \CI::load()->library('form_validation');
     \CI::load()->model(array('ProductOptions', 'Categories', 'DigitalProducts', 'Customers'));
     \CI::lang()->load('digital_products');
     \CI::form_validation()->set_error_delimiters('<div class="error">', '</div>');
     $data['groups'] = \CI::Customers()->get_groups();
     $data['categories'] = \CI::Categories()->get_categories_tiered();
     $data['file_list'] = \CI::DigitalProducts()->getList();
     $data['page_title'] = lang('product_form');
     //default values are empty if the product is new
     $data['id'] = '';
     $data['sku'] = '';
     $data['primary_category'] = '';
     $data['name'] = '';
     $data['slug'] = '';
     $data['description'] = '';
     $data['excerpt'] = '';
     $data['weight'] = '';
     $data['track_stock'] = '';
     $data['seo_title'] = '';
     $data['meta'] = '';
     $data['shippable'] = '';
     $data['taxable'] = '';
     $data['fixed_quantity'] = '';
     $data['quantity'] = '';
     $data['enabled'] = '';
     $data['related_products'] = [];
     $data['product_categories'] = [];
     $data['images'] = [];
     $data['product_files'] = [];
     $data['productOptions'] = [];
     foreach ($data['groups'] as $group) {
         $data['enabled_' . $group->id] = '';
         $data['price_' . $group->id] = '';
         $data['saleprice_' . $group->id] = '';
     }
     // get_manufacturers
     $data['manufacturers'] = \CI::Products()->get_manufacturers(true);
     //create the photos array for later use
     $data['photos'] = [];
     if ($id) {
         // get the existing file associations and create a format we can read from the form to set the checkboxes
         $pr_files = \CI::DigitalProducts()->getAssociationsByProduct($id);
         foreach ($pr_files as $f) {
             $data['product_files'][] = $f->file_id;
         }
         // get product & options data
         $data['productOptions'] = \CI::ProductOptions()->getProductOptions($id);
         $product = \CI::Products()->find($id, true);
         //if the product does not exist, redirect them to the product list with an error
         if (!$product) {
             \CI::session()->set_flashdata('error', lang('error_not_found'));
             redirect('admin/products');
         }
         //helps us with the slug generation
         $this->product_name = \CI::input()->post('slug', $product->slug);
         //set values to db values
         $data['id'] = $id;
         $data['sku'] = $product->sku;
         $data['primary_category'] = $product->primary_category;
         $data['name'] = $product->name;
         $data['seo_title'] = $product->seo_title;
         $data['meta'] = $product->meta;
         $data['slug'] = $product->slug;
         $data['description'] = $product->description;
         $data['excerpt'] = $product->excerpt;
         $data['weight'] = $product->weight;
         $data['dimensions'] = $product->dimensions;
         $data['days'] = $product->days;
         $data['ogirin'] = $product->ogirin;
         $data['track_stock'] = $product->track_stock;
         $data['shippable'] = $product->shippable;
         $data['quantity'] = $product->quantity;
         $data['taxable'] = $product->taxable;
         $data['fixed_quantity'] = $product->fixed_quantity;
         $data['manufacturer'] = $product->manufacturers;
         $data['protection_class'] = $product->protection_class;
         $data['document_link'] = $product->document_link;
         foreach ($data['groups'] as $group) {
             $data['enabled_' . $group->id] = $product->{'enabled_' . $group->id};
             $data['price_' . $group->id] = $product->{'price_' . $group->id};
             $data['saleprice_' . $group->id] = $product->{'saleprice_' . $group->id};
         }
         //make sure we haven't submitted the form yet before we pull in the images/related products from the database
         if (!\CI::input()->post('submit')) {
             $data['product_categories'] = [];
             foreach ($product->categories as $product_category) {
                 $data['product_categories'][] = $product_category->id;
             }
             $data['related_products'] = $product->related_products;
             $data['images'] = (array) json_decode($product->images);
         }
         // get document file
         $data['documents'] = \CI::Products()->get_documents($id);
         if ($data['primary_category'] == 1) {
             // engine
             $engine = \CI::Products()->find_engine($id);
             $data['prime'] = isset($engine->prime) ? $engine->prime : '';
             $data['standby'] = isset($engine->standby) ? $engine->standby : '';
             $data['prime_2'] = isset($engine->prime_2) ? $engine->prime_2 : '';
             $data['standby_2'] = isset($engine->standby_2) ? $engine->standby_2 : '';
             //$data['funnel_phi'] = isset($engine->funnel_phi) ? $engine->funnel_phi : '';
             $data['type_cooled'] = isset($engine->type_cooled) ? $engine->type_cooled : '';
             $data['type_fuel'] = isset($engine->type_fuel) ? $engine->type_fuel : '';
             $data['dBA'] = isset($engine->dBA) ? $engine->dBA : '';
             $data['rpm'] = isset($engine->rpm) ? $engine->rpm : 1500;
             $data['rpm_2'] = isset($engine->rpm_2) ? $engine->rpm_2 : 1800;
             $fuel_consumption = \CI::Products()->find_fuel_consumption($id, 50);
             $data['standby_fuel_con_1'] = isset($fuel_consumption->standby_fuel_con_1) ? $fuel_consumption->standby_fuel_con_1 : '';
             $data['standby_fuel_con_2'] = isset($fuel_consumption->standby_fuel_con_2) ? $fuel_consumption->standby_fuel_con_2 : '';
             $data['standby_fuel_con_3'] = isset($fuel_consumption->standby_fuel_con_3) ? $fuel_consumption->standby_fuel_con_3 : '';
             $data['prime_fuel_con_1'] = isset($fuel_consumption->prime_fuel_con_1) ? $fuel_consumption->prime_fuel_con_1 : '';
             $data['prime_fuel_con_2'] = isset($fuel_consumption->prime_fuel_con_2) ? $fuel_consumption->prime_fuel_con_2 : '';
             $data['prime_fuel_con_3'] = isset($fuel_consumption->prime_fuel_con_3) ? $fuel_consumption->prime_fuel_con_3 : '';
             $fuel_consumption = \CI::Products()->find_fuel_consumption($id, 60);
             $data['standby_fuel_con_2_1'] = isset($fuel_consumption->standby_fuel_con_2_1) ? $fuel_consumption->standby_fuel_con_2_1 : '';
             $data['standby_fuel_con_2_2'] = isset($fuel_consumption->standby_fuel_con_2_2) ? $fuel_consumption->standby_fuel_con_2_2 : '';
             $data['standby_fuel_con_2_3'] = isset($fuel_consumption->standby_fuel_con_2_3) ? $fuel_consumption->standby_fuel_con_2_3 : '';
             $data['prime_fuel_con_2_1'] = isset($fuel_consumption->prime_fuel_con_2_1) ? $fuel_consumption->prime_fuel_con_2_1 : '';
             $data['prime_fuel_con_2_2'] = isset($fuel_consumption->prime_fuel_con_2_2) ? $fuel_consumption->prime_fuel_con_2_2 : '';
             $data['prime_fuel_con_2_3'] = isset($fuel_consumption->prime_fuel_con_2_3) ? $fuel_consumption->prime_fuel_con_2_3 : '';
         }
         if ($data['primary_category'] == 2) {
             // canopy
             $alternator = \CI::Products()->find_alternator($id, 50);
             $data['hz'] = isset($alternator->hz) ? $alternator->hz : 50;
             $data['power'] = isset($alternator->power) ? $alternator->power : '';
             $data['efficiency'] = isset($alternator->efficiency) ? $alternator->efficiency : '';
             $data['efficiency_2'] = isset($alternator->efficiency_2) ? $alternator->efficiency_2 : '';
             $data['efficiency_3'] = isset($alternator->efficiency_3) ? $alternator->efficiency_3 : '';
             $data['efficiency_4'] = isset($alternator->efficiency_4) ? $alternator->efficiency_4 : '';
             $data['power_single_phase'] = isset($alternator->power_single_phase) ? $alternator->power_single_phase : '';
             $alternator_2 = \CI::Products()->find_alternator($id, 60);
             //print_r($alternator_2);exit;
             $data['hz_2'] = isset($alternator_2->hz) ? $alternator_2->hz : 60;
             $data['power_2'] = isset($alternator_2->power) ? $alternator_2->power : '';
             $data['efficiency_2_1'] = isset($alternator_2->efficiency) ? $alternator_2->efficiency : '';
             $data['efficiency_2_2'] = isset($alternator_2->efficiency_2) ? $alternator_2->efficiency_2 : '';
             $data['efficiency_2_3'] = isset($alternator_2->efficiency_3) ? $alternator_2->efficiency_3 : '';
             $data['efficiency_2_4'] = isset($alternator_2->efficiency_4) ? $alternator_2->efficiency_4 : '';
             $data['phase'] = isset($alternator->phase) ? $alternator->phase : '';
             $data['power_single_phase_2'] = isset($alternator->power_single_phase) ? $alternator->power_single_phase : '';
         }
         if ($data['primary_category'] == 4) {
             // alternators
             $canopy = \CI::Products()->find_canopy($id);
             //print_r($canopy);exit;
             $data['canopy_kVA_min'] = isset($canopy->kVA_min) ? $canopy->kVA_min : '';
             $data['canopy_kVA_max'] = isset($canopy->kVA_max) ? $canopy->kVA_max : '';
             $data['canopy_standard'] = isset($canopy->standard) ? $canopy->standard : 0;
         }
     }
     //if $data['related_products'] is not an array, make it one.
     if (!is_array($data['related_products'])) {
         $data['related_products'] = [];
     }
     if (!is_array($data['product_categories'])) {
         $data['product_categories'] = [];
     }
     //no error checking on these
     \CI::form_validation()->set_rules('caption', 'Caption');
     \CI::form_validation()->set_rules('primary_photo', 'Primary');
     \CI::form_validation()->set_rules('sku', 'lang:sku', 'trim');
     \CI::form_validation()->set_rules('seo_title', 'lang:seo_title', 'trim');
     \CI::form_validation()->set_rules('meta', 'lang:meta_data', 'trim');
     \CI::form_validation()->set_rules('name', 'lang:name', 'trim|required|max_length[64]');
     \CI::form_validation()->set_rules('slug', 'lang:slug', 'trim');
     \CI::form_validation()->set_rules('description', 'lang:description', 'trim');
     \CI::form_validation()->set_rules('excerpt', 'lang:excerpt', 'trim');
     \CI::form_validation()->set_rules('weight', 'lang:weight', 'trim|numeric|floatval');
     \CI::form_validation()->set_rules('track_stock', 'lang:track_stock', 'trim|numeric');
     \CI::form_validation()->set_rules('quantity', 'lang:quantity', 'trim|numeric');
     \CI::form_validation()->set_rules('shippable', 'lang:shippable', 'trim|numeric');
     \CI::form_validation()->set_rules('taxable', 'lang:taxable', 'trim|numeric');
     \CI::form_validation()->set_rules('fixed_quantity', 'lang:fixed_quantity', 'trim|numeric');
     foreach ($data['groups'] as $group) {
         \CI::form_validation()->set_rules('enabled_' . $group->id, lang('enabled') . '(' . $group->name . ')', 'trim|numeric');
         \CI::form_validation()->set_rules('price_' . $group->id, lang('price') . '(' . $group->name . ')', 'trim|numeric|floatval');
         \CI::form_validation()->set_rules('saleprice_' . $group->id, lang('saleprice') . '(' . $group->name . ')', 'trim|numeric|floatval');
     }
     /*
     if we've posted already, get the photo stuff and organize it
     if validation comes back negative, we feed this info back into the system
     if it comes back good, then we send it with the save item
     
     submit button has a value, so we can see when it's posted
     */
     if ($duplicate) {
         $data['id'] = false;
     }
     if (\CI::input()->post('submit')) {
         //reset the product options that were submitted in the post
         $data['ProductOptions'] = \CI::input()->post('option');
         $data['related_products'] = \CI::input()->post('related_products');
         $data['product_categories'] = \CI::input()->post('categories');
         $data['images'] = \CI::input()->post('images');
         $data['product_files'] = \CI::input()->post('downloads');
     }
     if (\CI::form_validation()->run() == FALSE) {
         $this->view('product_form', $data);
     } else {
         \CI::load()->helper('text');
         //first check the slug field
         $slug = \CI::input()->post('slug');
         //if it's empty assign the name field
         if (empty($slug) || $slug == '') {
             $slug = \CI::input()->post('name');
         }
         $slug = url_title(convert_accented_characters($slug), '-', TRUE);
         //validate the slug
         $slug = $id ? \CI::Products()->validate_slug($slug, $product->id) : \CI::Products()->validate_slug($slug);
         $save['id'] = $id;
         $save['sku'] = \CI::input()->post('sku');
         $save['name'] = \CI::input()->post('name');
         $save['seo_title'] = \CI::input()->post('seo_title');
         $save['meta'] = \CI::input()->post('meta');
         $save['description'] = \CI::input()->post('description');
         $save['excerpt'] = \CI::input()->post('excerpt');
         $save['weight'] = \CI::input()->post('weight');
         $save['days'] = \CI::input()->post('days');
         $save['track_stock'] = \CI::input()->post('track_stock');
         $save['fixed_quantity'] = \CI::input()->post('fixed_quantity');
         $save['quantity'] = \CI::input()->post('quantity');
         $save['shippable'] = \CI::input()->post('shippable');
         $save['taxable'] = \CI::input()->post('taxable');
         $save['dimensions'] = \CI::input()->post('dimensions');
         $save['ogirin'] = \CI::input()->post('ogirin');
         $save['manufacturers'] = \CI::input()->post('manufacturers');
         $save['protection_class'] = \CI::input()->post('protection_class');
         $save['document_link'] = \CI::input()->post('document_link');
         $post_images = \CI::input()->post('images');
         foreach ($data['groups'] as $group) {
             $save['enabled_' . $group->id] = \CI::input()->post('enabled_' . $group->id);
             $save['price_' . $group->id] = \CI::input()->post('price_' . $group->id);
             $save['saleprice_' . $group->id] = \CI::input()->post('saleprice_' . $group->id);
         }
         $save['slug'] = $slug;
         if ($primary = \CI::input()->post('primary_image')) {
             if ($post_images) {
                 foreach ($post_images as $key => &$pi) {
                     if ($primary == $key) {
                         $pi['primary'] = true;
                         continue;
                     }
                 }
             }
         }
         $save['images'] = json_encode($post_images);
         if (\CI::input()->post('related_products')) {
             $save['related_products'] = json_encode(\CI::input()->post('related_products'));
         } else {
             $save['related_products'] = '';
         }
         //save categories
         $categories = \CI::input()->post('categories');
         if (!$categories) {
             $categories = [];
         }
         //(\CI::input()->post('primary_category')) ? \CI::input()->post('primary_category') : 0;
         if (!\CI::input()->post('primary_category') && $categories) {
             $save['primary_category'] = $categories[0];
         } elseif (!\CI::input()->post('primary_category') && !$categories) {
             $save['primary_category'] = 0;
         } else {
             $save['primary_category'] = \CI::input()->post('primary_category');
         }
         // format options
         $options = [];
         if (\CI::input()->post('option')) {
             foreach (\CI::input()->post('option') as $option) {
                 $options[] = $option;
             }
         }
         // save product
         $product_id = \CI::Products()->save($save, $options, $categories);
         // save file
         //should check upload file
         $config['allowed_types'] = '*';
         $config['upload_path'] = 'uploads/documents';
         //config_item('digital_products_path');
         $config['remove_spaces'] = true;
         \CI::load()->library('upload', $config);
         if (\CI::upload()->do_upload()) {
             $upload_data = \CI::upload()->data();
             $document['product_id'] = $product_id;
             $document['file_name'] = $upload_data['file_name'];
             $document['display_name'] = \CI::input()->post('display_name');
             $document['size'] = $upload_data['file_size'];
             \CI::Products()->save_document($document);
         }
         /*else {
               $data['error'] 	= \CI::upload()->display_errors();
           }*/
         // save engine
         if ($data['primary_category'] == 1) {
             // engine
             $save_engine['product_id'] = $id;
             $save_engine['rpm'] = \CI::input()->post('rpm');
             $save_engine['prime'] = \CI::input()->post('prime');
             $save_engine['standby'] = \CI::input()->post('standby');
             $save_engine['rpm_2'] = \CI::input()->post('rpm_2');
             $save_engine['prime_2'] = \CI::input()->post('prime_2');
             $save_engine['standby_2'] = \CI::input()->post('standby_2');
             //$save_engine['funnel_phi']          = \CI::input()->post('funnel_phi');
             $save_engine['type_cooled'] = \CI::input()->post('type_cooled');
             $save_engine['type_fuel'] = \CI::input()->post('type_fuel');
             $save_engine['dBA'] = \CI::input()->post('dBA');
             $engine_id = \CI::Products()->save_engine($id, $engine->id, $save_engine);
             $save_fuel_consumption['standby_fuel_con_1'] = \CI::input()->post('standby_fuel_con_1');
             $save_fuel_consumption['standby_fuel_con_2'] = \CI::input()->post('standby_fuel_con_2');
             $save_fuel_consumption['standby_fuel_con_3'] = \CI::input()->post('standby_fuel_con_3');
             $save_fuel_consumption['prime_fuel_con_1'] = \CI::input()->post('prime_fuel_con_1');
             $save_fuel_consumption['prime_fuel_con_2'] = \CI::input()->post('prime_fuel_con_2');
             $save_fuel_consumption['prime_fuel_con_3'] = \CI::input()->post('prime_fuel_con_3');
             $save_fuel_consumption['product_id'] = $id;
             $save_fuel_consumption['rpm'] = 1500;
             $save_fuel_consumption['hz'] = 50;
             $engine_id = \CI::Products()->fuel_consumption($id, $fuel_consumption->id, $save_fuel_consumption);
             //  echo lqr();
             $save_fuel_consumption_2['standby_fuel_con_1'] = \CI::input()->post('standby_fuel_con_2_1');
             $save_fuel_consumption_2['standby_fuel_con_2'] = \CI::input()->post('standby_fuel_con_2_2');
             $save_fuel_consumption_2['standby_fuel_con_3'] = \CI::input()->post('standby_fuel_con_2_3');
             $save_fuel_consumption_2['prime_fuel_con_1'] = \CI::input()->post('prime_fuel_con_2_1');
             $save_fuel_consumption_2['prime_fuel_con_2'] = \CI::input()->post('prime_fuel_con_2_2');
             $save_fuel_consumption_2['prime_fuel_con_3'] = \CI::input()->post('prime_fuel_con_2_3');
             $save_fuel_consumption_2['product_id'] = $id;
             $save_fuel_consumption_2['rpm'] = 1800;
             $save_fuel_consumption_2['hz'] = 60;
             $engine_id = \CI::Products()->fuel_consumption($id, $fuel_consumption->id, $save_fuel_consumption_2);
         }
         // save alternators
         if ($data['primary_category'] == 2) {
             // engine
             $save_alternator['product_id'] = $id;
             $save_alternator['hz'] = 50;
             $save_alternator['phase'] = \CI::input()->post('phase');
             $save_alternator['efficiency'] = \CI::input()->post('efficiency');
             $save_alternator['efficiency_2'] = \CI::input()->post('efficiency_2');
             $save_alternator['efficiency_3'] = \CI::input()->post('efficiency_3');
             $save_alternator['efficiency_4'] = \CI::input()->post('efficiency_4');
             $save_alternator['power'] = \CI::input()->post('power');
             $save_alternator['power_single_phase'] = \CI::input()->post('power_single_phase');
             $alternator_id = \CI::Products()->save_alternator($id, $alternator->id, $save_alternator);
             $save_alternator['hz'] = 60;
             $save_alternator['phase'] = \CI::input()->post('phase');
             $save_alternator['efficiency'] = \CI::input()->post('efficiency_2_1');
             $save_alternator['efficiency_2'] = \CI::input()->post('efficiency_2_2');
             $save_alternator['efficiency_3'] = \CI::input()->post('efficiency_2_3');
             $save_alternator['efficiency_4'] = \CI::input()->post('efficiency_2_4');
             $save_alternator['power'] = \CI::input()->post('power_2');
             $save_alternator['power_single_phase'] = \CI::input()->post('power_single_phase_2');
             $alternator_id = \CI::Products()->save_alternator($id, $alternator_2->id, $save_alternator);
         }
         if ($data['primary_category'] == 4) {
             // engine
             $save_canopy['product_id'] = $id;
             $save_canopy['kVA_min'] = \CI::input()->post('kVA_min');
             $save_canopy['kVA_max'] = \CI::input()->post('kVA_max');
             $save_canopy['standard'] = \CI::input()->post('standard');
             $canopy_id = \CI::Products()->save_canopy($canopy->id, $save_canopy);
         }
         // add file associations
         // clear existsing
         \CI::DigitalProducts()->disassociate(false, $product_id);
         // save new
         $downloads = \CI::input()->post('downloads');
         if (is_array($downloads)) {
             foreach ($downloads as $d) {
                 \CI::DigitalProducts()->associate($d, $product_id);
             }
         }
         \CI::session()->set_flashdata('message', lang('message_saved_product'));
         //go back to the product list
         redirect('admin/products');
     }
 }
예제 #6
0
 public function delete($id)
 {
     \CI::DigitalProducts()->delete($id);
     \CI::session()->set_flashdata('message', lang('message_deleted_file'));
     redirect('admin/digital_products');
 }