/** * Get additional content data selected * * @param $result = Query result */ public static function post_find($result) { if ($result !== null) { if (is_array($result)) { foreach ($result as $item) { // It will first check if we already have result in temporary result, // and only execute query if we dont. That way we dont have duplicate queries // Get group attributes $item->get_attributes = static::lazy_load(function () use($item) { $attributes = Model_Attribute_To_Groups::find(function ($query) use($item) { return $query->where('group_id', $item->id); }, 'attribute_id'); if (!empty($attributes)) { $attributes = '(' . implode(',', array_keys($attributes)) . ')'; return Model_Attribute::find(function ($query) use($attributes) { return $query->where('id', 'IN', \DB::expr($attributes))->order_by('sort', 'asc'); }, 'id'); } return array(); }, $item->id, 'attributes'); } } } // return the result return $result; }
public function action_update($id = false) { if (!is_numeric($id)) { \Response::redirect('admin/attribute/group/list'); } // Get news item to edit if (!($item = Model_Attribute_Group::find_one_by_id($id))) { \Response::redirect('admin/attribute/group/list'); } \View::set_global('title', 'Edit Attribute Group'); if (\Input::post()) { $add = \Input::post('attributes.add', array()); $remove = \Input::post('attributes.remove', array()); if (\Input::post('add', false)) { foreach ($add as $value) { $group = Model_Attribute_To_Groups::forge(array('attribute_id' => $value, 'group_id' => $item->id)); $group->save(); } \Messages::success('Attributes successfully added.'); } else { if (\Input::post('remove', false)) { foreach ($remove as $value) { $group = Model_Attribute_To_Groups::find_one_by(array(array('attribute_id', '=', $value), array('group_id', '=', $item->id))); if (!is_null($group)) { $group->delete(); } } \Messages::success('Attributes successfully removed.'); } else { if (\Input::post('update', false)) { $item->set(\Input::post()); try { $item->save(); \Messages::success('Attribute group successfully updated.'); \Response::redirect(\Input::referrer(\Uri::create('admin/attribute/group/update/' . $item->id))); } catch (\Database_Exception $e) { // show validation errors \Messages::error('<strong>There was an error while trying to update attribute group</strong>'); // Uncomment lines below to show database errors //$errors = $e->getMessage(); //\Messages::error($errors); } } } } if (\Input::is_ajax()) { echo \Messages::display('left', false); exit; } else { \Response::redirect(\Input::referrer(\Uri::create('admin/attribute/group/list'))); } } $group = Model_Attribute_Group::find_one_by_id($id); /************ Get non related infotabs ***********/ $items = Model_Attribute::find(function ($query) use($group) { $related_ids = array(); foreach ($group->attributes as $attribute) { array_push($related_ids, $attribute->id); } if (!empty($related_ids)) { $related_ids = '(' . implode(',', $related_ids) . ')'; $query->where('id', 'NOT IN', \DB::expr($related_ids)); } // Order query $query->order_by('sort', 'asc'); $query->order_by('id', 'asc'); }); $group->not_related_attributes = $items ? $items : array(); \Theme::instance()->set_partial('content', $this->view_dir . 'update')->set('group', $group); }
public function action_list() { if (\Input::post('remove', false)) { $remove = \Input::post('attribute.remove', array()); $is_remove = 0; foreach ($remove as $value) { if (is_numeric($value)) { // Get news item to edit if ($item = Model_Attribute::find_one_by_id($value)) { // Delete item try { // Delete attribute options if (!empty($item->options)) { foreach ($item->options as $option) { \Request::forge('admin/attribute/option/delete/' . $option->id)->execute()->response(); } } $item->delete(); $is_remove = 1; } catch (\Database_Exception $e) { \Messages::error('<strong>There was an error while trying to delete attribute</strong>'); } } } } if ($is_remove) { \Messages::success('Attribute successfully deleted.'); } } \View::set_global('title', 'List Attributes'); /************ Start generating query ***********/ $items = Model_Attribute::find(function ($query) { // Get search filters foreach (\Input::get() as $key => $value) { if (!empty($value) || $value == '0') { switch ($key) { case 'title': $query->where($key, 'like', "%{$value}%"); break; } } } // Order query $query->order_by('sort', 'asc'); $query->order_by('id', 'asc'); }); /************ End generating query ***********/ // Reset to empty array if there are no result found by query if (is_null($items)) { $items = array(); } // Initiate pagination $pagination = \Hybrid\Pagination::make(array('total_items' => count($items), 'per_page' => \Input::get('per_page', 10), 'uri_segment' => null)); // Remove unwanted items, and show only required ones $items = array_slice($items, $pagination->offset, $pagination->per_page); \Theme::instance()->set_partial('content', $this->view_dir . 'list')->set('items', $items)->set('pagination', $pagination, false); }
public function reset_default($id = false) { // Reset default $default = Model_Attribute::find(array('where' => array('product_id' => $id, 'default' => 1))); if ($default) { foreach ($default as $item) { $item->default = 0; $item->save(); } } }
/** * Get product data (code, price, attributes, images) * * @access public * @param $product object * @return */ public static function product_data($product = false, $attributes_json = null, $post_select = false, $attributeid = false) { if (!$product) { return; } $out['current_attributes'] = null; $out['code'] = null; $out['images'] = null; $out['select'] = null; $out['select_name'] = null; $out['sx'] = null; // select (attribute) order $out['retail_price'] = null; $out['sale'] = null; $out['price'] = 0; $out['price_type'] = null; $current_attributes = array(); $select = array(); $select_name = array(); $sx = array(); $options = array(); if (json_decode($attributes_json) != null) { $current_attributes = Model_Attribute::find(array('where' => array('attributes' => $attributes_json, 'product_id' => $product->id, 'active' => 1), 'limit' => 1)); } //get attributes per product $prod_attributes = \Product\Model_Attribute::find_by_product_id($product->id); $a_attribute_ids = array(); if ($prod_attributes) { foreach ($prod_attributes as $attr_id) { array_push($a_attribute_ids, $attr_id->id); } } // NRB-Gem: if current URL is Pricing Group -> Products // Get pricing_attribute_price from current Pricing Group // Products can belong to multiple Pricing Groups $a_groups = \Sentry::user()->groups(); if ($a_attribute_ids) { if (\Sentry::check()) { $a_cond = array(array('product_attribute_id', 'in', $a_attribute_ids)); if (\Uri::segment(2) != 'admin' && \Sentry::user()->groups() && $a_groups[0]) { $a_cond['user_group_id'] = $a_groups[0]['id']; } if (\Uri::segment(3) == 'group' && \Uri::segment(4) == 'products') { $a_cond['pricing_group_id'] = \Uri::segment(5); } $product_sale_price = \Product\Model_Attribute_Price::find(array('where' => $a_cond)); } } // NRB-Gem: check if logged in user can buy product for discounted rate if (\Uri::segment(2) != 'admin' && \Sentry::user()->groups() && !\Sentry::user()->is_admin() && $a_groups[0]) { $o_product_group_options = \DB::select_array(array('able_to_buy'))->from('product_group_options')->join('product_to_groups')->on('product_group_options.product_group_id', '=', 'product_to_groups.group_id')->where('product_group_options.user_group_id', $a_groups[0]['id'])->where('able_to_buy', 1)->where('product_to_groups.product_id', $product->id)->execute(); $a_result = $o_product_group_options->as_array(); $product_sale_price = $a_result && $a_result[0]['able_to_buy'] ? $product_sale_price : false; } // Get all product attributes $product_attributes = Model_Product::get_product_attributes($product); // Product with attributes if ($product_attributes) { // Set current attribute if ($current_attributes && isset($product_attributes[$current_attributes[0]->id])) { $current_attributes = $product_attributes[$current_attributes[0]->id]; } elseif ($post_select && count($post_select) > 1) { foreach ($post_select as $sk => $sv) { if (!isset($post_select_pop)) { $post_select_pop = $post_select; array_pop($post_select_pop); } $count = count($post_select_pop); foreach ($product_attributes as $pk => $pv) { $i = 0; $not_exists = array(); foreach ($pv as $pkk => $pvv) { if ($i >= $count) { continue; } if (!isset($post_select_pop[$pvv->attribute->id]) || $post_select_pop[$pvv->attribute->id] != $pvv->option->id) { $not_exists[] = true; } $i++; } if (empty($not_exists)) { $current_attributes = $product_attributes[$pk]; break 2; } } if (empty($current_attributes) && $count > 1) { array_pop($post_select_pop); } } if (empty($current_attributes)) { $current_attributes = reset($product_attributes); } } elseif ($product->default_attributes) { $current_attributes = $product_attributes[reset($product->default_attributes)->id]; } else { $current_attributes = reset($product_attributes); } foreach ($current_attributes as $k => $v) { $options[] = $v->option->id; } foreach ($product_attributes as $k => $v) { if (is_array($v)) { foreach ($v as $kk => $vv) { $select_name[$vv->attribute->id] = $vv->attribute->name != '' ? $vv->attribute->name : $vv->attribute->title; $select_tmp[$k][$vv->attribute->id][$vv->option->id] = $vv->option->title; foreach ($options as $option_key => $option_value) { if ($kk == $option_key) { $select[$vv->attribute->id][$vv->option->id] = $vv->option->title; if ($vv->option->id != $option_value) { continue 3; } } } } } } if ($select) { $sx = array_keys($select_name); } $b_use_original_price = false; if (!empty($current_attributes)) { $attr_obj = $current_attributes[0]->product_attribute; $out['current_attributes'] = $current_attributes; $out['code'] = $attr_obj->product_code; if (!empty($attr_obj->images)) { $out['images'] = $attr_obj->images; } $out['select'] = $select; $out['select_name'] = $select_name; $out['sx'] = $sx; $out['stock_quantity'] = $attr_obj->stock_quantity; $out['retail_price'] = $attr_obj->retail_price; // NRB-Gem: changed condition from: (!empty($product_sale_price) && $product_sale_price[0]->price != 0) // fix for attributes with sale price are not displayed if (!empty($product_sale_price)) { $out['with_attr'] = array('attribute' => array()); foreach ($product_sale_price as $with_attr) { $out['with_attr']['able_discount'][$with_attr->product_attribute_id] = $with_attr->able_discount; if ($out['with_attr']['able_discount'][$with_attr->product_attribute_id]) { $out['with_attr']['discount'][$with_attr->product_attribute_id] = $with_attr->discount; $out['with_attr']['price'][$with_attr->product_attribute_id] = $with_attr->price; } $out['with_attr']['attribute'][$with_attr->product_attribute_id] = ''; foreach ($product_attributes as $o_prod_attr) { $a_attr = array(); foreach ($o_prod_attr as $o_attr) { if ($with_attr->product_attribute_id == $o_attr->product_attribute->id) { $a_attr[] = $o_attr->option->title; } } $out['with_attr']['attribute'][$with_attr->product_attribute_id] .= implode(' | ', $a_attr); } // NRB-Gem: do not display inactive attribute if (empty($out['with_attr']['attribute'][$with_attr->product_attribute_id])) { unset($out['with_attr']['attribute'][$with_attr->product_attribute_id]); unset($out['with_attr']['able_discount'][$with_attr->product_attribute_id]); unset($out['with_attr']['discount'][$with_attr->product_attribute_id]); unset($out['with_attr']['price'][$with_attr->product_attribute_id]); } } // NRB-Gem: get default prices foreach ($prod_attributes as $attr) { if (isset($out['with_attr']['discount'][$attr->id]) && $out['with_attr']['discount'][$attr->id] > 0) { $out['with_attr']['price'][$attr->id] = $attr->retail_price - $attr->retail_price * ($out['with_attr']['discount'][$attr->id] / 100); } else { if (isset($out['with_attr']['price'][$attr->id]) && !$out['with_attr']['price'][$attr->id]) { $out['with_attr']['price'][$attr->id] = $attr->sale_price ? $attr->sale_price : $attr->retail_price; } } } $out['sale'] = \Product\Model_Product::calculate_discount($product, $product_sale_price[0]->discount); $out['price'] = \Product\Model_Product::calculate_discount($product, $product_sale_price[0]->price); $out['price_type'] = 'sale_price'; // NRB-Gem: Set discounted rate from product_attribute_price Product attributes if ($attributes_json) { foreach ($current_attributes as $o_attribute) { if ($o_attribute->product_attribute->attributes == $attributes_json) { if ($out['with_attr']['able_discount'][$o_attribute->product_attribute->id]) { if ($out['with_attr']['discount'][$o_attribute->product_attribute->id] > 0) { $out['price'] = $out['retail_price'] - $out['retail_price'] * ($out['with_attr']['discount'][$o_attribute->product_attribute->id] / 100); } else { $out['price'] = $out['with_attr']['price'][$o_attribute->product_attribute->id]; } } else { $b_use_original_price = true; } } } } } else { $out['price'] = \Product\Model_Product::calculate_discount($product, $attr_obj->retail_price); $out['price_type'] = 'retail_price'; $b_use_original_price = true; } // NRB-Gem: Display original price if Discount price is not enabled (used by non-ajax request) if (\Uri::segment(1) == 'product' && !$attributes_json) { $a_prod_attr_keys = array_keys($product_attributes); $attr_id = $a_prod_attr_keys[0]; if (isset($out['with_attr']['able_discount'][$attr_id])) { if (isset($out['with_attr']['discount']) && $out['with_attr']['discount'][$attr_id] > 0) { $out['price'] = $out['retail_price'] - $out['retail_price'] * ($out['with_attr']['discount'][$attr_id] / 100); } else { $out['price'] = isset($out['with_attr']['price']) ? $out['with_attr']['price'][$attr_id] : ''; } } else { $b_use_original_price = true; $attributes_json = true; } } // NRB-Gem: Set original discounted rate for Product attributes if ($b_use_original_price && $attributes_json) { if ($attr_obj->sale_price > 0) { $out['price'] = $attr_obj->sale_price; $out['price_type'] = 'sale_price'; } else { $out['price'] = $attr_obj->retail_price; unset($out['price_type']); } } } } else { $p = $product->attributes[0]; $out['current_attributes'] = null; $out['code'] = $p->product_code; $out['images'] = $p->images; $out['retail_price'] = $p->retail_price; $out['stock_quantity'] = $p->stock_quantity; $out['able_discount'] = isset($product_sale_price[0]) ? $product_sale_price[0]->able_discount : 0; if (!empty($product_sale_price) && $product_sale_price[0]->price != 0) { if ($out['able_discount']) { $out['sale'] = \Product\Model_Product::calculate_discount($p, $product_sale_price[0]->discount); $out['price'] = \Product\Model_Product::calculate_discount($p, $product_sale_price[0]->price); } $out['price_type'] = 'sale_price'; } else { $out['sale'] = isset($product_sale_price[0]) ? $product_sale_price[0]->discount : 0; $out['price'] = \Product\Model_Product::calculate_discount($product, $p->retail_price); $out['price_type'] = 'retail_price'; } // NRB-Gem: Display original price if Discount price is not enabled if ($out['able_discount']) { if ($product_sale_price[0]->discount > 0) { $out['price'] = $p->retail_price - $p->retail_price * ($product_sale_price[0]->discount / 100); } } else { if ($p->sale_price > 0) { $out['price'] = $p->sale_price; $out['price_type'] = 'sale_price'; } else { unset($out['price_type']); $out['price'] = $p->retail_price; } } } $prod_category = Model_Product_To_Categories::find_by_product_id($product->id); $category = \Product\Model_Category::find_one_by_id($prod_category[0]->category_id); $out['category_id'] = $prod_category ? $prod_category[0]->category_id : null; $out['category'] = $category ? $category->title : null; // Product without attributes if (empty($out['current_attributes'])) { $out['code'] = $product->code; } // Set images from product if there is no images in product attribute if (empty($out['images']) && !empty($product->images)) { $out['images'] = $product->images; } return $out; }
/** * Get product data (code, price, attributes, images) * * @access public * @param $product object * @return */ public static function product_data($product = false, $attributes_json = null, $post_select = false, $attributeid = false) { if (!$product) { return; } $out['current_attributes'] = null; $out['code'] = null; $out['images'] = null; $out['select'] = null; $out['select_name'] = null; $out['sx'] = null; // select (attribute) order $out['retail_price'] = null; $out['sale'] = null; $out['price'] = 0; $out['price_type'] = null; $current_attributes = array(); $select = array(); $select_name = array(); $sx = array(); $options = array(); if (json_decode($attributes_json) != null) { $current_attributes = Model_Attribute::find(array('where' => array('attributes' => $attributes_json, 'product_id' => $product->id, 'active' => 1), 'limit' => 1)); } // Get all product attributes $product_attributes = Model_Product::get_product_attributes($product); //var_dump($product_attributes); exit; // Product with attributes if ($product_attributes) { // Set current attribute if ($current_attributes && isset($product_attributes[$current_attributes[0]->id])) { $current_attributes = $product_attributes[$current_attributes[0]->id]; } elseif ($post_select && count($post_select) > 1) { foreach ($post_select as $sk => $sv) { if (!isset($post_select_pop)) { $post_select_pop = $post_select; array_pop($post_select_pop); } $count = count($post_select_pop); foreach ($product_attributes as $pk => $pv) { $i = 0; $not_exists = array(); foreach ($pv as $pkk => $pvv) { if ($i >= $count) { continue; } if (!isset($post_select_pop[$pvv->attribute->id]) || $post_select_pop[$pvv->attribute->id] != $pvv->option->id) { $not_exists[] = true; } $i++; } if (empty($not_exists)) { $current_attributes = $product_attributes[$pk]; break 2; } } if (empty($current_attributes) && $count > 1) { array_pop($post_select_pop); } } if (empty($current_attributes)) { $current_attributes = reset($product_attributes); } } elseif ($product->default_attributes) { $current_attributes = $product_attributes[reset($product->default_attributes)->id]; } else { $current_attributes = reset($product_attributes); } foreach ($current_attributes as $k => $v) { $options[] = $v->option->id; } foreach ($product_attributes as $k => $v) { if (is_array($v)) { foreach ($v as $kk => $vv) { $select_name[$vv->attribute->id] = $vv->attribute->name != '' ? $vv->attribute->name : $vv->attribute->title; $select_tmp[$k][$vv->attribute->id][$vv->option->id] = $vv->option->title; foreach ($options as $option_key => $option_value) { if ($kk == $option_key) { $select[$vv->attribute->id][$vv->option->id] = $vv->option->title; if ($vv->option->id != $option_value) { continue 3; } } } } } } if ($select) { $sx = array_keys($select_name); } if (!empty($current_attributes)) { $attr_obj = $current_attributes[0]->product_attribute; $out['current_attributes'] = $current_attributes; $out['code'] = $attr_obj->product_code; if (!empty($attr_obj->images)) { $out['images'] = $attr_obj->images; } $out['select'] = $select; $out['select_name'] = $select_name; $out['sx'] = $sx; $out['retail_price'] = $attr_obj->retail_price; if (!empty($attr_obj->sale_price[0]->price)) { $out['sale'] = $attr_obj->sale_price[0]->price; $out['price'] = $attr_obj->sale_price[0]->price; $out['price_type'] = 'sale_price'; } else { $out['price'] = $attr_obj->retail_price; $out['price_type'] = 'retail_price'; } } } elseif (isset($product->attributes[0]) && $product->attributes[0]->attribute_group_id == 0) { $p = $product->attributes[0]; $out['current_attributes'] = null; $out['code'] = $p->product_code; $out['images'] = $p->images; $out['retail_price'] = $p->retail_price; if (!empty($p->sale_price[0]->price)) { $out['sale'] = $p->sale_price[0]->price; $out['price'] = $p->sale_price[0]->price; $out['price_type'] = 'sale_price'; } else { $out['price'] = $p->retail_price; $out['price_type'] = 'retail_price'; } } // Product without attributes if (empty($out['current_attributes'])) { $out['code'] = $product->code; } // Set images from product if there is no images in product attribute if (empty($out['images']) && !empty($product->images)) { $out['images'] = $product->images; } return $out; }