1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace DcGeneral;
13:
14: use ContaoCommunityAlliance\Translator\Contao\LangArrayTranslator;
15: use ContaoCommunityAlliance\Translator\TranslatorChain;
16: use DcGeneral\Contao\Callback\Callbacks;
17: use DcGeneral\Controller\Ajax2X;
18: use DcGeneral\Controller\Ajax3X;
19: use DcGeneral\Controller\ControllerInterface;
20: use DcGeneral\Data\ModelInterface;
21: use DcGeneral\Event\EventPropagator;
22: use DcGeneral\Exception\DcGeneralRuntimeException;
23: use DcGeneral\Factory\DcGeneralFactory;
24: use DcGeneral\Factory\Event\PopulateEnvironmentEvent;
25: use DcGeneral\View\ViewInterface;
26:
27: 28: 29: 30: 31:
32:
33: class DC_General
34:
35: extends \DataContainer
36: implements DataContainerInterface
37: {
38: 39: 40: 41: 42:
43: protected $objEnvironment;
44:
45: 46: 47: 48: 49:
50: protected $arrDCA = null;
51:
52: 53: 54: 55: 56:
57: protected $arrFields = array();
58:
59: 60: 61: 62: 63: 64: 65: 66: 67:
68: public function __construct($strTable, array &$arrDCA = null, $blnOnloadCallback = true)
69: {
70: parent::__construct();
71:
72: $strTable = $this->getTablenameCallback($strTable);
73:
74:
75:
76: if ($arrDCA != null && $arrDCA['config'])
77: {
78: $this->arrDCA = $arrDCA;
79: }
80: else
81: {
82: $this->arrDCA = &$GLOBALS['TL_DCA'][$strTable];
83: }
84:
85: $dispatcher = $GLOBALS['container']['event-dispatcher'];
86:
87: $dispatcher->addListener(PopulateEnvironmentEvent::NAME, array($this, 'handlePopulateEnvironment'), 4800);
88: $propagator = new EventPropagator($dispatcher);
89:
90: $translator = new TranslatorChain();
91: $translator->add(new LangArrayTranslator($dispatcher));
92:
93: $factory = new DcGeneralFactory();
94:
95:
96:
97: $GLOBALS['objDcGeneral'] = $this;
98:
99: $factory
100: ->setContainerName($strTable)
101: ->setEventPropagator($propagator)
102: ->setTranslator($translator)
103: ->createDcGeneral();
104: unset($GLOBALS['objDcGeneral']);
105: $dispatcher->removeListener(PopulateEnvironmentEvent::NAME, array($this, 'handlePopulateEnvironment'));
106:
107:
108: switch (TL_MODE)
109: {
110: case 'FE':
111: $this->import('FrontendUser', 'User');
112: break;
113:
114: default:
115: case 'BE':
116: $this->import('BackendUser', 'User');
117: break;
118: }
119:
120:
121: if ($this->arrDCA['config']['forceEdit'])
122: {
123: $this->blnForceEdit = true;
124: $this->intId = 1;
125: }
126:
127:
128: $this->getEnvironment()->getClipboard()
129: ->loadFrom($this->getEnvironment());
130:
131:
132:
133:
134:
135: if ($_POST && \Environment::getInstance()->isAjaxRequest)
136: {
137: $this->getViewHandler()->handleAjaxCall();
138:
139:
140: if (version_compare(VERSION, '3.0', '>='))
141: {
142: $objHandler = new Ajax3X();
143: }
144: else
145: {
146: $objHandler = new Ajax2X();
147: }
148: $objHandler->executePostActions($this);
149: }
150: }
151:
152: 153: 154: 155: 156: 157: 158: 159: 160: 161:
162: public function handlePopulateEnvironment(PopulateEnvironmentEvent $event)
163: {
164: $this->objEnvironment = $event->getEnvironment();
165: }
166:
167: 168: 169: 170: 171: 172: 173:
174: protected function getTablenameCallback($strTable)
175: {
176: if (array_key_exists('tablename_callback', $GLOBALS['TL_DCA'][$strTable]['config'])
177: && is_array($GLOBALS['TL_DCA'][$strTable]['config']['tablename_callback']))
178: {
179: foreach ($GLOBALS['TL_DCA'][$strTable]['config']['tablename_callback'] as $callback)
180: {
181: $strCurrentTable = Callbacks::call($callback, $strTable, $this);
182:
183: if ($strCurrentTable != null)
184: {
185: $strTable = $strCurrentTable;
186: }
187: }
188: }
189:
190: return $strTable;
191: }
192:
193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203:
204: public function __get($name)
205: {
206: switch ($name)
207: {
208: case 'table':
209: return $this->getEnvironment()->getDataDefinition()->getName();
210: default:
211: }
212:
213: throw new DcGeneralRuntimeException('Unsupported getter function for \'' . $name . '\' in DC_General.');
214: }
215:
216: 217: 218: 219: 220:
221: public function getDCA()
222: {
223: return $this->arrDCA;
224: }
225:
226: 227: 228: 229: 230:
231: public function getName()
232: {
233: return $this->getEnvironment()->getDataDefinition()->getName();
234: }
235:
236: 237: 238: 239: 240: 241: 242:
243: public function getEnvironment()
244: {
245: if (!$this->objEnvironment)
246: {
247: throw new DcGeneralRuntimeException('No Environment set.');
248: }
249:
250: return $this->objEnvironment;
251: }
252:
253: 254: 255: 256: 257:
258: public function getViewHandler()
259: {
260: return $this->getEnvironment()->getView();
261: }
262:
263: 264: 265: 266: 267:
268: public function getControllerHandler()
269: {
270: return $this->getEnvironment()->getController();
271: }
272:
273: 274: 275: 276: 277: 278: 279: 280: 281:
282: public function __call($name, $arguments)
283: {
284: return call_user_func_array(array($this->getViewHandler(), $name), $arguments);
285: }
286:
287: 288: 289: 290: 291: 292: 293:
294: public function copy()
295: {
296: return $this->getViewHandler()->copy();
297: }
298:
299: 300: 301: 302: 303: 304: 305:
306: public function create()
307: {
308: return $this->getViewHandler()->create();
309: }
310:
311: 312: 313: 314: 315: 316: 317:
318: public function cut()
319: {
320: return $this->getViewHandler()->cut();
321: }
322:
323: 324: 325: 326: 327: 328: 329:
330: public function delete()
331: {
332: return $this->getViewHandler()->delete();
333: }
334:
335: 336: 337: 338: 339: 340: 341:
342: public function edit()
343: {
344: return $this->getViewHandler()->edit();
345: }
346:
347: 348: 349: 350: 351: 352: 353:
354: public function move()
355: {
356: return $this->getViewHandler()->move();
357: }
358:
359: 360: 361: 362: 363: 364: 365:
366: public function show()
367: {
368: return $this->getViewHandler()->show();
369: }
370:
371: 372: 373: 374: 375: 376: 377:
378: public function showAll()
379: {
380: return $this->getViewHandler()->showAll();
381: }
382:
383: 384: 385: 386: 387: 388: 389:
390: public function undo()
391: {
392: return $this->getViewHandler()->undo();
393: }
394: }
395: