Forums / Developer / What's wrong with this code?

What's wrong with this code?

Author Message

Clemens T

Monday 03 October 2005 4:35:21 am

hey all!
I'm trying to add a binaryfile to a contentobjectattribute, here's my first attempt (doesn't work):

$attribs =& $contentObject->contentObjectAttributes();
for($i=0;$i<count($attribs);$i++){
	switch($attribs[$i]->attribute("contentclass_attribute_identifier")) {
		case 'commentaar':
			$attribs[$i]->setAttribute('data_text', $http->postVariable( 'commentaar' ) );
			$attribs[$i]->store();
		case 'bestand':
			include_once( 'lib/ezutils/classes/ezhttpfile.php' );

	        	if ( eZHTTPFile::canFetch( 'userfile') ){
		        	$file =& eZHTTPFile::fetch('userfile');
			  	$dir = eZSys::storageDirectory( );
	
		        	$mimeData = eZMimeType::findByFileContents( $file->attribute( 'original_filename' ) );
			  	$mimeCategory = $file['MimeCategory'];
			  	//$mime = explode( '/', $mimeData['name'] );
	           		//$file->store( false, false, $mimeData );
			 	print "mimeDataName:".$mimeData['name']."<br><br><br>";
				print "attributid:".$attribs[$i]->attribute( 'id' );
	            		$binary =& eZBinaryFile::create( $attribs[$i]->attribute( 'id' ), 1 );

	        		$binary->setAttribute( 'contentobject_attribute_id', $attribs[$i]->attribute( 'id' ) );
				$binary->setAttribute( 'version', $attribs[$i]->attribute( 'version' ) );
				$binary->setAttribute( 'filename', $file->attribute( 'original_filename'));
				$binary->setAttribute( 'original_filename', $file->attribute( 'original_filename'));
				$binary->setAttribute( 'mime_type', $mimeData['name'] );
				
				$binary->store( );
				print ("<br><br>binary:");
				var_dump($binary);

				$attribs[$i]->setContent( $binary );
				$attribs[$i]->store();
		   	}

		break;
	}
}

Any enhancement/suggestion/comment will do... hopefully I'll get there eventually!

Thanks a lot,
Clemens

*- pike

Thursday 02 November 2006 5:01:15 pm

Hoi Clemens

I was looking for something else when I bumped into kernel/classes/ezcontentupload.php

function handleUpload( &$result, $httpFileIdentifier, $location, $existingNode )

not exactly what you're looking for either; because it uses upload.ini to determine what object class to use for a mimetype, and then chooses its attribute itself. but it could help / aka the code you need might be in there ?

for example, I see
$status = $dataMap[$fileAttribute]->insertHTTPFile( $object, $publishVersion, eZContentObject::defaultLanguage(), $file, $mimeData, $storeResult );

oh, .. ok, .. insertHTTPFile looks like 99% your code :-) except for

  $db =& eZDB::instance();
  $db->begin();
   ....code
  $db->commit();

eh; dont know.

good luck!
*pike

---------------
The class eZContentObjectTreeNode does.