public function action_order() { define("SECONDS_PER_DAY", 3600 * 24); $now = time(); $cart = Session::get('cart'); $user = Model_User::find($this->sessUser->id); $cart_info = []; $order = Model_Order::forge(); $order->user_id = $user->id; $order->created_at = $now; $order->save(); if (!is_null($cart)) { foreach ($cart as $product_id => $quantity) { $product = Model_Product::find($product_id); $order_product = Model_OrderProduct::forge(); $order_product->order_id = $order->id; $order_product->product_id = $product->id; $order_product->quantity = $quantity; $order_product->price = $product->price; $order_product->save(); } } Session::delete('cart'); return Response::redirect('/cart'); }
/** * Get additional content data selected * * @param $result = Query result */ public static function post_find($result) { if ($result !== null) { if (is_array($result)) { foreach ($result as $item) { // It will first check if we already have result in temporary result, // and only execute query if we dont. That way we dont have duplicate queries // Get related products $item->get_products = static::lazy_load(function () use($item) { $products = Model_Product_To_Groups::find(function ($query) use($item) { return $query->where('group_id', $item->id); }, 'product_id'); if (!empty($products)) { $products = '(' . implode(',', array_keys($products)) . ')'; return Model_Product::find(function ($query) use($products, $item) { $query->where('id', 'IN', \DB::expr($products)); $query->order_by('sort', 'asc'); return $query; }, 'id'); } return array(); }, $item->id, 'products'); // Get content children $item->get_children = static::lazy_load(function () use($item) { return Model_Group::find(array('where' => array('parent_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'children'); // Get group images $item->get_images = static::lazy_load(function () use($item) { return Model_Group_Image::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'images'); // Get group discount options // Use one more item property (user_group_id) // It can be set using methods (set_user_group_id and get_user_group_id) $item->get_discount_options = static::lazy_load(function () use($item) { $options = Model_Group_Options::find(array('where' => array('user_group_id' => $item->get_user_group_id(), 'product_group_id' => $item->id))); return isset($options[0]) ? $options[0] : array(); }, $item->id, 'discount_options', 'object'); // Get group discount options // Use one more item property (user_group_id) // It can be set using methods (set_user_group_id and get_user_group_id) $item->get_discounts = static::lazy_load(function () use($item) { return Model_Group_Discounts::find(array('where' => array('user_group_id' => $item->get_user_group_id(), 'product_group_id' => $item->id), 'order_by' => array('qty' => 'asc')), 'qty'); }, $item->id, 'discounts'); // DO NOT CHANGE THIS TWO FUNCTIONS AS THEY ARE NEEDED $item->set_user_group_id = function ($user_group_id = NULL) { \Product\Model_Group::$user_group_id = $user_group_id; }; $item->get_user_group_id = function () { return \Product\Model_Group::$user_group_id; }; } } } // return the result return $result; }
public static function get_by_code($code, $option = array()) { try { $product = Model_Product::find('all', array('select' => !empty($option['all']) ? $option['select'] : array(), 'where' => !empty($option['where']) ? array_merge(array(array('code' => $code)), $option['where']) : array('code' => $code))); return self::map_product($product)[0]; } catch (Exception $e) { Log::write('ERROR', $e->getMessage()); } return false; }
public function action_delete($id = null) { if ($product = Model_Product::find($id)) { $product->delete(); Session::set_flash('success', e('Deleted product #' . $id)); } else { Session::set_flash('error', e('Could not delete product #' . $id)); } Response::redirect('admin/products'); }
public function action_orderDetails($order_id) { $order_info = []; $order_details = Model_OrderProduct::find('all', array('where' => array(['order_id', $order_id]))); foreach ($order_details as $order_detail) { $product = Model_Product::find($order_detail->product_id); $order_info[] = (object) ['id' => $product->id, 'name' => $product->name, 'price' => $product->price, 'quantity' => $order_detail->quantity]; } $data = ['order_info' => $order_info, 'order_id' => $order_id, 'message' => Session::get_flash('message'), 'remove_sure' => Session::get_flash('remove_sure')]; return Response::forge(View::forge('user/orderDetails.tpl', $data)); }
public function newAction() { $param['product_code'] = $this->_getParam('product_code'); $param['product_name'] = $this->_getParam('product_name'); $param['short_description'] = $this->_getParam('short_description'); $param['description'] = $this->_getParam('description'); $param['active'] = $this->_getParam('active'); $app = Model_Product::find($param['product_code']); if ($app) { throw new Exception("Product Code '" . $param['product_code'] . "' already exist."); } $p = new Model_Product(); $p->newProduct($param); echo json_encode(array('success' => true)); }
/** * Get additional content data selected * * @param $result = Query result */ public static function post_find($result) { if ($result !== null) { if (is_array($result)) { foreach ($result as $item) { // It will first check if we already have result in temporary result, // and only execute query if we dont. That way we dont have duplicate queries // Get related products $item->get_products = static::lazy_load(function () use($item) { $products = Model_Product_To_Groups::find(function ($query) use($item) { return $query->where('group_id', $item->id); }, 'product_id'); if (!empty($products)) { $products = '(' . implode(',', array_keys($products)) . ')'; return Model_Product::find(function ($query) use($products, $item) { $query->where('id', 'IN', \DB::expr($products)); $query->order_by('sort', 'asc'); return $query; }, 'id'); } return array(); }, $item->id, 'products'); // Get content children $item->get_children = static::lazy_load(function () use($item) { return Model_Group::find(array('where' => array('parent_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'children'); // Get group images $item->get_images = static::lazy_load(function () use($item) { return Model_Group_Image::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'images'); // Get product groups $item->get_product_groups = static::lazy_load(function () use($item) { $product_groups = \Product\Model_Group::find_by_type('discount'); foreach ($product_groups as $product_group) { $product_group->set_user_group_id($item->id); } return $product_groups; }, $item->id, 'product_groups'); } } } // return the result return $result; }
public function action_productinfo($product_id) { $change = Input::post('change'); $quantity = Input::post('quantity'); $cart = Session::get('cart', []); if (!is_null($change)) { $cart[$product_id] = $quantity; Session::set('cart', $cart); return Response::redirect('/cart'); } else { if (isset($cart[$product_id])) { $quantity = $cart[$product_id]; } if (is_null($quantity)) { $quantity = 0; } } $product = Model_Product::find($product_id); $data = ['product' => $product, 'quantity' => $quantity]; $view = View::forge('home/productInfo.tpl', $data); $view->set('description', $product->description, false); return Response::forge($view); }
public function get_search_items($category_id = false) { // Override category_id if its a search $category_id = \Input::get('category_id', $category_id); $product_ids = array(); // If we are viewing category products // We need to find all products from that and child categories if (is_numeric($category_id)) { $category = \Product\Model_Category::find_one_by_id($category_id); if ($category) { \View::set_global('category', $category); if ($category->all_products) { $product_ids = array_keys($category->all_products); } } } // If we are filtering products by category and there is nothing found // We return empty array of products without even going to database if (empty($product_ids) && is_numeric($category_id)) { $items = array(); } else { /************ Start generating query ***********/ $items = Model_Product::find(function ($query) use($product_ids) { if (!empty($product_ids)) { $query->where('product.id', 'in', $product_ids); } // Get search filters foreach (\Input::get() as $key => $value) { if (!empty($value) || $value == '0') { switch ($key) { case 'title': $query->select('product.*'); $query->distinct(); $query->join('product_attributes'); $query->on('product.id', '=', 'product_attributes.product_id'); $query->where('product.' . $key, 'like', "%{$value}%"); // ->or_where('product.code', 'like', "%$value%") //->or_where('product_attributes.product_code', 'like', "%$value%"); break; case 'status': if (is_numeric($value)) { $query->where($key, $value); } break; case 'active_from': $date = strtotime($value); if ($date) { $query->where($key, '>=', $date); } break; case 'active_to': $date = strtotime($value); if ($date) { $query->where($key, '<=', $date); } break; } } } // Order query $query->order_by('product.sort', 'asc'); $query->order_by('product.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); $status = array('false' => 'Select', '1' => 'Active', '0' => 'Inactive', '2' => 'Active in period'); $group = Model_Group::find(); return array('items' => $items, 'group' => $group, 'pagination' => $pagination, 'status' => $status); }
public function action_myproducts() { $products = \Product\Model_Product::filter_by_group(); // Products $items = array(); if ($products) { $items = Model_Product::find(array('where' => array(array('id', 'in', array_keys($products['assigned'])))), 'id'); } $category = null; $parent_category = null; // 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', 15), '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 . 'subcategory')->set('page_title', 'My Products', false)->set('category', $category, false)->set('parent_category', $parent_category, false)->set('items', $items, false)->set('pagination', $pagination, false); }
public function action_products_assigned($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 Products'); // Update group products if (\Input::post()) { $add = \Input::post('products.add', array()); $remove = \Input::post('products.remove', array()); $update = \Input::post('products.update', array()); if (\Input::post('add', false)) { foreach ($add as $value) { $related = Model_Product_To_Groups::forge(array('group_id' => $item->id, 'product_id' => $value)); $related->save(); // NRB-Gem: Get current Customer Group for this Pricing Group $product_group_option = \Product\Model_Group_Options::find_one_by(array('active' => 1, 'product_group_id' => $id)); $user_group_id = null; if ($product_group_option) { $user_group_id = $product_group_option->user_group_id; } // NRB-Gem: add attributes $a_attributes = \Product\Model_Attribute::find(function ($query) use($value) { $query->where('product_id', '=', $value); $query->where('active', '=', 1); }); $a_attribute_ids = array(); if (count($a_attributes)) { foreach ($a_attributes as $o_attr) { $o_attr_price = new Model_Attribute_Price(array('product_attribute_id' => $o_attr->id, 'pricing_group_id' => $item->id, 'user_group_id' => $user_group_id, 'type' => 'sale_price')); $o_attr_price->save(); } } } \Messages::success('Products successfully added to group.'); } else { if (\Input::post('remove', false)) { foreach ($remove as $value) { $related = Model_Product_To_Groups::find_one_by(array(array('group_id', '=', $item->id), array('product_id', '=', $value))); if (!is_null($related)) { $related->delete(); // NRB-Gem: remove attributes $a_attributes = \Product\Model_Attribute::find(function ($query) use($value) { $query->where('product_id', '=', $value); }); $a_attribute_ids = array(); if (count($a_attributes)) { foreach ($a_attributes as $o_attr) { $a_attribute_ids[] = $o_attr->id; } } if (count($a_attribute_ids)) { \DB::delete('product_attribute_price')->where('product_attribute_id', 'IN', \DB::expr('(' . implode(',', $a_attribute_ids) . ')'))->where('pricing_group_id', '=', $item->id)->execute(); } } } \Messages::success('Products successfully removed from group.'); } } if (!empty($update)) { foreach ($update['discount'] as $key => $value) { if (isset($update['attr_id'][$key])) { foreach ($update['attr_id'][$key] as $attribute_id) { $update_sale_price = Model_Attribute_Price::find_one_by(array(array('product_attribute_id', '=', $attribute_id), array('pricing_group_id', '=', $item->id))); if ($update_sale_price) { $update_sale_price->set(array('discount' => $update['discount'][$key][$attribute_id], 'able_discount' => $update['able_discount'][$key][$attribute_id], 'price' => $update['price'][$key][$attribute_id])); $update_sale_price->save(); } } } else { $prod_attributes = \Product\Model_Attribute::find_by_product_id($key); $attribute_id = $prod_attributes[0]->id; $o_attr_price_list = Model_Attribute_Price::find_one_by(array(array('product_attribute_id', '=', $attribute_id), array('pricing_group_id', '<>', $item->id))); if ($o_attr_price_list) { $o_attr_price_list->delete(); } $update_sale_price = Model_Attribute_Price::find_one_by(array(array('product_attribute_id', '=', $attribute_id), array('pricing_group_id', '=', $item->id))); if ($update_sale_price) { $update_sale_price->set(array('discount' => $update['discount'][$key], 'able_discount' => $update['able_discount'][$key], 'price' => $update['price'][$key])); $update_sale_price->save(); } } } \Messages::success('Products successfully updated.'); } // if(\Input::is_ajax()) // { // echo \Messages::display('left', false); // exit; // } // else // { \Response::redirect(\Input::referrer(\Uri::admin('current'))); // } } $group = Model_Group::find_one_by_id($id); /************ Get non related infotabs ***********/ $items = Model_Product::find(function ($query) use($item) { $product_ids = array(); // NRB-Gem: Comment out, to enable multiple pricing groups for a product // Products can be in only ONE discounted group // But can exist in MULTIPLE standard groups // if($item->type == 'pricing') // { // $discounted_products = Model_Product::in_group_type('pricing'); // foreach($discounted_products as $product) // array_push($product_ids, $product->id); // } foreach ($item->products as $product) { array_push($product_ids, $product->id); } if (!empty($product_ids)) { $product_ids = '(' . implode(',', $product_ids) . ')'; $query->where('id', 'NOT IN', \DB::expr($product_ids)); } // Filters foreach (\Input::get() as $key => $value) { if (!empty($value) || $value == '0') { switch ($key) { case 'title': $query->where($key, 'like', "%{$value}%"); break; case 'status': if (is_numeric($value)) { $query->where($key, $value); } break; case 'category_id': if (is_numeric($value)) { $products = Model_Product_To_Categories::find(function ($query) use($value) { return $query->where('category_id', $value); }, 'product_id'); if (empty($products)) { $products = array(0); } $query->where('id', 'IN', array_keys($products)); } break; } } } // Order query $query->order_by('sort', 'asc'); $query->order_by('id', 'asc'); }); $group->not_related_products = null; $not_related_products = $items ? $items : array(); // product_group_options $customer_groups = Model_Group_Options::find_by(array('active' => 1, 'product_group_id' => $id)); $customer_group = array(); if ($customer_groups) { $customer_group = Model_Customer_Group::find_one_by_id($customer_groups[0]->user_group_id); } // 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); $status = array('false' => 'Select', '1' => 'Active', '0' => 'Inactive', '2' => 'Active in period'); \Theme::instance()->set_partial('content', $this->view_dir . 'products_assigned')->set('group', $group, false)->set('items', $items, false)->set('pagination', $pagination, false)->set('status', $status, false)->set('customer_group', $customer_group, false); }
/** * 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 product categories $item->get_categories = static::lazy_load(function () use($item) { $categories = Model_Product_To_Categories::find(function ($query) use($item) { return $query->where('product_id', $item->id); }, 'category_id'); if (!empty($categories)) { $categories = '(' . implode(',', array_keys($categories)) . ')'; return Model_Category::find(function ($query) use($categories) { return $query->where('id', 'IN', \DB::expr($categories)); }, 'id'); } return array(); }, $item->id, 'categories'); // Get product infotabs $item->get_infotabs = static::lazy_load(function () use($item) { return Model_Product_To_Infotabs::find(function ($query) use($item) { $query->order_by('sort', 'asc'); return $query->where('product_id', $item->id); }); }, $item->id, 'infotabs'); // Get product groups $item->get_groups = static::lazy_load(function () use($item) { $groups = Model_Product_To_Groups::find(function ($query) use($item) { return $query->where('product_id', $item->id); }, 'group_id'); if (!empty($groups)) { $groups = '(' . implode(',', array_keys($groups)) . ')'; return Model_Group::find(function ($query) use($groups, $item) { $query->where('id', 'IN', \DB::expr($groups)); $query->order_by('sort', 'asc'); return $query; }, 'id'); } return array(); }, $item->id, 'groups'); // Get product groups $item->get_pricing_group = static::lazy_load(function () use($item) { $groups = Model_Product_To_Groups::find(function ($query) use($item) { return $query->where('product_id', $item->id); }, 'group_id'); if (!empty($groups)) { $groups = '(' . implode(',', array_keys($groups)) . ')'; $groups = Model_Group::find(function ($query) use($groups, $item) { $query->where('type', 'pricing'); $query->where('id', 'IN', \DB::expr($groups)); $query->order_by('sort', 'asc'); return $query; }); return !empty($groups) ? $groups[0] : false; } return false; }, $item->id, 'pricing_group', 'object'); // Get related products $item->get_related_products = static::lazy_load(function () use($item) { $related = Model_Product_To_Related::find(function ($query) use($item) { return $query->where('product_id', $item->id); }, 'related_id'); if (!empty($related)) { $related = '(' . implode(',', array_keys($related)) . ')'; return Model_Product::find(function ($query) use($related, $item) { $query->where('id', 'IN', \DB::expr($related)); $query->where('id', '<>', $item->id); $query->order_by('sort', 'asc'); return $query; }, 'id'); } return array(); }, $item->id, 'related_products'); // Get upsell products $item->get_upsell_products = static::lazy_load(function () use($item) { $upsell = Model_Product_To_Upsell::find(function ($query) use($item) { return $query->where('product_id', $item->id); }, 'upsell_id'); if (!empty($upsell)) { $upsell = '(' . implode(',', array_keys($upsell)) . ')'; return Model_Product::find(function ($query) use($upsell, $item) { $query->select(Model_Product_To_Upsell::get_protected('_table_name') . '.discount', Model_Product::get_protected('_table_name') . '.*'); $query->join(Model_Product_To_Upsell::get_protected('_table_name')); $query->on(Model_Product_To_Upsell::get_protected('_table_name') . '.upsell_id', '=', Model_Product::get_protected('_table_name') . '.id'); $query->where(Model_Product::get_protected('_table_name') . '.id', 'IN', \DB::expr($upsell)); $query->where(Model_Product::get_protected('_table_name') . '.id', '<>', $item->id); $query->order_by(Model_Product::get_protected('_table_name') . '.sort', 'asc'); return $query; }, 'id'); } return array(); }, $item->id, 'upsell_products'); // Get content images $item->get_images = static::lazy_load(function () use($item) { return Model_Product_Image::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'images'); // Get content files $item->get_files = static::lazy_load(function () use($item) { return Model_Product_File::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'files'); // Get content videos $item->get_videos = static::lazy_load(function () use($item) { return Model_Product_Video::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'videos'); // Get content children $item->get_seo = static::lazy_load(function () use($item) { return Model_Product_Seo::find_one_by_content_id($item->id); }, $item->id, 'seo', 'object'); // Get attributes $item->get_attributes = static::lazy_load(function () use($item) { return Model_Attribute::find(array('where' => array('product_id' => $item->id))); }, $item->id, 'attributes'); // Get default attribute $item->get_default_attributes = static::lazy_load(function () use($item) { return Model_Attribute::find(array('where' => array('product_id' => $item->id, 'default' => 1, 'active' => 1))); }, $item->id, 'default_attributes'); // Get all active prices $item->get_all_prices = static::lazy_load(function () use($item) { $product_attributes = Model_Attribute::find(function ($query) use($item) { $query->join(Model_Attribute_Price::get_protected('_table_name')); $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id'); $query->where('product_id', $item->id); $query->and_where('attributes', '!=', ''); $query->and_where('active', 1); return $query; }, 'id'); return $product_attributes; }, $item->id, 'unit_price'); // Get prices for product $item->get_prices = static::lazy_load(function () use($item) { $product_attributes = Model_Attribute::find(function ($query) use($item) { $query->join(Model_Attribute_Price::get_protected('_table_name')); $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id'); $query->where('product_id', $item->id); return $query; }, 'id'); return $product_attributes; }, $item->id, 'unit_price'); // Get unit price $item->get_unit_price = static::lazy_load(function () use($item) { $product_attributes = Model_Attribute::find(function ($query) use($item) { $query->join(Model_Attribute_Price::get_protected('_table_name')); $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id'); $query->where('product_id', $item->id); $query->and_where('type', 'unit_price'); $query->and_where('attributes', '!=', ''); $query->and_where('active', 1); $query->limit(1); return $query; }, 'id'); return $product_attributes; }, $item->id, 'unit_price'); // Get sale price $item->get_sale_price = static::lazy_load(function () use($item) { $product_attributes = Model_Attribute::find(function ($query) use($item) { $query->join(Model_Attribute_Price::get_protected('_table_name')); $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id'); $query->where('product_id', $item->id); $query->and_where('type', 'sale_price'); $query->and_where('attributes', '!=', ''); $query->and_where('active', 1); $query->limit(1); return $query; }, 'id'); return $product_attributes; }, $item->id, 'sale_price'); // Get default price (RRP or Sale price) $item->get_default_price = static::lazy_load(function () use($item) { $out = array(0); $product_attributes = Model_Attribute::find(function ($query) use($item) { $query->join(Model_Attribute_Price::get_protected('_table_name')); $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id'); $query->where('product_id', $item->id); $query->and_where('type', 'sale_price'); $query->and_where('attributes', '!=', ''); $query->and_where('active', 1); $query->and_where('default', 1); $query->limit(1); return $query; }, 'id'); if ($product_attributes) { $obj = reset($product_attributes); if ($obj->price > 0) { $out[0] = $obj->price; $out[1] = 'sale_price'; } else { $out[0] = $obj->retail_price; $out[1] = 'retail_price'; } } return $out; }, $item->id, 'default_price'); // Get active attribute group $item->get_active_attribute_group = static::lazy_load(function () use($item) { if ($attribute = Model_Attribute::find_one_by_product_id($item->id)) { return $attribute->attribute_group; } return false; }, $item->id, 'active_attribute_group'); // Get product data $item->get_data = static::lazy_load(function () use($item) { return \Product\Model_Product::product_data($item); }, $item->id, 'data'); } } } // return the result return $result; }
public static function run($type = null, $options = array()) { $data = array(); $photo_name = array(); try { Upload::process(); } catch (Exception $e) { $data['error'] = Lang::get('notice.upload.no_file'); return $data; } if (Upload::is_valid()) { Upload::save(); } foreach (Upload::get_errors() as $file) { $data['error'] = $file['errors']['0']['message']; return $data; } foreach (Upload::get_files() as $file) { $resize = self::resize_photo($file['saved_to'], $file['saved_as'], $type); $photo_name[] = $file['saved_as']; } if ($resize) { switch ($type) { case 'icon': if (Model_Base_User::update($options['user_id'], array('user_photo' => $photo_name[0]))) { $old_photo = $file['saved_to'] . $type . '/' . $options['user_photo']; if (File::exists($old_photo)) { File::delete($old_photo); } $data['photo_name'] = _PATH_ICON_ . $photo_name[0]; } else { $data['error'] = Lang::get('notice.upload.save_icon_error'); } break; case 'category': $old_photo = $options['type'] === 'new' ?: $file['saved_to'] . $type . '/' . Model_Category::find($options['category_id'])->category_photo; if (Model_Base_Category::update($options['category_id'], array('category_photo' => $photo_name[0]))) { if (File::exists($old_photo)) { File::delete($old_photo); } $data['photo_name'] = _PATH_CATEGORY_ . $photo_name[0]; } else { $data['error'] = Lang::get('notice.upload.save_category_error'); } break; case 'product': $old_photo = $file['saved_to'] . $type . '/' . Model_Product::find($options['product_id'])->product_photo; if (Model_Base_Product::update($options['product_id'], array('product_photo' => $photo_name[0]))) { if (File::exists($old_photo)) { File::delete($old_photo); } $data['photo_name'] = _PATH_PRODUCT_ . $photo_name[0]; } else { $data['error'] = Lang::get('notice.upload.save_product_error'); } break; case 'photo': if ($options['type'] === 'sub_product_photo') { $photo_props = array('product_id' => $options['product_id'], 'photo_name' => $photo_name[0]); if ($photo_id = Model_Base_Photo::insert($photo_props)) { $data['photo_id'] = $photo_id; $data['photo_name'] = _PATH_PHOTO_ . $photo_name[0]; } else { $data['error'] = Lang::get('notice.upload.save_photo_error'); } } break; default: $data['error'] = Lang::get('system_error'); break; } } else { $data['error'] = Lang::get('system_error'); } return $data; }
private function check_code_exist($product_id, $code) { $flag = Model_Product::find(function ($query) use($product_id, $code) { $query->where('id', '!=', $product_id); $query->and_where('code', '=', $code); }); if ($flag) { return true; } return false; }
public function action_modifyProduct() { $product_id = Input::post('product_id'); $name = Input::post('name'); $price = Input::post('price'); $category = Input::post('category'); $product = Model_Product::find($product_id); $description = Input::post('description'); $image = Input::post('image'); $doit = Input::post('doit'); $validator = $this->addModifyValidator(); $message = ''; $category_id = $product->category_id; //$category = Model_Category::find($product->category_id); if (!is_null($doit)) { try { if (!$validator->run(Input::post())) { throw new Exception(); } // $product = Model_Product::forge(); $product->name = $name; $valid = (object) $validator->validated(); $product->category_id = $category_id; $product->price = $valid->price; $product->description = $description; $product->image = $image; $product->save(); return Response::redirect("/home/productInfo/{$product->id}"); } catch (Exception $ex) { $message = $ex->getLine(); } } else { $category = Model_Category::find($product->category_id); $category = $category->name; $name = $product->name; } $data = array('product' => $product, 'name' => $name, 'message' => $message, 'product_id' => $product_id, 'category' => $category, 'price' => $price, 'description' => $description, 'image' => $image); $view = View::forge("admin/modifyProduct.tpl", $data); $view->set('validator', $validator, false); return Response::forge($view); }
public function get_search_items($item) { /************ Get non upsell products ***********/ $items = Model_Product::find(function ($query) use($item) { $upsell_ids = array($item->id); foreach ($item->upsell_products as $upsell) { array_push($upsell_ids, $upsell->id); } if (!empty($upsell_ids)) { $upsell_ids = '(' . implode(',', $upsell_ids) . ')'; $query->where('id', 'NOT IN', \DB::expr($upsell_ids)); } // Order query $query->order_by('sort', 'asc'); $query->order_by('id', 'asc'); }); $item->not_upsell_products = $items ? $items : array(); /************ End generating query ***********/ foreach ($item->not_upsell_products as $item_key => $item_value) { foreach (\Input::get() as $key => $value) { if (!empty($value) || $value == '0') { switch ($key) { case 'title': if (stripos($item_value->title, $value) === false) { unset($item->not_upsell_products[$item_key]); } break; case 'status': if (is_numeric($value)) { if ($item_value->status != $value) { unset($item->not_upsell_products[$item_key]); } } break; case 'active_from': $date = strtotime($value); if ($date) { if ($item_value->active_from < $value) { unset($item->not_upsell_products[$item_key]); } } break; case 'active_to': $date = strtotime($value); if ($date) { if ($item_value->active_to > $value) { unset($item->not_upsell_products[$item_key]); } } break; case 'category_id': if (is_numeric($value)) { if (!array_key_exists($value, $item_value->categories)) { unset($item->not_upsell_products[$item_key]); } } break; } } } } // Initiate pagination $pagination = \Hybrid\Pagination::make(array('total_items' => count($item->not_upsell_products), 'per_page' => \Input::get('per_page', 10), 'uri_segment' => null)); // Remove unwanted items, and show only required ones $item->not_upsell_products = array_slice($item->not_upsell_products, $pagination->offset, $pagination->per_page); $status = array('false' => 'Select', '1' => 'Active', '0' => 'Inactive', '2' => 'Active in period'); return array('item' => $item, 'pagination' => $pagination, 'status' => $status); }
public static function get_products($category_id = false, $sort = 'asc') { static::$sort = $sort; $out = array(); $products = Model_Product_To_Categories::find(function ($query) use($category_id) { return $query->where('category_id', $category_id); }, 'product_id'); if (!empty($products)) { $products = '(' . implode(',', array_keys($products)) . ')'; $out = Model_Product::find(function ($query) use($products) { $query->where('id', 'IN', \DB::expr($products)); return $query; }, 'id'); } if (!empty($out)) { usort($out, array('\\Product\\Model_Category', 'sort_product_by_price')); } return $out; }