Пример #1
0
 /**
  * Auto-Generate an alias for an entity.
  *
  * @param string      $alias
  * @param \DC_General $dc
  *
  * @return string
  * @throws Exception
  */
 public static function generateAlias($alias, \DC_General $dc)
 {
     /** @var EntityInterface $entity */
     $entity = $dc->getCurrentModel()->getEntity();
     $autoAlias = false;
     // Generate alias if there is none
     if (!strlen($alias)) {
         $autoAlias = true;
         if ($entity->__has('title')) {
             $alias = standardize($entity->getTitle());
         } elseif ($entity->__has('name')) {
             $alias = standardize($entity->getName());
         } else {
             return '';
         }
     }
     $entityClass = new \ReflectionClass($entity);
     $keys = explode(',', $entityClass->getConstant('KEY'));
     $entityManager = EntityHelper::getEntityManager();
     $queryBuilder = $entityManager->createQueryBuilder();
     $queryBuilder->select('COUNT(e.' . $keys[0] . ')')->from($entityClass->getName(), 'e')->where($queryBuilder->expr()->eq('e.alias', ':alias'))->setParameter(':alias', $alias);
     static::extendQueryWhereId($queryBuilder, $dc->getCurrentModel()->getID(), $entity);
     $query = $queryBuilder->getQuery();
     $duplicateCount = $query->getResult(Query::HYDRATE_SINGLE_SCALAR);
     // Check whether the news alias exists
     if ($duplicateCount && !$autoAlias) {
         throw new Exception(sprintf($GLOBALS['TL_LANG']['ERR']['aliasExists'], $alias));
     }
     // Add ID to alias
     if ($duplicateCount && $autoAlias) {
         $alias .= '-' . $dc->getCurrentModel()->getID();
     }
     return $alias;
 }
 /**
  * Callback when an item is opened in edit view.
  * This method filters invariant attributes from the palette, when editing a variant.
  *
  * @param string the input palette
  *
  * @return string the filtered palette.
  */
 public function parseRootPaletteCallback($arrPalette)
 {
     $objModelRow = $this->objDC->getCurrentModel();
     if ($objModelRow) {
         $objNativeItem = $objModelRow->getItem();
         $objMetaModel = $objNativeItem->getMetaModel();
         if ($objMetaModel->hasVariants() && !$objNativeItem->isVariantBase()) {
             // loop over all attributes and remove those from rendering that are invariant.
             foreach (array_keys($objMetaModel->getInVariantAttributes()) as $strAttrName) {
                 foreach ($arrPalette as $intKey => $arrPaletteDef) {
                     $this->removeAttributeFromPalette($strAttrName, $arrPalette[$intKey]['palette']);
                     if (!count($arrPalette[$intKey]['palette'])) {
                         unset($arrPalette[$intKey]);
                     }
                 }
             }
         }
     }
     $arrPalette = parent::parseRootPaletteCallback($arrPalette);
     return $arrPalette;
 }
Пример #3
0
 /**
  * Creates or renames the MetaModel table according to the given name.
  * Updates variant support information.
  *
  * @param DC_General $objDC the datacontainer where the model is loaded.
  *
  * @return void
  */
 public function onSubmitCallback(DC_General $objDC)
 {
     // table name changed?
     $strOldTableName = '';
     if ($objDC->getId()) {
         $objMetaModel = $this->Database->prepare("SELECT tableName FROM tl_metamodel WHERE id=?")->limit(1)->executeUncached($objDC->getId());
         if ($objMetaModel->numRows) {
             $strOldTableName = $objMetaModel->tableName;
         }
     }
     $objDBModel = $objDC->getCurrentModel();
     $strNewTableName = $objDBModel->getProperty('tableName');
     // table name is different.
     if ($strNewTableName != $strOldTableName) {
         if ($strOldTableName && $this->Database->tableExists($strOldTableName, null, true)) {
             MetaModelTableManipulation::renameTable($strOldTableName, $strNewTableName);
             // TODO: notify fields that the MetaModel has changed its table name.
         } else {
             MetaModelTableManipulation::createTable($strNewTableName);
         }
     }
     MetaModelTableManipulation::setVariantSupport($strNewTableName, $objDBModel->getProperty('varsupport'));
 }
 /**
  * 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() . "&amp;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());
         }
     }
 }
Пример #5
0
 /**
  * Returns an array with all valid tables that can be used as parent table.
  *
  * Excludes the metamodel table itself in ctable mode, as that one would be "selftree" then and not ctable.
  *
  * @param DC_General $objDC The general DataContainer calling us.
  *
  * @return string[] the tables.
  */
 public function getTables(DC_General $objDC)
 {
     if ($objDC->getCurrentModel()->getProperty('rendertype') == 'ctable') {
         $blnOmit = $this->getMetaModel($objDC)->getTableName();
     }
     $tables = array();
     foreach ($this->Database->listTables() as $table) {
         if (!($blnOmit && $blnOmit == $table)) {
             $tables[$table] = $table;
         }
     }
     return $tables;
 }