1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 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: 25: 26: 27: 28: 29:
30: class ListView extends BaseView
31: {
32: 33: 34: 35: 36:
37: public function loadCollection()
38: {
39: $environment = $this->getEnvironment();
40: $definition = $environment->getDataDefinition();
41: $backendView = $this->getViewSection();
42:
43:
44: $listingConfig = $backendView->getListingConfig();
45:
46: $objCurrentDataProvider = $environment->getDataProvider();
47: $objParentDataProvider = $environment->getDataProvider($definition->getBasicDefinition()->getParentDataProvider());
48: $objConfig = $environment->getController()->getBaseConfig();
49:
50:
51: $objConfig->setSorting($listingConfig->getDefaultSortingFields());
52:
53: $this->getPanel()->initialize($objConfig);
54:
55: $objCollection = $objCurrentDataProvider->fetchAll($objConfig);
56:
57:
58: if (isset($objCondition)
59: && ($this->getViewSection()->getListingConfig()->getGroupingMode() == ListingConfigInterface::GROUP_CHAR)
60: )
61: {
62: foreach ($objCollection as $objModel)
63: {
64:
65: $arrFilter = $objCondition->getInverseFilter($objModel);
66: $objConfig = $objParentDataProvider->getEmptyConfig()->setFilter($arrFilter);
67: $objParent = $objParentDataProvider->fetch($objConfig);
68:
69:
70: $objModel->setMeta(DCGE::MODEL_PID, $objParent->getId());
71: }
72: }
73:
74: return $objCollection;
75: }
76:
77: 78: 79: 80: 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:
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:
107: 'class' => 'tl_folder_tlist col_'
108: ,
109: 'content' => $label[0]
110: );
111: }
112:
113: $arrTableHead[] = array(
114: 'class' => 'tl_folder_tlist tl_right_nowrap',
115: 'content' => ' '
116: );
117: }
118:
119: return $arrTableHead;
120: }
121:
122: 123: 124: 125: 126: 127: 128: 129: 130: 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:
143:
144:
145: if ($groupingInformation)
146: {
147: $remoteNew = $this->formatCurrentValue(
148: $groupingInformation['property'],
149: $objModelRow,
150: $groupingInformation['mode'],
151: $groupingInformation['length']
152: );
153:
154:
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: 180: 181: 182: 183: 184:
185: protected function viewList($collection)
186: {
187: $environment = $this->getEnvironment();
188: $definition = $environment->getDataDefinition();
189:
190: $groupingInformation = $this->getGroupingMode();
191:
192:
193: $this->setListViewLabel($collection, $groupingInformation);
194:
195:
196: foreach ($collection as $i => $objModel)
197: {
198:
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:
204: $objModel->setMeta(
205: DCGE::MODEL_BUTTONS,
206: $this->generateButtons($objModel, $previous, $next)
207: );
208: }
209: }
210:
211:
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:
233: ->addToTemplate('pdp', '', $objTemplate)
234: ->addToTemplate('cdp', $definition->getName(), $objTemplate)
235: ->addToTemplate('selectButtons', $this->getSelectButtons(), $objTemplate);
236:
237:
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: 250: 251: 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:
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: 303: 304: 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:
317: return implode("\n", $arrReturn);
318: }
319: }
320: