Forums / Developer / Creating a Simple Content Object via PHP

Creating a Simple Content Object via PHP

Author Message

Dave Dykshoorn

Friday 19 September 2003 4:12:29 pm

I am looking for a simple guide to creating content objects via php. I have looked and found instructions on editing content, deleting content, and creating content classes however creating content objects seems to have been missed at the present.

All I really want to know is how to create a content object from a content class with 1 Text Line. That should be darn easy if I could find some reference. If anyone could help me out with a few lines of code it would be greatly appreciated.

Thanks in advance,

Dave

Willie Seabrook

Saturday 20 September 2003 5:46:57 am

Have you made any progress dave? I'm trying to accomplish this sort of thing also.

I have to import the data of about 5000 users into ezpublish system. Are there any docs on importing data? As well as the users problem I also have other data that needs to be turned into content objects.

Ez system people: help?

Regards,
Willie

Willie Seabrook

Monday 22 September 2003 3:28:36 am

Still looking around for a solution to this.... I thought I had found one in the sdk docs though... right at the bottom of the page at http://ez.no/sdk/kernel/view/content_objects

However, one can see that the author was interrupted to more important things :-)

Regards,
Willie

Willie Seabrook

Monday 22 September 2003 3:51:32 am

I've tried to scramble an approximate attempt together... many questions though: Want to help me along Dave? Have *you* made any progress?

Come on EZ!!!! One of you could demonstrate this is 5 minutes where it will take us a week to figure out the whole content tree model etc on our own!!!! :-( :-(

Regards,
Willie

###############################

$contentClassId = 3; //For ezUser, we would make this the ezUser class id but otherwise just pick and choose.
$name = "Blah?"; //What should this be?
$userId = 1; //The userId creating it. Since we are running this outside of the web for mainainence purposes its fine to use root.

$contentObject obj = ezContentObject::create($name, $contentClassId, $userId);

for each attribute that the content class has
$obj->setAttribute("name", "value"); //What if its a binary file or something like that?

$contentObject->store();

I don't see any way to place the new object in folders etc

$newUser = ezUser::create($contentObjectId);
$newUser->store();

#################################3

Wenyue Yu

Monday 22 September 2003 4:26:58 am

Hi,

I made a simple example according to Willie's code:

-----------------------------------------------------

$contentClassId = 3; //For ezUser, we would make this the ezUser
//class id but otherwise just pick and choose.

//fetch this class
$class =& eZContentClass::fetch( 3 );

$userID = 1; //The userId creating it. Since we are
//running this outside of the web for mainainence purposes its fine to use root.

// Instantiate the object with user $userID and put it in section 1.
$contentObject =& $class->instantiate( $userID, 1 );

$name = "Blah?"; //What should this be?
//Answer: Object name

$contentObject->setAttribute( 'name', $name );

$parentNodeID = ? // choose where to put the user

$nodeAssignment =& eZNodeAssignment::create( array(
'contentobject_id' => $contentObject->attribute( 'id' ),
'contentobject_version' => $contentObject->attribute( 'current_version' ),
'parent_node' => $parentNodeID,
'sort_field' => 2,
'sort_order' => 0,
'is_main' => 1
)
);
$nodeAssignment->store();

$version =& $contentObject->version( 1 );

$version->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT );
$version->store();

$contentObjectID = $contentObject->attribute( 'id' );
$contentObjectAttributes =& $version->contentObjectAttributes();

// Set attribute valuse for this class depending on attributes it has and its attribute datatype
// Should be setAttribute( 'data_int', $attributeValue ) if the attribute is integer.
// If user, image, binary file, you also need to stort something in extra tables.
//You need to know how does
// eZ publish store these kind of content.
$contentObjectAttributes[0]->setAttribute( 'data_text', $attributeValue );
$contentObjectAttributes[0]->store();

$contentObjectAttributes[1]->setAttribute( 'data_text', $attributeValue );
$contentObjectAttributes[1]->store();

...

$contentObject->store();

// Publish it
include_once( 'lib/ezutils/classes/ezoperationhandler.php' );
$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObjectID,
'version' => 1 ) );

Dave Dykshoorn

Monday 22 September 2003 8:26:50 am

Hey thanks for helping me out Willie and eZp Crew. Sorry I couldn't contribute that much. I posted on my way out the door friday and just got in today.

I appreciate the code example and am about to put it into place. I'll let you know how the results go.

Again thanks

Dave

Dave Dykshoorn

Monday 22 September 2003 9:01:35 am

wenyue, (or whoever else knows), you mentioned that storing binary files need to store information in other tables. Can you expand on this a little. For instance say I have a file called test.pdf that I want to store with my content. Its located in C:\temp (or even better, as a blob in another mysql db table) how would I do this, using your sample code?

$contentObjectAttributes[0]->setAttribute( 'binaryfile', $attributeValue );
$contentObjectAttributes[0]->store();

Thanks in advance,

Dave

Dave Dykshoorn

Monday 22 September 2003 2:28:42 pm

I'm able to store content now of common int and text types however can you (or anyone else for that matter) explain how to store an object relation list and a binary file attribute with a new content object?

Basically I'm looking for a replacement for this, which i know is completely bogus:

$attributeValue = $file;
$contentObjectAttributes[1]->setAttribute( 'binary_file', $attributeValue );
$contentObjectAttributes[1]->store();

Wenyue Yu

Tuesday 23 September 2003 5:35:52 am

Hi,

It is a little bit complex to store binary file. It may work using following code:

$contentObjectAttribute =& $contentObjectAttributes[1]; // binary attribute

// $fileFileName and $fileOriginalFileName
// could be the same, for example "test.pdf"
saveFile( $fileFileName, $fileOriginalFileName, $contentObjectAttribute );
$contentObjectAttributes[1]->store();

// First, you need to put all files you want to
//upload under folder var/storage/files/ before you
//use this function
function saveFile( $fileFileName, $fileOriginalFileName, &$contentObjectAttribute )
{
include_once( "kernel/classes/datatypes/ezbinaryfile/ezbinaryfile.php" );
include_once( "lib/ezutils/classes/ezmimetype.php" );
include_once( "lib/ezutils/classes/ezdir.php" );
$contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
$version = $contentObjectAttribute->attribute( "version" );

$file =& eZBinaryFile::create( $contentObjectAttributeID , $version );

$file->setAttribute( "contentobject_attribute_id", $contentObjectAttributeID );
$file->setAttribute( "version", $version );
$file->setAttribute( "filename", $fileFileName );
$file->setAttribute( "original_filename", $fileOriginalFileName );

$mimeObj = new eZMimeType();
$mime = $mimeObj->mimeTypeFor( false, $fileOriginalFileName );
$file->setAttribute( "mime_type", $mime );
$file->store();

$sys =& eZSys::instance();
$storage_dir = $sys->storageDirectory();

list( $subdir, $extension )= split ("/", $mime );

$file_dir = $storage_dir . '/' . "original/" . $subdir;

if ( !file_exists( $file_dir ) )
{
eZDir::mkdir( $file_dir, 0777, true);
}

$source_file = $storage_dir . "/files/$fileFileName";
$target_file = $storage_dir . "/original/" . $subdir . "/$fileFileName";
copy($source_file, $target_file );
}

Dave Dykshoorn

Tuesday 23 September 2003 3:08:29 pm

Thank you for your help wenyue, I will try that out in the next couple days and let you know the results.

Dave

Dave Dykshoorn

Thursday 25 September 2003 8:58:41 am

Its official: at 8:30pm PST I successfully saved new content which includes searchable PDF's with code!

Thanks Wenyue for your help!

Dave

Willie Seabrook

Thursday 25 September 2003 5:42:21 pm

Hey Dave,

I was at the same stage as you on wednesday but have since had to spend my time fixing sendmail (Blah!!!) Would you be so kind as to email the code you used to me?? It would be very much appreciated!

willie [dot] seabrook [at] levity.co.nz

Regards,
Willie Seabrook

Claus Jensen

Wednesday 08 October 2003 3:17:05 am

Hello folks,
would it be possible to have a look at the code you produced to achieve this. It would be very much appreciated. If you put it out as a comment here or sent me an email: cuj[att]norgit[dot]no. Thanks in advance:)

And by the way, thanks to ez for sometimes sharing some of that information;)

cheers,
claÜs

Claus Jensen

Wednesday 08 October 2003 4:43:22 am

Hello again!
It seems to be working even vith some errors, but....
after trying out this code to import some content objects (using version 3.2-1), I get an error when I try to remove these objects. It says:

Fatal error: Undefined class name 'ezurlalias' in /data/ezpublish/3.2-1_Metadata/kernel/classes/ezcontentobjecttreenode.php on line 1902
Fatal error: eZ publish did not finish it's request

And some debug says:
Warning: PHP
Oct 08 2003 14:54:23
Undefined index: real_translation in /data/ezpublish/3.2-1_Metadata/kernel/classes/ezcontentobjecttreenode.php on line 1990

