/** * Constructor. * * @access public * @param ViewEntity $oEntity * @param string $sFieldName * @param ModelCore $oModel * @throws ORM\Mapping\MappingException * @throws Exception\Fatal * @since 1.0.0-alpha * @version 1.0.0-alpha */ public function __construct(ViewEntity $oEntity, $sFieldName, ModelCore &$oModel) { $this->oEntity = $oEntity; $this->sName = $sFieldName; $this->oModel = $oModel; // check if entity has locales $sTypeFromModel = NULL; $oLocales = $this->oModel->hasLocales() ? $this->oModel->getLocales() : FALSE; /* @var $oLocales \Plethora\ModelCore\Locales */ // get data form field view if ($this->oModel->hasField($sFieldName)) { $this->mValue = $this->oModel->{$sFieldName}; if (in_array($sFieldName, $this->oModel->getMetadata()->getFieldNames())) { $aMapping = $this->oModel->getMetadata()->getFieldMapping($sFieldName); $sTypeFromModel = $aMapping['type']; } else { $sTypeFromModel = 'associacion'; } } elseif ($oLocales !== FALSE && $oLocales->hasField($sFieldName)) { $this->mValue = $oLocales->{$sFieldName}; if (in_array($sFieldName, $oLocales->getMetadata()->getFieldNames())) { $aMapping = $oLocales->getMetadata()->getFieldMapping($sFieldName); $sTypeFromModel = $aMapping['type']; } else { $sTypeFromModel = 'associacion'; } } $this->sTypeFromModel = $sTypeFromModel; // change value of field if it's associacion if ($this->sTypeFromModel === 'associacion') { if ($this->oEntity->getConfigurator()->getCurrentLevel() < $this->oEntity->getConfigurator()->getMaxLevel()) { $oAssociacion = $this->mValue; /* @var $oAssociacion ModelCore */ $aEntityFields = $this->getEntity()->getConfigurator()->getFields(); if (!isset($aEntityFields[$this->getName()])) { $this->mValue = $oAssociacion; } elseif ($oAssociacion instanceof ORM\PersistentCollection) { /* @var $oCollection ORM\PersistentCollection */ $oCollection = $oAssociacion; if ($oCollection->count() > 0) { $aList = $oCollection->toArray(); $oTmpModelRelation = array_shift($aList); /* @var $oTmpModelRelation ModelCore */ array_unshift($aList, $oTmpModelRelation); unset($oTmpModelRelation); $oConfigurator = ViewList\Configurator::factory()->setList($aList)->setFields($aEntityFields[$this->getName()])->setCurrentLevel($this->oEntity->getConfigurator()->getCurrentLevel() + 1)->setMaxLevel($this->oEntity->getConfigurator()->getMaxLevel())->setProfile($this->oEntity->getConfigurator()->getProfile()); $this->mValue = ViewList::factory($oConfigurator)->getView()->render(); } } elseif ($oAssociacion instanceof ModelCore) { /* @var $oAssociacion ModelCore */ $oConfigurator = ViewEntity\Configurator::factory($oAssociacion)->setFields($aEntityFields[$this->getName()])->setCurrentLevel($this->oEntity->getConfigurator()->getCurrentLevel() + 1)->setMaxLevel($this->oEntity->getConfigurator()->getMaxLevel())->setProfile($this->oEntity->getConfigurator()->getProfile()); $this->mValue = ViewEntity::factory($oConfigurator)->getView()->render(); } } else { $this->mValue = '!ENTITY TREE LEVEL REACHED!'; } } // if it's not an associacion, format value of particular field if ($this->mValue !== NULL) { $aFormatters = $this->oModel->getConfig()->getFieldFormatters($this->sName); $sProfile = $this->getEntity()->getConfigurator()->getProfile(); $this->mOriginalValue = $this->mValue; foreach ($aFormatters as $oFormatter) { /* @var $oFormatter \Plethora\View\FieldFormatter */ if ($oFormatter->isAvailableFor($sProfile)) { $oFormatter->setField($this); if (is_array($this->mValue)) { $this->mValue = $oFormatter->formatArray($this->mValue); } elseif ($this->mValue instanceof ORM\PersistentCollection) { $mValueToRefactor = $this->mValue; /* @var $mValueToRefactor ORM\PersistentCollection */ $this->mValue = $oFormatter->formatArray($mValueToRefactor->toArray()); } else { $this->mValue = $oFormatter->format($this->mValue); } } } if (is_array($this->mValue) || is_object($this->mValue) && !method_exists($this->mValue, '__toString')) { throw new Exception\Fatal(__('Field value ":file" need to be formatted to string format (:type at the moment).', ['file' => $this->sName, 'type' => gettype($this->mValue)])); } } }