1
|
|
|
<?php
|
|
|
|
|
2
|
|
|
/**
|
3
|
|
|
* @author Alexey Tatarinov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2016 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
*/
|
8
|
1 |
|
Yii::import('frontend.share.components.ManyRecordInserter');
|
9
|
1 |
|
Yii::import('backend.modules.product.components.FacetPropertyIterator');
|
10
|
1 |
|
Yii::import('backend.modules.product.components.FacetModificationPropertyIterator');
|
11
|
1 |
|
Yii::import('backend.modules.product.components.FacetParameterIterator');
|
12
|
|
|
|
13
|
|
|
/**
|
14
|
|
|
* Class FacetIndexer
|
15
|
|
|
* @mixin CurrentTransactionBehavior
|
16
|
|
|
*/
|
17
|
|
|
class FacetIndexer extends CComponent
|
18
|
|
|
{
|
19
|
|
|
public $indexTable = '{{faceted_search}}';
|
20
|
|
|
|
21
|
|
|
public $modificationList = array();
|
22
|
|
|
|
23
|
|
|
/**
|
24
|
|
|
* @var CDbCommandBuilder
|
25
|
|
|
*/
|
26
|
|
|
private $builder;
|
27
|
|
|
|
28
|
|
|
/**
|
29
|
|
|
* @var ManyRecordInserter
|
30
|
|
|
*/
|
31
|
|
|
private $manyRecordInserter;
|
32
|
|
|
|
33
|
|
|
private $chunkSize;
|
34
|
|
|
|
35
|
24 |
|
public function __construct($chunkSize = 25000)
|
36
|
|
|
{
|
37
|
24 |
|
$this->chunkSize = $chunkSize;
|
38
|
24 |
|
$this->builder = Yii::app()->db->commandBuilder;
|
39
|
24 |
|
$this->manyRecordInserter = new ManyRecordInserter($this->indexTable, $this->chunkSize);
|
40
|
24 |
|
$this->manyRecordInserter->attachEventHandler('onSave', array($this, 'onSaveRecords'));
|
41
|
|
|
|
42
|
24 |
|
$this->attachBehaviors($this->behaviors());
|
43
|
24 |
|
}
|
44
|
|
|
|
45
|
24 |
|
public function behaviors()
|
46
|
|
|
{
|
47
|
|
|
return array(
|
48
|
24 |
|
'currentTransactionBehavior' => array('class' => 'frontend.share.behaviors.CurrentTransactionBehavior')
|
49
|
24 |
|
);
|
50
|
|
|
}
|
51
|
|
|
|
52
|
4 |
|
public function reindexProducts(array $productIdList = null)
|
53
|
|
|
{
|
54
|
|
|
try
|
55
|
|
|
{
|
56
|
4 |
|
$this->beginTransaction();
|
57
|
|
|
|
58
|
4 |
|
$this->clearIndexByProductIdList($productIdList);
|
59
|
|
|
|
60
|
4 |
|
$propertyList = BFacetedParameter::model()->getProperties();
|
61
|
4 |
|
$this->indexIterator(new FacetPropertyIterator($propertyList, $this->chunkSize, $productIdList));
|
62
|
4 |
|
$this->indexIterator(new FacetModificationPropertyIterator($propertyList, $this->chunkSize, $productIdList));
|
63
|
|
|
|
64
|
4 |
|
$parameterList = BFacetedParameter::model()->getParameters();
|
65
|
4 |
|
$this->indexIterator(new FacetParameterIterator($parameterList, $this->chunkSize, $productIdList));
|
66
|
|
|
|
67
|
4 |
|
$this->manyRecordInserter->save(true);
|
68
|
|
|
|
69
|
4 |
|
$this->commitTransaction();
|
70
|
|
|
}
|
71
|
4 |
|
catch(Exception $e)
|
72
|
|
|
{
|
73
|
|
|
$this->rollbackTransaction();
|
74
|
|
|
|
75
|
|
|
throw $e;
|
76
|
|
|
}
|
77
|
4 |
|
}
|
78
|
|
|
|
79
|
|
|
public function reindexAll()
|
80
|
|
|
{
|
81
|
|
|
$this->reindexProducts();
|
82
|
|
|
}
|
83
|
|
|
|
84
|
|
|
public function clearIndexByParameterNameIdList(array $parameterNameIdList)
|
85
|
|
|
{
|
86
|
|
|
$criteria = new CDbCriteria();
|
87
|
|
|
$criteria->addInCondition('param_id', $parameterNameIdList);
|
88
|
|
|
|
89
|
|
|
$this->clearIndex($criteria);
|
90
|
|
|
}
|
91
|
|
|
|
92
|
4 |
|
public function clearIndexByProductIdList(array $productIdList = null)
|
93
|
|
|
{
|
94
|
4 |
|
$criteria = new CDbCriteria();
|
95
|
|
|
|
96
|
4 |
|
if( !empty($productIdList) )
|
97
|
4 |
|
$criteria->addInCondition('product_id', $productIdList);
|
98
|
|
|
|
99
|
4 |
|
$this->clearIndex($criteria);
|
100
|
4 |
|
}
|
101
|
|
|
|
102
|
4 |
|
public function clearIndex(CDbCriteria $criteria = null)
|
103
|
|
|
{
|
104
|
4 |
|
$criteria = is_null($criteria) ? new CDbCriteria() : $criteria;
|
105
|
|
|
|
106
|
4 |
|
$this->builder->createDeleteCommand($this->indexTable, $criteria)->execute();
|
107
|
4 |
|
}
|
108
|
|
|
|
109
|
4 |
|
public function onSaveRecords(CEvent $event)
|
110
|
|
|
{
|
111
|
4 |
|
$this->raiseEvent('onSaveRecords', $event);
|
112
|
4 |
|
}
|
113
|
|
|
|
114
|
4 |
|
protected function indexIterator(SqlIterator $iterator)
|
115
|
|
|
{
|
116
|
4 |
|
foreach($iterator as $attributes)
|
117
|
|
|
{
|
118
|
3 |
|
foreach($attributes as $attribute)
|
119
|
|
|
{
|
120
|
|
|
if( empty($attribute) )
|
121
|
|
|
return;
|
122
|
|
|
|
123
|
|
|
$this->manyRecordInserter->addAttributes($attribute);
|
124
|
|
|
$this->manyRecordInserter->save();
|
125
|
3 |
|
}
|
126
|
4 |
|
}
|
127
|
|
|
}
|
128
|
|
|
} |
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.