inadmissible

Enhancedselection fix for PHP 4.4

December 23rd, 2005

Last week I’ve updated some eZ publish extensions to make them work with PHP 4.4. One of them was the enhancedselection datatype, an excellent contribution by Hans Melis.

With PHP 4.3 (eZ publish releases before 3.7), the datatype worked fine. But when using PHP 4.4 (eZ publish 3.7+), the attribute’s class level configuration wasn’t saved correctly.

The enhancedselection datatype uses the eZ publish xml library to store the selection’s possible options. That’s where it went wrong. The original code to store the options in an XML structure is:

$doc = new eZDOMDocument( "selection" );
$root =& $doc->createElementNode( "ezenhancedselection" );
$doc->setRoot( $root );

$options =& $doc->createElementNode( "options" );
$root->appendChild( $options );

foreach ( $currentOptions as $optionArray )
{
    $optionNode =& $doc->createElementNode( "option" );
    $optionNode->appendAttribute( $doc->createAttributeNode( "id", $optionArray['id'] ) );
    $optionNode->appendAttribute( $doc->createAttributeNode( 'name', $optionArray['name'] ) );
    $optionNode->appendAttribute( $doc->createAttributeNode( 'identifier', $optionArray['identifier'] ) );
    $optionNode->appendAttribute( $doc->createAttributeNode( 'priority', $optionArray['priority'] ) );
    $options->appendChild( $optionNode );
}

$xml =& $doc->toString();

Suppose you’ve added three options. This should result in the following XML string:

<?xml version="1.0" encoding="UTF-8"?>
<ezenhancedselection>
  <options>
    <option id="0"
            name="Value 1"
            identifier="Identifier 1"
            priority="1" />
    <option id="2"
            name="Value 2"
            identifier="Identifier 2"
            priority="2" />
    <option id="3"
            name="Value 3"
            identifier="Identifier 3"
            priority="3" />
  </options>
</ezenhancedselection>

But with PHP 4.4, you would get this XML string:

<?xml version="1.0" encoding="UTF-8"?>
<ezenhancedselection>
  <options>
    <option id="3"
            name="Value 3"
            identifier="Identifier 3"
            priority="3" />
    <option id="3"
            name="Value 3"
            identifier="Identifier 3"
            priority="3" />
    <option id="3"
            name="Value 3"
            identifier="Identifier 3"
            priority="3" />
  </options>
</ezenhancedselection>

All options have been replaced by te last one.

A first correction is to call the functions createElementNode and createAttributeNode as class methods, because the eZDOMDocument code includes Doxygen comments, marking these functions static.

The function createElementNode and toString are called by reference, but in eZ publish 3.7+ they don’t return by reference. So we’ll have to correct this too.

So far, the code will be:

$doc = new eZDOMDocument( "selection" );
$root = eZDOMDocument::createElementNode( "ezenhancedselection" );
$doc->setRoot( $root );

$options = eZDOMDocument::createElementNode( "options" );
$root->appendChild( $options );

foreach ( $currentOptions as $optionArray )
{
    $optionNode = eZDOMDocument::createElementNode( "option" );
    $optionNode->appendAttribute( eZDOMDocument::createAttributeNode( "id", $optionArray['id'] ) );
    $optionNode->appendAttribute( eZDOMDocument::createAttributeNode( 'name', $optionArray['name'] ) );
    $optionNode->appendAttribute( eZDOMDocument::createAttributeNode( 'identifier', $optionArray['identifier'] ) );
    $optionNode->appendAttribute( eZDOMDocument::createAttributeNode( 'priority', $optionArray['priority'] ) );
    $options->appendChild( $optionNode );
}

$xml = $doc->toString();

The call to $options->appendChild takes the $optionNode argument by reference. So the function’s argument will point to whatever value we assign to $optionNode. That’s why all options got replaced by the last one. At least in PHP 4.4. In PHP 4.3, there were some bugs related to references, and I suppose it’s because of these bugs that this code ever worked.

Finally, I’ve used an array to hold the different option elements in the loop:

$doc = new eZDOMDocument( "selection" );
$root = eZDOMDocument::createElementNode( "ezenhancedselection" );
$doc->setRoot( $root );

$options = eZDOMDocument::createElementNode( "options" );
$root->appendChild( $options );

$optionNodes = array();
foreach ( $currentOptions as $i => $optionArray )
{
    $optionNodes[$i] = eZDOMDocument::createElementNode( "option" );
    $optionNodes[$i]->appendAttribute( eZDOMDocument::createAttributeNode( "id", $optionArray['id'] ) );
    $optionNodes[$i]->appendAttribute( eZDOMDocument::createAttributeNode( 'name', $optionArray['name'] ) );
    $optionNodes[$i]->appendAttribute( eZDOMDocument::createAttributeNode( 'identifier', $optionArray['identifier'] ) );
    $optionNodes[$i]->appendAttribute( eZDOMDocument::createAttributeNode( 'priority', $optionArray['priority'] ) );
    $options->appendChild( $optionNodes[$i] );
}

$xml = $doc->toString();

This makes the datatype work with PHP 4.4, while remaining compatible with PHP 4.3.

Entry Filed under: PHP, eZ Publish

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

December 2005
M T W T F S S
    Jan »
 1234
567891011
12131415161718
19202122232425
262728293031  

Most Recent Posts


payday loans unsecured payday loans credit card offers no fax cash advances personal loans consolidate your debt personal money loans no fax payday loans instant personal loans personal finance loans bad credit credit cards payday loan with no fax home mortgage refinance loans overnight cash loans 1000 payday loans cash advances cash advance payday loans payday cash loans fast cash no credit check loans till payday credit card debt consolidation personal loans for people with bad credit fast cash payday loans home mortgage refinancing credit cards quick cash loans overnight cash advance unsecured loans debt management services federal student loan consolidation mortgage loans refinance home loans bad credit loans instant cash advances same day payday loans debt consolidation no credit check student loans mortgage banking need money fast unsecured personal loans refinance home equity loans home equity loans apply for a credit card same day cash advances debt consolidation loans government student loans credit card application home mortgage loans faxless payday loans quick payday loans no credit check personal loans student loans refinance mortgages home mortgage refinance low interest payday loans bad credit unsecured loans debt management free debt consultation same day payday loans bad credit mortgage refinance student loan debt consolidation no credit check payday loans no faxing payday loans emergency payday loans personal loan lenders fast personal loans debt collection faxless cash advance bad credit report repair no fax cash advance fast cash advances mortgage refinance quick personal loans credit debt help 24 hour payday loans guaranteed online personal loans bad credit personal loans fast cash advance payday loans mortgage refinancing home loan lenders high risk personal loans guaranteed personal loans low interest personal loans instant payday loans unsecured debt consolidation loans