1: <?php
2:
3: 4: 5:
6: abstract class Quform_Element_Multi extends Quform_Element_Field
7: {
8: 9: 10: 11:
12: protected $options = array();
13:
14: 15: 16: 17: 18:
19: public function addOption(array $option)
20: {
21: $this->options[] = $option;
22: }
23:
24: 25: 26: 27: 28:
29: public function addOptions(array $options)
30: {
31: foreach ($options as $option) {
32: $this->addOption($option);
33: }
34: }
35:
36: 37: 38: 39: 40:
41: public function setOptions(array $options)
42: {
43: $this->clearOptions();
44: $this->addOptions($options);
45: }
46:
47: 48: 49:
50: public function clearOptions()
51: {
52: $this->options = array();
53: }
54:
55: 56: 57: 58: 59:
60: public function getOptions()
61: {
62: return $this->options;
63: }
64:
65: 66: 67: 68: 69:
70: protected function getCssSelectors()
71: {
72: return parent::getCssSelectors() + array(
73: 'options' => '%s .quform-input-%s .quform-options',
74: 'option' => '%s .quform-input-%s .quform-option',
75: 'optionRadioButton' => '%s .quform-input-%s .quform-option .quform-field-radio',
76: 'optionCheckbox' => '%s .quform-input-%s .quform-option .quform-field-checkbox',
77: 'optionLabel' => '%s .quform-input-%s .quform-option .quform-option-label',
78: 'optionLabelHover' => '%s .quform-input-%s .quform-option .quform-option-label:hover',
79: 'optionLabelSelected' => '%s .quform-input-%s .quform-option .quform-field:checked + .quform-option-label',
80: 'optionIcon' => '%s .quform-input-%s .quform-option .quform-option-icon',
81: 'optionIconSelected' => '%s .quform-input-%s .quform-option .quform-option-icon-selected',
82: 'optionText' => '%s .quform-input-%s .quform-option .quform-option-text',
83: 'optionTextSelected' => '%s .quform-input-%s .quform-option .quform-field:checked + .quform-option-label .quform-option-text'
84: );
85: }
86:
87: 88: 89: 90: 91: 92:
93: public function isLogicRuleMatch(array $rule)
94: {
95: $value = $this->getValue();
96:
97: if (is_array($value)) {
98: $match = false;
99:
100: if (count($value)) {
101: if ($rule['operator'] == 'not_empty') {
102: $match = true;
103: } elseif ($rule['operator'] == 'neq') {
104: $match = true;
105:
106: foreach ($value as $val) {
107: if ($val === $rule['value']) {
108: $match = false;
109: break;
110: }
111: }
112: } else {
113: foreach ($value as $val) {
114: if ($this->isLogicValueMatch($val, $rule)) {
115: $match = true;
116: break;
117: }
118: }
119: }
120: } elseif ($rule['operator'] == 'neq' || $rule['operator'] == 'empty') {
121: $match = true;
122: }
123:
124: return $match;
125: }
126:
127: return $this->isLogicValueMatch($value, $rule);
128: }
129:
130: 131: 132: 133: 134: 135:
136: protected function prepareContext(array $context = array())
137: {
138: $context = parent::prepareContext($context);
139:
140:
141: if ( ! in_array($context['labelPosition'], array('', 'left'), true)) {
142: $context['labelPosition'] = '';
143: }
144:
145:
146: $context['tooltipType'] = 'icon';
147:
148: if (is_string($this->config('optionsStyle')) && $this->config('optionsStyle') != 'inherit') {
149: $context['optionsStyle'] = $this->config('optionsStyle');
150:
151: if ($this->config('optionsStyle') == 'button') {
152: if (is_string($this->config('optionsButtonStyle')) && $this->config('optionsButtonStyle') != 'inherit') {
153: $context['optionsButtonStyle'] = $this->config('optionsButtonStyle');
154: }
155:
156: if (is_string($this->config('optionsButtonSize')) && $this->config('optionsButtonSize') != 'inherit') {
157: $context['optionsButtonSize'] = $this->config('optionsButtonSize');
158: }
159:
160: if (is_string($this->config('optionsButtonWidth')) && $this->config('optionsButtonWidth') != 'inherit') {
161: $context['optionsButtonWidth'] = $this->config('optionsButtonWidth');
162:
163: if ($this->config('optionsButtonWidth') == 'custom' && Quform::isNonEmptyString($this->config('optionsButtonWidthCustom'))) {
164: $context['optionsButtonWidthCustom'] = $this->config('optionsButtonWidthCustom');
165: }
166: }
167:
168: if (is_string($this->config('optionsButtonIconPosition')) && $this->config('optionsButtonIconPosition') != 'inherit') {
169: $context['optionsButtonIconPosition'] = $this->config('optionsButtonIconPosition');
170: }
171: }
172: }
173:
174: return $context;
175: }
176:
177: 178: 179: 180: 181: 182: 183:
184: public function getOptionValue(array $option, $key)
185: {
186: $value = Quform::get($option, $key);
187:
188: if ($value === null) {
189: $value = Quform::get(call_user_func(array(get_class($this), 'getDefaultOptionConfig')), $key);
190: }
191:
192: return $value;
193: }
194:
195: 196: 197: 198: 199: 200:
201: public static function getDefaultOptionConfig($key = null)
202: {
203: $config = array(
204: 'label' => '',
205: 'value' => ''
206: );
207:
208: if (Quform::isNonEmptyString($key)) {
209: return Quform::get($config, $key);
210: }
211:
212: return $config;
213: }
214: }
215: