1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: namespace DcGeneral\Panel;
14:
15: use DcGeneral\Data\ConfigInterface;
16: use DcGeneral\View\ViewTemplateInterface;
17:
18: 19: 20: 21: 22:
23: class DefaultSearchElement
24: extends AbstractElement
25: implements SearchElementInterface
26: {
27: 28: 29: 30: 31:
32: protected $arrProperties;
33:
34: 35: 36: 37: 38:
39: protected $strSelectedProperty;
40:
41: 42: 43: 44: 45:
46: protected $mixValue;
47:
48: 49: 50: 51: 52:
53: protected function getPersistent()
54: {
55: $arrValue = array();
56: if ($this->getInputProvider()->hasPersistentValue('search'))
57: {
58: $arrValue = $this->getInputProvider()->getPersistentValue('search');
59: }
60:
61: if (array_key_exists($this->getEnvironment()->getDataDefinition()->getName(), $arrValue))
62: {
63: return $arrValue[$this->getEnvironment()->getDataDefinition()->getName()];
64: }
65:
66: return array();
67: }
68:
69: 70: 71: 72: 73: 74: 75: 76: 77:
78: protected function setPersistent($strProperty, $strValue)
79: {
80: $arrValue = array();
81: $definitionName = $this->getEnvironment()->getDataDefinition()->getName();
82:
83: if ($this->getInputProvider()->hasPersistentValue('search'))
84: {
85: $arrValue = $this->getInputProvider()->getPersistentValue('search');
86: }
87:
88: if (!empty($strValue))
89: {
90: if (!is_array($arrValue[$definitionName]))
91: {
92: $arrValue[$definitionName] = array();
93: }
94:
95: if ($strValue)
96: {
97: $arrValue[$definitionName]['field'] = $strProperty;
98: $arrValue[$definitionName]['value'] = $strValue;
99: }
100: else
101: {
102: unset($arrValue[$definitionName]);
103: }
104: }
105: else
106: {
107: unset($arrValue[$definitionName]);
108: }
109:
110: $this->getInputProvider()->setPersistentValue('search', $arrValue);
111: }
112:
113: 114: 115:
116: public function initialize(ConfigInterface $objConfig, PanelElementInterface $objElement = null)
117: {
118: $input = $this->getInputProvider();
119: $value = null;
120: $field = null;
121:
122: if ($this->getPanel()->getContainer()->updateValues() && $input->hasValue('tl_field'))
123: {
124: $field = $input->getValue('tl_field');
125: $value = $input->getValue('tl_value');
126:
127: $this->setPersistent($field, $value);
128: }
129: elseif ($input->hasPersistentValue('search'))
130: {
131: $persistent = $this->getPersistent();
132: if ($persistent)
133: {
134: $field = $persistent['field'];
135: $value = $persistent['value'];
136: }
137: }
138:
139: $this->setSelectedProperty($field);
140: $this->setValue($value);
141:
142: if (!($this->getSelectedProperty() && $this->getValue()))
143: {
144: return;
145: }
146:
147: $arrCurrent = $objConfig->getFilter();
148: if (!is_array($arrCurrent))
149: {
150: $arrCurrent = array();
151: }
152:
153: $objConfig->setFilter(array_merge_recursive(
154: $arrCurrent,
155: array(
156: array(
157: 'operation' => 'AND',
158: 'children' => array(array(
159: 'operation' => 'LIKE',
160: 'property' => $this->getSelectedProperty(),
161: 'value' => sprintf('*%s*', $this->getValue())
162: ))
163: )
164: )
165: ));
166: }
167:
168: 169: 170:
171: public function render(ViewTemplateInterface $objTemplate)
172: {
173: $arrOptions = array();
174:
175: foreach ($this->getPropertyNames() as $field)
176: {
177: $arrLabel = $this
178: ->getEnvironment()
179: ->getDataDefinition()
180: ->getPropertiesDefinition()
181: ->getProperty($field)
182: ->getLabel();
183: $arrOptions[] = array
184: (
185: 'value' => $field,
186: 'content' => is_array($arrLabel) ? $arrLabel[0] : $arrLabel,
187: 'attributes' => ($field == $this->getSelectedProperty()) ? ' selected="selected"' : ''
188: );
189: }
190:
191: $objTemplate->class = 'tl_select' . (!is_null($this->getValue()) ? ' active' : '');
192: $objTemplate->options = $arrOptions;
193: $objTemplate->value = $this->getValue();
194:
195: return $this;
196: }
197:
198: 199: 200:
201: public function addProperty($strProperty)
202: {
203: $this->arrProperties[] = $strProperty;
204:
205: return $this;
206: }
207:
208: 209: 210:
211: public function getPropertyNames()
212: {
213: return $this->arrProperties;
214: }
215:
216: 217: 218:
219: public function setSelectedProperty($strProperty = '')
220: {
221: $this->strSelectedProperty = $strProperty;
222:
223: return $this;
224: }
225:
226: 227: 228:
229: public function getSelectedProperty()
230: {
231: return $this->strSelectedProperty;
232: }
233:
234: 235: 236:
237: public function setValue($mixValue = null)
238: {
239: $this->mixValue = $mixValue;
240:
241: return $this;
242: }
243:
244: 245: 246:
247: public function getValue()
248: {
249: return $this->mixValue;
250: }
251: }
252: