-
-
Save Fi1osof/c944711fbeff9badf534 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Класс выборки товаров | |
*/ | |
require_once dirname(dirname(dirname(__FILE__))).'/resources/getdata.class.php'; | |
class modWebCatalogProductsGetdataProcessor extends modWebResourcesGetdataProcessor{ | |
public function initialize(){ | |
$this->setDefaultProperties(array( | |
'hot' => false, // Получать новинки | |
"sort" => "sm_price", | |
"dir" => "ASC", | |
)); | |
$query = $this->getProperty('query', null); | |
if(isset($query)){ | |
if(mb_strlen($query, 'utf-8') < 3){ | |
return "Укажите минимум 3 символа"; | |
} | |
} | |
$this->setProperties(array( | |
"base_currency_id" => (int)$this->modx->getOption("shopmodx.default_currency"), // ID базовой валюты магазина | |
)); | |
return parent::initialize(); | |
} | |
public function prepareQueryBeforeCount(xPDOQuery $c) { | |
# print '<pre>'; | |
# print_r($this->properties); | |
# print '</pre>'; | |
$c = parent::prepareQueryBeforeCount($c); | |
$alias = $c->getAlias(); | |
$c->innerJoin('ShopmodxProduct', 'Product'); | |
$base_currency_id = (int)$this->getProperty('base_currency_id'); | |
// Курс валюты | |
$c->leftJoin('modResource', "base_currency_doc", "base_currency_doc.id = {$base_currency_id}"); | |
$c->leftJoin('modResource', "currency_doc", "currency_doc.id = Product.sm_currency"); | |
$c->leftJoin('modTemplateVarResource', "course_tv", "course_tv.contentid = Product.sm_currency"); | |
$columns = explode(",", $this->modx->getSelectColumns('ShopmodxProduct', "Product", null, array( | |
'sm_price', | |
'sm_currency', | |
), true)); | |
$columns = array_merge($columns, array( | |
"Product.id as `product_id`", | |
# "Parent.id as category_id", | |
# "Parent.uri as category_uri", | |
# "Parent.pagetitle as category_title", | |
"course_tv.value as course", | |
# "if({$base_currency_id} != Product.sm_currency && course_tv.value, round(Product.sm_price * course_tv.value, 2), Product.sm_price) as a_sm_price", | |
"if({$base_currency_id} != Product.sm_currency && course_tv.value, round(Product.sm_price * course_tv.value, 2), Product.sm_price) as sm_price", | |
"if({$base_currency_id} != Product.sm_currency && course_tv.value, {$base_currency_id}, Product.sm_currency) as sm_currency", | |
"if({$base_currency_id} != Product.sm_currency && course_tv.value, base_currency_doc.pagetitle, currency_doc.pagetitle) as currency_code", | |
)); | |
$c->select($columns); | |
# print $base_currency_id; | |
# exit; | |
$where = array(); | |
if($this->getProperty('hot')){ | |
$c->innerJoin('modTemplateVarResource', 'hot', "hot.contentid = {$this->classKey}.id AND hot.tmplvarid = 8 AND hot.value='1'"); | |
} | |
// Производитель | |
$c->leftJoin('modTemplateVarResource', 'tv_brand', "tv_brand.contentid = {$this->classKey}.id AND tv_brand.tmplvarid = 50"); | |
$c->leftJoin('modResource', 'Brand', "Brand.deleted = 0 AND tv_brand.value = Brand.id"); | |
if($brand = $this->getProperty('brand')){ | |
if(!is_array($brand)){ | |
$brand = array_map('intval', explode(',', $brand)); | |
} | |
if(count($brand) > 1){ | |
$where['Brand.id:in'] = $brand; | |
} | |
else{ | |
$where['Brand.id'] = (int)current($brand); | |
} | |
# print_r($brand); | |
# exit; | |
} | |
/* | |
Диапазон цен | |
*/ | |
# if( | |
# $prices = $this->getProperty('prices') | |
# AND $prices = explode(",", $prices) | |
# AND count($prices) == 2 | |
# ){ | |
# $c->where(array( | |
# "sm_price between {$prices[0]} AND {$prices[1]}", | |
# )); | |
# | |
# # $c->prepare(); | |
# # print $c->toSQL(); | |
# # exit; | |
# } | |
# $this->modx->log(1, print_r($this->properties, 1)); | |
if($where){ | |
$c->where($where); | |
# $c->prepare(); | |
# print $c->toSQL(); | |
# exit; | |
} | |
// Поиск товаров в категории и подкатегориях | |
if($category_id = (int)$this->getProperty('category_id')){ | |
$categories = array($category_id); | |
$this->getCategories($category_id, $categories); | |
$c->where(array( | |
"parent:IN" => $categories, | |
)); | |
} | |
return $c; | |
} | |
protected function getCount(xPDOQuery & $c){ | |
if(!$sortKey = $this->getProperty('sort')){ | |
$sortClassKey = $this->getSortClassKey(); | |
$sortKey = $this->modx->getSelectColumns($sortClassKey,$this->getProperty('sortAlias',$sortClassKey),'',array($this->getProperty('sort'))); | |
} | |
$c = $this->prepareCountQuery($c); | |
$cc = clone $c; | |
$alias = $cc->getAlias(); | |
$cc->select(array( | |
"{$alias}.id as object_id", | |
)); | |
# $c->groupby('sm_price'); | |
$cc->prepare(); | |
$sql = "SELECT count(distinct object_id) FROM ({$cc->toSQL()}) as t "; | |
if( | |
$prices = $this->getProperty('prices') | |
AND $prices = explode(",", $prices) | |
AND count($prices) == 2 | |
){ | |
# $c->where(array( | |
# "a_sm_price between {$prices[0]} AND {$prices[1]}", | |
# )); | |
$sql .= " WHERE sm_price between {$prices[0]} AND {$prices[1]}"; | |
# print $c->toSQL(); | |
# exit; | |
} | |
# $s = $c->prepare(); | |
# print $sql; | |
$s = $this->modx->prepare($sql); | |
# print $c->toSQL(); | |
$s->execute(); | |
if($s->errorCode() !== "00000"){ | |
$this->modx->log(xPDO::LOG_LEVEL_ERROR, __CLASS__); | |
$this->modx->log(xPDO::LOG_LEVEL_ERROR, print_r($s->errorInfo(), true)); | |
$this->modx->log(xPDO::LOG_LEVEL_ERROR, $sql); | |
} | |
# print_r($s->errorInfo()); | |
# | |
# exit; | |
# if(!$this->total = $this->countTotal($this->classKey,$c)){ | |
# return false; | |
# } | |
if(!$this->total = current($s->fetch(PDO::FETCH_NUM))){ | |
return false; | |
} | |
# print $this->total; | |
# | |
# exit; | |
$cc = $this->prepareQueryAfterCount($cc); | |
# if($sortKey){ | |
# $c->sortby($sortKey,$this->getProperty('dir')); | |
# } | |
$query = clone $cc; | |
$query->prepare(); | |
$sql = "SELECT distinct object_id as id FROM ({$query->toSQL()}) as t "; | |
if( | |
$prices = $this->getProperty('prices') | |
AND $prices = explode(",", $prices) | |
AND count($prices) == 2 | |
){ | |
# $c->where(array( | |
# "a_sm_price between {$prices[0]} AND {$prices[1]}", | |
# )); | |
$sql .= " WHERE sm_price between {$prices[0]} AND {$prices[1]}"; | |
# print $c->toSQL(); | |
# exit; | |
} | |
# print $cc->toSQL(); | |
# print $sql; | |
# | |
# exit; | |
if($sortKey){ | |
$dir = $this->getProperty('dir', "ASC"); | |
$c->sortby($sortKey,$this->getProperty('dir')); | |
$sql .= " ORDER BY {$sortKey} {$dir}"; | |
} | |
$limit = intval($this->getProperty('limit')); | |
$start = intval($this->getProperty('start')); | |
if ($limit || $start) { | |
# $query->limit($limit,$start); | |
$sql .= " LIMIT {$start}, {$limit}"; | |
} | |
# $query = $this->prepareUniqObjectsQuery($query); | |
if($stmt = $this->modx->prepare($sql) AND $stmt->execute() AND $rows = $stmt->fetchAll(PDO::FETCH_ASSOC)){ | |
$IDs = array(); | |
foreach($rows as $row){ | |
$IDs[] = $row['id']; | |
} | |
if ($this->flushWhere && isset($c->query['where'])) $c->query['where'] = array(); | |
$c->where(array( | |
"{$this->classKey}.id:IN" => $IDs, | |
)); | |
} | |
else{ | |
if($stmt AND $stmt->errorCode() !== "00000"){ | |
$this->modx->log(xPDO::LOG_LEVEL_ERROR, __CLASS__); | |
$this->modx->log(xPDO::LOG_LEVEL_ERROR, print_r($stmt->errorInfo(), true)); | |
$this->modx->log(xPDO::LOG_LEVEL_ERROR, $sql); | |
} | |
return false; | |
} | |
return $c; | |
} | |
protected function prepareCountQuery(xPDOQuery & $query){ | |
$query = parent::prepareCountQuery($query); | |
$where = array(); | |
if($word = $this->getProperty('query')){ | |
$query->where(array( | |
"pagetitle:like" => "%{$word}%", | |
"OR:longtitle:like" => "%{$word}%", | |
"OR:content:like" => "%{$word}%", | |
)); | |
} | |
if($where){ | |
$query->where($where); | |
} | |
return $query; | |
} | |
protected function setSelection(xPDOQuery $c) { | |
$c = parent::setSelection($c); | |
$c->leftJoin('modResource', "Parent"); | |
// Курс валюты | |
$c->leftJoin('modResource', "course_doc", "course_doc.id = Product.sm_currency"); | |
$c->leftJoin('modContextSetting', "setting", "setting.context_key = 'web' AND setting.key = course_doc.pagetitle"); | |
$c->leftJoin('docGallery', "gallery", "gallery.resource_id = {$this->classKey}.id"); | |
$c->select(array( | |
# "Product.*", | |
# "Product.id as `product_id`", | |
"Parent.id as category_id", | |
"Parent.uri as category_uri", | |
"Parent.pagetitle as category_title", | |
"if(setting.value, setting.value, course_tv.value) as course", | |
"gallery.src as image_src", | |
"Brand.id as brand_id", | |
"Brand.pagetitle as brand_pagetitle", | |
"Brand.uri as brand_uri", | |
// "setting.value as setting_currency_value", | |
)); | |
/*print '</pre>'; | |
$c->prepare(); | |
print $c->toSQL(); | |
exit;*/ | |
return $c; | |
} | |
public function afterIteration(array $list){ | |
$list = parent::afterIteration($list); | |
foreach($list as & $l){ | |
if( | |
!empty($l['course']) | |
AND $course = (float)$l['course'] | |
){ | |
} | |
else{ | |
$course = 1; | |
} | |
if(!isset($l['old_price'])){ | |
$l['old_price'] = null; | |
} | |
// Новая цена | |
if(!empty($l['tvs']['new_price']['value'])){ | |
$price_action_till = ''; | |
if(!empty($l['tvs']['price_action_till']['value'])){ | |
$price_action_till = strtotime($l['tvs']['price_action_till']['value']); | |
} | |
if(!$price_action_till OR $price_action_till > time()){ | |
$l['old_price'] = $l['sm_price']; | |
$l['sm_price'] = round($l['tvs']['new_price']['value'] * $course); | |
$l['price_action_till'] = $price_action_till; | |
} | |
} | |
# if( | |
# !empty($l['course']) | |
# AND $course = (float)$l['course'] | |
# ){ | |
# $l['sm_price'] = round($l['sm_price'] * $course, 0); | |
# $l['old_price'] = round($l['old_price'] * $course, 0); | |
# } | |
# else{ | |
# $course = 1; | |
# } | |
$l['variables'] = array(); | |
if(!empty($l['tvs']['variables']['value'])){ | |
$variables = array_map('trim', explode(",", $l['tvs']['variables']['value'])); | |
$l['variables'] = $variables; | |
} | |
/* | |
Цены | |
*/ | |
$l['prices'] = array(); | |
// Допцены из Migx | |
if( | |
!empty($l['tvs']['prices']['value']) | |
AND $prices = json_decode($l['tvs']['prices']['value'], true) | |
){ | |
foreach($prices as $price){ | |
$l['prices'][$price['MIGX_id']] = array( | |
"price" => $course ? round($price['price'] * $course, 2) : round($price['price'], 2), | |
"param" => $price['param'], | |
); | |
} | |
} | |
} | |
return $list; | |
} | |
protected function getCategories($parent, array & $categories){ | |
$q = $this->modx->newQuery('modResource', array( | |
'deleted' => 0, | |
'published' => 1, | |
'hidemenu' => 0, | |
'isfolder' => 1, | |
'parent' => $parent, | |
)); | |
$q->select(array( | |
'id', | |
)); | |
if($s = $q->prepare() AND $s->execute()){ | |
while($row = $s->fetch(PDO::FETCH_ASSOC)){ | |
$categories[] = $row['id']; | |
$this->getCategories($row['id'], $categories); | |
} | |
} | |
return $categories; | |
} | |
} | |
return 'modWebCatalogProductsGetdataProcessor'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment