Index: kernel/classes/ezcontentfunctions.php =================================================================== --- kernel/classes/ezcontentfunctions.php (revision 24219) +++ kernel/classes/ezcontentfunctions.php (working copy) @@ -192,6 +192,155 @@ return $contentObject; } + + /** + * Updates an existing content object. + * + * This function works like createAndPublishObject + * + * Here is an example + * + * + * + *
'; + * + * $now = $now = date( 'Y/m/d H:i:s', time() ); + * $xmlDeclaration = ' + *
'; + * + * $attributeList = array( 'name' => 'Name ' . $now, + * 'short_name' => 'Short name ' . $now, + * 'short_description' => $xmlDeclaration . 'Short description '. $now . '
', + * 'description' => $xmlDeclaration . 'Description '. $now . '
', + * 'show_children' => false); + * + * $params = array(); + * $params['attributes'] = $attributeList; + * // $params['remote_id'] = $now; + * // $params['section_id'] = 3; + * // $params['language'] = 'ger-DE'; + * + * $result = eZContentFunctions::updateAndPublishObject( $contentObject, $params ); + * + * if( $result ) + * print( 'Update OK' ); + * else + * print( 'Failed' ); + * } + * ?> + *
+ * @param eZContentObject an eZContentObject object + * @param array an array with the attributes to update + * @static + * @return bool true if the object has been successfully updated, false otherwise + */ + public static function updateAndPublishObject( eZContentObject $object, $params ) + { + if( !array_key_exists( 'attributes', $params ) and !is_array( $params['attributes'] ) and count( $params['attributes'] ) > 0 ) + { + eZDebug::writeError( 'No attributes specified for object' . $object->attribute( 'id' ), + 'eZContentFunctions::updateAndPublishObject' ); + return false; + } + + $storageDir = ''; + $languageCode = false; + $mustStore = false; + + if ( array_key_exists( 'remote_id', $params ) ) + { + $object->setAttribute( 'remote_id', $params['remote_id'] ); + $mustStore = true; + } + + if ( array_key_exists( 'section_id', $params ) ) + { + $object->setAttribute( 'section_id', $params['section_id'] ); + $mustStore = true; + } + + if( $mustStore ) + $object->store(); + + if( array_key_exists( 'storage_dir', $params ) ) + $storageDir = $params['storage_dir']; + + if( array_key_exists( 'language', $params ) and $params['language'] != false ) + { + $languageCode = $params['language']; + } + else + { + $initialLanguageID = $object->attribute( 'initial_language_id' ); + $language = eZContentLanguage::fetch( $initialLanguageID ); + $languageCode = $language->attribute( 'locale' ); + } + + $db = eZDB::instance(); + $db->begin(); + + $newVersion = $object->createNewVersion( false, true, $languageCode ); + + if( !$newVersion instanceof eZContentObjectVersion ) + { + eZDebug::writeError( 'Unable to create a new version for object ' . $object->attribute( 'id' ), + 'eZContentFunctions::updateAndPublishObject' ); + + $db->rollback(); + + return false; + } + + $newVersion->setAttribute( 'modified', time() ); + $newVersion->store(); + + $attributeList = $newVersion->attribute( 'contentobject_attributes' ); + + $attributesData = $params['attributes']; + + foreach( $attributeList as $attribute ) + { + $attributeIdentifier = $attribute->attribute( 'contentclass_attribute_identifier' ); + if ( array_key_exists( $attributeIdentifier, $attributesData ) ) + { + $dataString = $attributesData[$attributeIdentifier]; + switch ( $datatypeString = $attribute->attribute( 'data_type_string' ) ) + { + case 'ezimage': + case 'ezbinaryfile': + case 'ezmedia': + { + $dataString = $storageDir . $dataString; + break; + } + default: + } + + $attribute->fromString( $dataString ); + $attribute->store(); + } + } + + $db->commit(); + + $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $newVersion->attribute( 'contentobject_id' ), + 'version' => $newVersion->attribute( 'version' ) ) ); + + if( $operationResult['status'] == eZModuleOperationInfo::STATUS_CONTINUE ) + return true; + + return false; + } } ?> \ No newline at end of file