1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* @author Sergey Glagolev <[email protected]> |
4
|
|
|
* @link https://github.com/shogodev/argilla/ |
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo |
6
|
|
|
* @license http://argilla.ru/LICENSE |
7
|
|
|
* @package frontend.models.xml |
8
|
|
|
*/ |
9
|
|
|
Yii::import('ext.retailcrm.components.product.RetailCrmProductList'); |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class RetailCrmDataProvider |
13
|
|
|
*/ |
14
|
|
|
class RetailCrmDataProvider |
15
|
|
|
{ |
16
|
|
|
public $catalogStructure = array('section'); |
17
|
|
|
|
18
|
|
|
protected $productCounter; |
19
|
|
|
|
20
|
|
|
protected $offersIterator; |
21
|
|
|
|
22
|
|
|
public function __construct(CDbCriteria $criteria) |
23
|
|
|
{ |
24
|
|
|
$this->criteria = $criteria; |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public function getShop() |
31
|
|
|
{ |
32
|
|
|
return array( |
33
|
|
|
'name' => 'relax-market.ru', |
34
|
|
|
'company' => 'Relax market', |
35
|
|
|
'url' => 'http://www.relax-market.ru/' |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function getCategories() |
43
|
|
|
{ |
44
|
|
|
$criteria = new CDbCriteria(); |
45
|
|
|
$criteria->addCondition('product.parent IS NULL OR product.parent=0'); |
46
|
|
|
$criteria->mergeWith($this->criteria); |
47
|
|
|
|
48
|
|
|
$baseStructureElement = Arr::get($this->catalogStructure, 0); |
49
|
|
|
|
50
|
|
|
$categories = array(); |
51
|
|
|
foreach(ProductAssignment::model()->getAssignments($criteria) as $assignment) |
52
|
|
|
{ |
53
|
|
|
if( empty($assignment[$baseStructureElement.'_id']) ) |
54
|
|
|
continue; |
55
|
|
|
|
56
|
|
|
$idElements = array(); |
57
|
|
|
foreach($this->catalogStructure as $structureElement) |
58
|
|
|
{ |
59
|
|
|
$idIndex = $structureElement.'_id'; |
60
|
|
|
$nameIndex = $structureElement.'_name'; |
61
|
|
|
|
62
|
|
|
if( !empty($assignment[$idIndex]) ) |
63
|
|
|
{ |
64
|
|
|
$idElements[] = $assignment[$idIndex]; |
65
|
|
|
$id = implode('_', $idElements); |
66
|
|
|
|
67
|
|
|
if( !isset($categories[$id]) ) |
68
|
|
|
{ |
69
|
|
|
$categories[$id] = array( |
70
|
|
|
'id' => $id, |
71
|
|
|
'name' => XmlHelper::escape($assignment[$nameIndex]) |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$parentIdElements = $idElements; |
75
|
|
|
array_pop($parentIdElements); |
76
|
|
|
if( !empty($parentIdElements) ) |
77
|
|
|
{ |
78
|
|
|
$categories[$id]['parent'] = $id = implode('_', $parentIdElements); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $categories; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getOffers() |
89
|
|
|
{ |
90
|
|
|
if( is_null($this->offersIterator) ) |
91
|
|
|
{ |
92
|
|
|
$productList = $this->getProductList(); |
93
|
|
|
$this->offersIterator = new OfferIterator($productList->getDataProvider(), 100); |
94
|
|
|
$this->offersIterator->productList = $productList; |
95
|
|
|
$this->offersIterator->buildCategoryCallback = array($this, 'buildCategory'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->offersIterator; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function buildCategory(Product $product) |
102
|
|
|
{ |
103
|
|
|
$idElements = array(); |
104
|
|
|
foreach($this->catalogStructure as $structureElement) |
105
|
|
|
{ |
106
|
|
|
if( !empty($product->{$structureElement}) ) |
107
|
|
|
$idElements[] = $product->{$structureElement}->id; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$categoryIds = array(implode('_', $idElements)); |
111
|
|
|
|
112
|
|
|
return $categoryIds; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return RetailCrmProductList |
117
|
|
|
*/ |
118
|
|
|
protected function getProductList() |
119
|
|
|
{ |
120
|
|
|
$criteria = new CDbCriteria(); |
121
|
|
|
$criteria->mergeWith($this->criteria); |
122
|
|
|
|
123
|
|
|
$productList = new ProductList($criteria, null, false, null); |
124
|
|
|
|
125
|
|
|
return $productList; |
126
|
|
|
} |
127
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.