Esempio n. 1
0
 public function get_search_items($category_id = 0)
 {
     // Override category_id if its a search
     $category_id = \Input::get('category_id', $category_id);
     // Reset $parent_id if its invalid value
     is_numeric($category_id) or $category_id = 0;
     if ($category_id) {
         $category = \Product\Model_Category::find_one_by_id($category_id);
         if ($category) {
             \View::set_global('category', $category);
         }
     }
     /************ Start generating query ***********/
     $items = Model_Category::find(function ($query) use($category_id) {
         // Select only root pages
         $query->where('parent_id', $category_id);
         // Get search 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 '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('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);
     $status = array('false' => 'Select', '1' => 'Active', '0' => 'Inactive', '2' => 'Active in period');
     return array('items' => $items, 'pagination' => $pagination, 'status' => $status);
 }
Esempio n. 2
0
<?php

// Get categories
$categories = \Product\Model_Category::find(function ($query) {
    $query->order_by('sort', 'asc');
    $query->order_by('id', 'asc');
});
$category = isset($category) ? $category : false;
$link = isset($link) ? $link : 'update';
$selected = isset($selected) ? $selected : false;
?>

<?php 
if ($category) {
    ?>

<div class="side_tree_holder">
    <div class="tree_heading">
        <h4>Product Categories</h4>
        <div id="sidetreecontrol" class="sidetreecontrol"><a href="#">Collapse All</a><a href="#">Expand All</a></div>
    </div>
    <div class="tree_content">
        <div id="sidetree">
        
        	<?php 
    if (!$category && empty($categories)) {
        ?>
        		<div class="wide"><span class="req">Note: </span> There are no categories yet.</div>
        	<?php 
    } else {
        ?>
Esempio n. 3
0
 /**
  * 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;
 }
?>
<!-- EOF Image Banner with Title -->

<div class="app-content">
  <div class="container">
    <!-- Show Title if there is image, if none show intro description -->
    <h2 class="copy-title"><?php 
echo !empty($item->images) ? $item->description_intro : $item->title;
?>
</h2>
    <!-- EOF Show Title if there is image, if none show intro description -->
  <?php 
// get all active parent category
$categories = \Product\Model_Category::find(function ($query) {
    $query->where('parent_id', 0);
    $query->where('status', 1);
    $query->order_by('sort', 'asc');
});
?>
  <!-- check if it has category -->
  <?php 
if ($categories) {
    ?>
    <div class="category-products">
      <div class="row">
        <?php 
    foreach ($categories as $category) {
        ?>
          <div class="col-md-4 col-sm-6">
            <div class="product-item">
              <a href="<?php 
Esempio n. 5
0
 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('id', 'in', $product_ids);
             }
             // Get search filters
             foreach (\Input::get() as $key => $value) {
                 if (!empty($value) || $value == '0') {
                     switch ($key) {
                         case 'title':
                             $query->where($key, 'like', "%{$value}%")->or_where('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('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);
     $status = array('false' => 'Select', '1' => 'Active', '0' => 'Inactive', '2' => 'Active in period');
     return array('items' => $items, 'pagination' => $pagination, 'status' => $status);
 }
Esempio n. 6
0
 /**
  * Index page
  * 
  * @access  public
  * @param   $slug
  */
 public function action_index($slug = false, $gallery_id = null)
 {
     // Get home page
     $home_page = \Config::get('details.locked_items.home_page');
     $gallery_page = \Config::get('details.locked_items.gallery');
     $item = false;
     if ($slug == 'referrals' && strtolower(\Sentry::user()->groups()[0]['name']) != 'club members') {
         \Messages::error('Only club members can access this page.');
         \Response::redirect('/');
     }
     //var_dump($slug);die();
     // Find content by slug
     if ($slug !== false) {
         $item = \Page\Model_Page::get_by_slug($slug);
     } elseif ($slug === false) {
         $item = \Page\Model_Page::find_one_by_id($home_page);
     }
     if ($item) {
         // Home page is always without slug so we redirect if someone tries to access home page with slug
         if ($item->id == $home_page && $slug !== false) {
             \Response::redirect(\Uri::base(false));
         }
     } else {
         // Send to 404 page
         throw new \HttpNotFoundException();
     }
     $result = \Page\Model_Page::get_content_file($item->id);
     if ($result) {
         foreach ($result as $rs) {
             $item->content_file_pdf = $rs->file;
             //pdf file
             $item->content_file_title = $rs->title;
             //pdf title
         }
     }
     // Default view file
     $view_file = 'default';
     // Fill in home page slug if none is supplied
     if ($slug === false && $item->id == $home_page) {
         $slug = $item->seo->slug;
         // Home page has its standard template
         $view_file = 'home';
     } else {
         // Search for page specific template
         $files = \File::read_dir($this->theme_view_path);
         if (!empty($files)) {
             foreach ($files as $file) {
                 $path_parts = pathinfo($file);
                 if (strpos($path_parts['filename'], '_') === 0) {
                     $file_id = substr($path_parts['filename'], strripos($path_parts['filename'], '_') + 1);
                     if (is_numeric($file_id) && $item->id == $file_id) {
                         $view_file = $path_parts['filename'];
                     }
                 }
             }
         }
     }
     $stock_options = \Config::load('stock-option.db');
     // TODO delete this
     srand($item->id);
     $this->page_theme = \Theme::instance()->set_partial('content', $this->view_dir . $view_file);
     $this->page_theme->set('item', $item, false);
     $this->page_theme->set('manage_stock', $stock_options['manage_stock'], false);
     $this->page_theme->set('hide_out_of_stock', $stock_options['hide_out_of_stock'], false);
     $this->page_theme->set('allow_buy_out_of_stock', $stock_options['allow_buy_out_of_stock'], false);
     if ($item->id == $gallery_page) {
         if ($gallery_id && \Input::is_ajax()) {
             // This will return gallery images only if it is ajax call
             $this->returnGalleryImages($gallery_id);
         }
     }
     // Data for home page
     if ($view_file == 'home') {
         $featured_products = \Product\Model_Group::find_one_by_id(4);
         if ($featured_products) {
             $featured_products = $featured_products->products;
         }
         $this->page_theme->set('featured_products', $featured_products, false);
         $categories = \Product\Model_Category::find(array('where' => array('parent_id' => 0, 'status' => 1), 'order_by' => array('sort' => 'asc'), 'limit' => 8));
         $this->page_theme->set('categories', $categories, false);
         $applications = \Application\Model_Application::find(array('where' => array('parent_id' => 0, 'status' => 1), 'order_by' => array('sort' => 'asc'), 'limit' => 8));
         $this->page_theme->set('applications', $applications, false);
         // $this->page_theme->set('blog', $this->wp_blog(), false);
     }
     \View::set_global('title', $item->seo->meta_title ?: $item->title);
     \View::set_global('meta_description', $item->seo->meta_description ?: '');
     \View::set_global('meta_keywords', $item->seo->meta_keywords ?: '');
     $robots = array('meta_robots_index' => $item->seo->meta_robots_index == 1 ? 'index' : 'noindex', 'meta_robots_follow' => $item->seo->meta_robots_follow == 1 ? 'follow' : 'nofollow');
     \View::set_global('robots', $robots);
     \View::set_global('canonical', $item->seo->canonical_links);
     \View::set_global('h1_tag', $item->seo->h1_tag);
     if ($item->seo->redirect_301) {
         \Response::redirect($item->seo->redirect_301);
     }
 }
        echo $margin();
        ?>
">
                                        <?php 
        echo \Form::label('Payment Status');
        ?>
                                        <?php 
        echo \Form::select('payment_status', \Input::get('payment_status'), array('false' => 'Select') + $payment_status, array('class' => 'select_init filter_width'));
        ?>
                                    </div>
                                <?php 
    }
    ?>
                                
                                <?php 
    $categories = \Product\Model_Category::fetch_pair('id', 'title', array('order_by' => array('title' => 'asc')));
    if (!is_array($categories)) {
        $categories = array();
    }
    ?>
                                <?php 
    if (in_array('product_category', $options)) {
        ?>
                                    <div class="filter_width <?php 
        echo $margin();
        ?>
">
                                        <?php 
        echo \Form::label('Product Category');
        ?>
                                        <?php 
Esempio n. 8
0
 public function get_search_products($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 (\Input::get()) {
         if (\Input::get('status') != 'select') {
             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 ***********/
         if (\Input::get()) {
             if (\Input::get('status') != 'select') {
                 $items = \Product\Model_Product::find(function ($query) use($product_ids) {
                     if (!empty($product_ids)) {
                         $query->where('id', 'in', $product_ids);
                     }
                     // Get search filters
                     foreach (\Input::get() as $key => $value) {
                         if (!empty($value) || $value == '0') {
                             switch ($key) {
                                 case 'title':
                                     $query->where($key, 'like', "%{$value}%")->or_where('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('sort', 'asc');
                     $query->order_by('id', 'asc');
                 });
             }
         }
     }
     /************ End generating query ***********/
     // Reset to empty array if there are no result found by query
     if (empty($items)) {
         $items = array();
     }
     return array('items' => $items);
 }
Esempio n. 9
0
 /**
  * Get additional content data selected
  * 
  * @param $result = Query result
  */
 public static function post_find($result)
 {
     $config = \Config::load('stock-option.db');
     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 category products
                 $item->get_products = static::lazy_load(function () use($item, $config) {
                     $products = Model_Product_To_Categories::find(function ($query) use($item) {
                         return $query->where('category_id', $item->id);
                     }, 'product_id');
                     if (!empty($products)) {
                         $products = '(' . implode(',', array_keys($products)) . ')';
                         return Model_Product::find(function ($query) use($products, $item, $config) {
                             $query->select('product.*');
                             if ($config['hide_out_of_stock']) {
                                 $query->join('product_attributes');
                                 $query->on('product.id', '=', 'product_attributes.product_id');
                                 $query->where('product_attributes.stock_quantity', '>', 0);
                             }
                             $query->and_where('product.id', 'IN', \DB::expr($products));
                             $query->group_by('product.id');
                             $query->order_by('product.sort', 'asc');
                             // print_r((string)$query);
                             // die;
                             return $query;
                         }, 'id');
                     }
                     return array();
                 }, $item->id, 'products');
                 // Get all category products (including subcategory products)
                 $item->get_all_products = static::lazy_load(function () use($item) {
                     $categories_ids = \Product\Model_Category::get_all_children($item);
                     $products = Model_Product_To_Categories::find(function ($query) use($item, $categories_ids) {
                         return $query->where('category_id', 'in', $categories_ids);
                     }, '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, 'all_products');
                 // Get content images
                 $item->get_images = static::lazy_load(function () use($item) {
                     return Model_Category_Image::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc')));
                 }, $item->id, 'images');
                 // Get content children
                 $item->get_children = static::lazy_load(function () use($item) {
                     return \Product\Model_Category::find(array('where' => array('parent_id' => $item->id), 'order_by' => array('sort' => 'asc')));
                 }, $item->id, 'children');
                 // Get content children
                 $item->get_seo = static::lazy_load(function () use($item) {
                     return Model_Category_Seo::find_one_by_content_id($item->id);
                 }, $item->id, 'seo', 'object');
             }
         }
     }
     // return the result
     return $result;
 }