Did any of you encounter this? Here is my code, its partly from wenyue yu's script and another import.php script I found:
#!/usr/bin/php
<?php
set_time_limit( 0 );
//print( "Starting metadata import\n" );
include_once( "lib/ezutils/classes/ezdebug.php" );
//eZDebug::setHandleType( EZ_HANDLE_FROM_PHP );
include_once( "lib/ezutils/classes/ezmodule.php" );
eZModule::setGlobalPathList( array( "kernel" ) );
include_once( 'lib/ezutils/classes/ezexecution.php' );
include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
eZDebug::setHandleType( EZ_HANDLE_TO_PHP );
eZDebug::setLogFileEnabled( false );
eZINI::setIsCacheEnabled( false );
function eZDBCleanup()
{
if ( class_exists( 'ezdb' )
and eZDB::hasInstance() )
{
$db =& eZDB::instance();
$db->setIsSQLOutputEnabled( false );
}
// session_write_close();
}
function eZFatalError()
{
eZDebug::setHandleType( EZ_HANDLE_NONE );
print( "Fatal error: eZ publish did not finish it's request\n" );
print( "The execution of eZ publish was abruptly ended." );
}
eZExecution::addCleanupHandler( 'eZDBCleanup' );
eZExecution::addFatalErrorHandler( 'eZFatalError' );
$db =& eZDB::instance();
$db->setIsSQLOutputEnabled( false );

$contentClassId = 28; //For metadata bruk 28

//fetch this class
$class =& eZContentClass::fetch( $contentClassId );

$userID = 1; //The userId creating it. Root = 1

// Instantiate the object with user $userID and put it in section 1.
$contentObject =& $class->instantiate( $userID, 1 );

$name = "Blah? blablablablabla"; //What should this be?
//Answer: Object name

$contentObject->setAttribute( 'name', $name );

$parentNodeID = 164; // choose where to put the object

$nodeAssignment =& eZNodeAssignment::create( array(
'contentobject_id' => $contentObject->attribute( 'id' ),
'contentobject_version' => $contentObject->attribute( 'current_version' ),
'parent_node' => $parentNodeID,
'sort_field' => 2,
'sort_order' => 0,
'is_main' => 1
)
);
$nodeAssignment->store();

$version =& $contentObject->version( 1 );

$version->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT );
$version->store();

$contentObjectID = $contentObject->attribute( 'id' );
$contentObjectAttributes =& $version->contentObjectAttributes();

// Set attribute values for this class depending on attributes it has and its attribute datatype
// Should be setAttribute( 'data_int', $attributeValue ) if the attribute is integer.
// If user, image, binary file, you also need to stort something in extra tables.
//You need to know how does
// eZ publish store these kind of content.

$attributeValue = 'Testing testing...claus';

$contentObjectAttributes[0]->setAttribute( 'data_text', $attributeValue );
$contentObjectAttributes[0]->store();

//$contentObjectAttributes[1]->setAttribute( 'data_text', $attributeValue );
//$contentObjectAttributes[1]->store();

//$contentObjectAttributes[2]->setAttribute( 'data_text', $attributeValue );
//$contentObjectAttributes[2]->store();

//$contentObjectAttributes[3]->setAttribute( 'data_text', $attributeValue );
//$contentObjectAttributes[3]->store();

$contentObjectAttributes[4]->setAttribute( 'data_text', $attributeValue );
$contentObjectAttributes[4]->store();

$contentObjectAttributes[5]->setAttribute( 'data_text', $attributeValue );
$contentObjectAttributes[5]->store();

$contentObjectAttributes[6]->setAttribute( 'data_text', $attributeValue );
$contentObjectAttributes[6]->store();

$contentObject->store();

// Publish it
include_once( 'lib/ezutils/classes/ezoperationhandler.php' );
$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObjectID,
'version' => 1 ) );

?>

Cheers,
claÜs

Ulitsa Tal Arik

Friday 02 December 2005 2:45:43 am

another type of problem :)
i have some code to create+save attribute+upload file...
almost everything work (everything work its mean - name, description stored, file saved in storage) , but file attribute didn't saved
(myme_type, size, etc)

function saveFile( $fileFileName, $fileOriginalFileName, &$contentObjectAttribute )
{
include_once( "kernel/classes/datatypes/ezbinaryfile/ezbinaryfile.php" );
include_once( "lib/ezutils/classes/ezmimetype.php" );
include_once( "lib/ezutils/classes/ezdir.php" );
$contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
$version = $contentObjectAttribute->attribute( "version" );

// Create a new mime object.
$mimeObj = new eZMimeType();
$mime = $mimeObj->mimeTypeFor( false, strtolower( $fileOriginalFileName ) );

list( $subdir, $extension )= split ("/", $mime );

$file =& eZBinaryFile::create( $contentObjectAttributeID , $version );
$file->setAttribute( "contentobject_attribute_id", $contentObjectAttributeID );
$file->setAttribute( "version", $version );
$file->setAttribute( "filename", $fileFileName );
$file->setAttribute( "original_filename", $fileFileName );
$file->setAttribute( "mime_type", $mime );	

$file->store();

$sys =& eZSys::instance();
$storage_dir = $sys->storageDirectory();

$file_dir = $storage_dir . '/' . "original/" . $subdir;
if ( !file_exists( $file_dir ) )
{
eZDir::mkdir( $file_dir, 0777, true);
}

$source_file = "c:/temp/".$fileFileName;
$target_file = $storage_dir . "/original/" . $subdir . "/$fileFileName";
copy($source_file, $target_file );
}