[ Index ]
 

Code source de eZ Publish 3.9.0

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

title

Body

[fermer]

/bin/php/ -> convertprice2multiprice.php (source)

   1  #!/usr/bin/env php
   2  <?php
   3  //
   4  // Created on: <20-Mar-2006 15:00:00 dl>
   5  //
   6  // SOFTWARE NAME: eZ publish
   7  // SOFTWARE RELEASE: 3.9.0
   8  // BUILD VERSION: 17785
   9  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  10  // SOFTWARE LICENSE: GNU General Public License v2.0
  11  // NOTICE: >
  12  //   This program is free software; you can redistribute it and/or
  13  //   modify it under the terms of version 2.0  of the GNU General
  14  //   Public License as published by the Free Software Foundation.
  15  //
  16  //   This program is distributed in the hope that it will be useful,
  17  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  18  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19  //   GNU General Public License for more details.
  20  //
  21  //   You should have received a copy of version 2.0 of the GNU General
  22  //   Public License along with this program; if not, write to the Free
  23  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  24  //   MA 02110-1301, USA.
  25  //
  26  //
  27  
  28  // file  bin/php/convertprice2multiprice.php
  29  
  30  // description: the script will go through all classes and objects with 'ezprice'
  31  //              datatype changing it to the 'ezmultiprice' datatype.
  32  //              Note: the IDs and indentifiers of the classes, class attributes,
  33  //                    objects, object attributes will not be changed.
  34  //              Resulting 'ezmultiprice' will have 1 'custom' price(with value of
  35  //              'ezprice') in currency of the current locale(the 'currency' object
  36  //              will be created if it doesn't exist).
  37  
  38  
  39  // script initializing
  40  include_once ( 'kernel/classes/ezscript.php' );
  41  
  42  global $cli;
  43  global $currencyList;
  44  
  45  $currencyList = false;
  46  
  47  $cli =& eZCLI::instance();
  48  $script =& eZScript::instance( array( 'description' => ( "\n" .
  49                                                           "This script will convert objects with 'price' datatype to\n" .
  50                                                           "the objects with 'multiprice' datatype.\n" ),
  51                                        'use-session' => false,
  52                                        'use-modules' => true,
  53                                        'use-extensions' => false,
  54                                        'user' => true ) );
  55  $script->startup();
  56  
  57  $scriptOptions = $script->getOptions( "",
  58                                        "",
  59                                        array(),
  60                                        false,
  61                                        array( 'user' => true )
  62                                       );
  63  
  64  
  65  $script->initialize();
  66  
  67  include_once ( 'kernel/classes/ezcontentobjecttreenode.php' );
  68  include_once ( 'kernel/classes/ezcontentclass.php' );
  69  include_once ( 'kernel/shop/classes/ezshopfunctions.php' );
  70  include_once ( 'kernel/shop/classes/ezcurrencydata.php' );
  71  include_once ( 'kernel/classes/datatypes/ezmultiprice/ezmultipricetype.php' );
  72  include_once ( 'kernel/classes/datatypes/ezmultiprice/ezmultiprice.php' );
  73  include_once ( 'kernel/shop/classes/ezmultipricedata.php' );
  74  
  75  
  76  $convertedObjectsCount = 0;
  77  
  78  $classList = eZContentClass::fetchList();
  79  
  80  $db =& eZDB::instance();
  81  $db->begin();
  82  foreach ( array_keys( $classList ) as $classListKey )
  83  {
  84      $class =& $classList[$classListKey];
  85      if ( eZShopFunctions::isSimplePriceClass( $class ) )
  86      {
  87          $classID =& $class->attribute( 'id' );
  88          $objectListCount = eZContentObject::fetchSameClassListCount( $classID );
  89          if ( $objectListCount == 0 )
  90          {
  91              $cli->output( "No objects found for '" . $class->attribute( 'name' ) . "' class" );
  92              continue;
  93          }
  94  
  95          $cli->output( "Processing objects of the '" . $class->attribute( 'name' ) . "' class" );
  96  
  97          $defaultCurrency = currencyForLocale();
  98  
  99          if ( !$defaultCurrency )
 100              $script->shutdown( 1 );
 101  
 102          $defaultCurrencyCode =& $defaultCurrency->attribute( 'code' );
 103  
 104          $priceClassAttribute = eZShopFunctions::priceAttribute( $class );
 105          $priceClassAttributeID =& $priceClassAttribute->attribute( 'id' );
 106  
 107          // replace 'ezprice' class attribute with 'ezmultiprice'.
 108          $priceClassAttribute->setAttribute( 'data_type_string', 'ezmultiprice' );
 109          $priceClassAttribute->setAttribute( EZ_DATATYPESTRING_DEFAULT_CURRENCY_CODE_FIELD, $defaultCurrencyCode );
 110          $priceClassAttribute->store();
 111  
 112          unset( $GLOBALS['eZContentClassAttributeCache'][$priceClassAttributeID] );
 113  
 114          // update objects
 115          $offset = 0;
 116          $limit = 1000;
 117  
 118          $cli->output( 'Converting', false );
 119          while ( $offset < $objectListCount )
 120          {
 121              $objectList = eZContentObject::fetchSameClassList( $class->attribute( 'id' ), true, $offset, $limit );
 122              $offset += count( $objectList );
 123  
 124              foreach ( array_keys( $objectList ) as $key )
 125              {
 126                  $object =& $objectList[$key];
 127                  $contentObjectID =& $object->attribute( 'id' );
 128                  $objectVersions =& $object->versions();
 129                  foreach ( $objectVersions as $objectVersion )
 130                  {
 131                      $version = $objectVersion->attribute( 'version' );
 132                      $objectAttributeList = eZContentObjectAttribute::fetchSameClassAttributeIDList( $priceClassAttributeID, true, $version, $contentObjectID );
 133  
 134                      foreach ( array_keys( $objectAttributeList ) as $objectAttributeKey )
 135                      {
 136                          $objectAttribute =& $objectAttributeList[$objectAttributeKey];
 137  
 138                          $priceValue =& $objectAttribute->attribute( 'data_float' );
 139  
 140                          $multiprice = eZMultiPriceData::create( $objectAttribute->attribute( 'id' ),
 141                                                                  $version,
 142                                                                  $defaultCurrencyCode,
 143                                                                  $priceValue,
 144                                                                  EZ_MULTIPRICEDATA_VALUE_TYPE_CUSTOM );
 145                          $multiprice->store();
 146  
 147                          $objectAttribute->setAttribute( 'data_type_string', 'ezmultiprice' );
 148                          $objectAttribute->setAttribute( 'data_float', 0 );
 149                          $objectAttribute->setAttribute( 'sort_key_int', 0 );
 150                          $objectAttribute->store();
 151                      }
 152                  }
 153  
 154                  $cli->output( '.', false );
 155                  ++$convertedObjectsCount;
 156              }
 157          }
 158          $cli->output( ' ' );
 159      }
 160  }
 161  
 162  
 163  // create/update autoprices.
 164  if ( is_array( $currencyList ) )
 165  {
 166      $cli->output( "Updating autoprices." );
 167  
 168      foreach ( $currencyList as $currencyCode => $currency )
 169      {
 170          eZMultiPriceData::createPriceListForCurrency( $currencyCode );
 171      }
 172  
 173      eZMultiPriceData::updateAutoprices();
 174  }
 175  
 176  $db->commit();
 177  
 178  include_once ( 'kernel/classes/ezcontentcachemanager.php' );
 179  eZContentCacheManager::clearAllContentCache();
 180  
 181  $cli->output( "Total converted objects: $convertedObjectsCount" );
 182  $cli->output( "Done." );
 183  
 184  $script->shutdown( 0 );
 185  
 186  function currencyForLocale( $localeString = false )
 187  {
 188      global $cli;
 189      global $currencyList;
 190      $currency = false;
 191  
 192      if ( $currencyList === false )
 193      {
 194          $currencyList = eZCurrencyData::fetchList();
 195      }
 196  
 197      $locale =& eZLocale::instance( $localeString );
 198      if ( is_object( $locale ) )
 199      {
 200          // get currency
 201          if ( $currencyCode = $locale->currencyShortName() )
 202          {
 203              if ( !isset( $currencyList[$currencyCode] ) )
 204              {
 205                  $cli->warning( "Currency '$currencyCode' doesn't exist" );
 206                  $cli->notice( "Creating currency '$currencyCode'... ", false );
 207  
 208                  $currencySymbol = $locale->currencySymbol();
 209                  $localeCode = $locale->localeFullCode();
 210  
 211                  if ( $currency = eZCurrencyData::create( $currencyCode, $currencySymbol, $localeCode, '0.00000', '1.00000', '0.00000' ) )
 212                  {
 213                      $cli->output( 'Ok' );
 214                      $currency->store();
 215                      $currencyList[$currencyCode] =& $currency;
 216                  }
 217                  else
 218                  {
 219                      $cli->error( 'Failed' );
 220                  }
 221              }
 222              else
 223              {
 224                  $currency =& $currencyList[$currencyCode];
 225              }
 226          }
 227          else
 228          {
 229              $cli->error( "Unable to find currency code for the '$localeString' locale" );
 230          }
 231      }
 232      else
 233      {
 234          $cli->error( "Unable to find '$localeString' locale" );
 235      }
 236  
 237      return $currency;
 238  }
 239  
 240  ?>


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