/**
  * @inheritdoc
  */
 protected function getCurrentPage()
 {
     /** @var Model $class */
     $class = $this->getModelClass();
     if (false === $this->current) {
         $this->current = $class::findByPk($this->dataContainer->id);
     }
     if (null === $this->current) {
         return null;
     }
     $pageId = $this->current->pid ? $this->current->getRelated('pid')->jumpTo : $this->current->jumpTo;
     return PageModel::findWithDetails($pageId);
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function getRelated($strKey, array $arrOptions = array())
 {
     $arrRelation = Relations::getRelation(static::$strTable, $strKey);
     if ($arrRelation !== false) {
         /** @type \Model $strClass */
         $strClass = static::getClassFromTable($arrRelation['related_table']);
         if (class_exists($strClass)) {
             $arrIds = \Database::getInstance()->prepare("SELECT " . $arrRelation['related_field'] . " FROM " . $arrRelation['table'] . " WHERE " . $arrRelation['reference_field'] . "=?")->execute($this->{$arrRelation['reference']})->fetchEach($arrRelation['related_field']);
             if (empty($arrIds)) {
                 return null;
             }
             $collection = array();
             // Fetch from registry first (only possible if no options and the relation field is the PK)
             if (empty($arrOptions) && $arrRelation['field'] === $strClass::getPk()) {
                 foreach ($arrIds as $k => $id) {
                     $model = \Model\Registry::getInstance()->fetch($arrRelation['related_table'], $id);
                     if ($model !== null) {
                         unset($arrIds[$k]);
                     }
                     $collection[$id] = $model;
                 }
             }
             // Fetch remaining
             if (!empty($arrIds)) {
                 $remainingModels = $strClass::findBy(array($arrRelation['related_table'] . "." . $arrRelation['field'] . " IN('" . implode("','", $arrIds) . "')"), null, $arrOptions);
                 foreach ($remainingModels as $remaining) {
                     $collection[$remaining->{$arrRelation['field']}] = $remaining;
                 }
             }
             $this->arrRelated[$strKey] = new \Model\Collection($collection, $strClass::getTable());
         }
     }
     return parent::getRelated($strKey, $arrOptions);
 }
Example #3
0
 /**
  * Auto-format model data based on DCA config
  * @param   \Model
  * @param   callable
  * @return  \ArrayObject
  */
 public static function generate(\Model $objModel = null, $varCallable = null)
 {
     if (null === $objModel) {
         return new \ArrayObject(array(), \ArrayObject::ARRAY_AS_PROPS);
     }
     $strTable = $objModel->getTable();
     $objDca = new \DcaExtractor($strTable);
     $arrRelations = $objDca->getRelations();
     $arrData = array();
     \System::loadLanguageFile($strTable);
     \Controller::loadDataContainer($strTable);
     $arrFields =& $GLOBALS['TL_DCA'][$strTable]['fields'];
     foreach ($objModel->row() as $strField => $varValue) {
         $arrAdditional = array();
         $strLabel = Format::dcaLabel($strTable, $strField);
         if (isset($arrRelations[$strField])) {
             $objRelated = $objModel->getRelated($strField);
             if ($objRelated == null) {
                 $arrData[$strField] = new Plain('', $strLabel, $arrAdditional);
             } elseif ($objRelated instanceof \Model\Collection) {
                 $arrCollection = array();
                 foreach ($objRelated as $objRelatedModel) {
                     $arrCollection[] = new Relation($objRelatedModel, '', array(), $varCallable);
                 }
                 $arrData[$strField] = new Collection($arrCollection, $strLabel);
             } else {
                 $arrData[$strField] = new Relation($objRelated, $strLabel, array(), $varCallable);
             }
             continue;
         }
         $arrAdditional['formatted'] = Format::dcaValue($strTable, $strField, $varValue);
         if (in_array($arrFields[$strField]['eval']['rgxp'], array('date', 'datim', 'time'))) {
             $arrData[$strField] = new Timestamp($varValue, $strLabel, $arrAdditional);
         } else {
             $arrData[$strField] = new Plain($varValue, $strLabel, $arrAdditional);
         }
     }
     if (null !== $varCallable) {
         call_user_func_array($varCallable, array($objModel, &$arrData));
     }
     return new \ArrayObject($arrData, \ArrayObject::ARRAY_AS_PROPS);
 }