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