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

  • Ajax
  • Ajax2X
  • Ajax3X
  • DefaultController

Interfaces

  • ControllerInterface
  • 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\Controller;
 14: 
 15: use DcGeneral\DataContainerInterface;
 16: use DcGeneral\Exception\DcGeneralRuntimeException;
 17: 
 18: /**
 19:  * Class Ajax - General purpose Ajax handler for "executePostActions" as we can not use the default Contao
 20:  * handling.
 21:  *
 22:  * See Contao core issue #5957. https://github.com/contao/core/pull/5957
 23:  *
 24:  * @author     Christian Schiffler <c.schiffler@cyberspectrum.de>
 25:  * @copyright  The MetaModels team.
 26:  * @license    LGPL.
 27:  */
 28: abstract class Ajax extends \Backend
 29: {
 30:     public function __construct()
 31:     {
 32:         // DO NOT! call parent::__construct(); as otherwise we will end up having references in this class.
 33:     }
 34: 
 35:     /**
 36:      * Compat wrapper for contao 2.X and 3.X - delegates to the relevant input handler.
 37:      *
 38:      * @param      $key
 39:      * @param bool $blnDecodeEntities
 40:      * @param bool $blnKeepUnused
 41:      *
 42:      * @return mixed
 43:      */
 44:     protected static function getGet($key, $blnDecodeEntities=false, $blnKeepUnused=false)
 45:     {
 46:         // TODO: use dependency injection here.
 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:      * Compat wrapper for contao 2.X and 3.X - delegates to the relevant input handler.
 59:      *
 60:      * @param      $key
 61:      * @param bool $blnDecodeEntities
 62:      *
 63:      * @return mixed
 64:      */
 65:     protected static function getPost($key, $blnDecodeEntities=false)
 66:     {
 67:         // TODO: use dependency injection here.
 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:         // TODO: this solution is really a mess, we DEFINATELY want to implement a proper functionality in the callback class to handle this.
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:          * @var \BackendUser $objUser
145:          */
146:         $objUser = \BackendUser::getInstance();
147:         $arrDCA  = $objDc->getDCA();
148:         $field   = self::getPost('field');
149: 
150:         // Check whether the field is a selector field and allowed for regular users (contao/core/#4427).
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:      * @param String $strAction
203:      * @param DataContainerInterface $objDc
204:      * @return void
205:      */
206:     public function executePostActions(DataContainerInterface $objDc)
207:     {
208:         // Check DC for a right data provider
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:             // Load nodes of the page structure tree. Compatible between 2.X and 3.X
218:             case 'loadStructure':
219:                 $this->loadStructure($objDc);
220:                 break;
221: 
222:             // Load nodes of the file manager tree
223:             case 'loadFileManager':
224:                 $this->loadFileManager($objDc);
225:                 break;
226: 
227:             // Load nodes of the page tree
228:             case 'loadPagetree':
229:                 $this->loadPagetree($objDc);
230:                 break;
231: 
232:                 // Load nodes of the file tree
233:             case 'loadFiletree':
234:                 $this->loadFiletree($objDc);
235:                 break;
236: 
237:             // Reload the page/file picker
238:             case 'reloadPagetree':
239:                 $this->reloadPagetree($objDc);
240:                 break;
241:             case 'reloadFiletree':
242:                 $this->reloadFiletree($objDc);
243:                 break;
244: 
245:             // Feature/unfeature an element
246:             case 'toggleFeatured':
247:                 $this->toggleFeatured($objDc);
248:                 break;
249: 
250:             // Toggle subpalettes
251:             case 'toggleSubpalette':
252:                 $this->toggleSubpalette($objDc);
253:                 break;
254: 
255:             // HOOK: pass unknown actions to callback functions
256:             default:
257:                 $this->callHooks(self::getPost('action'), $objDc);
258:                  break;
259:         }
260:     }
261: }
262: 
contao-community-alliance/dc-general API documentation generated by ApiGen 2.8.0