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