eZ Publish  [trunk]
ezoptiontype.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZOptionType class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 /*!
00012   \class eZOptionType ezoptiontype.php
00013   \ingroup eZDatatype
00014   \brief Stores option values
00015 
00016 */
00017 
00018 class eZOptionType extends eZDataType
00019 {
00020     const DEFAULT_NAME_VARIABLE = "_ezoption_default_name_";
00021 
00022     const DATA_TYPE_STRING = "ezoption";
00023 
00024     function eZOptionType()
00025     {
00026         $this->eZDataType( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', "Option", 'Datatype name' ),
00027                            array( 'serialize_supported' => true ) );
00028     }
00029 
00030     function validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00031     {
00032         $classAttribute = $contentObjectAttribute->contentClassAttribute();
00033         if ( $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) ) )
00034         {
00035             $value = $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) );
00036 
00037             if ( $contentObjectAttribute->validateIsRequired() and !$value )
00038             {
00039                 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
00040                                                                      'Input required.' ) );
00041                 return eZInputValidator::STATE_INVALID;
00042             }
00043         }
00044         else
00045         {
00046             $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
00047                                                                  'Input required.' ) );
00048             return eZInputValidator::STATE_INVALID;
00049         }
00050     }
00051 
00052     /*!
00053      Validates the input and returns true if the input was
00054      valid for this datatype.
00055     */
00056     function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00057     {
00058         $count = 0;
00059         $classAttribute = $contentObjectAttribute->contentClassAttribute();
00060         if ( $http->hasPostVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) ) )
00061         {
00062             $idList = $http->postVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) );
00063             $valueList = $http->postVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) );
00064             $dataName = $http->postVariable( $base . "_data_option_name_" . $contentObjectAttribute->attribute( "id" ) );
00065 
00066             if ( $http->hasPostVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) ) )
00067                 $optionAdditionalPriceList = $http->postVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) );
00068             else
00069                 $optionAdditionalPriceList = array();
00070 
00071             for ( $i = 0; $i < count( $valueList ); ++$i )
00072                 if ( trim( $valueList[$i] ) <> '' )
00073                 {
00074                     ++$count;
00075                     break;
00076                 }
00077             if ( $contentObjectAttribute->validateIsRequired() and trim( $dataName ) == '' )
00078             {
00079                 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
00080                                                                      'NAME is required.' ) );
00081                 return eZInputValidator::STATE_INVALID;
00082             }
00083             if ( $count != 0 )
00084             {
00085                 for ( $i=0;$i<count( $idList );$i++ )
00086                 {
00087                     $value =  $valueList[$i];
00088                     if ( trim( $value )== "" )
00089                     {
00090                         $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
00091                                                                              'The option value must be provided.' ) );
00092                         return eZInputValidator::STATE_INVALID;
00093                     }
00094                     if ( isset( $optionAdditionalPriceList[$i] ) &&
00095                          strlen( $optionAdditionalPriceList[$i] ) &&
00096                          !preg_match( "#^[-|+]?[0-9]+(\.){0,1}[0-9]{0,2}$#", $optionAdditionalPriceList[$i] ) )
00097                     {
00098                         $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
00099                                                                              'The Additional price value is not valid.' ) );
00100                         return eZInputValidator::STATE_INVALID;
00101                     }
00102                 }
00103             }
00104         }
00105         if ( $contentObjectAttribute->validateIsRequired() and
00106              !$classAttribute->attribute( 'is_information_collector' ) )
00107         {
00108             if ( $count == 0 )
00109             {
00110                 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
00111                                                                      'At least one option is required.' ) );
00112                 return eZInputValidator::STATE_INVALID;
00113             }
00114         }
00115         return eZInputValidator::STATE_ACCEPTED;
00116     }
00117 
00118     /*!
00119      Store content
00120     */
00121     function storeObjectAttribute( $contentObjectAttribute )
00122     {
00123         $option = $contentObjectAttribute->content();
00124         $contentObjectAttribute->setAttribute( "data_text", $option->xmlString() );
00125     }
00126 
00127     /*!
00128      Returns the content.
00129     */
00130     function objectAttributeContent( $contentObjectAttribute )
00131     {
00132         $option = new eZOption( "" );
00133 
00134         $option->decodeXML( $contentObjectAttribute->attribute( "data_text" ) );
00135 
00136         return $option;
00137     }
00138 
00139     /*!
00140      Returns the meta data used for storing search indeces.
00141     */
00142     function metaData( $contentObjectAttribute )
00143     {
00144         return $contentObjectAttribute->attribute( "data_text" );
00145     }
00146 
00147     /*!
00148      Fetches the http post var integer input and stores it in the data instance.
00149     */
00150     function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00151     {
00152         $optionName = $http->postVariable( $base . "_data_option_name_" . $contentObjectAttribute->attribute( "id" ) );
00153         if ( $http->hasPostVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) ) )
00154             $optionIDArray = $http->postVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) );
00155         else
00156             $optionIDArray = array();
00157         if ( $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) ) )
00158             $optionValueArray = $http->postVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) );
00159         else
00160             $optionValueArray = array();
00161         if ( $http->hasPostVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) ) )
00162             $optionAdditionalPriceArray = $http->postVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) );
00163         else
00164             $optionAdditionalPriceArray = array();
00165 
00166         $option = new eZOption( $optionName );
00167 
00168         $i = 0;
00169         foreach ( $optionIDArray as $id )
00170         {
00171             $option->addOption( array( 'value' => $optionValueArray[$i],
00172                                        'additional_price' => ( isset( $optionAdditionalPriceArray[$i] ) ? $optionAdditionalPriceArray[$i] : 0 ) ) );
00173             $i++;
00174         }
00175         $contentObjectAttribute->setContent( $option );
00176         return true;
00177     }
00178 
00179 
00180     /*!
00181      Fetches the http post variables for collected information
00182     */
00183     function fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute )
00184     {
00185         if ( $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) ) )
00186         {
00187             $optionValue = $http->postVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) );
00188 
00189             $collectionAttribute->setAttribute( 'data_int', $optionValue );
00190             $attr = $contentObjectAttribute->attribute( 'contentclass_attribute' );
00191 
00192             return true;
00193         }
00194         return false;
00195     }
00196 
00197     function customObjectAttributeHTTPAction( $http, $action, $contentObjectAttribute, $parameters )
00198     {
00199         switch ( $action )
00200         {
00201             case "new_option" :
00202             {
00203                 $option = $contentObjectAttribute->content( );
00204 
00205                 $postvarname = "ContentObjectAttribute" . "_data_option_remove_" . $contentObjectAttribute->attribute( "id" );
00206                 if ( $http->hasPostVariable( $postvarname ) )
00207                 {
00208                     $idArray = $http->postVariable( $postvarname );
00209                     $beforeID = array_shift( $idArray );
00210                     if ( $beforeID >= 0 )
00211                     {
00212                         $option->insertOption( array(), $beforeID );
00213                         $contentObjectAttribute->setContent( $option );
00214                         $contentObjectAttribute->store();
00215                         $option = new eZOption( "" );
00216                         $option->decodeXML( $contentObjectAttribute->attribute( "data_text" ) );
00217                         $contentObjectAttribute->setContent( $option );
00218                         return;
00219                     }
00220                 }
00221                 $option->addOption( "" );
00222                 $contentObjectAttribute->setContent( $option );
00223                 $contentObjectAttribute->store();
00224             }break;
00225             case "remove_selected" :
00226             {
00227                 $option = $contentObjectAttribute->content( );
00228                 $postvarname = "ContentObjectAttribute" . "_data_option_remove_" . $contentObjectAttribute->attribute( "id" );
00229                 $array_remove = $http->postVariable( $postvarname );
00230                 $option->removeOptions( $array_remove );
00231                 $contentObjectAttribute->setContent( $option );
00232                 $contentObjectAttribute->store();
00233                 $option = new eZOption( "" );
00234                 $option->decodeXML( $contentObjectAttribute->attribute( "data_text" ) );
00235                 $contentObjectAttribute->setContent( $option );
00236             }break;
00237             default :
00238             {
00239                 eZDebug::writeError( "Unknown custom HTTP action: " . $action, "eZOptionType" );
00240             }break;
00241         }
00242     }
00243 
00244     /*!
00245      Finds the option which has the ID that matches \a $optionID, if found it returns
00246      an option structure.
00247     */
00248     function productOptionInformation( $objectAttribute, $optionID, $productItem )
00249     {
00250         $option = $objectAttribute->attribute( 'content' );
00251         foreach( $option->attribute( 'option_list' ) as $optionArray )
00252         {
00253             if ( $optionArray['id'] == $optionID )
00254             {
00255                 return array( 'id' => $optionArray['id'],
00256                               'name' => $option->attribute( 'name' ),
00257                               'value' => $optionArray['value'],
00258                               'additional_price' => $optionArray['additional_price'] );
00259             }
00260         }
00261         return false;
00262     }
00263 
00264     /*!
00265      Returns the integer value.
00266     */
00267     function title( $contentObjectAttribute, $name = "name" )
00268     {
00269         $option = $contentObjectAttribute->content( );
00270 
00271         $value = $option->attribute( $name );
00272 
00273         return $value;
00274     }
00275 
00276     function hasObjectAttributeContent( $contentObjectAttribute )
00277     {
00278         $option = $contentObjectAttribute->content( );
00279         $options = $option->attribute( 'option_list' );
00280         return count( $options ) > 0;
00281     }
00282 
00283     /*!
00284      Sets the default value.
00285     */
00286     function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
00287     {
00288         if ( $currentVersion == false )
00289         {
00290             $option = $contentObjectAttribute->content();
00291             $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
00292             if ( !$option )
00293             {
00294                 $option = new eZOption( $contentClassAttribute->attribute( 'data_text1' ) );
00295             }
00296             else
00297             {
00298                 $option->setName( $contentClassAttribute->attribute( 'data_text1' ) );
00299             }
00300             $contentObjectAttribute->setAttribute( "data_text", $option->xmlString() );
00301             $contentObjectAttribute->setContent( $option );
00302         }
00303         else
00304         {
00305             $dataText = $originalContentObjectAttribute->attribute( "data_text" );
00306             $contentObjectAttribute->setAttribute( "data_text", $dataText );
00307         }
00308     }
00309 
00310     function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
00311     {
00312         $defaultValueName = $base . self::DEFAULT_NAME_VARIABLE . $classAttribute->attribute( 'id' );
00313         if ( $http->hasPostVariable( $defaultValueName ) )
00314         {
00315             $defaultValueValue = $http->postVariable( $defaultValueName );
00316 
00317             if ($defaultValueValue == "")
00318             {
00319                 $defaultValueValue = "";
00320             }
00321             $classAttribute->setAttribute( 'data_text1', $defaultValueValue );
00322             return true;
00323         }
00324         return false;
00325     }
00326 
00327     function toString( $contentObjectAttribute )
00328     {
00329 
00330         $option = $contentObjectAttribute->attribute( 'content' );
00331         $optionArray = array();
00332         $optionArray[] = $option->attribute( 'name' );
00333 
00334         $optionList = $option->attribute( 'option_list' );
00335 
00336         foreach ( $optionList as $key => $value )
00337         {
00338             $optionArray[] = $value['value'];
00339             $optionArray[] = $value['additional_price'];
00340         }
00341         return eZStringUtils::implodeStr( $optionArray, "|" );
00342     }
00343 
00344 
00345     function fromString( $contentObjectAttribute, $string )
00346     {
00347         if ( $string == '' )
00348             return true;
00349 
00350         $optionArray = eZStringUtils::explodeStr( $string, '|' );
00351 
00352         $option = new eZOption( "" );
00353 
00354         $option->OptionCount = 0;
00355         $option->Options = array();
00356         $option->Name = array_shift( $optionArray );
00357         $count = count( $optionArray );
00358         for ( $i = 0; $i < $count; $i +=2 )
00359         {
00360 
00361             $option->addOption( array( 'value' => array_shift( $optionArray ),
00362                                        'additional_price' => array_shift( $optionArray ) ) );
00363         }
00364 
00365 
00366         $contentObjectAttribute->setAttribute( "data_text", $option->xmlString() );
00367 
00368         return $option;
00369 
00370     }
00371     function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00372     {
00373         $defaultValue = $classAttribute->attribute( 'data_text1' );
00374         $dom = $attributeParametersNode->ownerDocument;
00375         $defaultValueNode = $dom->createElement( 'default-value' );
00376         $defaultValueNode->appendChild( $dom->createTextNode( $defaultValue ) );
00377         $attributeParametersNode->appendChild( $defaultValueNode );
00378     }
00379 
00380     function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00381     {
00382         $defaultValue = $attributeParametersNode->getElementsByTagName( 'default-value' )->item( 0 )->textContent;
00383         $classAttribute->setAttribute( 'data_text1', $defaultValue );
00384     }
00385 
00386     function serializeContentObjectAttribute( $package, $objectAttribute )
00387     {
00388         $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00389 
00390         $domDocument = new DOMDocument( '1.0', 'utf-8' );
00391         $success = $domDocument->loadXML( $objectAttribute->attribute( 'data_text' ) );
00392 
00393         $importedRoot = $node->ownerDocument->importNode( $domDocument->documentElement, true );
00394         $node->appendChild( $importedRoot );
00395 
00396         return $node;
00397     }
00398 
00399     function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
00400     {
00401         $xmlString = '';
00402         $optionNode = $attributeNode->getElementsByTagName( 'ezoption' )->item( 0 );
00403 
00404         if ( $optionNode )
00405         {
00406             $xmlString = $optionNode->ownerDocument->saveXML( $optionNode );
00407         }
00408         else
00409         {
00410             // backward compatibility
00411             $optionNode = $attributeNode->getElementsByTagName( 'data-text' )->item( 0 );
00412             if ( $optionNode )
00413             {
00414                 $xmlString = $optionNode->textContent;
00415             }
00416             else
00417             {
00418                 // dl: unknown case. Probably should be removed at all.
00419                 $optionNode = $attributeNode->firstChild;
00420                 $xmlString = $optionNode->getAttribute( 'local_name' ) == 'data-text' ? '' : $optionNode->textContent;
00421             }
00422         }
00423 
00424         $objectAttribute->setAttribute( 'data_text', $xmlString );
00425     }
00426 
00427     function isInformationCollector()
00428     {
00429         return true;
00430     }
00431 
00432     function supportsBatchInitializeObjectAttribute()
00433     {
00434         return true;
00435     }
00436 
00437     function batchInitializeObjectAttributeData( $classAttribute )
00438     {
00439         $option = new eZOption( $classAttribute->attribute( 'data_text1' ) );
00440         $db = eZDB::instance();
00441         return array( 'data_text' =>  "'" . $db->escapeString( $option->xmlString() ) . "'" );
00442     }
00443 }
00444 
00445 eZDataType::register( eZOptionType::DATA_TYPE_STRING, "eZOptionType" );
00446 
00447 ?>