Index: branches/5.2.x/units/products/products_event_handler.php =================================================================== diff -u -N -r14849 -r14908 --- branches/5.2.x/units/products/products_event_handler.php (.../products_event_handler.php) (revision 14849) +++ branches/5.2.x/units/products/products_event_handler.php (.../products_event_handler.php) (revision 14908) @@ -1,6 +1,6 @@ Array('self' => true), 'OnClearRecent' => Array('self' => true), 'OnRecommendProduct' => Array('self' => true), + 'OnAddToCompare' => Array('self' => true), + 'OnRemoveFromCompare' => Array('self' => true), + 'OnCancelCompare' => Array('self' => true), // admin 'OnQtyAdd' => Array('self' => 'add|edit'), @@ -458,79 +461,108 @@ */ function getTypeClauses(&$event) { - $types=$event->getEventParam('types'); - $except_types=$event->getEventParam('except'); + $types = $event->getEventParam('types'); + $types = $types ? explode(',', $types) : Array (); + + $except_types = $event->getEventParam('except'); + $except_types = $except_types ? explode(',', $except_types) : Array (); + $object =& $event->getObject(); + /* @var $object kDBList */ + $type_clauses = parent::getTypeClauses($event); - $type_clauses['featured']['include']='%1$s.Featured=1 AND '.TABLE_PREFIX.'CategoryItems.PrimaryCat = 1'; - $type_clauses['featured']['except']='%1$s.Featured!=1 AND '.TABLE_PREFIX.'CategoryItems.PrimaryCat = 1'; - $type_clauses['featured']['having_filter']=false; + $type_clauses['featured']['include'] = '%1$s.Featured = 1 AND ' . TABLE_PREFIX . 'CategoryItems.PrimaryCat = 1'; + $type_clauses['featured']['except'] = '%1$s.Featured != 1 AND ' . TABLE_PREFIX . 'CategoryItems.PrimaryCat = 1'; + $type_clauses['featured']['having_filter'] = false; - $type_clauses['onsale']['include']='%1$s.OnSale=1 AND '.TABLE_PREFIX.'CategoryItems.PrimaryCat = 1'; - $type_clauses['onsale']['except']='%1$s.OnSale!=1 AND '.TABLE_PREFIX.'CategoryItems.PrimaryCat = 1'; - $type_clauses['onsale']['having_filter']=false; + $type_clauses['onsale']['include'] = '%1$s.OnSale = 1 AND ' . TABLE_PREFIX . 'CategoryItems.PrimaryCat = 1'; + $type_clauses['onsale']['except'] = '%1$s.OnSale != 1 AND ' . TABLE_PREFIX . 'CategoryItems.PrimaryCat = 1'; + $type_clauses['onsale']['having_filter'] = false; // products from selected manufacturer: begin $manufacturer = $event->getEventParam('manufacturer'); if ( !$manufacturer ) { $manufacturer = $this->Application->GetVar('manuf_id'); } + if ( $manufacturer ) { - $type_clauses['manufacturer']['include'] = '%1$s.ManufacturerId='.$manufacturer.' AND PrimaryCat = 1'; - $type_clauses['manufacturer']['except'] = '%1$s.ManufacturerId!='.$manufacturer.' AND PrimaryCat = 1'; + $type_clauses['manufacturer']['include'] = '%1$s.ManufacturerId = ' . $manufacturer . ' AND PrimaryCat = 1'; + $type_clauses['manufacturer']['except'] = '%1$s.ManufacturerId != ' . $manufacturer . ' AND PrimaryCat = 1'; $type_clauses['manufacturer']['having_filter'] = false; } // products from selected manufacturer: end // recent products: begin $recent = $this->Application->RecallVar('recent_products'); - if ($recent) { + if ( $recent ) { $recent = unserialize($recent); - $type_clauses['recent']['include'] = '%1$s.ProductId IN ('.implode(',', $recent).') AND PrimaryCat = 1'; - $type_clauses['recent']['except'] = '%1$s.ProductId NOT IN ('.implode(',', $recent).') AND PrimaryCat = 1'; + $type_clauses['recent']['include'] = '%1$s.ProductId IN (' . implode(',', $recent) . ') AND PrimaryCat = 1'; + $type_clauses['recent']['except'] = '%1$s.ProductId NOT IN (' . implode(',', $recent) . ') AND PrimaryCat = 1'; } else { - $type_clauses['recent']['include']='0'; - $type_clauses['recent']['except']='1'; + $type_clauses['recent']['include'] = '0'; + $type_clauses['recent']['except'] = '1'; } - $type_clauses['recent']['having_filter']=false; + $type_clauses['recent']['having_filter'] = false; // recent products: end + // compare products: begin + $compare_products = $this->getCompareProducts(); + if ( $compare_products ) { + $compare_products = $this->Conn->qstrArray($compare_products); + $type_clauses['compare']['include'] = '%1$s.ProductId IN (' . implode(',', $compare_products) . ') AND PrimaryCat = 1'; + $type_clauses['compare']['except'] = '%1$s.ProductId NOT IN (' . implode(',', $compare_products) . ') AND PrimaryCat = 1'; + } + else { + $type_clauses['compare']['include'] = '0'; + $type_clauses['compare']['except'] = '1'; + } + $type_clauses['compare']['having_filter'] = false; + // compare products: end + // products already in shopping cart: begin - if (strpos($types, 'in_cart') !== false || strpos($except_types, 'in_cart') !== false) { + if ( in_array('in_cart', $types) || in_array('in_cart', $except_types) ) { $order_id = $this->Application->RecallVar('ord_id'); - if ($order_id) { - $in_cart = $this->Conn->GetCol('SELECT ProductId FROM '.TABLE_PREFIX.'OrderItems WHERE OrderId = '.$order_id); - if ($in_cart) { - $type_clauses['in_cart']['include'] = '%1$s.ProductId IN ('.implode(',', $in_cart).') AND PrimaryCat = 1'; - $type_clauses['in_cart']['except'] = '%1$s.ProductId NOT IN ('.implode(',', $in_cart).') AND PrimaryCat = 1'; + + if ( $order_id ) { + $sql = 'SELECT ProductId + FROM ' . TABLE_PREFIX . 'OrderItems + WHERE OrderId = ' . $order_id; + $in_cart = $this->Conn->GetCol($sql); + + if ( $in_cart ) { + $type_clauses['in_cart']['include'] = '%1$s.ProductId IN (' . implode(',', $in_cart) . ') AND PrimaryCat = 1'; + $type_clauses['in_cart']['except'] = '%1$s.ProductId NOT IN (' . implode(',', $in_cart) . ') AND PrimaryCat = 1'; } else { - $type_clauses['in_cart']['include']='0'; - $type_clauses['in_cart']['except']='1'; + $type_clauses['in_cart']['include'] = '0'; + $type_clauses['in_cart']['except'] = '1'; } } else { - $type_clauses['in_cart']['include']='0'; - $type_clauses['in_cart']['except']='1'; + $type_clauses['in_cart']['include'] = '0'; + $type_clauses['in_cart']['except'] = '1'; } - $type_clauses['in_cart']['having_filter']=false; + + $type_clauses['in_cart']['having_filter'] = false; } // products already in shopping cart: end // my downloadable products: begin - if (strpos($types, 'my_downloads') !== false || strpos($except_types, 'my_downloads') !== false) - { + if ( in_array('my_downloads', $types) || in_array('my_downloads', $except_types) ) { $user_id = $this->Application->RecallVar('user_id'); - $my_downloads = ($user_id > 0) ? $this->Conn->GetCol('SELECT ProductId FROM '.TABLE_PREFIX.'UserFileAccess WHERE PortalUserId = '.$user_id) : false; - if ($my_downloads) - { - $type_clauses['my_downloads']['include'] = '%1$s.ProductId IN ('.implode(',', $my_downloads).') AND PrimaryCat = 1'; - $type_clauses['my_downloads']['except'] = '%1$s.ProductId NOT IN ('.implode(',', $my_downloads).') AND PrimaryCat = 1'; + + $sql = 'SELECT ProductId + FROM ' . TABLE_PREFIX . 'UserFileAccess + WHERE PortalUserId = ' . $user_id; + $my_downloads = $user_id > 0 ? $this->Conn->GetCol($sql) : false; + + if ( $my_downloads ) { + $type_clauses['my_downloads']['include'] = '%1$s.ProductId IN (' . implode(',', $my_downloads) . ') AND PrimaryCat = 1'; + $type_clauses['my_downloads']['except'] = '%1$s.ProductId NOT IN (' . implode(',', $my_downloads) . ') AND PrimaryCat = 1'; } - else - { + else { $type_clauses['my_downloads']['include'] = '0'; $type_clauses['my_downloads']['except'] = '1'; } @@ -540,51 +572,54 @@ // my downloadable products: end // my favorite products: begin - if (strpos($types, 'wish_list') !== false || strpos($except_types, 'wish_list') !== false) { - $sql = 'SELECT ResourceId FROM '.$this->Application->getUnitOption('fav', 'TableName').' - WHERE PortalUserId = '.(int)$this->Application->RecallVar('user_id'); + if ( in_array('wish_list', $types) || in_array('wish_list', $except_types) ) { + $sql = 'SELECT ResourceId + FROM ' . $this->Application->getUnitOption('fav', 'TableName') . ' + WHERE PortalUserId = ' . (int)$this->Application->RecallVar('user_id'); $wishlist_ids = $this->Conn->GetCol($sql); - if ($wishlist_ids) { - $type_clauses['wish_list']['include'] = '%1$s.ResourceId IN ('.implode(',', $wishlist_ids).') AND PrimaryCat = 1'; - $type_clauses['wish_list']['except'] = '%1$s.ResourceId NOT IN ('.implode(',', $wishlist_ids).') AND PrimaryCat = 1'; + + if ( $wishlist_ids ) { + $type_clauses['wish_list']['include'] = '%1$s.ResourceId IN (' . implode(',', $wishlist_ids) . ') AND PrimaryCat = 1'; + $type_clauses['wish_list']['except'] = '%1$s.ResourceId NOT IN (' . implode(',', $wishlist_ids) . ') AND PrimaryCat = 1'; } else { - $type_clauses['wish_list']['include']='0'; - $type_clauses['wish_list']['except']='1'; + $type_clauses['wish_list']['include'] = '0'; + $type_clauses['wish_list']['except'] = '1'; } - $type_clauses['wish_list']['having_filter']=false; + + $type_clauses['wish_list']['having_filter'] = false; } // my favorite products: end // products from package: begin - if (strpos($types, 'content') !== false) { + if ( in_array('content', $types) || in_array('content', $except_types) ) { $object->removeFilter('category_filter'); $object->AddGroupByField('%1$s.ProductId'); - $item_type = $this->Application->getUnitOption('p', 'ItemType'); + $object_product =& $this->Application->recallObject($event->Prefix); + /* @var $object_product ProductsItem */ - $object_product = &$this->Application->recallObject($event->Prefix); - $content_ids_array = $object_product->GetPackageContentIds(); - if (sizeof($content_ids_array)==0) { - $content_ids_array = array('-1'); + if ( sizeof($content_ids_array) == 0 ) { + $content_ids_array = array ('-1'); } - if (sizeof($content_ids_array)>0) { - $type_clauses['content']['include'] = '%1$s.ProductId IN ('.implode(',', $content_ids_array).')'; + if ( sizeof($content_ids_array) > 0 ) { + $type_clauses['content']['include'] = '%1$s.ProductId IN (' . implode(',', $content_ids_array) . ')'; } else { - $type_clauses['content']['include']='0'; + $type_clauses['content']['include'] = '0'; } - $type_clauses['related']['having_filter']=false; + + $type_clauses['related']['having_filter'] = false; } // products from package: end $object->addFilter('not_virtual', '%1$s.Virtual = 0'); - if (!$this->Application->isAdminUser) { - $object->addFilter('expire_filter', '%1$s.Expire IS NULL OR %1$s.Expire > UNIX_TIMESTAMP()'); + if ( !$this->Application->isAdminUser ) { + $object->addFilter('expire_filter', '%1$s.Expire IS NULL OR %1$s.Expire > ' . adodb_mktime()); } return $type_clauses; @@ -1390,4 +1425,68 @@ $this->Application->setUnitOption($event->MasterEvent->Prefix, 'Clones', $clones); } } + + /** + * Adds product to comparison list + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnAddToCompare(kEvent &$event) + { + $products = $this->getCompareProducts(); + + $product_id = (int)$this->Application->GetVar($event->Prefix . '_id'); + + if ( $product_id ) { + $products[] = $product_id; + $this->Application->Session->SetCookie('compare_products', implode('|', array_unique($products))); + } + } + + /** + * Adds product to comparison list + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnRemoveFromCompare(kEvent &$event) + { + $products = $this->getCompareProducts(); + + $product_id = (int)$this->Application->GetVar($event->Prefix . '_id'); + + if ( $product_id && in_array($product_id, $products) ) { + $products = array_diff($products, Array ($product_id)); + $this->Application->Session->SetCookie('compare_products', implode('|', array_unique($products))); + } + } + + /** + * Cancels product compare + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnCancelCompare(kEvent &$event) + { + $this->Application->Session->SetCookie('compare_products', '', -1); + } + + /** + * Returns products, that needs to be compared with each other + * + * @return Array + * @access protected + */ + protected function getCompareProducts() + { + $products = $this->Application->GetVarDirect('compare_products', 'Cookie'); + $products = $products ? explode('|', $products) : Array (); + + return $products; + } } \ No newline at end of file