1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Alexey Tatarinov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2015 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
*/
|
8
|
|
|
|
9
|
|
|
/**
|
10
|
|
|
* Class ChangeParameterWidget виджет меняет параметры элемента в корзине
|
11
|
|
|
* Пример подключения:
|
12
|
|
|
* $this->widget('ChangeParameterWidget', array('element' => $data, 'items' => $data->getPersons()))
|
13
|
|
|
*/
|
14
|
|
|
class ChangeParameterWidget extends CWidget
|
15
|
|
|
{
|
16
|
|
|
const DROPDOWN_LIST = 'activeDropDownList';
|
17
|
|
|
|
18
|
|
|
/**
|
19
|
|
|
* @var BActiveRecord|FCollectionElementBehavior элемент корзины
|
20
|
|
|
*/
|
21
|
|
|
public $element;
|
22
|
|
|
|
23
|
|
|
public $items;
|
24
|
|
|
|
25
|
|
|
public $type = self::DROPDOWN_LIST;
|
26
|
|
|
|
27
|
|
|
public $parameterItemIndex = 'parameter';
|
28
|
|
|
|
29
|
|
|
public $cssClass = 'js-change-parameter';
|
30
|
|
|
|
31
|
|
|
public $data;
|
32
|
|
|
|
33
|
|
|
public $htmlOptions = array();
|
34
|
|
|
|
35
|
|
|
public $beforeAjaxScript = '';
|
36
|
|
|
|
37
|
|
|
public $afterAjaxScript = '';
|
38
|
|
|
|
39
|
|
|
private $render = true;
|
40
|
|
|
|
41
|
|
|
public function init()
|
42
|
|
|
{
|
43
|
|
|
if( !isset($this->element) )
|
44
|
|
|
throw new CHttpException(500, 'Ошибка. Не задано свойство element');
|
45
|
|
|
|
46
|
|
|
/**
|
47
|
|
|
* @var ProductParameter $parameter
|
48
|
|
|
*/
|
49
|
|
|
if( !($parameter = $this->element->getCollectionItems($this->parameterItemIndex)) || empty($this->items) )
|
|
|
|
|
50
|
|
|
{
|
51
|
|
|
$this->render = false;
|
52
|
|
|
return;
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
if( !isset($this->htmlOptions['data-index']) )
|
56
|
|
|
$this->htmlOptions['data-index'] = $this->element->collectionIndex;
|
57
|
|
|
|
58
|
|
|
$this->data = CHtml::listData($this->items, 'id', function($parameter) {
|
59
|
|
|
return $parameter->variant->name;
|
60
|
|
|
});
|
61
|
|
|
|
62
|
|
|
if( !isset($this->htmlOptions['data-type']) )
|
63
|
|
|
$this->htmlOptions['data-type'] = Utils::modelToSnakeCase($parameter);
|
64
|
|
|
|
65
|
|
|
if( !isset($this->htmlOptions['options']) )
|
66
|
|
|
{
|
67
|
|
|
$this->htmlOptions['options'] = CHtml::listData($this->items, 'id', function($item) use($parameter)
|
68
|
|
|
{
|
69
|
|
|
$optionAttributes = array();
|
70
|
|
|
|
71
|
|
|
if( $item->id == $parameter->id )
|
72
|
|
|
$optionAttributes['selected'] = 'selected';
|
73
|
|
|
|
74
|
|
|
return $optionAttributes;
|
75
|
|
|
});
|
76
|
|
|
}
|
77
|
|
|
$this->htmlOptions['class'] = !isset($this->htmlOptions['class']) ? $this->cssClass : $this->htmlOptions['class'].' '.$this->cssClass;
|
78
|
|
|
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
public function run()
|
82
|
|
|
{
|
83
|
|
|
if( $this->render )
|
84
|
|
|
{
|
85
|
|
|
$this->registerScript();
|
86
|
|
|
echo CHtml::activeDropDownList($this->element, 'id', $this->data, $this->htmlOptions);
|
87
|
|
|
}
|
88
|
|
|
}
|
89
|
|
|
|
90
|
|
|
private function registerScript()
|
91
|
|
|
{
|
92
|
|
|
Yii::app()->clientScript->registerScript(__CLASS__.'#Script', "
|
93
|
|
|
$('body').on('change', '.{$this->cssClass}', function() {
|
94
|
|
|
var data = {
|
95
|
|
|
'index' : $(this).data('index'),
|
96
|
|
|
{$this->parameterItemIndex} : {'type' : $(this).data('type'), 'id' : $(this).val()}
|
97
|
|
|
};
|
98
|
|
|
|
99
|
|
|
$.fn.collection('".Yii::app()->controller->basket->keyCollection."').send({
|
100
|
|
|
'action' : 'changeItems',
|
101
|
|
|
'data' : data,
|
102
|
|
|
'beforeAjaxScript' : function(element, data, action) {{$this->beforeAjaxScript}},
|
103
|
|
|
'afterAjaxScript' : function(element, data, action, response) {{$this->afterAjaxScript}}
|
104
|
|
|
});
|
105
|
|
|
});
|
106
|
|
|
");
|
107
|
|
|
}
|
108
|
|
|
} |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: