/** * Extract the attribute instance from the MetaModel. * * @param IMetaModel $metaModel The MetaModel instance. * * @param string $attributeId The attribute id. * * @return IAttribute * * @throws \RuntimeException When the attribute could not be retrieved. */ private function getAttributeFromMetaModel(IMetaModel $metaModel, $attributeId) { $attribute = $metaModel->getAttributeById($attributeId); if (!$attribute) { throw new \RuntimeException(sprintf('Could not retrieve attribute %s from MetaModel %s.', $attributeId, $metaModel->getTableName())); } return $attribute; }
/** * @return \MetaModels\IItems */ public function fetchMetaModelsItems() { $filter = $this->metaModel->getEmptyFilter(); if ($this->filter) { $this->filter->addRules($filter, array()); } $sortBy = ''; if ($this->sortBy) { $attribute = $this->metaModel->getAttributeById($this->sortBy); if ($attribute) { $sortBy = $attribute->getColName(); } } // TODO: Should we limit the attributes? What about translated ones? Con: Conditions need access to other attributes return $this->metaModel->findByFilter($filter, $sortBy, 0, 0, $this->sortDirection); }
/** * Collect the attribute settings for the given render setting. * * @param IMetaModel $metaModel The MetaModel instance to retrieve the settings for. * * @param ICollection $renderSetting The render setting. * * @return void */ public function collectAttributeSettings(IMetaModel $metaModel, $renderSetting) { $attributeRow = $this->serviceContainer->getDatabase()->prepare('SELECT * FROM tl_metamodel_rendersetting WHERE pid=? AND enabled=1 ORDER BY sorting')->execute($renderSetting->get('id')); while ($attributeRow->next()) { $attribute = $metaModel->getAttributeById($attributeRow->attr_id); if (!$attribute) { continue; } // TODO: we should provide attribute type based render setting elements in version 2.X. $attributeSetting = $renderSetting->getSetting($attribute->getColName()); if (!$attributeSetting) { $attributeSetting = $attribute->getDefaultRenderSettings(); } foreach ($attributeRow->row() as $strKey => $varValue) { if ($varValue) { $attributeSetting->set($strKey, deserialize($varValue)); } } $renderSetting->setSetting($attribute->getColName(), $attributeSetting); } }
/** * {@inheritdoc} */ public function prepare(MetaModel $metaModel, Items $items, DefinitionMapper $mapper, Filter $filter = null, $deferred = false) { if ($deferred != $this->model->deferred) { return; } $values = array(); $this->loadFallbackIcon($mapper); if ($this->model->iconAttribute) { $attribute = $metaModel->getAttributeById($this->model->iconAttribute); if (!$attribute) { return; } /** @var Item $item */ foreach ($items as $item) { $value = $item->get($attribute->getColName()); if ($value) { $values[$item->get('id')] = is_array($value) ? $value['id'] : $value; } } $this->preLoadIcons($values, $mapper); } }
/** * Prepare used values. * * @param MetaModel $metaModel The meta model. * @param Items $items The meta model items list. * @param Attribute $reference The reference attribute. * @param array $values The reference values. * @param array $icons The used icons. * @param array $styles The used styles. * * @return void */ protected function prepareValues(MetaModel $metaModel, Items $items, Attribute $reference, &$values, &$icons, &$styles) { $icon = $metaModel->getAttributeById($this->model->iconAttribute); $style = $metaModel->getAttributeById($this->model->styleAttribute); foreach ($items as $item) { $value = $item->get($reference->getColName()); $itemId = $item->get('id'); if ($value) { $values[$itemId] = $this->getAttributeValue($value); } if ($icon) { $value = $item->get($icon->getColName()); if ($value) { $icons[$itemId] = $this->getAttributeValue($value); } } if ($style) { $value = $item->get($style->getColName()); if ($value) { $styles[$itemId] = $this->getAttributeValue($value); } } } }
/** * Translate a property. * * @param array $property The property information to transform. * * @param IMetaModel $metaModel The MetaModel the property belongs to. * * @param string $legend The legend the property belongs to. * * @return bool */ protected function translateProperty($property, $metaModel, $legend) { $attribute = $metaModel->getAttributeById($property['attr_id']); // Dead meat. if (!$attribute) { return false; } $propName = $attribute->getColName(); $this->legends[$legend]['properties'][] = $propName; $this->properties[$propName] = array('info' => $attribute->getFieldDefinition($property)); return true; }