eZ Publish  [4.0]
ezpricetype.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZPriceType class
00004 //
00005 // Created on: <26-Apr-2002 16:54:35 bf>
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 eZPriceType ezpricetype.php
00033   \ingroup eZDatatype
00034   \brief Stores a price (float)
00035 
00036 */
00037 
00038 //include_once( "kernel/classes/ezdatatype.php" );
00039 //include_once( "kernel/classes/datatypes/ezprice/ezprice.php" );
00040 
00041 class eZPriceType extends eZDataType
00042 {
00043     const DATA_TYPE_STRING = "ezprice";
00044     const INCLUDE_VAT_FIELD = 'data_int1';
00045     const INCLUDE_VAT_VARIABLE = '_ezprice_include_vat_';
00046     const VAT_ID_FIELD = 'data_float1';
00047     const VAT_ID_VARIABLE = '_ezprice_vat_id_';
00048     const INCLUDED_VAT = 1;
00049     const EXCLUDED_VAT = 2;
00050 
00051     function eZPriceType()
00052     {
00053         $this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', "Price", 'Datatype name' ),
00054                            array( 'serialize_supported' => true,
00055                                   'object_serialize_map' => array( 'data_float' => 'price' ) ) );
00056     }
00057 
00058     /*!
00059      Validates the input and returns true if the input was
00060      valid for this datatype.
00061     */
00062     function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00063     {
00064         // Check "price inc/ex VAT" and "VAT type" fields.
00065         $vatTypeID = $http->postVariable( $base . '_ezprice_vat_id_' . $contentObjectAttribute->attribute( 'id' ) );
00066         $vatExInc = $http->postVariable( $base . '_ezprice_inc_ex_vat_' . $contentObjectAttribute->attribute( 'id' ) );
00067         if ( $vatExInc == 1 && $vatTypeID == -1 )
00068         {
00069             $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00070                                                                  'Dynamic VAT cannot be included.' ) );
00071             return eZInputValidator::STATE_INVALID;
00072         }
00073 
00074         // Check price.
00075         if ( $http->hasPostVariable( $base . "_data_price_" . $contentObjectAttribute->attribute( "id" ) ) )
00076         {
00077             $data = $http->postVariable( $base . "_data_price_" . $contentObjectAttribute->attribute( "id" ) );
00078 
00079             //include_once( 'lib/ezlocale/classes/ezlocale.php' );
00080             $locale = eZLocale::instance();
00081             $data = $locale->internalCurrency( $data );
00082             $classAttribute = $contentObjectAttribute->contentClassAttribute();
00083             if( !$contentObjectAttribute->validateIsRequired() && ( $data == "" ) )
00084             {
00085                 return eZInputValidator::STATE_ACCEPTED;
00086             }
00087             if ( preg_match( "#^[0-9]+(.){0,1}[0-9]{0,2}$#", $data ) )
00088                 return eZInputValidator::STATE_ACCEPTED;
00089 
00090             $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00091                                                                  'Invalid price.' ) );
00092             return eZInputValidator::STATE_INVALID;
00093         }
00094         else
00095         {
00096             return eZInputValidator::STATE_ACCEPTED;
00097         }
00098     }
00099 
00100     function storeObjectAttribute( $attribute )
00101     {
00102     }
00103 
00104     function metaData( $contentObjectAttribute )
00105     {
00106         return $contentObjectAttribute->attribute( "data_float" );
00107     }
00108 
00109     /*!
00110      reimp
00111     */
00112     function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
00113     {
00114         if ( $currentVersion != false )
00115         {
00116             $dataText = $originalContentObjectAttribute->attribute( "data_text" );
00117             $dataFloat = $originalContentObjectAttribute->attribute( "data_float" );
00118             $contentObjectAttribute->setAttribute( "data_float", $dataFloat );
00119             $contentObjectAttribute->setAttribute( "data_text", $dataText );
00120         }
00121     }
00122 
00123     /*!
00124      Set default class attribute value
00125     */
00126     function initializeClassAttribute( $classAttribute )
00127     {
00128         if ( $classAttribute->attribute( self::INCLUDE_VAT_FIELD ) == 0 )
00129             $classAttribute->setAttribute( self::INCLUDE_VAT_FIELD, self::INCLUDED_VAT );
00130         $classAttribute->store();
00131     }
00132     function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
00133     {
00134         $isVatIncludedVariable = $base . self::INCLUDE_VAT_VARIABLE . $classAttribute->attribute( 'id' );
00135         if ( $http->hasPostVariable( $isVatIncludedVariable ) )
00136         {
00137             $isVatIncluded = $http->postVariable( $isVatIncludedVariable );
00138             $classAttribute->setAttribute( self::INCLUDE_VAT_FIELD, $isVatIncluded );
00139         }
00140         $vatIDVariable = $base . self::VAT_ID_VARIABLE . $classAttribute->attribute( 'id' );
00141         if ( $http->hasPostVariable( $vatIDVariable  ) )
00142         {
00143             $vatID = $http->postVariable( $vatIDVariable  );
00144             $classAttribute->setAttribute( self::VAT_ID_FIELD, $vatID );
00145         }
00146         return true;
00147     }
00148 
00149     /*!
00150      Fetches the http post var integer input and stores it in the data instance.
00151     */
00152     function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00153     {
00154         $data = $http->postVariable( $base . "_data_price_" . $contentObjectAttribute->attribute( "id" ) );
00155         $vatType = $http->postVariable( $base . '_ezprice_vat_id_' . $contentObjectAttribute->attribute( 'id' ) );
00156         $vatExInc = $http->postVariable( $base . '_ezprice_inc_ex_vat_' . $contentObjectAttribute->attribute( 'id' ) );
00157 
00158         //include_once( 'lib/ezlocale/classes/ezlocale.php' );
00159         $locale = eZLocale::instance();
00160         $data = $locale->internalCurrency( $data );
00161 
00162         $data_text = $vatType . ',' . $vatExInc;
00163 
00164         $contentObjectAttribute->setAttribute( "data_float", $data );
00165         $contentObjectAttribute->setAttribute( 'data_text', $data_text );
00166 
00167         return true;
00168     }
00169 
00170     /*!
00171      Returns the content.
00172     */
00173     function objectAttributeContent( $contentObjectAttribute )
00174     {
00175         $classAttribute = $contentObjectAttribute->contentClassAttribute();
00176         $storedPrice = $contentObjectAttribute->attribute( "data_float" );
00177         $price = new eZPrice( $classAttribute, $contentObjectAttribute, $storedPrice );
00178 
00179         if ( $contentObjectAttribute->attribute( 'data_text' ) != '' )
00180         {
00181             list( $vatType, $vatExInc ) = explode( ',', $contentObjectAttribute->attribute( "data_text" ), 2 );
00182 
00183             $price->setAttribute( 'selected_vat_type', $vatType );
00184             $price->setAttribute( 'is_vat_included', $vatExInc );
00185         }
00186 
00187         return $price;
00188     }
00189 
00190     /*!
00191      Returns class content.
00192     */
00193     function classAttributeContent( $classAttribute )
00194     {
00195         $contentObjectAttribute = false;
00196         $price = new eZPrice( $classAttribute, $contentObjectAttribute );
00197         return $price;
00198     }
00199 
00200     function contentActionList( $classAttribute )
00201     {
00202         return array( array( 'name' => ezi18n( 'kernel/classes/datatypes', 'Add to basket' ),
00203                              'action' => 'ActionAddToBasket'
00204                              ),
00205                       array( 'name' => ezi18n( 'kernel/classes/datatypes', 'Add to wish list' ),
00206                              'action' => 'ActionAddToWishList'
00207                              ) );
00208     }
00209 
00210     function title( $contentObjectAttribute, $name = null )
00211     {
00212         return $contentObjectAttribute->attribute( "data_float" );
00213     }
00214 
00215     /*!
00216      \reimp
00217     */
00218     function sortKey( $contentObjectAttribute )
00219     {
00220         $intPrice = (int)($contentObjectAttribute->attribute( 'data_float' ) * 100.00);
00221         return $intPrice;
00222     }
00223 
00224     /*!
00225      \reimp
00226     */
00227     function sortKeyType()
00228     {
00229         return 'int';
00230     }
00231 
00232     function hasObjectAttributeContent( $contentObjectAttribute )
00233     {
00234         return true;
00235     }
00236 
00237     function toString( $contentObjectAttribute )
00238     {
00239 
00240         $price = $contentObjectAttribute->attribute( 'content' );
00241         $vatType =$price->attribute( 'selected_vat_type' );
00242 
00243         $priceStr = implode( '|', array( $price->attribute( 'price' ), $vatType->attribute( 'id' ) , ($price->attribute( 'is_vat_included' ) )? 1:0 ) );
00244         return $priceStr;
00245     }
00246 
00247 
00248     function fromString( $contentObjectAttribute, $string )
00249     {
00250         if ( $string == '' )
00251             return true;
00252 
00253         $priceData = explode( '|', $string );
00254         if ( count( $priceData ) != 3 )
00255             return false;
00256 
00257         $dataText = $priceData[1] . ',' . $priceData[2];
00258         $price = $priceData[0];
00259 
00260         $contentObjectAttribute->setAttribute( "data_float", $price );
00261         $contentObjectAttribute->setAttribute( 'data_text', $dataText );
00262 
00263         return true;
00264     }
00265 
00266     /*!
00267      \reimp
00268     */
00269     function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00270     {
00271         $price = $classAttribute->content();
00272         if ( $price )
00273         {
00274             $vatIncluded = $price->attribute( 'is_vat_included' );
00275             $vatTypes = $price->attribute( 'vat_type' );
00276 
00277             $dom = $attributeParametersNode->ownerDocument;
00278             $vatIncludedNode = $dom->createElement( 'vat-included' );
00279             $vatIncludedNode->setAttribute( 'is-set', $vatIncluded ? 'true' : 'false' );
00280             $attributeParametersNode->appendChild( $vatIncludedNode );
00281             $vatTypeNode = $dom->createElement( 'vat-type' );
00282             $chosenVatType = $classAttribute->attribute( 'data_float1' );
00283             $gotVat = false;
00284             foreach ( $vatTypes as $vatType )
00285             {
00286                 $id = $vatType->attribute( 'id' );
00287                 if ( $id == $chosenVatType )
00288                 {
00289                     $vatTypeNode->setAttribute( 'name', $vatType->attribute( 'name' ) );
00290                     $vatTypeNode->setAttribute( 'percentage', $vatType->attribute( 'percentage' ) );
00291                     $gotVat = true;
00292                     break;
00293                 }
00294             }
00295             if ( $gotVat )
00296                 $attributeParametersNode->appendChild( $vatTypeNode );
00297         }
00298     }
00299 
00300     /*!
00301      \reimp
00302     */
00303     function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00304     {
00305         $vatNode = $attributeParametersNode->getElementsByTagName( 'vat-included' )->item( 0 );
00306         $vatIncluded = strtolower( $vatNode->getAttribute( 'is-set' ) ) == 'true';
00307         if ( $vatIncluded )
00308             $vatIncluded = self::INCLUDED_VAT;
00309         else
00310             $vatIncluded = self::EXCLUDED_VAT;
00311 
00312         $classAttribute->setAttribute( self::INCLUDE_VAT_FIELD, $vatIncluded );
00313         $vatTypeNode = $attributeParametersNode->getElementsByTagName( 'vat-type' )->item( 0 );
00314         $vatName = $vatTypeNode->getAttribute( 'name' );
00315         $vatPercentage = $vatTypeNode->getAttribute( 'percentage' );
00316         $vatID = false;
00317         $vatTypes = eZVatType::fetchList();
00318         foreach ( $vatTypes as $vatType )
00319         {
00320             if ( $vatType->attribute( 'name' ) == $vatName and
00321                  $vatType->attribute( 'percentage' ) == $vatPercentage )
00322             {
00323                 $vatID = $vatType->attribute( 'id' );
00324                 break;
00325             }
00326         }
00327         if ( !$vatID )
00328         {
00329             $vatType = eZVatType::create();
00330             $vatType->setAttribute( 'name', $vatName );
00331             $vatType->setAttribute( 'percentage', $vatPercentage );
00332             $vatType->store();
00333             $vatID = $vatType->attribute( 'id' );
00334         }
00335         $classAttribute->setAttribute( self::VAT_ID_FIELD, $vatID );
00336     }
00337 }
00338 
00339 eZDataType::register( eZPriceType::DATA_TYPE_STRING, "eZPriceType" );
00340 
00341 ?>