GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 764334...261594 )
by Alexey
33:15
created

ProductList::setAssignment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
ccs 10
cts 10
cp 1
crap 2
1
<?php
2
/**
3
 * @author Sergey Glagolev <[email protected]>
4
 * @link https://github.com/shogodev/argilla/
5
 * @copyright Copyright &copy; 2003-2014 Shogo
6
 * @license http://argilla.ru/LICENSE
7
 * @package frontend.models.product
8
 *
9
 * @property CDbCriteria $criteria
10
 */
11
class ProductList extends BaseList
12
{
13
  public $parameterNameModel = 'ProductParameterName';
14
15
  public $parameterModel = 'ProductParameter';
16
17
  public static $sortingRange = array(
18
    'popular_up' => 'IF(t.position=0, 1, 0), t.position DESC, dump DESC, IF(price=0, 1, 0)',
19
    'popular_down' => 'IF(t.position=0, 1, 0), t.position ASC, dump DESC, IF(price=0, 1, 0)',
20
    'price_up' => 'IF(price=0, 1, 0), price ASC',
21
    'price_down' => 'IF(price=0, 1, 0), price DESC',
22
    'name_up' => 'name ASC',
23
    'name_down' => 'name DESC',
24
    'available_up' => 'dump DESC',
25
    'available_down' => 'dump ASC',
26
  );
27
28
  /**
29
   * @var CDbCriteria
30
   */
31
  public $parametersCriteria;
32
33
  public $autoloadModifications = false;
34
35 2
  public function init()
36
  {
37 2
    $this->criteria->compare('t.visible', 1);
38 2
    $this->criteria->addCondition('t.parent IS NULL');
39 2
    ProductAssignment::model()->addAssignmentCondition($this->criteria);
40
41 2
    $prefix = $this->getTablePrefix();
42 2
    $this->parametersCriteria = new CDbCriteria();
43 2
    $this->parametersCriteria->addColumnCondition(array($prefix.'.section' => 1, $prefix.'.section_list' => 1, $prefix.'.key' => ProductParameter::BASKET_KEY), 'OR');
44 2
  }
45
46 2
  protected function afterFetchData($event)
47
  {
48 2
    $this->setImages();
49 2
    $this->setParameters();
50 2
    $this->setAssignments();
51 2
    if( $this->autoloadModifications )
52 2
      $this->setModifications();
53 2
  }
54
55 2
  protected function setParameters()
56
  {
57 2
    $criteria = new CDbCriteria();
58 2
    $criteria->select = 'param_id';
59 2
    $criteria->distinct = true;
60 2
    $criteria->addInCondition('product_id', $this->dataProvider->getKeys());
61
62 2
    $command = Yii::app()->db->commandBuilder->createFindCommand(ProductParameter::model()->tableName(), $criteria);
63 2
    $usedProductParameterIds = $command->queryColumn();
64
65 2
    $this->parametersCriteria->addInCondition('t.id', $usedProductParameterIds);
66
67 2
    $modelName = $this->parameterNameModel;
68 2
    $names = $modelName::model()->search($this->parametersCriteria);
69 2
    $parameters = array();
70
71
    /**
72
     * @var $products Product[]
73
     */
74 2
    $products = $this->getDataProvider()->getData();
75 2
    foreach($products as $product)
76
    {
77 2
      $product->setParameters();
78
79 2
      foreach($names as $name)
80
      {
81 2
        $productParameterName = clone $name;
82 2
        $productParameterName->setProductId($product->id);
83 2
        $product->addParameter($productParameterName);
84 2
        $parameters[] = $productParameterName;
85 2
      }
86 2
    }
87
88 2
    $modelName = $this->parameterModel;
89 2
    $modelName::model()->setParameterValues($parameters);
90 2
  }
91
92 2
  protected function setAssignments()
93
  {
94 2
    $assignments = array('section', 'type', 'category', 'collection');
95 2
    $criteria = new CDbCriteria(array('select' => 'a.product_id'));
96 2
    $criteria->addInCondition('product_id', $this->dataProvider->getKeys(true));
97 2
    $productAssignments = ProductAssignment::model()->getAssignments($criteria);
98
99 2
    foreach($assignments as $assignment)
100
    {
101 2
      $this->setAssignment($productAssignments, $assignment);
102 2
    }
103 2
  }
104
105 2
  protected function setAssignment($productAssignments, $modelName)
106
  {
107 2
    $models = array();
108 2
    $assignments = CHtml::listData($productAssignments, 'product_id', $modelName.'_id');
109 2
    $keys = array_unique(array_values($assignments));
110 2
    $records = $this->findRecords('id', 'Product'.ucfirst($modelName), $keys, new CDbCriteria(array('index' => 'id')));
111
112 2
    foreach($assignments as $product => $assignment)
113
    {
114 2
      $models[$product] = Arr::get($records, $assignment, null);
115 2
    }
116
117 2
    $this->setRecords($modelName, $models);
118 2
  }
119
120 2
  protected function setModifications()
121
  {
122
    /**
123
     * @var Product[] $productLinks
124
     */
125
    $productLinks = array();
126
127
    foreach($this->dataProvider->getData() as $product)
128
    {
129 1
      $productLinks[$product->id] = $product;
130 2
    }
131
132 2
    $criteria = new CDbCriteria();
133
    $criteria->addInCondition('t.parent', $this->dataProvider->getKeys());
134
    $modificationList = new ProductModificationList($criteria, null, false);
135
136
    foreach($modificationList->getDataProvider()->getData() as $modification)
137
    {
138
      $productLinks[$modification->parent]->addRelatedRecord('modifications', $modification, true);
139
    }
140
  }
141
}