/** * Add an field to the model. If a field with the same name has already been added, * it will be replaced by the given field. * * @param Opus_Model_Field $field Field instance that gets appended to the models field collection. * @return Opus_Model_Abstract Provide fluent interface. */ public function addField(Opus_Model_Field $field) { $this->_fields[$field->getName()] = $field; $field->setOwningModelClass(get_class($this)); return $this; }
/** * Overwrited setter mechanism to handle link retrieval properly. * * @see Opus_Model_Abstract::_setFieldValue() */ protected function _setFieldValue(Opus_Model_Field $field, $values) { $fieldname = $field->getName(); $linkmodelclass = $field->getLinkModelClass(); if (!is_null($values) and !is_null($linkmodelclass)) { // Workaround for link_-tables with ternary relations. It's not // beautyful, but it works for now. There won't be an easier // solution without major changes on the framework/schema, since // we cannot know the type of ternary relations at this point. $ternaryRelationName = null; if (isset($this->_externalFields[$fieldname]['addprimarykey'][0])) { $ternaryRelationName = $this->_externalFields[$fieldname]['addprimarykey'][0]; } $values_as_array = is_array($values); $values = is_array($values) ? $values : array($values); foreach ($values as $i => $value) { $linkmodel = null; if ($value instanceof Opus_Model_Dependent_Link_Abstract === true) { $linkmodel = $value; } else { if (is_null($this->getId()) or is_null($value->getId())) { // If any of the linked models hasn't been stored yet. $linkmodel = new $linkmodelclass(); $linkmodel->setModel($value); } else { $linkId = array($this->getId(), $value->getId()); if (isset($ternaryRelationName)) { $linkId[] = $ternaryRelationName; } try { $linkmodel = new $linkmodelclass($linkId); } catch (Opus_Model_NotFoundException $e) { $linkmodel = new $linkmodelclass(); } $linkmodel->setModel($value); } } $values[$i] = $linkmodel; } if (!$values_as_array) { $values = $values[0]; } } return parent::_setFieldValue($field, $values); }
/** * Map field information to a DOMDocument. * * @param Opus_Model_Field $field Contains informations about mapping field. * @param DOMDocument $dom General DOM document. * @param DOMNode $rootNode Node where to add created structure. * @return void * * FIXME: remove code duplication (duplicates Opus_Model_Xml_Version*) */ protected function _mapField(Opus_Model_Field $field, DOMDocument $dom, DOMNode $rootNode) { $fieldName = $field->getName(); $modelClass = $field->getValueModelClass(); $fieldValues = $field->getValue(); if (true === $this->_config->_excludeEmpty) { if (true === is_null($fieldValues) or is_string($fieldValues) && trim($fieldValues) == '' or is_array($fieldValues) && empty($fieldValues)) { return; } } if (null === $modelClass) { // workaround for simple fields with multiple values if (true === $field->hasMultipleValues()) { $fieldValues = implode(',', $fieldValues); } if ($fieldValues instanceof DateTimeZone) { $fieldValues = $fieldValues->getName(); } // Replace invalid XML-1.0-Characters by UTF-8 replacement character. $fieldValues = preg_replace('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F\\x7F]/', "� ", $fieldValues); $rootNode->setAttribute($fieldName, $fieldValues); } else { if (!is_array($fieldValues)) { $fieldValues = array($fieldValues); } foreach ($fieldValues as $value) { $childNode = $dom->createElement($fieldName); if ($value instanceof Opus_Model_AbstractDb) { if ($value instanceof Opus_Model_Dependent_Link_Abstract) { $modelId = $value->getLinkedModelId(); } else { $modelId = $value->getId(); } // Ignore compound keys. if (false === is_array($modelId)) { $childNode->setAttribute('Id', $modelId); } } $rootNode->appendChild($childNode); // if a field has no value then is nothing more to do // TODO maybe must be there an other solution // FIXME remove code duplication (duplicates Opus_Model_Xml_Version*) if (is_null($value)) { continue; } // delivers a URI if a mapping for the given model exists $uri = $this->_createXlinkRef($value); if (null !== $uri) { $childNode->setAttribute('xlink:type', 'simple'); $childNode->setAttribute('xlink:href', $uri); $this->_mapAttributes($value, $dom, $childNode, true); } else { $this->_mapAttributes($value, $dom, $childNode); } } } }
/** * Map field information to a DOMDocument. * * @param Opus_Model_Field $field Contains informations about mapping field. * @param DOMDocument $dom General DOM document. * @param DOMNode $rootNode Node where to add created structure. * @return void * * FIXME: remove code duplication (duplicates Opus_Model_Xml_Version*) */ protected function _mapField(Opus_Model_Field $field, DOMDocument $dom, DOMNode $rootNode) { $fieldName = $field->getName(); $modelClass = $field->getValueModelClass(); $fieldValues = $field->getValue(); if (true === $this->_config->_excludeEmpty) { if (true === is_null($fieldValues) or is_string($fieldValues) && trim($fieldValues) == '' or is_array($fieldValues) && empty($fieldValues)) { return; } } if (null === $modelClass) { // create a new element $element = $dom->createElement($fieldName); // workaround for simple fields with multiple values if (true === $field->hasMultipleValues()) { $fieldValues = implode(',', $fieldValues); } if ($fieldValues instanceof DateTimeZone) { $fieldValues = $fieldValues->getName(); } // set value //if (empty($fieldValues) === false) $element->nodeValue = htmlspecialchars($fieldValues); $rootNode->appendChild($element); } else { if (!is_array($fieldValues)) { $fieldValues = array($fieldValues); } foreach ($fieldValues as $value) { $childNode = $dom->createElement($fieldName); $rootNode->appendChild($childNode); // if a field has no value then is nothing more to do // TODO maybe must be there an other solution // FIXME remove code duplication (duplicates Opus_Model_Xml_Version*) if (is_null($value)) { continue; } // delivers a URI if a mapping for the given model exists $uri = $this->_createXlinkRef($value); if (null !== $uri) { $childNode->setAttribute('xlink:type', 'simple'); $childNode->setAttribute('xlink:href', $uri); $this->_mapAttributes($value, $dom, $childNode, true); } else { $this->_mapAttributes($value, $dom, $childNode); } } } }