Exemplo n.º 1
0
 /**
  * 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;
 }
Exemplo n.º 2
0
 /**
  * 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;
 }
Exemplo n.º 3
0
 private function add_attribute_price($post, $data)
 {
     $a_products = Model_Product_To_Groups::find_by(array('product_id' => $post['product_id']));
     if ($a_products) {
         foreach ($a_products as $o_product) {
             // Insert or update option
             $a_attr = Model_Attribute_Price::find_by(array('product_attribute_id' => $data['attr_id'], 'pricing_group_id' => $o_product->group_id));
             if ($a_attr) {
                 $insert_price = $a_attr[0];
             } else {
                 $insert_price = Model_Attribute_Price::forge();
             }
             //$insert_price->price = $post[$post['price_type'] . '_new'][$data['key']];
             //$insert_price->active = $post['active_new'][$key];
             // NRB-Gem: Set customer groups for this product
             // product_group_options.user_group_id => product_group_options.product_group_id = $o_product->group_id
             $o_group = Model_Group_Options::find_by(array('product_group_id' => $o_product->group_id, 'active' => 1), null, 1);
             $data = array('product_attribute_id' => $data['attr_id'], 'type' => $post['price_type'], 'product_group_discount_id' => $post['product_group_discount_id'], 'pricing_group_id' => $o_product->group_id, 'user_group_id' => $o_group ? $o_group[0]->user_group_id : null);
             $insert_price->set($data);
             $insert_price->save();
         }
     }
 }
Exemplo n.º 4
0
 public function action_delete($id = false)
 {
     if (is_numeric($id)) {
         // Get news item to edit
         if ($item = Model_Group::find_one_by_id($id)) {
             // Delete item
             try {
                 // Delete all relations of this group with products
                 $groups = Model_Product_To_Groups::find_by_group_id($item->id);
                 if (!empty($groups)) {
                     foreach ($groups as $group) {
                         $group->delete();
                     }
                 }
                 $groups = Model_Group_Options::find_by_product_group_id($item->id);
                 if (!empty($groups)) {
                     foreach ($groups as $group) {
                         $group->delete();
                     }
                 }
                 if (!empty($item->images)) {
                     foreach ($item->images as $image) {
                         $this->delete_image($image->image);
                         $image->delete();
                     }
                 }
                 // Delete group
                 $item->delete();
                 \Messages::success('Group successfully deleted.');
             } catch (\Database_Exception $e) {
                 // show validation errors
                 \Messages::error('<strong>There was an error while trying to delete group</strong>');
                 // Uncomment lines below to show database errors
                 //$errors = $e->getMessage();
                 //\Messages::error($errors);
             }
         }
     }
     \Response::redirect(\Input::referrer());
 }
Exemplo n.º 5
0
 public static function filter_by_group()
 {
     $out['assigned'] = array();
     $out['not_assigned'] = array();
     $products_assigned = array();
     $products_not_assigned = array();
     $in_groups = array();
     $not_in_groups = array();
     if (parent::check_logged()) {
         $groups = Model_Group_Options::find(array(), 'product_group_id');
         $user = \Sentry::user();
         $user_group = $user->groups();
         $user_group = $user_group[0];
         if (isset($user_group['id'])) {
             $in_groups = Model_Group_Options::find(array('where' => array('user_group_id' => $user_group['id'], 'able_to_buy' => 1)), 'product_group_id');
             $not_in_groups = Model_Group_Options::find(array('where' => array(array('user_group_id', '!=', $user_group['id']))), 'product_group_id');
             if (!is_array($in_groups)) {
                 $in_groups = array();
             }
             if (!is_array($not_in_groups)) {
                 $not_in_groups = array();
             }
             $not_in_groups = array_diff_key($not_in_groups, $in_groups);
         }
     } else {
         $not_in_groups = Model_Group_Options::find(array(), 'product_group_id');
     }
     if (!empty($in_groups)) {
         $groups = '(' . implode(',', array_keys($in_groups)) . ')';
         $products_assigned = Model_Product_To_Groups::find(function ($query) use($groups) {
             $query->where('group_id', 'IN', \DB::expr($groups));
             return $query;
         }, 'product_id');
     }
     if (!empty($not_in_groups)) {
         $groups = '(' . implode(',', array_keys($not_in_groups)) . ')';
         $products_not_assigned = Model_Product_To_Groups::find(function ($query) use($groups) {
             $query->where('group_id', 'IN', \DB::expr($groups));
             return $query;
         }, 'product_id');
     }
     $out['assigned'] = $products_assigned;
     $out['not_assigned'] = $products_not_assigned;
     return $out;
 }