Overview

Namespaces

  • DcGeneral
    • Clipboard
    • Contao
      • Callback
      • Compatibility
      • DataDefinition
        • Definition
      • Dca
        • Builder
          • Legacy
        • Definition
        • Palette
        • Populator
      • Event
      • View
        • Contao2BackendView
          • Event
    • Controller
    • Data
    • DataDefinition
      • Builder
      • Definition
        • Properties
        • View
          • Panel
      • ModelRelationship
      • Palette
        • Builder
          • Event
        • Condition
          • Palette
          • Property
    • EnvironmentPopulator
    • Event
    • Exception
    • Factory
      • Event
    • Panel
    • View
      • Event

Classes

  • AbstractElement
  • DefaultFilterElement
  • DefaultLimitElement
  • DefaultPanel
  • DefaultPanelContainer
  • DefaultSearchElement
  • DefaultSortElement
  • DefaultSubmitElement

Interfaces

  • FilterElementInterface
  • LimitElementInterface
  • PanelContainerInterface
  • PanelElementInterface
  • PanelInterface
  • SearchElementInterface
  • SortElementInterface
  • SubmitElementInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * PHP version 5
  4:  * @package    generalDriver
  5:  * @author     Christian Schiffler <c.schiffler@cyberspectrum.de>
  6:  * @author     Stefan Heimes <stefan_heimes@hotmail.com>
  7:  * @author     Tristan Lins <tristan.lins@bit3.de>
  8:  * @copyright  The MetaModels team.
  9:  * @license    LGPL.
 10:  * @filesource
 11:  */
 12: 
 13: namespace DcGeneral\Panel;
 14: 
 15: use DcGeneral\Data\ConfigInterface;
 16: use DcGeneral\View\ViewTemplateInterface;
 17: 
 18: /**
 19:  * Default implementation of a limit panel element.
 20:  *
 21:  * @package DcGeneral\Panel
 22:  */
 23: class DefaultLimitElement
 24:     extends AbstractElement
 25:     implements LimitElementInterface
 26: {
 27:     /**
 28:      * The current offset.
 29:      *
 30:      * @var int
 31:      */
 32:     protected $intOffset;
 33: 
 34:     /**
 35:      * The current amount.
 36:      *
 37:      * @var int
 38:      */
 39:     protected $intAmount;
 40: 
 41:     /**
 42:      * The total amount of all valid entries.
 43:      *
 44:      * @var int
 45:      */
 46:     protected $intTotal;
 47: 
 48:     /**
 49:      * Retrieve the persistent value from the input provider.
 50:      *
 51:      * @return array
 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:      * Store the persistent value in the input provider.
 71:      *
 72:      * @param int $intOffset The offset.
 73:      *
 74:      * @param int $intAmount The amount of items to show.
 75:      *
 76:      * @return void
 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:      * @param mixed $idParent
108:      *
109:      * @param ConfigInterface $objConfig
110:      *
111:      * @return \DcGeneral\Data\ConfigInterface
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:      * {@inheritDoc}
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:             // TODO: we need to determine the perPage some better way.
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:                 // Hotfix the offset - we also might want to store it persistent.
196:                 // Another way would be to always stick on the "last" page when we hit the upper limit.
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:      * {@inheritDoc}
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:      * {@inheritDoc}
270:      */
271:     public function setOffset($intOffset)
272:     {
273:         $this->intOffset = $intOffset;
274: 
275:         return $this;
276:     }
277: 
278:     /**
279:      * {@inheritDoc}
280:      */
281:     public function getOffset()
282:     {
283:         return $this->intOffset;
284:     }
285: 
286:     /**
287:      * {@inheritDoc}
288:      */
289:     public function setAmount($intAmount)
290:     {
291:         $this->intAmount = $intAmount;
292: 
293:         return $this;
294:     }
295: 
296:     /**
297:      * {@inheritDoc}
298:      */
299:     public function getAmount()
300:     {
301:         return $this->intAmount;
302:     }
303: }
304: 
contao-community-alliance/dc-general API documentation generated by ApiGen 2.8.0