Skip to content

Instantly share code, notes, and snippets.

@Fi1osof
Last active December 20, 2015 14:49
Show Gist options
  • Save Fi1osof/d5a4dd427ee31b568e01 to your computer and use it in GitHub Desktop.
Save Fi1osof/d5a4dd427ee31b568e01 to your computer and use it in GitHub Desktop.
<?php
require_once dirname(dirname(dirname(__FILE__))).'/getdata.class.php';
class modWebCatalogProductsGetdataProcessor extends ShopmodxWebGetDataProcessor {
protected $key = '';
public $classKey = 'ShopmodxCatalogCache';
public function initialize(){
$this->setDefaultProperties(array(
'sort' => "",
'dir' => 'ASC',
'getPage' => false,
'limit' => 32,
'page' => !empty($_REQUEST['page']) ? (int)$_REQUEST['page'] : 0,
));
if($page = $this->getProperty('page') AND $page > 1 AND $limit = $this->getProperty('limit', 0)){
$this->setProperty('start', ($page-1) * $limit);
}
// Готовим ключ для кеша конечных данных
$this->prepareCacheKey();
return parent::initialize();
}
protected function prepareCacheKey(){
$this->key = 'catalog/category/'. md5(print_r($this->getProperties(), true));
}
public function process() {
if(!$result = $this->modx->cacheManager->get($this->key)){
$result = parent::process();
if(!empty($result['success'])){
$this->modx->cacheManager->set($this->key, $result);
}
}
else{
$result = $this->outputArray($result['object'], $result['total']);
}
return $result;
}
public function getData() {
$data = array(
'total' => 0,
'results' => array(),
);
$c = $this->modx->newQuery($this->classKey);
$c = $this->prepareQueryBeforeCount($c);
if(!$c = $this->getCount($c)){
return $data;
}
$c = $this->prepareQueryAfterCount($c);
$this->setSelection($c);
$data['total'] = $this->total;
$data['results'] = $this->getResults($c);
return $data;
}
public function prepareQueryBeforeCount(xPDOQuery $c) {
$c = parent::prepareQueryBeforeCount($c);
if($where = (array)$this->getProperty('where')){
$c->where($where);
}
$c->distinct();
$c->select(array(
"model_id",
"model_pagetitle",
"model_uri",
));
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')));
}
$query = clone $c;
$query = $this->prepareCountQuery($query);
if(!$this->total = $this->countTotal($this->classKey,$query)){
return false;
}
if($sortKey){
$c->sortby($sortKey,$this->getProperty('dir'));
}
$limit = intval($this->getProperty('limit'));
$start = intval($this->getProperty('start'));
if ($limit > 0) {
$c->limit($limit,$start);
}
return $c;
}
protected function prepareCountQuery(xPDOQuery & $query){
return $query;
}
protected function countTotal($className, xPDOQuery & $query){
$total = 0;
$key = 'catalog/category/total/'. md5(print_r($c->query['where'], true));
if(!$total = $this->modx->cacheManager->get($key)){
if (isset($query->query['columns'])) $query->query['columns'] = array();
$query->select(array("count(distinct model_id)"));
$query->prepare();
if($total = $this->modx->getValue($query->stmt)){
$this->modx->cacheManager->set($key, $total);
}
}
return $total;
}
protected function getResults(xPDOQuery & $c){
$data = array();
if($c->prepare() && $c->stmt->execute()){
$data = $c->stmt->fetchAll(PDO::FETCH_ASSOC);
}
return $data;
}
public function iterate(array $data) {
$list = array();
$list = $this->beforeIteration($list);
$this->currentIndex = 0;
foreach ($data['results'] as $row) {
$object_id = $row['object_id'];
if(empty($list[$object_id])){
$list[$object_id] = $row;
$list[$object_id]['tvs'] = array();
}
if(!empty($row['tv_name'])){
$list[$object_id]['tvs'][$row['tv_name']] = array(
'tv_id' => $row['tv_id'],
'value_id' => $row['tv_value_id'],
'value' => $row['tv_value'],
);
}
}
$list = $this->afterIteration($list);
return $list;
}
protected function setSelection(xPDOQuery $c) {
$c->select(array(
"model_id as object_id",
));
return $c;
}
public function outputArray(array $array, $count = false) {
if($this->getProperty('getPage') AND $limit = $this->getProperty('limit')){
$this->modx->setPlaceholder('total', $count);
$this->modx->runSnippet('getPage@getPage', array(
'limit' => $limit,
));
}
return parent::outputArray($array, $count);
}
}
return 'modWebCatalogProductsGetdataProcessor';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment