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 DefaultLimitElement
24: extends AbstractElement
25: implements LimitElementInterface
26: {
27: 28: 29: 30: 31:
32: protected $intOffset;
33:
34: 35: 36: 37: 38:
39: protected $intAmount;
40:
41: 42: 43: 44: 45:
46: protected $intTotal;
47:
48: 49: 50: 51: 52:
53: protected function getPersistent()
54: {
55: $arrValue = array();
56: if ($this->getInputProvider()->hasPersistentValue('limit'))
57: {
58: $arrValue = $this->getInputProvider()->getPersistentValue('limit');
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($intOffset, $intAmount)
79: {
80: $arrValue = array();
81: $definitionName = $this->getEnvironment()->getDataDefinition()->getName();
82:
83: if ($this->getInputProvider()->hasPersistentValue('limit'))
84: {
85: $arrValue = $this->getInputProvider()->getPersistentValue('limit');
86: }
87:
88: if ($intOffset)
89: {
90: if (!is_array($arrValue[$definitionName]))
91: {
92: $arrValue[$definitionName] = array();
93: }
94:
95: $arrValue[$definitionName]['offset'] = $intOffset;
96: $arrValue[$definitionName]['amount'] = $intAmount;
97: }
98: else
99: {
100: unset($arrValue[$definitionName]);
101: }
102:
103: $this->getInputProvider()->setPersistentValue('limit', $arrValue);
104: }
105:
106: 107: 108: 109: 110: 111: 112:
113: protected function addParentFilter($idParent, $objConfig)
114: {
115:
116: $objCurrentDataProvider = $this
117: ->getPanel()
118: ->getContainer()
119: ->getDataContainer()
120: ->getDataProvider();
121:
122: $objParentDataProvider = $this
123: ->getPanel()
124: ->getContainer()
125: ->getDataContainer()
126: ->getDataProvider('parent');
127:
128: if ($objParentDataProvider)
129: {
130: $objParent = $objParentDataProvider->fetch($objParentDataProvider->getEmptyConfig()->setId($idParent));
131:
132: $objCondition = $this->getDataContainer()->getEnvironment()->getDataDefinition()->getChildCondition(
133: $objParentDataProvider->getEmptyModel()->getProviderName(),
134: $objCurrentDataProvider->getEmptyModel()->getProviderName()
135: );
136:
137: if ($objCondition)
138: {
139: $arrBaseFilter = $objConfig->getFilter();
140: $arrFilter = $objCondition->getFilter($objParent);
141:
142: if ($arrBaseFilter)
143: {
144: $arrFilter = array_merge($arrBaseFilter, $arrFilter);
145: }
146:
147: $objConfig->setFilter(
148: array(
149: array(
150: 'operation' => 'AND',
151: 'children' => $arrFilter,
152: )
153: )
154: );
155: }
156: }
157:
158: return $objConfig;
159: }
160:
161: 162: 163:
164: public function initialize(ConfigInterface $objConfig, PanelElementInterface $objElement = null)
165: {
166: if (is_null($objElement))
167: {
168: $objTempConfig = $this->getOtherConfig($objConfig);
169: $arrTotal = $this
170: ->getEnvironment()
171: ->getDataProvider()
172: ->fetchAll($objTempConfig->setIdOnly(true));
173:
174: $this->intTotal = $arrTotal ? count($arrTotal) : 0;
175: $offset = 0;
176:
177: $amount = $GLOBALS['TL_CONFIG']['resultsPerPage'];
178:
179: $input = $this->getInputProvider();
180: if ($this->getPanel()->getContainer()->updateValues() && $input->hasValue('tl_limit'))
181: {
182: $limit = explode(',', $input->getValue('tl_limit'));
183: $offset = $limit[0];
184: $amount = $limit[1];
185:
186: $this->setPersistent($offset, $amount);
187: }
188:
189: $persistent = $this->getPersistent();
190: if ($persistent)
191: {
192: $offset = $persistent['offset'];
193: $amount = $persistent['amount'];
194:
195:
196:
197: if ($offset > $this->intTotal)
198: {
199: $offset = 0;
200: }
201: }
202:
203: if (!is_null($offset))
204: {
205: $this->setOffset($offset);
206: $this->setAmount($amount);
207: }
208: }
209:
210: $objConfig->setStart($this->getOffset());
211: $objConfig->setAmount($this->getAmount());
212: }
213:
214: 215: 216:
217: public function render(ViewTemplateInterface $objTemplate)
218: {
219: $arrOptions = array
220: (
221: array
222: (
223: 'value' => 'tl_limit',
224: 'attributes' => '',
225: 'content' => $GLOBALS['TL_LANG']['MSC']['filterRecords']
226: )
227: );
228:
229: $optionsTotal = ceil(($this->intTotal / $GLOBALS['TL_CONFIG']['resultsPerPage']));
230:
231: for ($i = 0; $i < $optionsTotal; $i++)
232: {
233: $first = ($i * $GLOBALS['TL_CONFIG']['resultsPerPage']);
234: $thisLimit = $first . ',' . $GLOBALS['TL_CONFIG']['resultsPerPage'];
235: $upperLimit = ($first + $GLOBALS['TL_CONFIG']['resultsPerPage']);
236:
237: if ($upperLimit > $this->intTotal)
238: {
239: $upperLimit = $this->intTotal;
240: }
241:
242: $arrOptions[] = array
243: (
244: 'value' => $thisLimit,
245: 'attributes' => ($this->getOffset() == $first) ? ' selected="selected"' : '',
246: 'content' => ($first + 1) . ' - ' . $upperLimit
247: );
248: }
249:
250: if ($this->intTotal > $GLOBALS['TL_CONFIG']['resultsPerPage'])
251: {
252: $arrOptions[] = array
253: (
254: 'value' => 'all',
255: 'attributes' =>
256: (($this->getOffset() == 0) && ($this->getAmount() == $this->intTotal))
257: ? ' selected="selected"'
258: : '',
259: 'content' => $GLOBALS['TL_LANG']['MSC']['filterAll']
260: );
261: }
262:
263: $objTemplate->options = $arrOptions;
264:
265: return $this;
266: }
267:
268: 269: 270:
271: public function setOffset($intOffset)
272: {
273: $this->intOffset = $intOffset;
274:
275: return $this;
276: }
277:
278: 279: 280:
281: public function getOffset()
282: {
283: return $this->intOffset;
284: }
285:
286: 287: 288:
289: public function setAmount($intAmount)
290: {
291: $this->intAmount = $intAmount;
292:
293: return $this;
294: }
295:
296: 297: 298:
299: public function getAmount()
300: {
301: return $this->intAmount;
302: }
303: }
304: