1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: namespace DcGeneral\Controller;
14:
15: use DcGeneral\DataContainerInterface;
16: use DcGeneral\Exception\DcGeneralRuntimeException;
17:
18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: abstract class Ajax extends \Backend
29: {
30: public function __construct()
31: {
32:
33: }
34:
35: 36: 37: 38: 39: 40: 41: 42: 43:
44: protected static function getGet($key, $blnDecodeEntities=false, $blnKeepUnused=false)
45: {
46:
47: if (version_compare(VERSION, '3.0', '>='))
48: {
49: return \Input::get($key, $blnDecodeEntities, $blnKeepUnused);
50: }
51: else
52: {
53: return \Input::getInstance()->get($key, $blnDecodeEntities, $blnKeepUnused);
54: }
55: }
56:
57: 58: 59: 60: 61: 62: 63: 64:
65: protected static function getPost($key, $blnDecodeEntities=false)
66: {
67:
68: if (version_compare(VERSION, '3.0', '>='))
69: {
70: return \Input::post($key, $blnDecodeEntities);
71: }
72: else
73: {
74: return \Input::getInstance()->post($key, $blnDecodeEntities);
75: }
76: }
77:
78: protected static function getAjaxId()
79: {
80: return preg_replace('/.*_([0-9a-zA-Z]+)$/', '$1', self::getPost('id'));
81: }
82:
83: protected static function getAjaxKey()
84: {
85: $strAjaxKey = str_replace('_' . self::getAjaxId(), '', self::getPost('id'));
86:
87: if (self::getGet('act') == 'editAll')
88: {
89: $strAjaxKey = preg_replace('/(.*)_[0-9a-zA-Z]+$/', '$1', $strAjaxKey);
90: }
91:
92: return $strAjaxKey;
93: }
94:
95: protected static function getAjaxName()
96: {
97: if (self::getGet('act') == 'editAll')
98: {
99: return preg_replace('/.*_([0-9a-zA-Z]+)$/', '$1', self::getPost('name'));
100: }
101:
102: return self::getPost('name');
103: }
104:
105: protected function loadStructure(DataContainerInterface $objDc)
106: {
107: echo $objDc->ajaxTreeView($this->getAjaxId(), intval(self::getPost('level')));
108: exit;
109: }
110:
111: protected function loadFileManager(DataContainerInterface $objDc)
112: {
113: echo $objDc->ajaxTreeView(self::getPost('folder', true), intval(self::getPost('level')));
114: exit;
115: }
116:
117: abstract protected function loadPagetree(DataContainerInterface $objDc);
118:
119: abstract protected function loadFiletree(DataContainerInterface $objDc);
120:
121: abstract protected function reloadPagetree(DataContainerInterface $objDc);
122:
123: abstract protected function reloadFiletree(DataContainerInterface $objDc);
124:
125: protected function toggleFeatured(DataContainerInterface $objDc)
126: {
127:
128: $strClass = $objDc->getTable();
129: if (class_exists($strClass, false))
130: {
131: $dca = new $strClass();
132:
133: if (method_exists($dca, 'toggleFeatured'))
134: {
135: $dca->toggleFeatured(self::getPost('id'), ((self::getPost('state') == 1) ? true : false));
136: }
137: }
138: exit;
139: }
140:
141: protected function toggleSubpalette(DataContainerInterface $objDc)
142: {
143: 144: 145:
146: $objUser = \BackendUser::getInstance();
147: $arrDCA = $objDc->getDCA();
148: $field = self::getPost('field');
149:
150:
151: if (!is_array($arrDCA['palettes']['__selector__'])
152: || !in_array($field, $arrDCA['palettes']['__selector__'])
153: || ($arrDCA['fields'][$field]['exclude'] && !$objUser->hasAccess($objDc->getTable() . '::' . $field, 'alexf'))
154: )
155: {
156: $this->log(
157: 'Field "' . $field . '" is not an allowed selector field (possible SQL injection attempt)',
158: 'Ajax executePostActions()',
159: TL_ERROR
160: );
161: header('HTTP/1.1 400 Bad Request');
162: die('Bad Request');
163: }
164:
165: if (self::getPost('load'))
166: {
167: if (self::getGet('act') == 'editAll')
168: {
169: throw new DcGeneralRuntimeException("Ajax editAll unimplemented, I do not know what to do.", 1);
170: echo $objDc->editAll(self::getAjaxId(), self::getPost('id'));
171: }
172: else{
173: echo $objDc->generateAjaxPalette(self::getPost('field'));
174: }
175: }
176: exit;
177: }
178:
179: protected function callHooks($strAction, DataContainerInterface $objDc)
180: {
181: if (isset($GLOBALS['TL_HOOKS']['executePostActions']) && is_array($GLOBALS['TL_HOOKS']['executePostActions']))
182: {
183: foreach ($GLOBALS['TL_HOOKS']['executePostActions'] as $callback)
184: {
185: if (in_array('getInstance', get_class_methods($callback[0])))
186: {
187: $objHook = call_user_func(array($callback[0], 'getInstance'));
188: }
189: else
190: {
191: $objHook = new $callback[0]();
192: }
193:
194: $objHook->$callback[1]($strAction, $objDc);
195: }
196: }
197: exit;
198: }
199:
200: 201: 202: 203: 204: 205:
206: public function executePostActions(DataContainerInterface $objDc)
207: {
208:
209: if (!$objDc instanceof DataContainerInterface)
210: {
211: return;
212: }
213: header('Content-Type: text/html; charset=' . $GLOBALS['TL_CONFIG']['characterSet']);
214:
215: switch (self::getPost('action'))
216: {
217:
218: case 'loadStructure':
219: $this->loadStructure($objDc);
220: break;
221:
222:
223: case 'loadFileManager':
224: $this->loadFileManager($objDc);
225: break;
226:
227:
228: case 'loadPagetree':
229: $this->loadPagetree($objDc);
230: break;
231:
232:
233: case 'loadFiletree':
234: $this->loadFiletree($objDc);
235: break;
236:
237:
238: case 'reloadPagetree':
239: $this->reloadPagetree($objDc);
240: break;
241: case 'reloadFiletree':
242: $this->reloadFiletree($objDc);
243: break;
244:
245:
246: case 'toggleFeatured':
247: $this->toggleFeatured($objDc);
248: break;
249:
250:
251: case 'toggleSubpalette':
252: $this->toggleSubpalette($objDc);
253: break;
254:
255:
256: default:
257: $this->callHooks(self::getPost('action'), $objDc);
258: break;
259: }
260: }
261: }
262: