[ 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/ezmultiprice/ -> ezmultipricetype.php (source)

   1  <?php
   2  //
   3  // Definition of eZMultiPriceType class
   4  //
   5  // Created on: <04-Nov-2005 16:54:35 dl>
   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 eZMultiPriceType ezmultipricetype.php
  31    \ingroup eZMultiDatatype
  32    \brief Stores a price in multicurrency.
  33  
  34  */
  35  
  36  include_once ( 'kernel/classes/ezdatatype.php' );
  37  include_once ( 'kernel/classes/datatypes/ezmultiprice/ezmultiprice.php' );
  38  include_once ( 'lib/ezutils/classes/ezstringutils.php' );
  39  define( 'EZ_DATATYPESTRING_MULTIPRICE', 'ezmultiprice' );
  40  define( 'EZ_DATATYPESTRING_DEFAULT_CURRENCY_CODE_FIELD', 'data_text1' );
  41  define( 'EZ_DATATYPESTRING_CURRENCY_CODE_VARIABLE', '_ezmultiprice_currency_code_' );
  42  define( 'EZ_DATATYPESTRING_MULTIPRICE_INCLUDE_VAT_FIELD', 'data_int1' );
  43  define( 'EZ_DATATYPESTRING_MULTIPRICE_INCLUDE_VAT_VARIABLE', '_ezmultiprice_include_vat_' );
  44  define( 'EZ_DATATYPESTRING_MULTIPRICE_VAT_ID_FIELD', 'data_float1' );
  45  define( 'EZ_DATATYPESTRING_MULTIPRICE_VAT_ID_VARIABLE', '_ezmultiprice_vat_id_' );
  46  define( 'EZ_MULTIPRICE_INCLUDED_VAT', 1 );
  47  define( 'EZ_MULTIPRICE_EXCLUDED_VAT', 2 );
  48  
  49  class eZMultiPriceType extends eZDataType
  50  {
  51      function eZMultiPriceType()
  52      {
  53          $this->eZDataType( EZ_DATATYPESTRING_MULTIPRICE, ezi18n( 'kernel/classes/datatypes', 'Multi-price', 'Datatype name' ),
  54                              array( 'serialize_supported' => true ) );
  55      }
  56  
  57      /*!
  58       Validates the input and returns true if the input was
  59       valid for this datatype.
  60      */
  61      function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
  62      {
  63          // Check "price inc/ex VAT" and "VAT type" fields.
  64          $vatTypeID = $http->postVariable( $base . '_ezmultiprice_vat_id_' . $contentObjectAttribute->attribute( 'id' ) );
  65          $vatExInc = $http->postVariable( $base . '_ezmultiprice_inc_ex_vat_' . $contentObjectAttribute->attribute( 'id' ) );
  66  
  67  
  68          if ( $vatExInc == 1 && $vatTypeID == -1 )
  69          {
  70              $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
  71                                                                   'Dynamic VAT cannot be included.' ) );
  72              return EZ_INPUT_VALIDATOR_STATE_INVALID;
  73          }
  74  
  75          // Check price.
  76          if ( $http->hasPostVariable( $base . '_price_array_' . $contentObjectAttribute->attribute( "id" ) ) )
  77          {
  78              $customPriceList = $http->postVariable( $base . '_price_array_' . $contentObjectAttribute->attribute( "id" ) );
  79              foreach ( $customPriceList as $currencyCode => $value )
  80              {
  81                  if( $contentObjectAttribute->validateIsRequired() || ( $value != '' ) )
  82                  {
  83                      if ( !preg_match( "#^[0-9]+(.){0,1}[0-9]{0,2}$#", $value ) )
  84                      {
  85                          $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
  86                                                                               "Invalid price for '%currencyCode' currency ",
  87                                                                               false,
  88                                                                               array( '%currencyCode' => $currencyCode ) ) );
  89                          return EZ_INPUT_VALIDATOR_STATE_INVALID;
  90                      }
  91                  }
  92              }
  93          }
  94  
  95          return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
  96      }
  97  
  98      function storeObjectAttribute( &$attribute )
  99      {
 100          $multiprice =& $attribute->attribute( 'content' );
 101          $multiprice->store();
 102      }
 103  
 104      /* ???????????????
 105      function metaData( $contentObjectAttribute )
 106      {
 107          return eZPriceType::metaData( $contentObjectAttribute );
 108          //return $contentObjectAttribute->attribute( "data_float" );
 109      }
 110      */
 111  
 112      /*!
 113       Set default class attribute value
 114      */
 115      function initializeClassAttribute( &$classAttribute )
 116      {
 117          if ( $classAttribute->attribute( EZ_DATATYPESTRING_MULTIPRICE_INCLUDE_VAT_FIELD ) == 0 )
 118              $classAttribute->setAttribute( EZ_DATATYPESTRING_MULTIPRICE_INCLUDE_VAT_FIELD, EZ_MULTIPRICE_INCLUDED_VAT );
 119          $classAttribute->store();
 120      }
 121  
 122      /*!
 123       Set default object attribute value.
 124      */
 125      function postInitializeObjectAttribute( &$objectAttribute, $currentVersion, &$originalContentObjectAttribute )
 126      {
 127          $contentClassAttribute =& $objectAttribute->contentClassAttribute();
 128          $multiprice = new eZMultiPrice( $contentClassAttribute, $objectAttribute );
 129  
 130          if ( $currentVersion == false )
 131          {
 132              $defaultCurrency = $contentClassAttribute->attribute( EZ_DATATYPESTRING_DEFAULT_CURRENCY_CODE_FIELD );
 133              $multiprice->setCustomPrice( $defaultCurrency, '0.00' );
 134              $multiprice->updateAutoPriceList();
 135              $multiprice->store();
 136          }
 137          else
 138          {
 139              $originalMultiprice =& $originalContentObjectAttribute->content();
 140              $multiprice = new eZMultiPrice( $contentClassAttribute, $objectAttribute );
 141  
 142              $priceList =& $originalMultiprice->priceList();
 143              foreach ( $priceList as $price )
 144                  $multiprice->setPriceByCurrency( $price->attribute( 'currency_code' ), $price->attribute( 'value' ), $price->attribute( 'type') );
 145  
 146              $multiprice->store();
 147          }
 148      }
 149  
 150      function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
 151      {
 152          $currencyCodeVariable = $base . EZ_DATATYPESTRING_CURRENCY_CODE_VARIABLE . $classAttribute->attribute( 'id' );
 153          if ( $http->hasPostVariable( $currencyCodeVariable ) )
 154          {
 155              $currencyCode = $http->postVariable( $currencyCodeVariable );
 156              $classAttribute->setAttribute( EZ_DATATYPESTRING_DEFAULT_CURRENCY_CODE_FIELD, $currencyCode );
 157          }
 158  
 159          $isVatIncludedVariable = $base . EZ_DATATYPESTRING_MULTIPRICE_INCLUDE_VAT_VARIABLE . $classAttribute->attribute( 'id' );
 160          if ( $http->hasPostVariable( $isVatIncludedVariable ) )
 161          {
 162              $isVatIncluded = $http->postVariable( $isVatIncludedVariable );
 163              $classAttribute->setAttribute( EZ_DATATYPESTRING_MULTIPRICE_INCLUDE_VAT_FIELD, $isVatIncluded );
 164          }
 165          $vatIDVariable = $base . EZ_DATATYPESTRING_MULTIPRICE_VAT_ID_VARIABLE . $classAttribute->attribute( 'id' );
 166          if ( $http->hasPostVariable( $vatIDVariable  ) )
 167          {
 168              $vatID = $http->postVariable( $vatIDVariable  );
 169              $classAttribute->setAttribute( EZ_DATATYPESTRING_MULTIPRICE_VAT_ID_FIELD, $vatID );
 170          }
 171          return true;
 172      }
 173  
 174      /*!
 175       Fetches the http post var integer input and stores it in the data instance.
 176      */
 177      function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
 178      {
 179          $multiprice =& $contentObjectAttribute->attribute( 'content' );
 180  
 181          $priceArrayName = $base . "_price_array_" . $contentObjectAttribute->attribute( "id" );
 182          if ( $http->hasPostVariable( $priceArrayName ) )
 183          {
 184              $customPriceList = $http->postVariable( $priceArrayName );
 185  
 186              foreach ( $customPriceList as $currencyCode => $value )
 187                  $multiprice->setCustomPrice( $currencyCode, $value );
 188          }
 189  
 190          $multiprice->updateAutoPriceList();
 191  
 192          $vatType = $http->postVariable( $base . '_ezmultiprice_vat_id_' . $contentObjectAttribute->attribute( 'id' ) );
 193          $vatExInc = $http->postVariable( $base . '_ezmultiprice_inc_ex_vat_' . $contentObjectAttribute->attribute( 'id' ) );
 194          $multiprice->setAttribute( 'selected_vat_type', $vatType );
 195          $multiprice->setAttribute( 'is_vat_included', $vatExInc );
 196  
 197          $data_text = $vatType . ',' . $vatExInc;
 198          $contentObjectAttribute->setAttribute( 'data_text', $data_text );
 199  
 200          return true;
 201      }
 202  
 203      /*!
 204       Returns the content.
 205      */
 206      function &objectAttributeContent( &$contentObjectAttribute )
 207      {
 208          $classAttribute =& $contentObjectAttribute->contentClassAttribute();
 209          $multiprice = new eZMultiPrice( $classAttribute, $contentObjectAttribute );
 210  
 211          if ( $contentObjectAttribute->attribute( 'data_text' ) != '' )
 212          {
 213              list( $vatType, $vatExInc ) = explode( ',', $contentObjectAttribute->attribute( 'data_text' ), 2 );
 214  
 215              $multiprice->setAttribute( 'selected_vat_type', $vatType );
 216              $multiprice->setAttribute( 'is_vat_included', $vatExInc );
 217          }
 218  
 219          return $multiprice;
 220      }
 221  
 222      /*!
 223       Returns class content.
 224      */
 225      function &classAttributeContent( &$classAttribute )
 226      {
 227          $contentObjectAttribute = false;
 228          $multiprice = new eZMultiPrice( $classAttribute, $contentObjectAttribute );
 229          return $multiprice;
 230      }
 231  
 232      function customObjectAttributeHTTPAction( $http, $action, &$contentObjectAttribute )
 233      {
 234          switch ( $action )
 235          {
 236              case 'set_custom_price' :
 237              {
 238                  $selectedCurrencyName = 'ContentObjectAttribute' . '_selected_currency_' . $contentObjectAttribute->attribute( 'id' );
 239                  if ( $http->hasPostVariable( $selectedCurrencyName ) )
 240                  {
 241                      $selectedCurrency = $http->postVariable( $selectedCurrencyName );
 242                      $multiprice =& $contentObjectAttribute->content();
 243  
 244                      // to keep right order of currency after adding we do 'remove' and 'add'
 245                      // instead of just '$multiprice->setCustomPrice( $currencyCode, false )'
 246                      $price =& $multiprice->priceByCurrency( $selectedCurrency );
 247                      $multiprice->removePriceByCurrency( $selectedCurrency );
 248                      $multiprice->setCustomPrice( $selectedCurrency, $price->attribute( 'value' ) );
 249  
 250                      $multiprice->store();
 251                  }
 252              }break;
 253  
 254              case 'remove_prices' :
 255              {
 256                  $removePriceArrayName = 'ContentObjectAttribute' . '_remove_price_array_' . $contentObjectAttribute->attribute( 'id' );
 257                  if ( $http->hasPostVariable( $removePriceArrayName ) )
 258                  {
 259                      $removePriceArray = $http->postVariable( $removePriceArrayName );
 260                      $multiprice =& $contentObjectAttribute->content();
 261  
 262                      foreach( $removePriceArray as $currencyCode => $value )
 263                          $multiprice->setAutoPrice( $currencyCode, false );
 264  
 265                      $multiprice->updateAutoPriceList();
 266                      $multiprice->store();
 267                  }
 268              }break;
 269  
 270              default :
 271              {
 272                  eZDebug::writeError( 'Unknown custom HTTP action: ' . $action, 'eZMultiPriceType' );
 273              }break;
 274          }
 275      }
 276  
 277      function contentActionList( )
 278      {
 279          return array( array( 'name' => ezi18n( 'kernel/classes/datatypes', 'Add to basket' ),
 280                               'action' => 'ActionAddToBasket'
 281                               ),
 282                        array( 'name' => ezi18n( 'kernel/classes/datatypes', 'Add to wish list' ),
 283                               'action' => 'ActionAddToWishList'
 284                               ) );
 285      }
 286  
 287      /*!
 288       Clean up stored object attribute
 289      */
 290      function deleteStoredObjectAttribute( &$objectAttribute, $version = null )
 291      {
 292          $multiprice =& $objectAttribute->content();
 293          $multiprice->remove( $objectAttribute->attribute( 'id' ), $version );
 294      }
 295  
 296      function title( &$contentObjectAttribute )
 297      {
 298          return '';
 299      }
 300  
 301      function hasObjectAttributeContent( &$contentObjectAttribute )
 302      {
 303          return true;
 304      }
 305  
 306      function toString( $contentObjectAttribute )
 307      {
 308  
 309          $multiprice = $contentObjectAttribute->attribute( 'content' );
 310  
 311          $priceList = $multiprice->attribute( 'price_list' );
 312  
 313          $priceArray = explode( ',', $contentObjectAttribute->attribute( 'data_text' ) );
 314          foreach ( $priceList as $priceData )
 315          {
 316              $type = $priceData->attribute( 'type' );
 317              if ( $type == 1 )
 318              {
 319                  $type = 'CUSTOM';
 320              }
 321              else if ( $type == 2 )
 322              {
 323                  $type = 'AUTO';
 324              }
 325              else
 326                  $type = 'LIMIT';
 327              $priceArray = array_merge(  $priceArray, array( $priceData->attribute( 'currency_code'), $priceData->attribute( 'value' ), $type ) );
 328          }
 329          return eZStringUtils::implodeStr( $priceArray, '|' );
 330      }
 331  
 332  
 333      function fromString( &$contentObjectAttribute, $string )
 334      {
 335          if ( $string == '' )
 336              return true;
 337  
 338          $multiprice = $contentObjectAttribute->attribute( 'content' );
 339  
 340          $multipriceData =  eZStringUtils::explodeSTR( $string, '|' );
 341  
 342          $vatType = array_shift( $multipriceData );
 343          $vatExInc = array_shift( $multipriceData );
 344  
 345          $contentObjectAttribute->setAttribute( 'data_text', $vatType . ',' . $vatExInc );
 346  
 347          while ( $multipriceData )
 348          {
 349              $currencyCode = array_shift( $multipriceData );
 350              $value = array_shift( $multipriceData );
 351  
 352              $type = array_shift( $multipriceData );
 353              if ( $type == 'CUSTOM' )
 354              {
 355                  $type = 1;
 356              }
 357              else if ( $type == 'AUTO' )
 358              {
 359                  $type = 2;
 360              }
 361              else
 362                  $type = 5000;
 363  
 364              $multiprice->setPriceByCurrency( $currencyCode, $value, $type );
 365  
 366          }
 367          $multiprice->store();
 368          return $multiprice;
 369  
 370      }
 371  
 372      /*!
 373       \reimp
 374      */
 375      function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
 376      {
 377          $price =& $classAttribute->content();
 378          if ( $price )
 379          {
 380              $vatIncluded = $price->attribute( 'is_vat_included' );
 381              $vatTypes = $price->attribute( 'vat_type' );
 382              $attributeParametersNode->appendChild( eZDOMDocument::createElementNode( 'vat-included',
 383                                                                                       array( 'is-set' => $vatIncluded ? 'true' : 'false' ) ) );
 384              $vatTypeNode = eZDOMDocument::createElementNode( 'vat-type' );
 385              $chosenVatType = $classAttribute->attribute( EZ_DATATYPESTRING_MULTIPRICE_VAT_ID_FIELD );
 386              $gotVat = false;
 387              foreach ( $vatTypes as $vatType )
 388              {
 389                  $id = $vatType->attribute( 'id' );
 390                  if ( $id == $chosenVatType )
 391                  {
 392                      $vatTypeNode->appendAttribute( eZDOMDocument::createAttributeNode( 'name', $vatType->attribute( 'name' ) ) );
 393                      $vatTypeNode->appendAttribute( eZDOMDocument::createAttributeNode( 'percentage', $vatType->attribute( 'percentage' ) ) );
 394                      $gotVat = true;
 395                      break;
 396                  }
 397              }
 398              if ( $gotVat )
 399                  $attributeParametersNode->appendChild( $vatTypeNode );
 400  
 401              $defualtCurrency = $classAttribute->attribute( EZ_DATATYPESTRING_DEFAULT_CURRENCY_CODE_FIELD );
 402              $attributeParametersNode->appendChild( eZDOMDocument::createElementNode( 'default-currency', array( 'code' => $defualtCurrency ) ) );
 403          }
 404      }
 405  
 406      /*!
 407       \reimp
 408      */
 409      function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
 410      {
 411          $vatNode =& $attributeParametersNode->elementByName( 'vat-included' );
 412          $vatIncluded = strtolower( $vatNode->attributeValue( 'is-set' ) ) == 'true';
 413          if ( $vatIncluded )
 414              $vatIncluded = EZ_MULTIPRICE_INCLUDED_VAT;
 415          else
 416              $vatIncluded = EZ_MULTIPRICE_EXCLUDED_VAT;
 417  
 418          $classAttribute->setAttribute( EZ_DATATYPESTRING_MULTIPRICE_INCLUDE_VAT_FIELD, $vatIncluded );
 419          $vatTypeNode =& $attributeParametersNode->elementByName( 'vat-type' );
 420          $vatName = $vatTypeNode->attributeValue( 'name' );
 421          $vatPercentage = $vatTypeNode->attributeValue( 'percentage' );
 422          $vatID = false;
 423          $vatTypes = eZVATType::fetchList();
 424          foreach ( array_keys( $vatTypes ) as $vatTypeKey )
 425          {
 426              $vatType =& $vatTypes[$vatTypeKey];
 427              if ( $vatType->attribute( 'name' ) == $vatName and
 428                   $vatType->attribute( 'percentage' ) == $vatPercentage )
 429              {
 430                  $vatID = $vatType->attribute( 'id' );
 431                  break;
 432              }
 433          }
 434          if ( !$vatID )
 435          {
 436              $vatType = eZVATType::create();
 437              $vatType->setAttribute( 'name', $vatName );
 438              $vatType->setAttribute( 'percentage', $vatPercentage );
 439              $vatType->store();
 440              $vatID = $vatType->attribute( 'id' );
 441          }
 442          $classAttribute->setAttribute( EZ_DATATYPESTRING_MULTIPRICE_VAT_ID_FIELD, $vatID );
 443  
 444          $defaultCurrency =& $attributeParametersNode->elementByName( 'default-currency' );
 445          $currencyCode = $defaultCurrency->attributeValue( 'code' );
 446          $classAttribute->setAttribute( EZ_DATATYPESTRING_DEFAULT_CURRENCY_CODE_FIELD, $currencyCode );
 447      }
 448  
 449  
 450      /*!
 451       \reimp
 452      */
 453      function serializeContentObjectAttribute( &$package, &$objectAttribute )
 454      {
 455          $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
 456  
 457          $multiprice =& $objectAttribute->content();
 458          $domDocument = $multiprice->DOMDocument();
 459  
 460          $node->appendChild( $domDocument->root() );
 461  
 462          return $node;
 463      }
 464  
 465      /*!
 466       \reimp
 467      */
 468      function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode )
 469      {
 470          $rootNode = $attributeNode->firstChild();
 471  
 472          $multiprice =& $objectAttribute->content();
 473          $multiprice->decodeDOMTree( $rootNode );
 474      }
 475  
 476      function customSorting()
 477      {
 478          return true;
 479      }
 480  
 481      function customSortingSQL( $params )
 482      {
 483          $multipriceTableAlias = "mp";
 484  
 485          if ( isset( $params['table_alias_suffix'] ) )
 486              $multipriceTableAlias .= $params['table_alias_suffix'];
 487  
 488          $sql = array( 'from' => '',
 489                        'where' => '',
 490                        'sorting_field' => '' );
 491  
 492          $sql['from'] =  "ezmultipricedata $multipriceTableAlias";
 493  
 494          $and = '';
 495          if ( isset( $params['contentobject_attr_id'] ) )
 496          {
 497              $sql['where'] = "
 498                       $multipriceTableAlias.contentobject_attr_id = {$params['contentobject_attr_id']}";
 499              $and = ' AND';
 500          }
 501  
 502          if ( isset( $params['contentobject_attr_version'] ) )
 503          {
 504              $sql['where'] .= "
 505                      $and $multipriceTableAlias.contentobject_attr_version = {$params['contentobject_attr_version']}";
 506              $and = ' AND';
 507          }
 508  
 509          if ( !isset( $params['currency_code'] ) )
 510          {
 511              include_once ( 'kernel/shop/classes/ezshopfunctions.php' );
 512              $params['currency_code'] = eZShopFunctions::preferredCurrencyCode();
 513          }
 514  
 515          if ( $params['currency_code'] !== false )
 516          {
 517              $sql['where'] .= "
 518                      $and $multipriceTableAlias.currency_code = '{$params['currency_code']}'";
 519              $and = ' AND';
 520          }
 521  
 522          $sql['sorting_field'] = "$multipriceTableAlias.value";
 523  
 524          return $sql;
 525      }
 526  
 527      /*!
 528        \reimp
 529      */
 530      function diff( $old, $new, $options = false )
 531      {
 532          return null;
 533      }
 534  }
 535  
 536  eZDataType::register( EZ_DATATYPESTRING_MULTIPRICE, "ezmultipricetype" );
 537  
 538  ?>


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