/**
  * Create a \DataContainer instance from a given Model
  * @param \Model $objModel
  *
  * @return static
  */
 public static function getInstanceFromModel(\Model $objModel)
 {
     $objInstance = new static();
     $objInstance->strTable = $objModel->getTable();
     $objInstance->activeRecord = $objModel;
     $objInstance->intId = $objModel->id;
     return $objInstance;
 }
 /**
  * The isVisibleElement hook is used to dynamically load the grid definitions from the database.
  *
  * Known limitation: If being logged in in the backend and frontend preview is used with show all elements
  * it does not work!
  *
  * @param \Model $model     The row model.
  * @param bool   $isVisible The current visible state.
  *
  * @return bool
  *
  * @SuppressWarnings(PHPMD.Superglobals)
  */
 public function hookIsVisibleElement(\Model $model, $isVisible)
 {
     if (static::isActive() && ($model->getTable() == 'tl_module' && $model->type == 'subcolumns' || $model->getTable() == 'tl_content' && ($model->type == 'colsetStart' || $model->type == 'colsetPart'))) {
         if ($model->type == 'colsetPart') {
             $modelClass = get_class($model);
             $parent = $modelClass::findByPk($model->sc_parent);
             $type = $parent->sc_type;
             $gridId = $parent->bootstrap_grid;
         } else {
             $type = $model->sc_type;
             $gridId = $model->bootstrap_grid;
         }
         try {
             $this->updateSubcolumnsDefinition($gridId, $type);
         } catch (\Exception $e) {
             // Do not throw the exception in the frontend. If nothing could fetched the fallback is used.
         }
     }
     return $isVisible;
 }
 /**
  * Add a model to a collection
  *
  * @param \Model                 $objModel
  * @param \Model\Collection|null $objCollection
  *
  * @return \Model\Collection|null
  */
 public static function addModelToCollection(\Model $objModel, \Model\Collection $objCollection = null)
 {
     $arrRegistered = array();
     if ($objCollection !== null) {
         while ($objCollection->next()) {
             if ($objCollection->getTable() !== $objModel->getTable) {
                 return $objCollection;
             }
             $intId = $objCollection->{$objModel::getPk()};
             $arrRegistered[$intId] = $objCollection->current();
         }
     }
     $arrRegistered[$objModel->{$objModel::getPk()}] = $objModel;
     return static::createCollection(array_filter(array_values($arrRegistered)), $objModel->getTable());
 }
 /**
  * Retorna a Model do Objeto
  * @return Model
  * @throws Exception
  */
 public function _getModel()
 {
     if (!$this->Model or !is_a($this->Model, 'Model')) {
         throw new Exception(get_class($this) . ': Não possuí uma Model.');
     } else {
         if (!$this->Table) {
             throw new Exception(get_class($this) . ': Não foi informada a tabela do objecto.');
         } else {
             if ($this->Table != $this->Model->getTable()) {
                 throw new Exception(get_class($this) . ": ValueObject.table.{$this->Table} != Model.table.{$this->Model->getTable()}");
             }
         }
     }
     return $this->Model;
 }
Example #5
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);
 }
Example #6
0
 public static function idExists($id, $class)
 {
     if (!$class || !is_subclass_of($class, 'Model')) {
         throw new Exception("Model class name must be provided ('{$class}' was given).");
     }
     if ($id instanceof $class) {
         return $id;
     }
     if (!is_numeric($id)) {
         return null;
     }
     if (isset(self::$loaded[$class][$id]) && self::$loaded[$class][$id]->getId() > 0) {
         return self::$loaded[$class][$id];
     }
     if (!($table = Model::getTable($class))) {
         return;
     }
     $db = self::getDB($class);
     return (bool) $db->execute("SELECT COUNT(id) FROM `%s` WHERE id = %d", $table, $id);
 }
 /**
  * Create element id for the model.
  *
  * @param \Model      $model     The model being passed.
  * @param string|null $elementId Optional forced id.
  *
  * @return string
  */
 protected function getElementId(\Model $model, $elementId = null)
 {
     if ($elementId) {
         return $elementId;
     }
     return $model->alias ?: str_replace('tl_leaflet_', '', $model->getTable()) . '_' . $model->id;
 }
Example #8
0
 /**
  * Check if an alias is registered
  *
  * @param \Model $objModel The model object
  * @param string $strAlias The alias name
  * @param mixed  $varValue The value of the alias
  *
  * @return boolean True if the alias is registered
  */
 public function isRegisteredAlias(\Model $objModel, $strAlias, $varValue)
 {
     $strTable = $objModel->getTable();
     return isset($this->arrAliases[$strTable][$strAlias][$varValue]);
 }
 /**
  * Create a mobel by passing a Contao model
  *
  * @param \Model $model
  * @return \DcGeneral\Data\ModelInterface
  */
 public static function byContaoModel(\Model $model)
 {
     return static::create($model->getTable(), $model->{$model->getPk()}, $model->row());
 }
Example #10
0
 /**
  * Adds the entity to the container
  *
  * @param \Illuminate\Database\Eloquent\Model
  * @return self
  */
 public function push(Model $entity, string $name = null) : self
 {
     $name = is_null($name) ? $entity->getTable() : $name;
     $this->entities[$name] = $entity;
     return $this;
 }
Example #11
0
 /**
  * @param string $entityName
  * @return Model
  */
 public function getModel($entityName)
 {
     $modelName = $entityName . 'Model';
     if (!$this->isRegistered($modelName)) {
         if ($this->includeFile($modelName, 'models')) {
             $model = new $modelName($this->getDatabase());
         } else {
             $model = new Model($this->getDatabase());
         }
         $this->includeFile($entityName, 'models');
         $model->setEntity($entityName);
         if (null === $model->getTable()) {
             $tableName = $this->get('StringUtil')->underscore($entityName);
             $model->setTable($tableName);
         }
         $this->add($model, $modelName);
     }
     return $this->get($modelName);
 }