Sunday 30 July 2006 10:42:01 am
Hi, Importing objects into eZ publish is really pretty straightforward. I start with the code associated with this book http://ez.no/products/books/learning_ez_publish_3, which has (or had) some scripts online. I changed the code to use attribute mapping with an array to map the CSV fields to the attributes and indicate the type of data for storage. The code listed here is intended to be added to the code from the book, it isn't the full listing. My answer to the issue of importing the data is that the PHP import script available as supplementary materials for the book is excellent. I am submitting my modifications to the book's code, because they simplify the code. I have used this basic approach to import thousands of objects into eZ, very successfully. <b>Map array definition</b>
/*
$attribute_map
indexed by the name of the attribute
index indicates the index of the data array that contains the data for import
type indicates the type of data which will be stored
if you are unsure how the data is stored (what type),
use MySQL to view sample data with the type in question
*/
$attribute_map = array(
'ID Number'=> array ( 'index'=>0,'type'=>'data_int'),
'First Name' => array ( 'index'=>1,'type'=>'data_text'),
'Last Name' => array ( 'index'=>2,'type'=>'data_text'),
'Composite' => array ('index'=>'composite','type'=>'data_text'),
'Date of birth' => array ('index'=>'date','type'=>'data_int'),
'Price' => array('index'=>4,'type'=>'data_float'),
'Comments'=>array('index'=>'comments','type'=>'data_text'));
<b>Extraction of CSV data and placement into an array</b>
$fp = fopen('data.txt', 'r');
while ($data = fgetcsv($fp, 1000, '\t'))
{
/* composite is created from imported data */
$data['composite']=$data[0] . '-' . $data[2];
/* comments is an XML_block, and must be wrapped properly on import */
$data['comments']=XML_wrap($data[5]);
/* if the data comes in as a string (for example 5/21/2006), this will convert it */
$data['date']=make_date($data[3]);
<b>Use of the attribute map array to populate the content object</b>
foreach (array_keys($contentObjectAttributes) as $key)
{
$contentObjectAttribute =& $contentObjectAttributes[$key];
$contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();
$attributeName = $contentClassAttribute->attribute('name');
$attributeType = $attribute_map[$attributeName]['type'];
$attributeData = trim($data[$attribute_map[$attributeName]['index']]);
if ($attributeType != 'data_image') // This code doesn't handle images
{
$contentObjectAttribute->setAttribute($attributeType,$attributeData);
$contentObjectAttribute->store();
}
}
<b>Supporting functions</b>
function XML_wrap($text)
{
return '<?xml version="1.0" encoding="UTF-8"?>' ."\n".
'<section xmlns:image="http://ez.no/namespaces/ezpublish3/image/"'."\n".
' xmlns:xhtml="http://ez.no/namespaces/ezpublish3/xhtml/"'."\n".
' xmlns:custom="http://ez.no/namespaces/ezpublish3/custom/"><paragraph>'.
$text."</paragraph>\n</section>\n";
}
function make_date ($text)
{
global $cli;
$text=strtr($text,' ','-');
if (($timestamp = strtotime($text)) === -1)
$cli->output("Invalid string ($node_id - $date)",true);
return $timestamp;
}
A final note on large document collections. Large quantities of text in eZ can create extremely large searchindexes. In addition, translating them from different formats can be difficult. Consider using the Mussen search engine and the editor.
|