/system/modules/generalDriver/GeneralControllerDefault.php
PHP | 3451 lines | 2190 code | 518 blank | 743 comment | 447 complexity | a102491a343744dd43b95a222c7fa40d MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * PHP version 5
- * @package generalDriver
- * @author Stefan Heimes <cms@men-at-work.de>
- * @copyright The MetaModels team.
- * @license LGPL.
- * @filesource
- */
- class GeneralControllerDefault extends Controller implements InterfaceGeneralController
- {
- /* /////////////////////////////////////////////////////////////////////
- * ---------------------------------------------------------------------
- * Vars
- * ---------------------------------------------------------------------
- * ////////////////////////////////////////////////////////////////// */
- // Objects -----------------------
- /**
- * Current DC General
- *
- * @var DC_General
- */
- protected $objDC = null;
- /**
- * Contao Encrypt class
- * @var Encryption
- */
- protected $objEncrypt = null;
- // Current -----------------------
- /**
- * A list with all current ID`s
- * @var array
- */
- protected $arrInsertIDs = array();
- // States ------------------------
- /**
- * State of Show/Close all
- * @var boolean
- */
- protected $blnShowAllEntries = false;
- // Misc. -------------------------
- /**
- * Error msg
- *
- * @var string
- */
- protected $notImplMsg = "<div style='text-align:center; font-weight:bold; padding:40px;'>The function/view "%s" is not implemented.<br />Please <a target='_blank' style='text-decoration:underline' href='http://now.metamodel.me/en/sponsors/become-one#payment'>support us</a> to add this important feature!</div>";
- /**
- * Field for the function sortCollection
- *
- * @var string $arrColSort
- */
- protected $arrColSort;
- /* /////////////////////////////////////////////////////////////////////
- * ---------------------------------------------------------------------
- * Magic functions
- * ---------------------------------------------------------------------
- * ////////////////////////////////////////////////////////////////// */
- public function __construct()
- {
- parent::__construct();
- // Import
- $this->import('Encryption');
- // Check some vars
- $this->blnShowAllEntries = ($this->Input->get('ptg') == 'all') ? 1 : 0;
- }
- public function __call($name, $arguments)
- {
- switch ($name)
- {
- default:
- throw new Exception("Error Processing Request: " . $name, 1);
- break;
- };
- }
- /* /////////////////////////////////////////////////////////////////////
- * ---------------------------------------------------------------------
- * Getter & Setter
- * ---------------------------------------------------------------------
- * ////////////////////////////////////////////////////////////////// */
- /**
- * Get DC General
- * @return DC_General
- */
- public function getDC()
- {
- return $this->objDC;
- }
- /**
- * Set DC General
- * @param DC_General $objDC
- */
- public function setDC($objDC)
- {
- $this->objDC = $objDC;
- }
- /**
- * Get filter for the data provider
- *
- * @todo Somtimes we don't need all filtersettings
- * @todo add new var like level = all, root, parent etc.
- * @todo check where we use this.
- * @todo it`s a nice function, maybe a core function ?
- *
- * @return array();
- */
- protected function getFilter()
- {
- $arrFilter = $this->getDC()->getFilter();
- if ($arrFilter)
- {
- return $arrFilter;
- }
- // Custom filter
- if (is_array($this->getDC()->arrDCA['list']['sorting']['filter']) && !empty($this->getDC()->arrDCA['list']['sorting']['filter']))
- {
- $arrFilters = array();
- foreach ($this->getDC()->arrDCA['list']['sorting']['filter'] as $filter)
- {
- $arrFilters[] = array('operation' => '=', 'property' => $filter[0], 'value' => $filter[1]);
- }
- if (count($arrFilters))
- {
- $this->getDC()->setFilter(array(array('operation' => 'AND', 'childs' => $arrFilters)));
- }
- }
- if (is_array($this->getDC()->arrDCA['list']['sorting']['root']) && !empty($this->getDC()->arrDCA['list']['sorting']['root']))
- {
- $arrFilters = array();
- foreach ($this->getDC()->arrDCA['list']['sorting']['root'] as $mixId)
- {
- $arrFilters[] = array('operation' => '=', 'property' => 'id', 'value' => $mixId);
- }
- if (count($arrFilters))
- {
- $this->getDC()->setFilter(array(array('operation' => 'OR', 'childs' => $arrFilters)));
- }
- }
- // TODO: we need to transport all the fields from the root conditions via the url and set filters accordingly here.
- // FIXME: this is only valid for mode 4 appearantly, fix for other views.
- if ($this->Input->get('table') && !is_null($this->getDC()->getParentTable()))
- {
- $objParentDP = $this->getDC()->getDataProvider('parent');
- $objParentItem = $objParentDP->fetch($objParentDP->getEmptyConfig()->setId(CURRENT_ID));
- $objCollection = $objParentDP->getEmptyCollection();
- // no parent item found, might have been deleted - we transparently create it for our filter to be able to filter to nothing.
- // TODO: shall we rather bail with "parent not found" than pushing all of this to the database?
- if (!$objParentItem)
- {
- $objParentItem = $objParentDP->getEmptyModel();
- $objParentItem->setID(CURRENT_ID);
- }
- $objCollection->add($objParentItem);
- // NOTE: we set the parent collection here, which will get used in the parentView() routine.
- $this->getDC()->setCurrentParentCollection($objCollection);
- $arrFilter = $this->getDC()->getChildCondition($objParentItem, 'self');
- $this->getDC()->setFilter($arrFilter);
- }
- // FIXME implement panel filter from session
- // FIXME all panels write into $this->getDC()->setFilter() or setLimit.
- return $this->getDC()->getFilter();
- }
- /**
- * Get limit for the data provider
- *
- * @return array
- */
- protected function calculateLimit()
- {
- // Get the limit form the DCA
- if (!is_null($this->getDC()->getLimit()))
- {
- return trimsplit(',', $this->getDC()->getLimit());
- }
- $arrSession = Session::getInstance()->getData();
- $strFilter = ($this->getDC()->arrDCA['list']['sorting']['mode'] == 4) ? $this->getDC()->getTable() . '_' . CURRENT_ID : $this->getDC()->getTable();
- // Load from Session - Set all
- if ($arrSession['filter'][$strFilter]['limit'] == 'all')
- {
- // Get max amount
- $objConfig = $this->getDC()->getDataProvider()->getEmptyConfig()->setFilter($this->getFilter());
- $intMax = $this->getDC()->getDataProvider()->getCount($objConfig);
- $this->getDC()->setLimit("0,$intMax");
- }
- // Load from Session
- else if (strlen($arrSession['filter'][$strFilter]['limit']) != 0)
- {
- $this->getDC()->setLimit($arrSession['filter'][$strFilter]['limit']);
- }
- // Check if the current limit is higher than the limit resultsPerPage
- $arrLimit = trimsplit(",", $this->getDC()->getLimit());
- $intMaxPerPage = $arrLimit[1] - $arrLimit[0];
- if ($intMaxPerPage > $GLOBALS['TL_CONFIG']['resultsPerPage'] && $arrSession['filter'][$strFilter]['limit'] != 'all')
- {
- $this->getDC()->setLimit($arrLimit[0] . ', ' . ($arrLimit[0] + $GLOBALS['TL_CONFIG']['resultsPerPage']));
- }
- // Fallback and limit check
- if (is_null($this->getDC()->getLimit()))
- {
- $this->getDC()->setLimit('0,' . $GLOBALS['TL_CONFIG']['resultsPerPage']);
- }
- return trimsplit(',', $this->getDC()->getLimit());
- }
- /**
- * Set the sorting and first sorting.
- * Use the default ones from DCA or the session values.
- *
- * @todo SH:CS: Add findInSet if we have the functions for foreignKey
- * @return void
- */
- protected function establishSorting()
- {
- // Get sorting fields
- $arrSortingFields = array();
- foreach ($this->getDC()->arrDCA['fields'] as $k => $v)
- {
- if ($v['sorting'])
- {
- if (is_null($v['flag']))
- {
- $arrSortingFields[$k] = DCGE::MODEL_SORTING_ASC;
- }
- else
- {
- $arrSortingFields[$k] = $v['flag'] % 2 ? DCGE::MODEL_SORTING_ASC : DCGE::MODEL_SORTING_DESC;
- }
- }
- }
- $this->getDC()->setSorting(array_keys($arrSortingFields));
- // Check if we have another sorting from session/panels
- $arrSession = Session::getInstance()->getData();
- $strSessionSorting = preg_replace('/\s+.*$/i', '', strval($arrSession['sorting'][$this->getDC()->getTable()]));
- if (isset($arrSortingFields[$strSessionSorting]))
- {
- $this->getDC()->setFirstSorting($strSessionSorting, $arrSortingFields[$strSessionSorting]);
- return;
- }
- // Set default values from DCA
- $arrSorting = (array) $this->getDC()->arrDCA['list']['sorting']['fields'];
- $strFirstSorting = preg_replace('/\s+.*$/i', '', strval($arrSorting[0]));
- if (!isset($this->getDC()->arrDCA['list']['sorting']['flag']))
- {
- $strFirstSortingOrder = DCGE::MODEL_SORTING_ASC;
- }
- else
- {
- $strFirstSortingOrder = $this->getDC()->arrDCA['list']['sorting']['flag'] % 2 ? DCGE::MODEL_SORTING_ASC : DCGE::MODEL_SORTING_DESC;
- }
- if (!strlen($strFirstSorting))
- {
- foreach (array('sorting', 'tstamp', 'pid', 'id') as $strField)
- {
- if ($this->getDC()->getDataProvider()->fieldExists($strField))
- {
- $strFirstSorting = $strField;
- break;
- }
- }
- }
- $this->getDC()->setFirstSorting($strFirstSorting, $strFirstSortingOrder);
- return;
- }
- /* /////////////////////////////////////////////////////////////////////
- * ---------------------------------------------------------------------
- * Core Support functions // Check Function
- * ---------------------------------------------------------------------
- * ////////////////////////////////////////////////////////////////// */
- /**
- * Redirects to the real back end module.
- */
- protected function redirectHome()
- {
- if ($this->Input->get('table') && $this->Input->get('id'))
- {
- $this->redirect(sprintf('contao/main.php?do=%s&table=%s&id=%s', $this->Input->get('do'), $this->getDC()->getTable(), $this->Input->get('id')));
- }
- $this->redirect('contao/main.php?do=' . $this->Input->get('do'));
- }
- /**
- * Check if the curren model support multi language.
- * Load the language from SESSION, POST or use a fallback.
- *
- * @return int return the mode multilanguage, singellanguage, see DCGE.php
- */
- protected function checkLanguage()
- {
- // Load basic informations
- $intID = $this->getDC()->getId();
- $objDataProvider = $this->getDC()->getDataProvider();
- // Check if current dataprovider supports multilanguage
- if (in_array('InterfaceGeneralDataMultiLanguage', class_implements($objDataProvider)))
- {
- $objLanguagesSupported = $this->getDC()->getDataProvider()->getLanguages($intID);
- }
- else
- {
- $objLanguagesSupported = null;
- }
- //Check if we have some languages
- if ($objLanguagesSupported == null)
- {
- return DCGE::LANGUAGE_SL;
- }
- // Load language from Session
- $arrSession = $this->Session->get("dc_general");
- if (!is_array($arrSession))
- {
- $arrSession = array();
- }
- // try to get the language from session
- if (isset($arrSession["ml_support"][$this->getDC()->getTable()][$intID]))
- {
- $strCurrentLanguage = $arrSession["ml_support"][$this->getDC()->getTable()][$intID];
- }
- else
- {
- $strCurrentLanguage = $GLOBALS['TL_LANGUAGE'];
- }
- // Make a array from the collection
- $arrLanguage = array();
- foreach ($objLanguagesSupported as $value)
- {
- $arrLanguage[$value->getID()] = $value->getProperty("name");
- }
- // Get/Check the new language
- if (strlen($this->Input->post("language")) != 0 && $_POST['FORM_SUBMIT'] == 'language_switch')
- {
- if (key_exists($this->Input->post("language"), $arrLanguage))
- {
- $strCurrentLanguage = $this->Input->post("language");
- $arrSession["ml_support"][$this->getDC()->getTable()][$intID] = $strCurrentLanguage;
- }
- else if (key_exists($strCurrentLanguage, $arrLanguage))
- {
- $arrSession["ml_support"][$this->getDC()->getTable()][$intID] = $strCurrentLanguage;
- }
- else
- {
- $objlanguageFallback = $objDataProvider->getFallbackLanguage();
- $strCurrentLanguage = $objlanguageFallback->getID();
- $arrSession["ml_support"][$this->getDC()->getTable()][$intID] = $strCurrentLanguage;
- }
- }
- $this->Session->set("dc_general", $arrSession);
- $objDataProvider->setCurrentLanguage($strCurrentLanguage);
- return DCGE::LANGUAGE_ML;
- }
- /**
- * Check if is editable AND not clodes
- */
- protected function checkIsWritable()
- {
- // Check if table is editable
- if (!$this->getDC()->isEditable())
- {
- $this->log('Table ' . $this->getDC()->getTable() . ' is not editable', 'DC_General - Controller - copy()', TL_ERROR);
- $this->redirect('contao/main.php?act=error');
- }
- // Check if table is editable
- if ((!$this->getDC()->getId()) && $this->getDC()->isClosed())
- {
- $this->log('Table ' . $this->getDC()->getTable() . ' is closed', 'DC_General - Controller - copy()', TL_ERROR);
- $this->redirect('contao/main.php?act=error');
- }
- }
- /* /////////////////////////////////////////////////////////////////////
- * ---------------------------------------------------------------------
- * Clipboard functions
- * ---------------------------------------------------------------------
- * ////////////////////////////////////////////////////////////////// */
- /**
- * Clear clipboard
- * @param boolean $blnRedirect True - redirect to home site
- */
- protected function resetClipboard($blnRedirect = false)
- {
- // Get clipboard
- $arrClipboard = $this->loadClipboard();
- $this->getDC()->setClipboardState(false);
- unset($arrClipboard[$this->getDC()->getTable()]);
- // Save
- $this->saveClipboard($arrClipboard);
- // Redirect
- if ($blnRedirect == true)
- {
- $this->redirectHome();
- }
- // Set DC state
- $this->getDC()->setClipboardState(false);
- }
- /**
- * Check clipboard state. Clear or save state of it.
- */
- protected function checkClipboard()
- {
- $arrClipboard = $this->loadClipboard();
- // Reset Clipboard
- if ($this->Input->get('clipboard') == '1')
- {
- $this->resetClipboard(true);
- }
- // Add new entry
- else if ($this->Input->get('act') == 'paste')
- {
- $this->getDC()->setClipboardState(true);
- $arrClipboard[$this->getDC()->getTable()] = array(
- 'id' => $this->Input->get('id'),
- 'source' => $this->Input->get('source'),
- 'childs' => $this->Input->get('childs'),
- 'mode' => $this->Input->get('mode'),
- 'pdp' => $this->Input->get('pdp'),
- 'cdp' => $this->Input->get('cdp'),
- );
- switch ($this->Input->get('mode'))
- {
- case 'cut':
- // Id Array
- $arrIDs = array();
- $arrIDs[] = $this->Input->get('source');
- switch ($this->arrDCA['list']['sorting']['mode'])
- {
- case 5:
- // Run each id
- for ($i = 0; $i < count($arrIDs); $i++)
- {
- // Get current model
- $objCurrentConfig = $this->getDC()->getDataProvider()->getEmptyConfig();
- $objCurrentConfig->setId($arrIDs[$i]);
- $objCurrentModel = $this->getDC()->getDataProvider()->fetch($objCurrentConfig);
- // Get the join field
- $arrJoinCondition = $this->getDC()->getChildCondition($objCurrentModel, 'self');
- $objChildConfig = $this->getDC()->getDataProvider()->getEmptyConfig();
- $objChildConfig->setFilter($arrJoinCondition);
- $objChildConfig->setIdOnly(true);
- $objChildCollection = $this->getDC()->getDataProvider()->fetchAll($objChildConfig);
- foreach ($objChildCollection as $key => $value)
- {
- if (!in_array($value, $arrIDs))
- {
- $arrIDs[] = $value;
- }
- }
- }
- break;
- }
- $arrClipboard[$this->getDC()->getTable()]['ignoredIDs'] = $arrIDs;
- break;
- }
- $this->getDC()->setClipboard($arrClipboard[$this->getDC()->getTable()]);
- }
- // Check clipboard from session
- else if (key_exists($this->getDC()->getTable(), $arrClipboard))
- {
- $this->getDC()->setClipboardState(true);
- $this->getDC()->setClipboard($arrClipboard[$this->getDC()->getTable()]);
- }
- $this->saveClipboard($arrClipboard);
- }
- protected function loadClipboard()
- {
- $arrClipboard = $this->Session->get('CLIPBOARD');
- if (!is_array($arrClipboard))
- {
- $arrClipboard = array();
- }
- return $arrClipboard;
- }
- protected function saveClipboard($arrClipboard)
- {
- if (is_array($arrClipboard))
- {
- $this->Session->set('CLIPBOARD', $arrClipboard);
- }
- }
- /* /////////////////////////////////////////////////////////////////////
- * ---------------------------------------------------------------------
- * Core Functions
- * ---------------------------------------------------------------------
- * ////////////////////////////////////////////////////////////////// */
- /**
- * Cut and paste
- *
- * <p>
- * -= GET Parameter =-<br/>
- * act - Mode like cut | copy | and co <br/>
- * <br/>
- * after - ID of target element to insert after <br/>
- * into - ID of parent element to insert into <br/>
- * <br/>
- * id - Parent child ID used for redirect <br/>
- * pid - ID of the parent used in list mode 4,5 <br/>
- * source - ID of the element which should moved <br/>
- * <br/>
- * pdp - Parent Data Provider real name <br/>
- * cdp - Current Data Provider real name <br/>
- * <br/>
- * -= Deprecated =-<br/>
- * mode - 1 Insert after | 2 Insert into (NEVER USED AGAIN - Deprecated) <br/>
- * </p>
- */
- public function cut()
- {
- // Checks
- $this->checkIsWritable();
- // Get vars
- $mixAfter = $this->Input->get('after');
- $mixInto = $this->Input->get('into');
- $intId = $this->Input->get('id');
- $mixPid = $this->Input->get('pid');
- $mixSource = $this->Input->get('source');
- $strPDP = $this->Input->get('pdp');
- $strCDP = $this->Input->get('cdp');
- // Deprecated
- $intMode = $this->Input->get('mode');
- $mixChild = $this->Input->get('child');
- // Check basic vars
- if (empty($mixSource) || ( is_null($mixAfter) && is_null($mixInto) ) || empty($strCDP))
- {
- $this->log('Missing parameter for cut in ' . $this->getDC()->getTable(), __CLASS__ . ' - ' . __FUNCTION__, TL_ERROR);
- $this->redirect('contao/main.php?act=error');
- }
- // Get current DataProvider
- if (!empty($strCDP))
- {
- $objCurrentDataProvider = $this->getDC()->getDataProvider($strCDP);
- }
- else
- {
- $objCurrentDataProvider = $this->getDC()->getDataProvider();
- }
- if ($objCurrentDataProvider == null)
- {
- throw new Exception('Could not load current data provider in ' . __CLASS__ . ' - ' . __FUNCTION__);
- }
- // Get parent DataProvider, if set
- $objParentDataProvider = null;
- if (!empty($strPDP))
- {
- $objParentDataProvider = $this->objDC->getDataProvider($strPDP);
- if ($objCurrentDataProvider == null)
- {
- throw new Exception('Could not load parent data provider ' . $strPDP . ' in ' . __CLASS__ . ' - ' . __FUNCTION__);
- }
- }
- // Load the source model
- $objSrcModel = $objCurrentDataProvider->fetch($objCurrentDataProvider->getEmptyConfig()->setId($mixSource));
- // Check mode
- switch ($this->getDC()->arrDCA['list']['sorting']['mode'])
- {
- case 0:
- $this->getNewPosition($objCurrentDataProvider, $objParentDataProvider, $objSrcModel, DCGE::INSERT_AFTER_END, null, 'cut');
- break;
- case 1:
- case 2:
- case 3:
- case 4:
- $this->getNewPosition($objCurrentDataProvider, $objParentDataProvider, $objSrcModel, $mixAfter, $mixInto, 'cut');
- break;
- case 5:
- switch ($intMode)
- {
- case 1: // insert after
- // we want a new item in $strCDP having an optional parent in $strPDP (with pid item $mixPid) just after $mixAfter (in child tree conditions).
- // sadly, with our complex rules an getParent() is IMPOSSIBLE (or in other words way too costly as we would be forced to iterate through all items and check if this item would end up in their child collection).
- // therefore we get the child we want to be next of and set all fields to the same values as in the sibling to end up in the same parent.
- $objOtherChild = $objCurrentDataProvider->fetch($objCurrentDataProvider->getEmptyConfig()->setId($mixAfter));
- $this->getDC()->setSameParent($objSrcModel, $objOtherChild, $strCDP);
- // Update sorting.
- $this->getNewPosition($objCurrentDataProvider, $objParentDataProvider, $objSrcModel, $mixAfter, $mixInto, 'cut');
- break;
- case 2: // insert into
- // we want a new item in $strCDP having an optional parent in $strPDP (with pid item $mixPid) just as child of $mixAfter (in child tree conditions).
- // now check if we want to be inserted as root in our own condition - this means either no "after".
- if (($mixAfter == 0))
- {
- $this->setRoot($objSrcModel, 'self');
- }
- else
- {
- // enforce the child condition from our parent.
- $objMyParent = $objCurrentDataProvider->fetch($objCurrentDataProvider->getEmptyConfig()->setId($mixAfter));
- $this->setParent($objSrcModel, $objMyParent, 'self');
- }
- // Update sorting.
- $this->getNewPosition($objCurrentDataProvider, $objParentDataProvider, $objSrcModel, $mixAfter, $mixInto, 'cut');
- break;
- default:
- $this->log('Unknown create mode for copy in ' . $this->getDC()->getTable(), 'DC_General - Controller - copy()', TL_ERROR);
- $this->redirect('contao/main.php?act=error');
- break;
- }
- break;
- default:
- return vsprintf($this->notImplMsg, 'cut - Mode ' . $this->getDC()->arrDCA['list']['sorting']['mode']);
- break;
- }
- // Save new sorting
- $objCurrentDataProvider->save($objSrcModel);
- // Reset clipboard + redirect
- $this->resetClipboard(true);
- }
- /**
- * Copy a entry and all childs
- *
- * @return string error msg for an unknown mode
- */
- public function copy()
- {
- // Check
- $this->checkIsWritable();
- switch ($this->getDC()->arrDCA['list']['sorting']['mode'])
- {
- case 0:
- case 1:
- case 2:
- case 3:
- $intId = $this->Input->get('id');
- $intPid = (strlen($this->Input->get('pid')) != 0)? $this->Input->get('pid') : 0;
- if (strlen($intId) == 0)
- {
- $this->log('Missing parameter for copy in ' . $this->getDC()->getTable(), 'DC_General - Controller - copy()', TL_ERROR);
- $this->redirect('contao/main.php?act=error');
- }
- // Check
- $this->checkIsWritable();
- $this->checkLanguage($this->getDC());
- // Load fields and co
- $this->getDC()->loadEditableFields();
- $this->getDC()->setWidgetID($this->getDC()->getId());
- // Check if we have fields
- if (!$this->getDC()->hasEditableFields())
- {
- $this->redirect($this->getReferer());
- }
- // Load something
- $this->getDC()->preloadTinyMce();
- // Load record from data provider - Load the source model
- $objDataProvider = $this->getDC()->getDataProvider();
- $objSrcModel = $objDataProvider->fetch($objDataProvider->getEmptyConfig()->setId($intId));
- $objDBModel = clone $objSrcModel;
- $objDBModel->setMeta(DCGE::MODEL_IS_CHANGED, true);
- $this->getDC()->setCurrentModel($objDBModel);
- // Check if we have a auto submit
- $this->getDC()->updateModelFromPOST();
- // Check submit
- if ($this->getDC()->isSubmitted() == true)
- {
- if (isset($_POST["save"]))
- {
- // process input and update changed properties.
- if ($this->doSave($this->getDC()) !== false)
- {
- $this->reload();
- }
- }
- else if (isset($_POST["saveNclose"]))
- {
- // process input and update changed properties.
- if ($this->doSave($this->getDC()) !== false)
- {
- setcookie('BE_PAGE_OFFSET', 0, 0, '/');
- $_SESSION['TL_INFO'] = '';
- $_SESSION['TL_ERROR'] = '';
- $_SESSION['TL_CONFIRM'] = '';
- $this->redirect($this->getReferer());
- }
- }
- // Maybe Callbacks ? Yes, this is the first version of an simple
- // button callback system like dc_memory.
- else
- {
- $arrButtons = $this->getDC()->arrDCA['buttons'];
- if (is_array($arrButtons))
- {
- foreach ($arrButtons as $arrButton)
- {
- if (empty($arrButton) || !is_array($arrButton))
- {
- continue;
- }
- if (key_exists($arrButton['formkey'], $_POST))
- {
- $strClass = $arrButton['button_callback'][0];
- $strMethod = $arrButton['button_callback'][1];
- $this->import($strClass);
- $this->$strClass->$strMethod($this->getDC());
- break;
- }
- }
- }
- if (Input::getInstance()->post('SUBMIT_TYPE') !== 'auto')
- {
- $this->reload();
- }
- }
- }
- return;
- case 5:
- // Init Vars
- $intMode = $this->Input->get('mode');
- $intPid = $this->Input->get('pid');
- $intId = $this->Input->get('id');
- $intChilds = $this->Input->get('childs');
- if (strlen($intMode) == 0 || strlen($intPid) == 0 || strlen($intId) == 0)
- {
- $this->log('Missing parameter for copy in ' . $this->getDC()->getTable(), 'DC_General - Controller - copy()', TL_ERROR);
- $this->redirect('contao/main.php?act=error');
- }
- // Get the join field
- $arrJoinCondition = $this->getDC()->getJoinConditions('self');
- // Insert the copy
- $this->insertCopyModel($intId, $intPid, $intMode, $intChilds, $arrJoinCondition[0]['srcField'], $arrJoinCondition[0]['dstField'], $arrJoinCondition[0]['operation']);
- break;
- default:
- return vsprintf($this->notImplMsg, 'copy - Mode ' . $this->getDC()->arrDCA['list']['sorting']['mode']);
- break;
- }
- // Reset clipboard + redirect
- $this->resetClipboard(true);
- }
- /**
- * Create a new entry
- */
- public function create()
- {
- // Checks
- $this->checkIsWritable();
- $this->checkLanguage();
- // Load current values
- $objCurrentDataProvider = $this->getDC()->getDataProvider();
- // Load fields and co
- $this->getDC()->loadEditableFields();
- $this->getDC()->setWidgetID($this->getDC()->getId());
- // Check if we have fields
- if (!$this->getDC()->hasEditableFields())
- {
- $this->redirect($this->getReferer());
- }
- // Load something
- $this->getDC()->preloadTinyMce();
- // Load record from data provider
- $objDBModel = $objCurrentDataProvider->getEmptyModel();
- $this->getDC()->setCurrentModel($objDBModel);
- if ($this->getDC()->arrDCA['list']['sorting']['mode'] < 4)
- {
- // check if the pid id/word is set
- if ($this->Input->get('pid'))
- {
- $objParentDP = $this->objDC->getDataProvider('parent');
- $objParent = $objParentDP->fetch($objParentDP->getEmptyConfig()->setId($this->Input->get('pid')));
- $this->setParent($objDBModel, $objParent, 'self');
- }
- }
- else if ($this->getDC()->arrDCA['list']['sorting']['mode'] == 4)
- {
- // check if the pid id/word is set
- if ($this->Input->get('pid') == '')
- {
- $this->log('Missing pid for new entry in ' . $this->getDC()->getTable(), 'DC_General - Controller - create()', TL_ERROR);
- $this->redirect('contao/main.php?act=error');
- }
- $objDBModel->setProperty('pid', $this->Input->get('pid'));
- }
- else if ($this->getDC()->arrDCA['list']['sorting']['mode'] == 5 && $this->Input->get('mode') != '')
- {
- /**
- * Create in mode 5
- *
- * <p>
- * -= GET Parameter =-<br/>
- * act - create <br/>
- * after - ID of target element <br/>
- * mode - 1 Insert after | 2 Insert into <br/>
- * pid - Id of the parent used in list mode 4,5 <br/>
- * pdp - Parent Data Provider real name <br/>
- * cdp - Current Data Provider real name <br/>
- * id - Parent child id used for redirect <br/>
- * </p>
- */
- // Get vars
- $mixAfter = $this->Input->get('after');
- $intMode = $this->Input->get('mode');
- $mixPid = $this->Input->get('pid');
- $strPDP = $this->Input->get('pdp');
- $strCDP = $this->Input->get('cdp');
- $intId = $this->Input->get('id');
- // Check basic vars
- if (is_null($mixAfter) || empty($intMode) || empty($strCDP))
- {
- $this->log('Missing parameter for create in ' . $this->getDC()->getTable(), __CLASS__ . ' - ' . __FUNCTION__, TL_ERROR);
- $this->redirect('contao/main.php?act=error');
- }
- // Load current data provider
- $objCurrentDataProvider = $this->objDC->getDataProvider($strCDP);
- if ($objCurrentDataProvider == null)
- {
- throw new Exception('Could not load current data provider in ' . __CLASS__ . ' - ' . __FUNCTION__);
- }
- $objParentDataProvider = null;
- if (!empty($strPDP))
- {
- $objParentDataProvider = $this->objDC->getDataProvider($strPDP);
- if ($objParentDataProvider == null)
- {
- throw new Exception('Could not load parent data provider ' . $strPDP . ' in ' . __CLASS__ . ' - ' . __FUNCTION__);
- }
- }
- // first enforce the parent table conditions, if we have an parent.
- if (($strPDP != $strCDP) && $mixPid)
- {
- // parenting entry is root? we want to become so too.
- if ($this->isRootEntry($strPDP, $mixPid))
- {
- $this->setRoot($objDBModel, $strPDP);
- }
- else
- {
- // we have some parent model and can use that one.
- $objParentModel = $objParentDataProvider->fetch($objParentDataProvider->getEmptyConfig()->setId($mixPid));
- $this->setParent($objDBModel, $objParentModel, $strPDP);
- }
- // TODO: update sorting here.
- }
- switch ($this->Input->get('mode'))
- {
- case 1: // insert after
- // we want a new item in $strCDP having an optional parent in $strPDP (with pid item $mixPid) just after $mixAfter (in child tree conditions).
- // sadly, with our complex rules an getParent() is IMPOSSIBLE (or in other words way too costly as we would be forced to iterate through all items and check if this item would end up in their child collection).
- // therefore we get the child we want to be next of and set all fields to the same values as in the sibling to end up in the same parent.
- $objOtherChild = $objCurrentDataProvider->fetch($objCurrentDataProvider->getEmptyConfig()->setId($mixAfter));
- $this->getDC()->setSameParent($objDBModel, $objOtherChild, $strCDP);
- // TODO: update sorting here.
- break;
- case 2: // insert into
- // we want a new item in $strCDP having an optional parent in $strPDP (with pid item $mixPid) just as child of $mixAfter (in child tree conditions).
- // now check if we want to be inserted as root in our own condition - this means either no "after".
- if (($mixAfter == 0))
- {
- $this->setRoot($objDBModel, 'self');
- }
- else
- {
- // enforce the child condition from our parent.
- $objMyParent = $objCurrentDataProvider->fetch($objCurrentDataProvider->getEmptyConfig()->setId($mixAfter));
- $this->setParent($objDBModel, $objMyParent, 'self');
- }
- // TODO: update sorting here.
- break;
- default:
- $this->log('Unknown create mode for new entry in ' . $this->getDC()->getTable(), 'DC_General - Controller - create()', TL_ERROR);
- $this->redirect('contao/main.php?act=error');
- break;
- }
- // Reset clipboard
- $this->resetClipboard();
- }
- // Check if we have a auto submit
- $this->getDC()->updateModelFromPOST();
- // Check submit
- if ($this->getDC()->isSubmitted() == true && !$this->getDC()->isNoReload())
- {
- try
- {
- // Get new Position
- $strPDP = $this->Input->get('pdp');
- $strCDP = $this->Input->get('cdp');
- $mixAfter = $this->Input->get('after');
- $mixInto = $this->Input->get('into');
- $this->getNewPosition($this->objDC->getDataProvider($strPDP), $this->objDC->getDataProvider($strCDP), $this->objDC->getCurrentModel(), $mixAfter, $mixInto, 'create');
- if (isset($_POST["save"]))
- {
- // process input and update changed properties.
- if (($objModell = $this->doSave($this->getDC())) !== false)
- {
- // Callback
- $this->getDC()->getCallbackClass()->oncreateCallback($objDBModel->getID(), $objDBModel->getPropertiesAsArray());
- // Log
- $this->log('A new entry in table "' . $this->getDC()->getTable() . '" has been created (ID: ' . $objModell->getID() . ')', 'DC_General - Controller - create()', TL_GENERAL);
- // Redirect
- $this->redirect($this->addToUrl("id=" . $objDBModel->getID() . "&act=edit"));
- }
- }
- else if (isset($_POST["saveNclose"]))
- {
- // process input and update changed properties.
- if (($objModell = $this->doSave($this->getDC())) !== false)
- {
- setcookie('BE_PAGE_OFFSET', 0, 0, '/');
- $_SESSION['TL_INFO'] = '';
- $_SESSION['TL_ERROR'] = '';
- $_SESSION['TL_CONFIRM'] = '';
- // Callback
- $this->getDC()->getCallbackClass()->oncreateCallback($objDBModel->getID(), $objDBModel->getPropertiesAsArray());
- // Log
- $this->log('A new entry in table "' . $this->getDC()->getTable() . '" has been created (ID: ' . $objModell->getID() . ')', 'DC_General - Controller - create()', TL_GENERAL);
- // Redirect
- $this->redirect($this->getReferer());
- }
- }
- else
- {
- $arrButtons = $this->getDC()->arrDCA['buttons'];
- if (is_array($arrButtons))
- {
- foreach ($arrButtons as $arrButton)
- {
- if (empty($arrButton) || !is_array($arrButton))
- {
- continue;
- }
- if (key_exists($arrButton['formkey'], $_POST))
- {
- $strClass = $arrButton['button_callback'][0];
- $strMethod = $arrButton['button_callback'][1];
- $this->import($strClass);
- $this->$strClass->$strMethod($this->getDC());
- break;
- }
- }
- }
- }
- }
- catch (Exception $exc)
- {
- $_SESSION['TL_ERROR'][] = sprintf('Exception: %s in file %s on line %s', $exc->getMessage(), $exc->getFile(), $exc->getLine());
- }
- }
- }
- /**
- * Recurse through all childs in mode 5 and return their Ids.
- */
- protected function fetchMode5ChildsOf($objParentModel, $blnRecurse = true)
- {
- $arrJoinCondition = $this->getDC()->getChildCondition($objParentModel, 'self');
- // Build filter
- $objChildConfig = $this->getDC()->getDataProvider()->getEmptyConfig();
- $objChildConfig->setFilter($arrJoinCondition);
- // Get child collection
- $objChildCollection = $this->getDC()->getDataProvider()->fetchAll($objChildConfig);
- $arrIDs = array();
- foreach ($objChildCollection as $objChildModel)
- {
- $arrIDs[] = $objChildModel->getID();
- if ($blnRecurse)
- {
- $arrIDs = array_merge($arrIDs, $this->fetchMode5ChildsOf($objChildModel, $blnRecurse));
- }
- }
- return $arrIDs;
- }
- public function delete()
- {
- // Load current values
- $objCurrentDataProvider = $this->getDC()->getDataProvider();
- // Init some vars
- $intRecordID = $this->getDC()->getId();
- // Check if we have a id
- if (strlen($intRecordID) == 0)
- {
- $this->reload();
- }
- // Check if is it allowed to delete a record
- if ($this->getDC()->arrDCA['config']['notDeletable'])
- {
- $this->log('Table "' . $this->getDC()->getTable() . '" is not deletable', 'DC_General - Controller - delete()', TL_ERROR);
- $this->redirect('contao/main.php?act=error');
- }
- // Callback
- $this->getDC()->setCurrentModel($objCurrentDataProvider->fetch($objCurrentDataProvider->getEmptyConfig()->setId($intRecordID)));
- $this->getDC()->getCallbackClass()->ondeleteCallback();
- $arrDelIDs = array();
- // Delete record
- switch ($this->getDC()->arrDCA['list']['sorting']['mode'])
- {
- case 0:
- case 1:
- case 2:
- case 3:
- case 4:
- $arrDelIDs = array();
- $arrDelIDs[] = $intRecordID;
- break;
- case 5:
- $arrDelIDs = $this->fetchMode5ChildsOf($this->getDC()->getCurrentModel(), $blnRecurse = true);
- $arrDelIDs[] = $intRecordID;
- break;
- }
- // Delete all entries
- foreach ($arrDelIDs as $intId)
- {
- $this->getDC()->getDataProvider()->delete($intId);
- // Add a log entry unless we are deleting from tl_log itself
- if ($this->getDC()->getTable() != 'tl_log')
- {
- $this->log('DELETE FROM ' . $this->getDC()->getTable() . ' WHERE id=' . $intId, 'DC_General - Controller - delete()', TL_GENERAL);
- }
- }
- $this->redirect($this->getReferer());
- }
- public function edit()
- {
- // Load some vars
- $objCurrentDataProvider = $this->getDC()->getDataProvider();
- // Check
- $this->checkIsWritable();
- $this->checkLanguage($this->getDC());
- // Load an older Version
- if (strlen($this->Input->post("version")) != 0 && $this->getDC()->isVersionSubmit())
- {
- $this->loadVersion($this->getDC()->getId(), $this->Input->post("version"));
- }
- // Load fields and co
- $this->getDC()->loadEditableFields();
- $this->getDC()->setWidgetID($this->getDC()->getId());
- // Check if we have fields
- if (!$this->getDC()->hasEditableFields())
- {
- $this->redirect($this->getReferer());
- }
- // Load something
- $this->getDC()->preloadTinyMce();
- // Load record from data provider
- $objDBModel = $objCurrentDataProvider->fetch($objCurrentDataProvider->getEmptyConfig()->setId($this->getDC()->getId()));
- if ($objDBModel == null)
- {
- $objDBModel = $objCurrentDataProvider->getEmptyModel();
- }
- $this->getDC()->setCurrentModel($objDBModel);
- // Check if we have a auto submit
- $this->getDC()->updateModelFromPOST();
- // Check submit
- if ($this->getDC()->isSubmitted() == true)
- {
- if (isset($_POST["save"]))
- {
- // process input and update changed properties.
- if ($this->doSave($this->getDC()) !== false)
- {
- $this->reload();
- }
- }
- else if (isset($_POST["saveNclose"]))
- {
- // process input and update changed properties.
- if ($this->doSave($this->getDC()) !== false)
- {
- setcookie('BE_PAGE_OFFSET', 0, 0, '/');
- $_SESSION['TL_INFO'] = '';
- $_SESSION['TL_ERROR'] = '';
- $_SESSION['TL_CONFIRM'] = '';
- $this->redirect($this->getReferer());
- }
- }
- // Maybe Callbacks ? Yes, this is the first version of an simple
- // button callback system like dc_memory.
- else
- {
- $arrButtons = $this->getDC()->arrDCA['buttons'];
- if (is_array($arrButtons))
- {
- foreach ($arrButtons as $arrButton)
- {
- if (empty($arrButton) || !is_array($arrButton))
- {
- continue;
- }
- if (key_exists($arrButton['formkey'], $_POST))
- {
- $strClass = $arrButton['button_callback'][0];
- $strMethod = $arrButton['button_callback'][1];
- $this->import($strClass);
- $this->$strClass->$strMethod($this->getDC());
- break;
- }
- }
- }
- if (Input::getInstance()->post('SUBMIT_TYPE') !== 'auto')
- {
- $this->reload();
- }
- }
- }
- }
- /**
- * Show informations about one entry
- */
- public function show()
- {
- // Load check multi language
- $objCurrentDataProvider = $this->getDC()->getDataProvider();
- // Check
- $this->checkLanguage($this->getDC());
- // Load record from data provider
- $objDBModel = $objCurrentDataProvider->fetch($objCurrentDataProvider->getEmptyConfig()->setId($this->getDC()->getId()));
- if ($objDBModel == null)
- {
- $this->log('Could not find ID ' . $this->getDC()->getId() . ' in Table ' . $this->getDC()->getTable() . '.', 'DC_General show()', TL_ERROR);
- $this->redirect('contao/main.php?act=error');
- }
- $this->getDC()->setCurrentModel($objDBModel);
- }
- /**
- * Show all entries from a table
- *
- * @return void | String if error
- */
- public function showAll()
- {
- // Checks
- $this->checkClipboard();
- $this->checkPanelSubmit();
- // Setup
- $this->getDC()->setButtonId('tl_buttons');
- $this->establishSorting();
- $this->getFilter();
- $this->generatePanelFilter('set');
- // Switch mode
- switch ($this->getDC()->arrDCA['list']['sorting']['mode'])
- {
- case 0:
- case 1:
- case 2:
- case 3:
- $this->viewList();
- break;
- case 4:
- $this->viewParent();
- break;
- case 5:
- $this->treeViewM5();
- break;
- default:
- return vsprintf($this->notImplMsg, 'showAll - Mode ' . $this->getDC()->arrDCA['list']['sorting']['mode']);
- break;
- }
- // keep panel after real view compilation, as in there the limits etc will get compiled.
- $this->panel($this->getDC());
- }
- /* /////////////////////////////////////////////////////////////////////
- * ---------------------------------------------------------------------
- * AJAX
- * ---------------------------------------------------------------------
- * ////////////////////////////////////////////////////////////////// */
- public function ajaxTreeView($intID, $intLevel)
- {
- // Load current informations
- $objCurrentDataProvider = $this->getDC()->getDataProvider();
- $strToggleID = $this->getDC()->getTable() . '_tree';
- $arrToggle = $this->Session->get($strToggleID);
- if (!is_array($arrToggle))
- {
- $arrToggle = array();
- }
- $arrToggle[$intID] = 1;
- $this->Session->set($strToggleID, $arrToggle);
- // Init some vars
- $objTableTreeData = $objCurrentDataProvider->getEmptyCollection();
- $objRootConfig = $objCurrentDataProvider->getEmptyConfig();
- $objRootConfig->setId($intID);
- $objModel = $objCurrentDataProvider->fetch($objRootConfig);
- $this->treeWalkModel($objModel, $intLevel, $arrToggle, array('self'));
- foreach ($objModel->getMeta(DCGE::TREE_VIEW_CHILD_COLLECTION) as $objCollection)
- {
- foreach ($objCollection as $objSubModel)
- {
- $objTableTreeData->add($objSubModel);
- }
- }
- $this->getDC()->setCurrentCollecion($objTableTreeData);
- }
- /**
- * Loads the current model from the data provider and overrides the selector
- *
- * @param type $strSelector the name of the checkbox toggling the palette.
- */
- public function generateAjaxPalette($strSelector)
- {
- // Check
- $this->checkIsWritable();
- $this->checkLanguage($this->getDC());
- // Load fields and co
- $this->getDC()->loadEditableFields();
- $this->getDC()->setWidgetID($this->getDC()->getId());
- // Check if we have fields
- if (!$this->getDC()->hasEditableFields())
- {
- $this->redirect($this->getReferer());
- }
- // Load something
- $this->getDC()->preloadTinyMce();
- $objDataProvider = $this->getDC()->getDataProvider();
- // Load record from data provider
- $objDBModel = $objDataProvider->fetch($objDataProvider->getEmptyConfig()->setId($this->getDC()->getId()));
- if ($objDBModel == null)
- {
- $objDBModel = $objDataProvider->getEmptyModel();
- }
- $this->getDC()->setCurrentModel($objDBModel);
- // override the setting from POST now.
- $objDBModel->setProperty($strSelector, intval($this->Input->post('state')));
- }
- /* /////////////////////////////////////////////////////////////////////
- * ---------------------------------------------------------------------
- * Edit modes
- * ---------------------------------------------------------------------
- * ////////////////////////////////////////////////////////////////// */
- /**
- * Load an older version
- */
- protected function loadVersion($intID, $mixVersion)
- {
- $objCurrentDataProvider = $this->getDC()->getDataProvider();
- // Load record from version
- $objVersionModel = $objCurrentDataProvider->getVersion($intID, $mixVersion);
- // Redirect if there is no record with the given ID
- if ($objVersionModel == null)
- {
- $this->log('Could not load record ID ' . $intID . ' of table "' . $this->getDC()->getTable() . '"', 'DC_General - Controller - edit()', TL_ERROR);
- $this->redirect('contao/main.php?act=error');
- }
- $objCurrentDataProvider->save($objVersionModel);
- $objCurrentDataProvider->setVersionActive($intID, $mixVersion);
- // Callback onrestoreCallback
- $arrData = $objVersionModel->getPropertiesAsArray();
- $arrData["id"] = $objVersionModel->getID();
- $this->getDC()->getCallbackClass()->onrestoreCallback($intID, $this->getDC()->getTable(), $arrData, $mixVersion);
- $this->log(sprintf('Version %s of record ID %s (table %s) has been restored', $this->Input->post('version'), $this->getDC()->getId(), $this->getDC()->getTable()), 'DC_General - Controller - edit()', TL_GENERAL);
- // Reload page with new recored
- $this->reload();
- }
- /**
- * Perform low level saving of the current model in a DC.
- * NOTE: the model will get populated with the new values within this function.
- * Therefore the current submitted data will be stored within the model but only on
- * success also be saved into the DB.
- *
- * @return bool|InterfaceGeneralModel Model if the save operation was successful or unnecessary, false otherwise.
- */
- protected function doSave()
- {
- $objDBModel = $this->getDC()->getCurrentModel();
- // Check if table is closed
- if ($this->getDC()->arrDCA['config']['closed'] && !($objDBModel->getID()))
- {
- // TODO show alarm message
- $this->redirect($this->getReferer());
- }
- // if we may not store the value, we keep the changes
- // in the current model and return (DO NOT SAVE!).
- if ($this->getDC()->isNoReload() == true)
- {
- return false;
- }
- // Callback
- $this->getDC()->getCallbackClass()->onsubmitCallback();
- // Refresh timestamp
- if ($this->getDC()->getDataProvider()->fieldExists("tstamp") == true)
- {
- $objDBModel->setProperty("tstamp", time());
- }
- // Callback
- $this->getDC()->getCallbackClass()->onsaveCallback($objDBModel);
- // Check if we have a field with eval->alwaysSave
- foreach ($this->objDC->getFieldList() as $arrFieldSettings)
- {
- if($arrFieldSettings['eval']['alwaysSave'] == true)
- {
- $objDBModel->setMeta(DCGE::MODEL_IS_CHANGED, true);
- break;
- }
- }
- // $this->getNewPosition($objDBModel, 'create', null, false);
- // everything went ok, now save the new record
- if (!$objDBModel->getMeta(DCGE::MODEL_IS_CHANGED) && ($objDBModel->getID()))
- {
- return $objDBModel;
- }
- $this->getDC()->getDataProvider()->save($objDBModel);
- // Check if versioning is enabled
- if (isset($this->getDC()->arrDCA['config']['enableVersioning']) && $this->getDC()->arrDCA['config']['enableVersioning'] == true)
- {
- // Compare version and current record
- $mixCurrentVersion = $this->getDC()->getDataProvider()->getActiveVersion($objDBModel->getID());
- if ($mixCurrentVersion != null)
- {
- $mixCurrentVersion = $this->getDC()->getDataProvider()->getVersion($objDBModel->getID(), $mixCurrentVersion);
- if ($this->getDC()->getDataProvider()->sameModels($objDBModel, $mixCurrentVersion) == false)
- {
- // TODO: FE|BE switch
- $this->import('BackendUser', 'User');
- $this->getDC()->getDataProvider()->saveVersion($objDBModel, $this->User->username);
- }
- }
- else
- {
- // TODO: FE|BE switch
- $this->import('BackendUser', 'User');
- $this->getDC()->getDataProvider()->saveVersion($objDBModel, $this->User->username);
- }
- }
- // Return the current model
- return $objDBModel;
- }
- /**
- * Calculate the new position of an element
- *
- * Warning this function needs the cdp (current data provider).
- * Warning this function needs the pdp (parent data provider).
- *
- * Based on backbone87 PR - "creating items in parent modes generates sorting value of 0"
- *
- * @param InterfaceGeneralData $objCDP - Current data provider
- * @param InterfaceGeneralData $objPDP - Parent data provider
- * @param InterfaceGeneralModel $objDBModel - Model of element which should moved
- * @param mixed $mixAfter - Target element
- * @param string $strMode - Mode like cut | create and so on
- * @param integer $intInsertMode - Insert Mode => 1 After | 2 Into
- * @param mixed $mixParentID - Parent ID of table or element
- *
- * @return void
- */
- protected function getNewPosition($objCDP, $objPDP, $objDBModel, $mixAfter, $mixInto, $strMode, $mixParentID = null, $intInsertMode = null, $blnWithoutReorder = false)
- {
- // Check if we have a sorting field, if not skip here.
- if (!$objCDP->fieldExists('sorting'))
- {
- return;
- }
- // Load default DataProvider.
- if (is_null($objCDP))
- {
- $objCDP = $this->getDC()->getDataProvider();
- }
- if ($mixAfter === DCGE::INSERT_AFTER_START)
- {
- $mixAfter = 0;
- }
- // Search for the highest sorting. Default - Add to end off all.
- // ToDo: We have to check the child <=> parent condition . To get all sortings for one level.
- // If we get a after 0, add to top.
- if ($mixAfter === 0) {
- // Build filter for conditions
- $arrFilter = array();
- if (in_array($this->getDC()->arrDCA['list']['sorting']['mode'], array(4, 5, 6)))
- {
- $arrConditions = $this->objDC->getRootConditions($objCDP->getEmptyModel()->getProviderName());
- if ($arrConditions)
- {
- foreach ($arrConditions as $arrCondition)
- {
- if (key_exists('remote', $arrCondition))
- {
- $arrFilter[] = array(
- 'value' => Input::getInstance()->get($arrCondition['remote']),
- 'property' => $arrCondition['property'],
- 'operation' => $arrCondition['operation']
- );
- }
- else if (key_exists('remote_value', $arrCondition))
- {
- $arrFilter[] = array(
- 'value' => Input::getInstance()->get($arrCondition['remote_value']),
- 'property' => $arrCondition['property'],
- 'operation' => $arrCondition['operation']
- );
- }
- else
- {
- $arrFilter[] = array(
- 'value' => $arrCondition['value'],
- 'property' => $arrCondition['property'],
- 'operation' => $arrCondition['operation']
- );
- }
- }
- }
- }
- // Build config
- $objConfig = $objCDP->getEmptyConfig();
- $objConfig->setFields(array('sorting'));
- $objConfig->setSorting(array('sorting' => DCGE::MODEL_SORTING_ASC));
- $objConfig->setAmount(1);
- $objConfig->setFilter($arrFilter);
- $objCollection = $objCDP->fetchAll($objConfig);
- if ($objCollection->length())
- {
- $intLowestSorting = $objCollection->get(0)->getProperty('sorting');
- $intNextSorting = round($intLowestSorting / 2);
- }
- else
- {
- $intNextSorting = 256;
- }
- // Check if we have a valide sorting.
- if (($intLowestSorting < 2 || $intNextSorting <= 2) && !$blnWithoutReorder)
- {
- // ToDo: Add child <=> parent config.
- $objConfig = $objCDP->getEmptyConfig();
- $objConfig->setFilter($arrFilter);
- $this->reorderSorting($objConfig);
- $this->getNewPosition($objCDP, $objPDP, $objDBModel, $mixAfter, $mixInto, $strMode, $mixParentID, $intInsertMode, true);
- return;
- }
- // Fallback to valid sorting.
- else if ($intNextSorting <= 2)
- {
- $intNextSorting = 256;
- }
- $objDBModel->setProperty('sorting', $intNextSorting);
- }
- // If we get a after, search for the right value.
- else if (!empty($mixAfter))
- {
- // Init some vars.
- $intAfterSorting = 0;
- $intNextSorting = 0;
- // Get "after" sorting value value.
- $objAfterConfig = $objCDP->getEmptyConfig();
- $objAfterConfig->setAmount(1);
- $objAfterConfig->setFilter(array(array(
- 'value' => $mixAfter,
- 'property' => 'id',
- 'operation' => '='
- )));
- $objAfterCollection = $objCDP->fetch…
Large files files are truncated, but you can click here to view the full file