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

  • BaseView
  • ContaoBackendViewTemplate
  • ContaoWidgetManager
  • ListView
  • ParentView
  • TreeView

Interfaces

  • BackendViewInterface
  • 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\Contao\View\Contao2BackendView;
 14: 
 15: use DcGeneral\Data\CollectionInterface;
 16: use DcGeneral\Data\DCGE;
 17: use DcGeneral\Data\ModelInterface;
 18: use DcGeneral\Contao\DataDefinition\Definition\Contao2BackendViewDefinitionInterface;
 19: use DcGeneral\DataDefinition\Definition\View\ListingConfigInterface;
 20: use DcGeneral\Event\PreDuplicateModelEvent;
 21: use DcGeneral\Event\PostDuplicateModelEvent;
 22: 
 23: /**
 24:  * Class ListView.
 25:  *
 26:  * The implementation of a plain listing view.
 27:  *
 28:  * @package DcGeneral\Contao\View\Contao2BackendView
 29:  */
 30: class ListView extends BaseView
 31: {
 32:     /**
 33:      * Load the collection of child items and the parent item for the currently selected parent item.
 34:      *
 35:      * @return CollectionInterface
 36:      */
 37:     public function loadCollection()
 38:     {
 39:         $environment = $this->getEnvironment();
 40:         $definition  = $environment->getDataDefinition();
 41:         $backendView = $this->getViewSection();
 42: 
 43:         /** @var Contao2BackendViewDefinitionInterface $backendView */
 44:         $listingConfig = $backendView->getListingConfig();
 45: 
 46:         $objCurrentDataProvider = $environment->getDataProvider();
 47:         $objParentDataProvider  = $environment->getDataProvider($definition->getBasicDefinition()->getParentDataProvider());
 48:         $objConfig              = $environment->getController()->getBaseConfig();
 49: 
 50:         // Initialize sorting.
 51:         $objConfig->setSorting($listingConfig->getDefaultSortingFields());
 52: 
 53:         $this->getPanel()->initialize($objConfig);
 54: 
 55:         $objCollection = $objCurrentDataProvider->fetchAll($objConfig);
 56: 
 57:         // If we want to group the elements, do so now.
 58:         if (isset($objCondition)
 59:             && ($this->getViewSection()->getListingConfig()->getGroupingMode() == ListingConfigInterface::GROUP_CHAR)
 60:         )
 61:         {
 62:             foreach ($objCollection as $objModel)
 63:             {
 64:                 /** @var ModelInterface $objModel */
 65:                 $arrFilter = $objCondition->getInverseFilter($objModel);
 66:                 $objConfig = $objParentDataProvider->getEmptyConfig()->setFilter($arrFilter);
 67:                 $objParent = $objParentDataProvider->fetch($objConfig);
 68: 
 69:                 // TODO: wouldn't it be wiser to link the model instance instead of the id of the parenting model?
 70:                 $objModel->setMeta(DCGE::MODEL_PID, $objParent->getId());
 71:             }
 72:         }
 73: 
 74:         return $objCollection;
 75:     }
 76: 
 77:     /**
 78:      * Return the table heading.
 79:      *
 80:      * @return array
 81:      */
 82:     protected function getTableHead()
 83:     {
 84:         $arrTableHead      = array();
 85:         $definition        = $this->getEnvironment()->getDataDefinition();
 86:         $properties        = $definition->getPropertiesDefinition();
 87:         $viewDefinition    = $this->getViewSection();
 88:         $listingDefinition = $viewDefinition->getListingConfig();
 89: 
 90:         // Generate the table header if the "show columns" option is active.
 91:         if ($listingDefinition->getShowColumns())
 92:         {
 93:             foreach ($properties->getPropertyNames() as $f)
 94:             {
 95:                 $property = $properties->getProperty($f);
 96:                 if ($property)
 97:                 {
 98:                     $label = $property->getLabel();
 99:                 }
100:                 else
101:                 {
102:                     $label = $f;
103:                 }
104: 
105:                 $arrTableHead[] = array(
106:                     // FIXME: getAdditionalSorting() unimplemented
107:                     'class'   => 'tl_folder_tlist col_'
108:                     /* . $f . ((in_array($f, $definition->getAdditionalSorting())) ? ' ordered_by' : '') */,
109:                     'content' => $label[0]
110:                 );
111:             }
112: 
113:             $arrTableHead[] = array(
114:                 'class'   => 'tl_folder_tlist tl_right_nowrap',
115:                 'content' => '&nbsp;'
116:             );
117:         }
118: 
119:         return $arrTableHead;
120:     }
121: 
122:     /**
123:      * Set label for list view.
124:      *
125:      * @param CollectionInterface $collection          The collection containing the models.
126:      *
127:      * @param array               $groupingInformation The grouping information as retrieved via
128:      *                                                 BaseView::getGroupingMode().
129:      *
130:      * @return void
131:      */
132:     protected function setListViewLabel($collection, $groupingInformation)
133:     {
134:         $viewDefinition = $this->getViewSection();
135:         $listingConfig  = $viewDefinition->getListingConfig();
136:         $remoteCur      = null;
137:         $groupClass     = 'tl_folder_tlist';
138:         $eoCount        = -1;
139: 
140:         foreach ($collection as $objModelRow)
141:         {
142:             /** @var \DcGeneral\Data\ModelInterface $objModelRow */
143: 
144:             // Build the sorting groups.
145:             if ($groupingInformation)
146:             {
147:                 $remoteNew = $this->formatCurrentValue(
148:                     $groupingInformation['property'],
149:                     $objModelRow,
150:                     $groupingInformation['mode'],
151:                     $groupingInformation['length']
152:                 );
153: 
154:                 // Add the group header if it differs from the last header.
155:                 if (!$listingConfig->getShowColumns()
156:                     && ($groupingInformation['mode'] !== ListingConfigInterface::GROUP_NONE)
157:                     && (($remoteNew != $remoteCur) || ($remoteCur === null))
158:                 )
159:                 {
160:                     $eoCount = -1;
161: 
162:                     $objModelRow->setMeta(DCGE::MODEL_GROUP_VALUE, array(
163:                         'class' => $groupClass,
164:                         'value' => $remoteNew
165:                     ));
166: 
167:                     $groupClass = 'tl_folder_list';
168:                     $remoteCur  = $remoteNew;
169:                 }
170:             }
171: 
172:             $objModelRow->setMeta(DCGE::MODEL_EVEN_ODD_CLASS, (((++$eoCount) % 2 == 0) ? 'even' : 'odd'));
173: 
174:             $objModelRow->setMeta(DCGE::MODEL_LABEL_VALUE, $this->formatModel($objModelRow));
175:         }
176:     }
177: 
178:     /**
179:      * Generate list view from current collection.
180:      *
181:      * @param CollectionInterface $collection The collection containing the models.
182:      *
183:      * @return string
184:      */
185:     protected function viewList($collection)
186:     {
187:         $environment = $this->getEnvironment();
188:         $definition  = $environment->getDataDefinition();
189: 
190:         $groupingInformation = $this->getGroupingMode();
191: 
192:         // Set label.
193:         $this->setListViewLabel($collection, $groupingInformation);
194: 
195:         // Generate buttons.
196:         foreach ($collection as $i => $objModel)
197:         {
198:             // Regular buttons - only if not in select mode!
199:             if (!$this->isSelectModeActive())
200:             {
201:                 $previous = ((!is_null($collection->get($i - 1))) ? $collection->get($i - 1) : null);
202:                 $next     = ((!is_null($collection->get($i + 1))) ? $collection->get($i + 1) : null);
203:                 /** @var \DcGeneral\Data\ModelInterface $objModel */
204:                 $objModel->setMeta(
205:                     DCGE::MODEL_BUTTONS,
206:                     $this->generateButtons($objModel, $previous, $next)
207:                 );
208:             }
209:         }
210: 
211:         // Add template.
212:         if (isset($groupingInformation['mode']) && ($groupingInformation['mode'] != ListingConfigInterface::GROUP_NONE))
213:         {
214:             $objTemplate = $this->getTemplate('dcbe_general_grouping');
215:         }
216:         elseif (isset($groupingInformation['property']) && ($groupingInformation['property'] != ''))
217:         {
218:             $objTemplate = $this->getTemplate('dcbe_general_listView_sorting');
219:         }
220:         else
221:         {
222:             $objTemplate = $this->getTemplate('dcbe_general_listView');
223:         }
224: 
225:         $this
226:             ->addToTemplate('tableName', strlen($definition->getName()) ? $definition->getName() : 'none', $objTemplate)
227:             ->addToTemplate('collection', $collection, $objTemplate)
228:             ->addToTemplate('select', $this->getEnvironment()->getInputProvider()->getParameter('act'), $objTemplate)
229:             ->addToTemplate('action', ampersand(\Environment::getInstance()->request, true), $objTemplate)
230:             ->addToTemplate('mode', ($groupingInformation ? $groupingInformation['mode'] : null), $objTemplate)
231:             ->addToTemplate('tableHead', $this->getTableHead(), $objTemplate)
232:             // Set dataprovider from current and parent.
233:             ->addToTemplate('pdp', '', $objTemplate)
234:             ->addToTemplate('cdp', $definition->getName(), $objTemplate)
235:             ->addToTemplate('selectButtons', $this->getSelectButtons(), $objTemplate);
236: 
237:         // Add breadcrumb, if we have one.
238:         $strBreadcrumb = $this->breadcrumb();
239:         if ($strBreadcrumb != null)
240:         {
241:             $this->addToTemplate('breadcrumb', $strBreadcrumb, $objTemplate);
242:         }
243: 
244:         return $objTemplate->parse();
245:     }
246: 
247: 
248:     /**
249:      * Copy mode - this redirects to edit.
250:      *
251:      * @return string
252:      */
253:     public function copy()
254:     {
255:         $this->checkLanguage();
256: 
257:         $environment  = $this->getEnvironment();
258:         $dataProvider = $environment->getDataProvider();
259:         $modelId      = $environment->getInputProvider()->getParameter('id');
260:         $model        = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($modelId));
261: 
262:         if (strlen($modelId))
263:         {
264:             $model = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($modelId));
265:         }
266:         else
267:         {
268:             throw new DcGeneralRuntimeException('Missing model id.');
269:         }
270: 
271:         // We need to keep the original data here.
272:         $copyModel = clone $model;
273: 
274:         $preFunction = function($environment, $model, $originalModel)
275:         {
276:             $copyEvent = new PreDuplicateModelEvent($environment, $model);
277:             $environment->getEventPropagator()->propagate(
278:                 $copyEvent::NAME,
279:                 $copyEvent,
280:                 array(
281:                     $environment->getDataDefinition()->getName(),
282:                 )
283:             );
284:         };
285: 
286:         $postFunction = function($environment, $model, $originalModel)
287:         {
288:             $copyEvent = new PostDuplicateModelEvent($environment, $model);
289:             $environment->getEventPropagator()->propagate(
290:                 $copyEvent::NAME,
291:                 $copyEvent,
292:                 array(
293:                     $environment->getDataDefinition()->getName(),
294:                 )
295:             );
296:         };
297: 
298:         return $this->createEditMask($copyModel, $model, $preFunction, $postFunction);
299:     }
300: 
301:     /**
302:      * Show all entries from one table.
303:      *
304:      * @return string
305:      */
306:     public function showAll()
307:     {
308:         $this->checkClipboard();
309:         $collection = $this->loadCollection();
310: 
311:         $arrReturn            = array();
312:         $arrReturn['panel']   = $this->panel();
313:         $arrReturn['buttons'] = $this->generateHeaderButtons('tl_buttons_a');
314:         $arrReturn['body']    = $this->viewList($collection);
315: 
316:         // Return all.
317:         return implode("\n", $arrReturn);
318:     }
319: }
320: 
contao-community-alliance/dc-general API documentation generated by ApiGen 2.8.0