/** * Parses the XML for the attributes in $classAttributeIdentifiers, and fixes the relations for $object * @param eZContentObject $object * @param array $classAttributeIdentifiers * @return int The number of created relations */ function restoreXmlRelations(eZContentObject $object, array $classAttributeIdentifiers) { $currentVersion = $object->currentVersion(); $langMask = $currentVersion->attribute('language_mask'); $languageList = eZContentLanguage::decodeLanguageMask($langMask, true); $languageList = $languageList['language_list']; // nothing to do if the object isn't translated if (count($languageList) < 2) { return 0; } $attributeArray = $object->fetchAttributesByIdentifier($classAttributeIdentifiers, $currentVersion->attribute('version'), $languageList); $embedRelationsCount = $object->relatedContentObjectCount(false, 0, array('AllRelations' => eZContentObject::RELATION_EMBED)); $linkRelationsCount = $object->relatedContentObjectCount(false, 0, array('AllRelations' => eZContentObject::RELATION_LINK)); $embeddedObjectIdArray = $linkedObjectIdArray = array(); foreach ($attributeArray as $attribute) { $xmlText = eZXMLTextType::rawXMLText($attribute); $dom = new DOMDocument('1.0', 'utf-8'); if (!$dom->loadXML($xmlText)) { continue; } // linked objects $linkedObjectIdArray = array_merge($linkedObjectIdArray, getRelatedObjectList($dom->getElementsByTagName('link'))); // embedded objects $embeddedObjectIdArray = array_merge($embeddedObjectIdArray, getRelatedObjectList($dom->getElementsByTagName('embed')), getRelatedObjectList($dom->getElementsByTagName('embed-inline'))); } $doCommit = false; $restoredRelations = 0; if (!empty($embeddedObjectIdArray)) { $object->appendInputRelationList($embeddedObjectIdArray, eZContentObject::RELATION_EMBED); $restoredRelations += count($embeddedObjectIdArray) - $embedRelationsCount; $doCommit = true; } if (!empty($linkedObjectIdArray)) { $object->appendInputRelationList($linkedObjectIdArray, eZContentObject::RELATION_LINK); $restoredRelations += count($linkedObjectIdArray) - $linkRelationsCount; $doCommit = true; } if ($doCommit) { $object->commitInputRelations($currentVersion->attribute('version')); } return $restoredRelations; }
/** * Method triggered on publish for xml text datatype * * This method makes sure that links from all translations of an xml text * are registered in the ezurl_object_link table, and thus retained, if * previous versions of an object are removed. * * It also checks for embedded objects in other languages xml, and makes * sure the matching object relations are stored for the publish version. * * @param eZContentObjectAttribute $contentObjectAttribute * @param eZContentObject $object * @param array $publishedNodes * @return boolean */ function onPublish($contentObjectAttribute, $object, $publishedNodes) { $currentVersion = $object->currentVersion(); $langMask = $currentVersion->attribute('language_mask'); // We find all translations present in the current version. We calculate // this from the language mask already present in the fetched version, // so no further round-trip to the DB is required. $languageList = eZContentLanguage::decodeLanguageMask($langMask, true); $languageList = $languageList['language_list']; // We want to have the class attribute identifier of the attribute // containing the current ezxmltext, as we then can use the more efficient // eZContentObject->fetchAttributesByIdentifier() to get the data $identifier = $contentObjectAttribute->attribute('contentclass_attribute_identifier'); $attributeArray = $object->fetchAttributesByIdentifier(array($identifier), $currentVersion->attribute('version'), $languageList); foreach ($attributeArray as $attribute) { $xmlText = eZXMLTextType::rawXMLText($attribute); $dom = new DOMDocument('1.0', 'utf-8'); if (!$dom->loadXML($xmlText)) { continue; } // urls $urlIdArray = array(); foreach ($dom->getElementsByTagName('link') as $link) { // We are looking for external 'http://'-style links, not the internal // object or node links. if ($link->hasAttribute('url_id')) { $urlIdArray[] = $link->getAttribute('url_id'); } } if (count($urlIdArray) > 0) { eZSimplifiedXMLInput::updateUrlObjectLinks($attribute, $urlIdArray); } // linked objects $linkedObjectIdArray = $this->getRelatedObjectList($dom->getElementsByTagName('link')); // embedded objects $embeddedObjectIdArray = array_merge($this->getRelatedObjectList($dom->getElementsByTagName('embed')), $this->getRelatedObjectList($dom->getElementsByTagName('embed-inline'))); if (!empty($embeddedObjectIdArray)) { $object->appendInputRelationList($embeddedObjectIdArray, eZContentObject::RELATION_EMBED); } if (!empty($linkedObjectIdArray)) { $object->appendInputRelationList($linkedObjectIdArray, eZContentObject::RELATION_LINK); } if (!empty($linkedObjectIdArray) || !empty($embeddedObjectIdArray)) { $object->commitInputRelations($currentVersion->attribute('version')); } } }
/** * Set content object attributes * * @private * @param eZContentObject $object * @param array( attributeIdentifier => attributeStringValue ) $attributesValues * @return void */ private function setObjectAttributes(eZContentObject $object, array $attributesValues) { $attributes = $object->dataMap(); foreach ($attributesValues as $identifier => $value) { if (isset($attributes[$identifier])) { $attribute = $attributes[$identifier]; switch ($attribute->attribute('data_type_string')) { case 'ezimage': $arr = explode('|', trim($value)); $source = str_replace(' ', '%20', $arr[0]); if (file_exists($source)) { // Handle local files $content = $attribute->attribute('content'); $content->initializeFromFile($source, isset($arr[1]) ? $arr[1] : null); $content->store($attribute); } else { // Handle remote files $filename = 'var/cache/' . md5(microtime()) . substr($source, strrpos($source, '.')); if (!empty($source)) { if (in_array('curl', get_loaded_extensions())) { $ch = curl_init(); $out = fopen($filename, 'w'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_URL, $source); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FILE, $out); curl_exec($ch); curl_close($ch); fclose($out); } else { copy($source, $filename); } } if (file_exists($filename)) { $content = $attribute->attribute('content'); $content->initializeFromFile($filename, isset($arr[1]) ? $arr[1] : null); $content->store($attribute); unlink($filename); } } break; case 'ezxmltext': $parser = new eZOEInputParser(); $value = '<div>' . trim($value) . '</div>'; $document = $parser->process($value); $urlIDArray = $parser->getUrlIDArray(); if (count($urlIDArray) > 0) { eZOEXMLInput::updateUrlObjectLinks($attribute, $urlIDArray); } $object->appendInputRelationList($parser->getLinkedObjectIDArray(), eZContentObject::RELATION_LINK); $object->appendInputRelationList($parser->getEmbeddedObjectIDArray(), eZContentObject::RELATION_EMBED); $value = $document ? eZXMLTextType::domString($document) : null; $attribute->fromString($value); break; default: if (is_callable(array($attribute, 'fromString'))) { $attribute->fromString($value); } else { $attribute->setAttribute('data_text', $value); } } $attribute->store(); } } }