00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 class eZContentFunctions
00031 {
00032 static function createAndPublishObject( $params )
00033 {
00034
00035
00036
00037
00038 $parentNodeID = $params['parent_node_id'];
00039 $classIdentifier = $params['class_identifier'];
00040 $creatorID = isset( $params['creator_id'] ) ? $params['creator_id'] : false;
00041 $attributesData = isset( $params['attributes'] ) ? $params['attributes'] : false;
00042 $storageDir = isset( $params['storage_dir'] ) ? $params['storage_dir'] : '';
00043
00044 $contentObject = false;
00045
00046 $parentNode = eZContentObjectTreeNode::fetch( $parentNodeID, false, false );
00047
00048 if ( is_array( $parentNode ) )
00049 {
00050 $contentClass = eZContentClass::fetchByIdentifier( $classIdentifier );
00051 if ( is_object( $contentClass ) )
00052 {
00053 $db = eZDB::instance();
00054 $db->begin();
00055
00056 $contentObject = $contentClass->instantiate( $creatorID );
00057 $contentObject->store();
00058
00059 $nodeAssignment = eZNodeAssignment::create( array( 'contentobject_id' => $contentObject->attribute( 'id' ),
00060 'contentobject_version' => $contentObject->attribute( 'current_version' ),
00061 'parent_node' => $parentNodeID,
00062 'is_main' => 1,
00063 'sort_field' => $contentClass->attribute( 'sort_field' ),
00064 'sort_order' => $contentClass->attribute( 'sort_order' ) ) );
00065 $nodeAssignment->store();
00066
00067 $version = $contentObject->version( 1 );
00068 $version->setAttribute( 'modified', eZDateTime::currentTimeStamp() );
00069 $version->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
00070 $version->store();
00071
00072 if ( is_array( $attributesData ) && count( $attributesData ) > 0 )
00073 {
00074 $attributes = $contentObject->attribute( 'contentobject_attributes' );
00075
00076 foreach( $attributes as $attribute )
00077 {
00078 $attributeIdentifier = $attribute->attribute( 'contentclass_attribute_identifier' );
00079 if ( isset( $attributesData[$attributeIdentifier] ) )
00080 {
00081 $dataString = $attributesData[$attributeIdentifier];
00082 switch ( $datatypeString = $attribute->attribute( 'data_type_string' ) )
00083 {
00084 case 'ezimage':
00085 case 'ezbinaryfile':
00086 case 'ezmedia':
00087 {
00088 $dataString = $storageDir . $dataString;
00089 break;
00090 }
00091 default:
00092 }
00093
00094 $attribute->fromString( $dataString );
00095 $attribute->store();
00096 }
00097 }
00098 }
00099
00100 $db->commit();
00101
00102
00103 $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObject->attribute( 'id' ),
00104 'version' => 1 ) );
00105 }
00106 else
00107 {
00108 eZDebug::writeError( "Content class with identifier '$classIdentifier' doesn't exist.", 'eZContentFunctions::createAndPublishObject' );
00109 }
00110 }
00111 else
00112 {
00113 eZDebug::writeError( "Node with id '$parentNodeID' doesn't exist.", 'eZContentFunctions::createAndPublishObject' );
00114 }
00115
00116 return $contentObject;
00117 }
00118 }
00119
00120 ?>