<?php
set_time_limit ( 0 );
require 'autoload.php';
$cli = eZCLI::instance();
 $db = eZDB::instance();
 $script = eZScript::instance( array( 'description' => "eZ Publish data import.\n\nSimple import script for use with eZ Publish",
                                     'use-session' => false,
	                                'use-modules' => true,
	                                'use-extensions' => true,
							 'debug-output' => true,
							 'debug-message' =>true) );


	
$script->startup();
	
$script->initialize();

/************************
*	Creating a Folder   *
************************/

//getting information required to setup the node:
$user = eZUser::fetchByName( 'Import' );//import our user (replace with the code for the previous step if necessary)
if (!$user){//if no user exists let's pull out the current user:
	$user = eZUser::currentUser();
}
$cli->output('Username: '.$user->Login);

$parent_node = eZContentObjectTreeNode::fetchByURLPath('my_imported_stuff');
$cli->output('Parent: '.$parent_node->Name);

/*We will use this to tell eZ where our new node will be stored, note the underscores rather than hyphens and that it is all in lowercase.*/

//setting general node details
$params = array();
$params ['class_identifier'] = 'folder'; //class name (found within setup=>classes in the admin if you need it
$params['creator_id'] = $user->ContentObjectID;//using the user created above
$params['parent_node_id']=$parent_node->NodeID;//pulling the node id out of the parent
$params['section_id'] = $parent_node->ContentObject->SectionID;
/*we don't need to do this as the section will default to that of the parent but if you want to use a different node for the section the same approach can be used, just pull out the node with the section you are using and then pull the SectionID from it.*/

//setting attribute values
$attributesData = array ( ) ;
$attributesData['name'] = 'A brand new folder' ; 
$attributesData ['short_name'] = 'Shorter name' ; 
//updating the structure for the valid XML
$XMLContent = '<h1>Big Title</h1><h2>Littler Title</h2><p>page content here. Go to <a href="http://www.ez.no">ez.no</a>?</p>';//my example content
//creating and setting up the parser
$parser = new eZSimplifiedXMLInputParser( );
$parser->setParseLineBreaks( true );
//parsing the content
$document = $parser->process( $XMLContent );
  
//adding the content to an object
$attributesData ['description'] = eZXMLTextType::domString( $document );


$params['attributes'] = $attributesData;

print_r($params);//lets print out the data so we know exactly what is being stored

//publishing the content:
$contentObject = eZContentFunctions::createAndPublishObject($params);

if ($contentObject)
{
	$cli->output('===========================');
	$cli->output('Output:');
$cli->output("Content Object ID: ".$contentObject->ID);
	$cli->output("Name: ".$contentObject->Name());
	$cli->output("Data Map: ".print_r($contentObject->DataMap(),true));
}

/************************
*	Creating an Image   *
************************/
//getting information required to setup the node:
$user = eZUser::fetchByName( 'Import' );//import our user (replace with the code for the previous step if necessary)
if (!$user){//if no user exists let's pull out the current user:
	$user = eZUser::currentUser();
}
$cli->output('Username: '.$user->Login);

$parent_node = eZContentObjectTreeNode::fetchByURLPath('media/images/imported_images');
$cli->output('Parent: '.$parent_node->Name);

//setting node details
$params = array();
$params ['class_identifier'] = 'image';
$params['creator_id'] = $user->ContentObjectID;//using the user extracted above
$params['parent_node_id']=$parent_node->NodeID;//pulling the node id out of the parent 
$params ['storage_dir' ] = $_SERVER['PWD'].'/var/ezflow_site/storage/import_images/';
/*required so ez knows where to look. The ending "/" required. $_SERVER['pwd'] is being used as the script is being run through the command line. IÕve created the folder Òimport_imagesÓ on the server and moved my image into it.*/

//setting attribute values
$attributesData = array ( ) ;
$attributesData['name'] = 'Adding a random image' ; 
$attributesData [ 'image' ] = 'my_image.jpg' ; 

//storing xml content for the caption
$XMLContent = "<p>David's Picture Test</p>";
$parser = new eZSimplifiedXMLInputParser( );
$parser->setParseLineBreaks( true );
$document = $parser->process( $XMLContent );
$xmlString = eZXMLTextType::domString( $document );
$attributesData ['caption'] = $xmlString;

$params['attributes'] = $attributesData;

//publishing node
$imageObject = eZContentFunctions::createAndPublishObject($params);
print_r($imageObject);

$script->shutdown();
?>
