Path

ez.no / ezpublish / documentation / development / importing attribute data


Importing attribute data

These documentation pages are no longer maintained. Please visit the new documentation site.

Importing attribute data

If the datatype is not plain text or a number, importing data into eZ publish is a bit more complicated. The code below gives routines to import complex data, in this case Enum's and xmltext, and a general function to import most datatypes.

The problem is that the kernel code is not consistent in importing data. Each datatype has a function storeObjectAttribute(), but in most cases it does not do anything, and the actual storing is done in a different function. Also, often
the values to be stored are extracted from the http post variables.

In the forums, reference has been already to an example file to importing simple data into eZ publish:
http://zev.ez.no/svn/nextgen/trunk/scrap/testimport.php

Enum

The following routine can import Enumerated datatype. The selected values are stored in an array $array_selectedEnumElement, as an Enum may contain only a single or multiple values.

include_once( 'kernel/classes/datatypes/ezenum/ezenumtype.php' ); 

 

function store_enum( $array_selectedEnumElement, &$contentObjectAttribute )

{

   // Adapted from function fetchObjectAttributeHTTPInput(...) in class eZEnumType;

   $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );

   $contentObjectAttributeVersion = $contentObjectAttribute->attribute( 'version' );

   $contentClassAttribute = $contentObjectAttribute->attribute( 'contentclass_attribute' );

   $contentClassAttributeID = $contentClassAttribute->attribute('id' );

   $contentClassAttributeVersion = $contentClassAttribute->attribute('version' );

   $array_enumValue = ezEnumValue::fetchAllElements( $contentClassAttributeID, 

                                                     $contentClassAttributeVersion );

   eZEnum::removeObjectEnumerations( $contentObjectAttributeID, $contentObjectAttributeVersion );

   foreach( $array_enumValue as $enumValue ) 

   {

       foreach( $array_selectedEnumElement as $selectedEnumElement ) 

       {

           if( $enumValue->EnumElement === $selectedEnumElement ) 

           {

               eZEnum::storeObjectEnumeration( $contentObjectAttributeID, 

                       $contentObjectAttributeVersion, $enumValue->ID, 

                       $enumValue->EnumElement, $enumValue->EnumValue );

           }

       }

   }

}

Xmltext

The following class takes a text (with some HTML formatting, e.g., bold, italics, lists, tables, and links) to xmldata and stores it in the attribute. See the general function for importing any datatype below on how to use it.

include_once( 'kernel/classes/datatypes/ezxmltext/handlers/input/ezsimplifiedxmlinput.php' );

 

class text2xml extends eZSimplifiedXMLInput

{

   function text2xml( &$xmlData, $contentObjectAttribute )

   {

       $this->eZSimplifiedXMLInput( $xmlData, 0, $contentObjectAttribute );

   }

 

 function &validateText( &$data, &$contentObjectAttribute )

   {

       $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );

       // Below is same as in the function validateInput(...) in class eZSimplifiedXMLInput

       eZDebug::writeDebug($data, "input data");

       // Set original input to a global variable

       $originalInput = "originalInput_" . $contentObjectAttributeID;

       $GLOBALS[$originalInput] = $data;

 

       // Set input valid true to a global variable

       $isInputValid = "isInputValid_" . $contentObjectAttributeID;

       $GLOBALS[$isInputValid] = true;

 

       $inputData = "<section xmlns:image='http://ez.no/namespaces/ezpublish3/image/' 

                               xmlns:xhtml='http://ez.no/namespaces/ezpublish3/xhtml/' >";

       $inputData .= "<paragraph>";

       $inputData .= $data;

       $inputData .= "</paragraph>";

       $inputData .= "</section>";

 

       $data =& $this->convertInput( $inputData );

       $message = $data[1];

       if ( $this->IsInputValid == false )

       {

           $GLOBALS[$isInputValid] = false;

           $errorMessage = null;

           foreach ( $message as $line )

           {

               $errorMessage .= $line .";";

           }

           $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',

                                                                $errorMessage,

                                                                'ezXMLTextType' ) );

           return EZ_INPUT_VALIDATOR_STATE_INVALID;

       }

       else

       {

           $dom = $data[0];

           $objects =& $dom->elementsByName( 'object' );

           if ( $objects !== null )

           {

               foreach ( array_keys( $objects ) as $objectKey )

               {

                   $object =& $objects[$objectKey];

                   $objectID = $object->attributeValue( 'id' );

                   $currentObject =& eZContentObject::fetch( $objectID );

                   $editVersion = $contentObjectAttribute->attribute('version');

                   $editObjectID = $contentObjectAttribute->attribute('contentobject_id');

                   $editObject =& eZContentObject::fetch( $editObjectID );

                   if ( $currentObject == null )

                   {

                       $GLOBALS[$isInputValid] = false;

                       $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',

                               'Object '. $objectID .' does not exist.', 'ezXMLTextType' ) );

                       return EZ_INPUT_VALIDATOR_STATE_INVALID;

                   }

                   else

                   {

                       $relatedObjects =& $editObject->relatedContentObjectArray( $editVersion );

                       $relatedObjectIDArray = array();

                       foreach ( $relatedObjects as $relatedObject )

                       {

                           $relatedObjectID = $relatedObject->attribute( 'id' );

                           $relatedObjectIDArray[] = $relatedObjectID;

                       }

                       if ( !in_array( $objectID, $relatedObjectIDArray ) )

                       {

                           $editObject->addContentObjectRelation( $objectID, $editVersion );

                       }

                   }

 

                   // If there are any image object with links.

                   $href = $object->attributeValueNS( 'ezurl_href',

                               'http://ez.no/namespaces/ezpublish3/image/' );

                   $urlID = $object->attributeValueNS( 'ezurl_id', 

                               'http://ez.no/namespaces/ezpublish3/image/' );

 

                   if ( $href != null )

                   {

                       $linkID =& eZURL::registerURL( $href );

                       $object->appendAttribute( $dom->createAttributeNodeNS(

                               'http://ez.no/namespaces/ezpublish3/image/', 'image:ezurl_id', $linkID ) );

                       $object->removeNamedAttribute( 'ezurl_href' );

                   }

 

                   if ( $urlID != null )

                   {

                       $url =& eZURL::url( $urlID );

                       if ( $url == null )

                       {

                           $GLOBALS[$isInputValid] = false;

                           $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',

                                   'Link '. $urlID .' does not exist.', 'ezXMLTextType' ) );

                           return EZ_INPUT_VALIDATOR_STATE_INVALID;

                       }

                   }

               }

           }

           $links =& $dom->elementsByName( 'link' );

 

           if ( $links !== null )

           {

               foreach ( array_keys( $links ) as $linkKey )

               {

                   $link =& $links[$linkKey];

                   if ( $link->attributeValue( 'id' ) != null )

                   {

                       $linkID = $link->attributeValue( 'id' );

                       $url =& eZURL::url( $linkID );

                       if ( $url == null )

                       {

                           $GLOBALS[$isInputValid] = false;

                           $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',

                                   'Link '. $linkID .' does not exist.', 'ezXMLTextType' ) );

                           return EZ_INPUT_VALIDATOR_STATE_INVALID;

                       }

                   }

                   if ( $link->attributeValue( 'href' ) != null )

                   {

                       $url = $link->attributeValue( 'href' );

                       $linkID =& eZURL::registerURL( $url );

                       $link->appendAttribute( $dom->createAttributeNode( 'id', $linkID ) );

                       $link->removeNamedAttribute( 'href' );

                   }

               }

           }

 

           $domString = $dom->toString();

 

  eZDebug::writeDebug($domString, "unprocessed xml");

  $domString = preg_replace( "#<paragraph> </paragraph>#", "<paragraph> </paragraph>", $domString );

  $domString = str_replace ( "<paragraph />" , "", $domString );

  $domString = str_replace ( "<line />" , "", $domString );

  $domString = str_replace ( "<paragraph></paragraph>" , "", $domString );

  //$domString = preg_replace( "#>[W]+<#", "><", $domString );

  $domString = preg_replace( "#<paragraph> </paragraph>#", "<paragraph />", $domString );

  $domString = preg_replace( "#<paragraph></paragraph>#", "", $domString );

 

  $domString = preg_replace( "#[

]+#", "", $domString );

  $domString = preg_replace( "#</LINE>#", "

", $domString );

  $domString = preg_replace( "#<PARAGRAPH>#", "

 

", $domString );

  eZDebug::writeDebug($domString, "domstring");

           $xml = new eZXML();

           $tmpDom =& $xml->domTree( $domString, array( 'CharsetConversion' => false ) );

//                $domString = $tmpDom->toString();

           $domString = eZXMLTextType::domString( $tmpDom );

 

           eZDebug::writeDebug($domString, "stored xml");

           $contentObjectAttribute->setAttribute( "data_text", $domString );

           $contentObjectAttribute->setValidationLog( $message );

 

           $paragraphs = $tmpDom->elementsByName( 'paragraph' );

 

           $classAttribute =& $contentObjectAttribute->contentClassAttribute();

           if ( $classAttribute->attribute( "is_required" ) == true )

           {

               if ( count( $paragraphs ) == 0 )

                   return EZ_INPUT_VALIDATOR_STATE_INVALID;

               else

                   return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;

           }

           else

               return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;

       }

       return EZ_INPUT_VALIDATOR_STATE_INVALID;

   }   

}

General data import

This function will import most datatypes and stores the value in the objectContentAttribute.

function store_attr( $data, &$contentObjectAttribute )

{

   $contentClassAttribute = $contentObjectAttribute->attribute( 'contentclass_attribute' );

   $dataTypeString = $contentClassAttribute->attribute( 'data_type_string' );

   switch( $dataTypeString ) {

       case 'ezfloat' :

       case 'ezprice' :

           $contentObjectAttribute->SetAttribute( 'data_float', $data );

           $contentObjectAttribute->store();

           break;

       case 'ezboolean' :

       case 'ezdate' :

       case 'ezdatetime' :

       case 'ezinteger' :

       case 'ezsubtreesubscription' :

       case 'eztime' :

           $contentObjectAttribute->SetAttribute( 'data_int', $data );

           $contentObjectAttribute->store();

           break;

       case 'ezemail' :

       case 'ezisbn' :

       case 'ezstring' :

       case 'eztext' :

       case 'ezurl' :

           $contentObjectAttribute->SetAttribute( 'data_text', $data );

           $contentObjectAttribute->store();

           break;

       case 'ezxmltext' :

           $dummy = "";

           $converter = new text2xml( $dummy, 0, $contentObjectAttribute );

           $converter->validateText( $data, $contentObjectAttribute );

           $contentObjectAttribute->SetAttribute( 'data_int', EZ_XMLTEXT_VERSION_TIMESTAMP );

           $contentObjectAttribute->store();

           break;

       case 'ezenum' :

           if( is_array( $data )) {

               store_enum( $data, $contentObjectAttribute );

           } else {

               store_enum( array( $data ), $contentObjectAttribute );

           }

           break;

       default :

           die( 'Can not store this datatype: ' . $dataTypeString );

   }

}

Comments

eZURL update for 3.6

To have this code working for 3.6, I had to replace:
$link->appendAttribute( $dom->createAttributeNode( 'id', $linkID ) );
with:
$link->appendAttribute( $dom->createAttributeNode( 'url_id', $linkID ) );

Now, links are well stored in "ezurl" table, but dont appear in "URL management" of admin access. Any idea ?

ObjectRelation atrribute ?

The function doesn't include objectrelation attribute, how to import it ?

Thanks

Addition to 'ezurl'

To store ezurl attributes correctly, you should extend the function store_attr like this:

....
case "ezurl":
$urlID =& eZURL::registerURL( $attributeValue );
$attribute->setAttribute( 'data_text', $attributeValue );
// store ezurl id in data_int
if( is_int( $urlID ) )
$attribute->setAttribute( 'data_int', $urlID );
$attribute->store();
break;
....

eZURL::registerURL checks if the URL already exists in the ezurl db table, if not, it is created.
It returns the URL id, which is stored in the attribute data_int value as reference.
URL validation is still missing here, I guess.

Text conversion

Thanks for the feedback. I used it to convert paragraphs of text (say, the main text of articles) that were stored in a database. These were not complete pages, so only elements like bold, italics, and links were used. I did some testing and found out that tables are also converted properly. To create paragraphs, you have to separate them with empty lines.

I have changed the class name html2xml to text2xml, which is less misguiding, I hope.

Huh ?

> The following class takes a text with HTML formatting (e.g., a page from a previous webpage that you want to convert to eZ Publish) to xmldata and stores

I misunderstood this completely - I thought this class was about converting HTML to eZPub XML - but it is not (the name 'html2xml' is probably a bit misguiding ).

--

$data =& $this->convertInput( $inputData );
--

This is the exact same thing as would happen if you did a copy/paste job into a XML-text form-field.

Could you expand your page with an example on HOW you are using this?

I am probably being thick as a brick - but this function will fail if you put avg. HTML into it. It will not convert H1, P, BR etc.. tags. It will not deal with the contents of a HEAD tag etc.. etc... In short - it may store the DATA that you give it provided it is wellformed (or almost wellformed) to begin with - but it will NOT convert html to xml.

Sorry for being a thicko :D Plz help me understand this thing.... what is it for ?

Contents

Development

Extensions
eZ publish datamodel
eZ publish tuning and stability
Importing attribute data
Kernel
Libraries
Scripting
Standards
System overview
Test Suite
Using Doxygen to create API documenta...


Created

01/08/2003
11:44:47 pm
by Harry Oosterveen

Last updated

12/08/2003
3:27:18 pm
by Harry Oosterveen

Authors

Harry Oosterveen



This page is part of the eZ Publish documentation. The documentation is available under the GNU Free Documentation License. All contributions will be released under the terms of this license.