eZ Publish  [4.0]
convertprice2multiprice.php
Go to the documentation of this file.
00001 #!/usr/bin/env php
00002 <?php
00003 //
00004 // Created on: <20-Mar-2006 15:00:00 dl>
00005 //
00006 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00007 // SOFTWARE NAME: eZ Publish
00008 // SOFTWARE RELEASE: 4.0.x
00009 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00010 // SOFTWARE LICENSE: GNU General Public License v2.0
00011 // NOTICE: >
00012 //   This program is free software; you can redistribute it and/or
00013 //   modify it under the terms of version 2.0  of the GNU General
00014 //   Public License as published by the Free Software Foundation.
00015 //
00016 //   This program is distributed in the hope that it will be useful,
00017 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //   GNU General Public License for more details.
00020 //
00021 //   You should have received a copy of version 2.0 of the GNU General
00022 //   Public License along with this program; if not, write to the Free
00023 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00024 //   MA 02110-1301, USA.
00025 //
00026 //
00027 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00028 //
00029 
00030 // file  bin/php/convertprice2multiprice.php
00031 
00032 // description: the script will go through all classes and objects with 'ezprice'
00033 //              datatype changing it to the 'ezmultiprice' datatype.
00034 //              Note: the IDs and indentifiers of the classes, class attributes,
00035 //                    objects, object attributes will not be changed.
00036 //              Resulting 'ezmultiprice' will have 1 'custom' price(with value of
00037 //              'ezprice') in currency of the current locale(the 'currency' object
00038 //              will be created if it doesn't exist).
00039 
00040 
00041 // script initializing
00042 //include_once( 'kernel/classes/ezscript.php' );
00043 
00044 require 'autoload.php';
00045 
00046 global $cli;
00047 global $currencyList;
00048 
00049 $currencyList = false;
00050 
00051 $cli = eZCLI::instance();
00052 $script = eZScript::instance( array( 'description' => ( "\n" .
00053                                                          "This script will convert objects with 'price' datatype to\n" .
00054                                                          "the objects with 'multiprice' datatype.\n" ),
00055                                       'use-session' => false,
00056                                       'use-modules' => true,
00057                                       'use-extensions' => false,
00058                                       'user' => true ) );
00059 $script->startup();
00060 
00061 $scriptOptions = $script->getOptions( "",
00062                                       "",
00063                                       array(),
00064                                       false,
00065                                       array( 'user' => true )
00066                                      );
00067 
00068 
00069 $script->initialize();
00070 
00071 //include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
00072 //include_once( 'kernel/classes/ezcontentclass.php' );
00073 //include_once( 'kernel/shop/classes/ezshopfunctions.php' );
00074 //include_once( 'kernel/shop/classes/ezcurrencydata.php' );
00075 //include_once( 'kernel/classes/datatypes/ezmultiprice/ezmultipricetype.php' );
00076 //include_once( 'kernel/classes/datatypes/ezmultiprice/ezmultiprice.php' );
00077 //include_once( 'kernel/shop/classes/ezmultipricedata.php' );
00078 
00079 
00080 $convertedObjectsCount = 0;
00081 
00082 $classList = eZContentClass::fetchList();
00083 
00084 $db = eZDB::instance();
00085 $db->begin();
00086 foreach ( $classList as $class )
00087 {
00088     if ( eZShopFunctions::isSimplePriceClass( $class ) )
00089     {
00090         $classID = $class->attribute( 'id' );
00091         $objectListCount = eZContentObject::fetchSameClassListCount( $classID );
00092         if ( $objectListCount == 0 )
00093         {
00094             $cli->output( "No objects found for '" . $class->attribute( 'name' ) . "' class" );
00095             continue;
00096         }
00097 
00098         $cli->output( "Processing objects of the '" . $class->attribute( 'name' ) . "' class" );
00099 
00100         $defaultCurrency = currencyForLocale();
00101 
00102         if ( !$defaultCurrency )
00103             $script->shutdown( 1 );
00104 
00105         $defaultCurrencyCode = $defaultCurrency->attribute( 'code' );
00106 
00107         $priceClassAttribute = eZShopFunctions::priceAttribute( $class );
00108         $priceClassAttributeID = $priceClassAttribute->attribute( 'id' );
00109 
00110         // replace 'ezprice' class attribute with 'ezmultiprice'.
00111         $priceClassAttribute->setAttribute( 'data_type_string', 'ezmultiprice' );
00112         $priceClassAttribute->setAttribute( eZMultiPriceType::DEFAULT_CURRENCY_CODE_FIELD, $defaultCurrencyCode );
00113         $priceClassAttribute->store();
00114 
00115         unset( $GLOBALS['eZContentClassAttributeCache'][$priceClassAttributeID] );
00116 
00117         // update objects
00118         $offset = 0;
00119         $limit = 1000;
00120 
00121         $cli->output( 'Converting', false );
00122         while ( $offset < $objectListCount )
00123         {
00124             $objectList = eZContentObject::fetchSameClassList( $class->attribute( 'id' ), true, $offset, $limit );
00125             $offset += count( $objectList );
00126 
00127             foreach ( $objectList as $object )
00128             {
00129                 $contentObjectID = $object->attribute( 'id' );
00130                 $objectVersions =& $object->versions();
00131                 foreach ( $objectVersions as $objectVersion )
00132                 {
00133                     $version = $objectVersion->attribute( 'version' );
00134                     $objectAttributeList = eZContentObjectAttribute::fetchSameClassAttributeIDList( $priceClassAttributeID, true, $version, $contentObjectID );
00135 
00136                     foreach ( $objectAttributeList as $objectAttribute )
00137                     {
00138                         $priceValue = $objectAttribute->attribute( 'data_float' );
00139 
00140                         $multiprice = eZMultiPriceData::create( $objectAttribute->attribute( 'id' ),
00141                                                                 $version,
00142                                                                 $defaultCurrencyCode,
00143                                                                 $priceValue,
00144                                                                 eZMultiPriceData::VALUE_TYPE_CUSTOM );
00145                         $multiprice->store();
00146 
00147                         $objectAttribute->setAttribute( 'data_type_string', 'ezmultiprice' );
00148                         $objectAttribute->setAttribute( 'data_float', 0 );
00149                         $objectAttribute->setAttribute( 'sort_key_int', 0 );
00150                         $objectAttribute->store();
00151                     }
00152                 }
00153 
00154                 $cli->output( '.', false );
00155                 ++$convertedObjectsCount;
00156             }
00157         }
00158         $cli->output( ' ' );
00159     }
00160 }
00161 
00162 
00163 // create/update autoprices.
00164 if ( is_array( $currencyList ) )
00165 {
00166     $cli->output( "Updating autoprices." );
00167 
00168     foreach ( $currencyList as $currencyCode => $currency )
00169     {
00170         eZMultiPriceData::createPriceListForCurrency( $currencyCode );
00171     }
00172 
00173     eZMultiPriceData::updateAutoprices();
00174 }
00175 
00176 $db->commit();
00177 
00178 //include_once( 'kernel/classes/ezcontentcachemanager.php' );
00179 eZContentCacheManager::clearAllContentCache();
00180 
00181 $cli->output( "Total converted objects: $convertedObjectsCount" );
00182 $cli->output( "Done." );
00183 
00184 $script->shutdown( 0 );
00185 
00186 function currencyForLocale( $localeString = false )
00187 {
00188     global $cli;
00189     global $currencyList;
00190     $currency = false;
00191 
00192     if ( $currencyList === false )
00193     {
00194         $currencyList = eZCurrencyData::fetchList();
00195     }
00196 
00197     $locale = eZLocale::instance( $localeString );
00198     if ( is_object( $locale ) )
00199     {
00200         // get currency
00201         if ( $currencyCode = $locale->currencyShortName() )
00202         {
00203             if ( !isset( $currencyList[$currencyCode] ) )
00204             {
00205                 $cli->warning( "Currency '$currencyCode' doesn't exist" );
00206                 $cli->notice( "Creating currency '$currencyCode'... ", false );
00207 
00208                 $currencySymbol = $locale->currencySymbol();
00209                 $localeCode = $locale->localeFullCode();
00210 
00211                 if ( $currency = eZCurrencyData::create( $currencyCode, $currencySymbol, $localeCode, '0.00000', '1.00000', '0.00000' ) )
00212                 {
00213                     $cli->output( 'Ok' );
00214                     $currency->store();
00215                     $currencyList[$currencyCode] = $currency;
00216                 }
00217                 else
00218                 {
00219                     $cli->error( 'Failed' );
00220                 }
00221             }
00222             else
00223             {
00224                 $currency = $currencyList[$currencyCode];
00225             }
00226         }
00227         else
00228         {
00229             $cli->error( "Unable to find currency code for the '$localeString' locale" );
00230         }
00231     }
00232     else
00233     {
00234         $cli->error( "Unable to find '$localeString' locale" );
00235     }
00236 
00237     return $currency;
00238 }
00239 
00240 ?>