示例#1
0
 /**
  * compile wrapper element
  */
 protected function compile()
 {
     // get included elements
     if ($this->objWrapper->getType() == ContentWrapper\Model::TYPE_START) {
         $this->Template->count = ContentWrapper\Repository::countRelatedElements($this->objWrapper);
         $cssID = $this->cssID;
         if ($cssID[0] == '') {
             $cssID[0] = sprintf($this->strIdentifier, $this->id);
             $this->cssID = $cssID;
         }
     } else {
         $start = \ContentModel::findByPk($this->bootstrap_parentId);
         if ($start !== null) {
             $start = new Attributes($start);
             $start->registerNamespaceAttributes($this->arrBootstrapAttributes);
             $start->cssID = deserialize($start->cssID, true);
             $this->Template->start = $start;
             if ($start->cssID[0] == '') {
                 $cssID = $start->cssID;
                 $cssID[0] = sprintf($this->strIdentifier, $start->id);
                 $this->cssID = $cssID;
             } else {
                 $this->cssID = $start->cssID;
             }
         }
     }
     $this->Template->identifier = $this->cssID[0];
 }
示例#2
0
 /**
  * Try to create wrapper elements, triggered by save_callback of type field
  * @param $value
  * @param $dc
  *
  * @return mixed
  * @throws \Exception
  */
 public function save($value, $dc)
 {
     // shortcuts
     $start = ContentWrapper\Model::TYPE_START;
     $stop = ContentWrapper\Model::TYPE_STOP;
     $sep = ContentWrapper\Model::TYPE_SEPARATOR;
     // handle issues with contao models
     if (version_compare(VERSION, '3.2', '>=')) {
         try {
             $model = new \ContentModel($dc->activeRecord);
         } catch (\Exception $e) {
             $model = \ContentModel::findByPk($dc->id);
         }
     } else {
         $model = new \ContentModel($dc->activeRecord);
     }
     $model->type = $value;
     $this->objModel = new ContentWrapper\Model($model);
     $sorting = $this->objModel->sorting;
     // getType will throw an exception if type is not found. use it to detect non content wrapper elements
     try {
         $type = $this->objModel->getType();
     } catch (\Exception $e) {
         return $value;
     }
     // check for existing parent element and try to create it if not existing
     if ($type != $start) {
         if ($this->objModel->bootstrap_parentId == '') {
             $parent = ContentWrapper\Repository::findPreviousElement($this->objModel, $start);
             if ($parent) {
                 // set relation to parent element
                 $this->objModel->bootstrap_parentId = $parent->id;
                 $end = ContentWrapper\Repository::findRelatedElement($this->objModel, $stop);
                 if ($end === null) {
                     $this->objModel->sorting = $parent->sorting + 2;
                 } elseif ($parent !== null && $parent->sorting > $end->sorting) {
                     $this->objModel->sorting = $end->sorting - 2;
                 }
                 // set relation to parent element
                 if ($this->objModel->getType() != $start) {
                     $this->objModel->bootstrap_parentId = $parent->id;
                 }
                 $this->objModel->getModel()->save();
             } elseif ($this->isTrigger($type, $start)) {
                 $sorting = $sorting - 2;
                 $this->createElement($sorting, $start);
             } else {
                 throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['wrapperStartNotExists'], $GLOBALS['TL_LANG']['CTE'][$value][0] ?: $value));
             }
         }
     }
     // create separators if possible
     if ($type != $sep && ($this->isTrigger($type, $sep) || $this->isTrigger($type, $sep, static::TRIGGER_DELETE))) {
         $config = $GLOBALS['BOOTSTRAP']['wrappers'][$this->objModel->getGroup()][$sep];
         $callback = $config['countExisting'];
         $this->import($callback[0]);
         $existing = $this->{$callback}[0]->{$callback}[1]($this->objModel);
         $callback = $config['countRequired'];
         $this->import($callback[0]);
         $required = $this->{$callback}[0]->{$callback}[1]($this->objModel);
         if ($existing < $required) {
             if ($this->isTrigger($type, $sep)) {
                 $count = $required - $existing;
                 for ($i = 0; $i < $count; $i++) {
                     $this->createElement($sorting, $sep);
                 }
                 $end = ContentWrapper\Repository::findRelatedElement($this->objModel, $stop);
                 if ($end && $end->sorting <= $sorting) {
                     $sorting = $sorting + 2;
                     $end->sorting = $sorting;
                     $end->save();
                 }
             }
         } elseif ($required < $existing) {
             if ($this->isTrigger($type, $sep, static::TRIGGER_DELETE)) {
                 $count = $existing - $required;
                 $parentId = $this->objModel->getType() == ContentWrapper\Model::TYPE_START ? $this->objModel->id : $this->objModel->bootstrap_parentId;
                 $this->Database->prepare('DELETE FROM tl_content WHERE bootstrap_parentId=? AND type=? ORDER BY sorting DESC')->limit($count)->execute($parentId, $this->objModel->getTypeName($sep));
             }
         }
     }
     // cereate end element
     if ($type == $start && $this->isTrigger($type, $stop)) {
         $end = ContentWrapper\Repository::countRelatedElements($this->objModel, $stop);
         if (!$end) {
             $end = $this->createElement($sorting, $stop);
         }
     }
     return $value;
 }