[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/kernel/classes/datatypes/ezprice/ -> ezpricetype.php (source)

   1  <?php
   2  //
   3  // Definition of eZPriceType class
   4  //
   5  // Created on: <26-Apr-2002 16:54:35 bf>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*!
  30    \class eZPriceType ezpricetype.php
  31    \ingroup eZDatatype
  32    \brief Stores a price (float)
  33  
  34  */
  35  
  36  include_once ( "kernel/classes/ezdatatype.php" );
  37  include_once ( "kernel/classes/datatypes/ezprice/ezprice.php" );
  38  
  39  define( "EZ_DATATYPESTRING_PRICE", "ezprice" );
  40  define( 'EZ_DATATYPESTRING_INCLUDE_VAT_FIELD', 'data_int1' );
  41  define( 'EZ_DATATYPESTRING_INCLUDE_VAT_VARIABLE', '_ezprice_include_vat_' );
  42  define( 'EZ_DATATYPESTRING_VAT_ID_FIELD', 'data_float1' );
  43  define( 'EZ_DATATYPESTRING_VAT_ID_VARIABLE', '_ezprice_vat_id_' );
  44  define( "EZ_PRICE_INCLUDED_VAT", 1 );
  45  define( "EZ_PRICE_EXCLUDED_VAT", 2 );
  46  
  47  class eZPriceType extends eZDataType
  48  {
  49      function eZPriceType()
  50      {
  51          $this->eZDataType( EZ_DATATYPESTRING_PRICE, ezi18n( 'kernel/classes/datatypes', "Price", 'Datatype name' ),
  52                             array( 'serialize_supported' => true,
  53                                    'object_serialize_map' => array( 'data_float' => 'price' ) ) );
  54      }
  55  
  56      /*!
  57       Validates the input and returns true if the input was
  58       valid for this datatype.
  59      */
  60      function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
  61      {
  62          // Check "price inc/ex VAT" and "VAT type" fields.
  63          $vatTypeID = $http->postVariable( $base . '_ezprice_vat_id_' . $contentObjectAttribute->attribute( 'id' ) );
  64          $vatExInc = $http->postVariable( $base . '_ezprice_inc_ex_vat_' . $contentObjectAttribute->attribute( 'id' ) );
  65          if ( $vatExInc == 1 && $vatTypeID == -1 )
  66          {
  67              $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
  68                                                                   'Dynamic VAT cannot be included.' ) );
  69              return EZ_INPUT_VALIDATOR_STATE_INVALID;
  70          }
  71  
  72          // Check price.
  73          if ( $http->hasPostVariable( $base . "_data_price_" . $contentObjectAttribute->attribute( "id" ) ) )
  74          {
  75              $data = $http->postVariable( $base . "_data_price_" . $contentObjectAttribute->attribute( "id" ) );
  76  
  77              include_once ( 'lib/ezlocale/classes/ezlocale.php' );
  78              $locale =& eZLocale::instance();
  79              $data = $locale->internalCurrency( $data );
  80              $classAttribute =& $contentObjectAttribute->contentClassAttribute();
  81              if( !$contentObjectAttribute->validateIsRequired() && ( $data == "" ) )
  82              {
  83                  return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
  84              }
  85              if ( preg_match( "#^[0-9]+(.){0,1}[0-9]{0,2}$#", $data ) )
  86                  return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
  87  
  88              $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
  89                                                                   'Invalid price.' ) );
  90              return EZ_INPUT_VALIDATOR_STATE_INVALID;
  91          }
  92          else
  93          {
  94              return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
  95          }
  96      }
  97  
  98      function storeObjectAttribute( &$attribute )
  99      {
 100      }
 101  
 102      function metaData( $contentObjectAttribute )
 103      {
 104          return $contentObjectAttribute->attribute( "data_float" );
 105      }
 106  
 107      /*!
 108       reimp
 109      */
 110      function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
 111      {
 112          if ( $currentVersion != false )
 113          {
 114              $dataText = $originalContentObjectAttribute->attribute( "data_text" );
 115              $dataFloat = $originalContentObjectAttribute->attribute( "data_float" );
 116              $contentObjectAttribute->setAttribute( "data_float", $dataFloat );
 117              $contentObjectAttribute->setAttribute( "data_text", $dataText );
 118          }
 119      }
 120  
 121      /*!
 122       Set default class attribute value
 123      */
 124      function initializeClassAttribute( &$classAttribute )
 125      {
 126          if ( $classAttribute->attribute( EZ_DATATYPESTRING_INCLUDE_VAT_FIELD ) == 0 )
 127              $classAttribute->setAttribute( EZ_DATATYPESTRING_INCLUDE_VAT_FIELD, EZ_PRICE_INCLUDED_VAT );
 128          $classAttribute->store();
 129      }
 130      function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
 131      {
 132          $isVatIncludedVariable = $base . EZ_DATATYPESTRING_INCLUDE_VAT_VARIABLE . $classAttribute->attribute( 'id' );
 133          if ( $http->hasPostVariable( $isVatIncludedVariable ) )
 134          {
 135              $isVatIncluded = $http->postVariable( $isVatIncludedVariable );
 136              $classAttribute->setAttribute( EZ_DATATYPESTRING_INCLUDE_VAT_FIELD, $isVatIncluded );
 137          }
 138          $vatIDVariable = $base . EZ_DATATYPESTRING_VAT_ID_VARIABLE . $classAttribute->attribute( 'id' );
 139          if ( $http->hasPostVariable( $vatIDVariable  ) )
 140          {
 141              $vatID = $http->postVariable( $vatIDVariable  );
 142              $classAttribute->setAttribute( EZ_DATATYPESTRING_VAT_ID_FIELD, $vatID );
 143          }
 144          return true;
 145      }
 146  
 147      /*!
 148       Fetches the http post var integer input and stores it in the data instance.
 149      */
 150      function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
 151      {
 152          $data = $http->postVariable( $base . "_data_price_" . $contentObjectAttribute->attribute( "id" ) );
 153          $vatType = $http->postVariable( $base . '_ezprice_vat_id_' . $contentObjectAttribute->attribute( 'id' ) );
 154          $vatExInc = $http->postVariable( $base . '_ezprice_inc_ex_vat_' . $contentObjectAttribute->attribute( 'id' ) );
 155  
 156          include_once ( 'lib/ezlocale/classes/ezlocale.php' );
 157          $locale =& eZLocale::instance();
 158          $data = $locale->internalCurrency( $data );
 159  
 160          $data_text = $vatType . ',' . $vatExInc;
 161  
 162          $contentObjectAttribute->setAttribute( "data_float", $data );
 163          $contentObjectAttribute->setAttribute( 'data_text', $data_text );
 164  
 165          return true;
 166      }
 167  
 168      /*!
 169       Returns the content.
 170      */
 171      function &objectAttributeContent( &$contentObjectAttribute )
 172      {
 173          $classAttribute =& $contentObjectAttribute->contentClassAttribute();
 174          $storedPrice = $contentObjectAttribute->attribute( "data_float" );
 175          $price = new eZPrice( $classAttribute, $contentObjectAttribute, $storedPrice );
 176  
 177          if ( $contentObjectAttribute->attribute( 'data_text' ) != '' )
 178          {
 179              list( $vatType, $vatExInc ) = explode( ',', $contentObjectAttribute->attribute( "data_text" ), 2 );
 180  
 181              $price->setAttribute( 'selected_vat_type', $vatType );
 182              $price->setAttribute( 'is_vat_included', $vatExInc );
 183          }
 184  
 185          return $price;
 186      }
 187  
 188      /*!
 189       Returns class content.
 190      */
 191      function &classAttributeContent( &$classAttribute )
 192      {
 193          $contentObjectAttribute = false;
 194          $price = new eZPrice( $classAttribute, $contentObjectAttribute );
 195          return $price;
 196      }
 197  
 198      function contentActionList( )
 199      {
 200          return array( array( 'name' => ezi18n( 'kernel/classes/datatypes', 'Add to basket' ),
 201                               'action' => 'ActionAddToBasket'
 202                               ),
 203                        array( 'name' => ezi18n( 'kernel/classes/datatypes', 'Add to wish list' ),
 204                               'action' => 'ActionAddToWishList'
 205                               ) );
 206      }
 207  
 208      function title( &$contentObjectAttribute )
 209      {
 210          return $contentObjectAttribute->attribute( "data_float" );
 211      }
 212  
 213      /*!
 214       \reimp
 215      */
 216      function sortKey( &$contentObjectAttribute )
 217      {
 218          $intPrice = (int)($contentObjectAttribute->attribute( 'data_float' ) * 100.00);
 219          return $intPrice;
 220      }
 221  
 222      /*!
 223       \reimp
 224      */
 225      function sortKeyType()
 226      {
 227          return 'int';
 228      }
 229  
 230      function hasObjectAttributeContent( &$contentObjectAttribute )
 231      {
 232          return true;
 233      }
 234  
 235      function toString( $contentObjectAttribute )
 236      {
 237  
 238          $price = $contentObjectAttribute->attribute( 'content' );
 239          $vatType =$price->attribute( 'selected_vat_type' );
 240  
 241          $priceStr = implode( '|', array( $price->attribute( 'price' ), $vatType->attribute( 'id' ) , ($price->attribute( 'is_vat_included' ) )? 1:0 ) );
 242          return $priceStr;
 243      }
 244  
 245  
 246      function fromString( &$contentObjectAttribute, $string )
 247      {
 248          if ( $string == '' )
 249              return true;
 250  
 251          $priceData = explode( '|', $string );
 252          if ( count( $priceData ) != 3 )
 253              return false;
 254  
 255          $dataText = $priceData[1] . ',' . $priceData[1];
 256          $price = $priceData[0];
 257  
 258          $contentObjectAttribute->setAttribute( "data_float", $price );
 259          $contentObjectAttribute->setAttribute( 'data_text', $dataText );
 260  
 261          return true;
 262      }
 263  
 264      /*!
 265       \reimp
 266      */
 267      function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
 268      {
 269          $price =& $classAttribute->content();
 270          if ( $price )
 271          {
 272              $vatIncluded = $price->attribute( 'is_vat_included' );
 273              $vatTypes = $price->attribute( 'vat_type' );
 274              $attributeParametersNode->appendChild( eZDOMDocument::createElementNode( 'vat-included',
 275                                                                                       array( 'is-set' => $vatIncluded ? 'true' : 'false' ) ) );
 276              $vatTypeNode = eZDOMDocument::createElementNode( 'vat-type' );
 277              $chosenVatType = $classAttribute->attribute( 'data_float1' );
 278              $gotVat = false;
 279              foreach ( $vatTypes as $vatType )
 280              {
 281                  $id = $vatType->attribute( 'id' );
 282                  if ( $id == $chosenVatType )
 283                  {
 284                      $vatTypeNode->appendAttribute( eZDOMDocument::createAttributeNode( 'name', $vatType->attribute( 'name' ) ) );
 285                      $vatTypeNode->appendAttribute( eZDOMDocument::createAttributeNode( 'percentage', $vatType->attribute( 'percentage' ) ) );
 286                      $gotVat = true;
 287                      break;
 288                  }
 289              }
 290              if ( $gotVat )
 291                  $attributeParametersNode->appendChild( $vatTypeNode );
 292          }
 293      }
 294  
 295      /*!
 296       \reimp
 297      */
 298      function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
 299      {
 300          $vatNode =& $attributeParametersNode->elementByName( 'vat-included' );
 301          $vatIncluded = strtolower( $vatNode->attributeValue( 'is-set' ) ) == 'true';
 302          if ( $vatIncluded )
 303              $vatIncluded = EZ_PRICE_INCLUDED_VAT;
 304          else
 305              $vatIncluded = EZ_PRICE_EXCLUDED_VAT;
 306  
 307          $classAttribute->setAttribute( EZ_DATATYPESTRING_INCLUDE_VAT_FIELD, $vatIncluded );
 308          $vatTypeNode =& $attributeParametersNode->elementByName( 'vat-type' );
 309          $vatName = $vatTypeNode->attributeValue( 'name' );
 310          $vatPercentage = $vatTypeNode->attributeValue( 'percentage' );
 311          $vatID = false;
 312          $vatTypes = eZVATType::fetchList();
 313          foreach ( array_keys( $vatTypes ) as $vatTypeKey )
 314          {
 315              $vatType =& $vatTypes[$vatTypeKey];
 316              if ( $vatType->attribute( 'name' ) == $vatName and
 317                   $vatType->attribute( 'percentage' ) == $vatPercentage )
 318              {
 319                  $vatID = $vatType->attribute( 'id' );
 320                  break;
 321              }
 322          }
 323          if ( !$vatID )
 324          {
 325              $vatType = eZVATType::create();
 326              $vatType->setAttribute( 'name', $vatName );
 327              $vatType->setAttribute( 'percentage', $vatPercentage );
 328              $vatType->store();
 329              $vatID = $vatType->attribute( 'id' );
 330          }
 331          $classAttribute->setAttribute( EZ_DATATYPESTRING_VAT_ID_FIELD, $vatID );
 332      }
 333  }
 334  
 335  eZDataType::register( EZ_DATATYPESTRING_PRICE, "ezpricetype" );
 336  
 337  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7