eZ Publish  [4.0]
ezproductcollectionitem.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZProductCollectionItem class
00004 //
00005 // Created on: <04-Jul-2002 13:45:10 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 eZProductCollectionItem ezproductcollection.php
00033   \brief eZProductCollectionItem handles one product item
00034   \ingroup eZKernel
00035 
00036 */
00037 
00038 //include_once( "kernel/classes/ezpersistentobject.php" );
00039 //include_once( "kernel/classes/ezcontentobject.php" );
00040 //include_once( "kernel/classes/ezproductcollectionitemoption.php" );
00041 
00042 
00043 class eZProductCollectionItem extends eZPersistentObject
00044 {
00045     function eZProductCollectionItem( $row )
00046     {
00047         $this->eZPersistentObject( $row );
00048     }
00049 
00050     static function definition()
00051     {
00052         return array( "fields" => array( "id" => array( 'name' => 'ID',
00053                                                         'datatype' => 'integer',
00054                                                         'default' => 0,
00055                                                         'required' => true ),
00056                                          "productcollection_id" => array( 'name' => "ProductCollectionID",
00057                                                                           'datatype' => 'integer',
00058                                                                           'default' => 0,
00059                                                                           'required' => true,
00060                                                                           'foreign_class' => 'eZProductCollection',
00061                                                                           'foreign_attribute' => 'id',
00062                                                                           'multiplicity' => '1..*' ),
00063                                          "contentobject_id" => array( 'name' => "ContentObjectID",
00064                                                                       'datatype' => 'integer',
00065                                                                       'default' => 0,
00066                                                                       'required' => true,
00067                                                                       'foreign_class' => 'eZContentObject',
00068                                                                       'foreign_attribute' => 'id',
00069                                                                       'multiplicity' => '1..*' ),
00070                                          "item_count" => array( 'name' => "ItemCount",
00071                                                                 'datatype' => 'integer',
00072                                                                 'default' => 0,
00073                                                                 'required' => true ),
00074                                          "price" => array( 'name' => "Price",
00075                                                            'datatype' => 'float',
00076                                                            'default' => 0,
00077                                                            'required' => true ),
00078                                          'is_vat_inc' => array( 'name' => "IsVATIncluded",
00079                                                                 'datatype' => 'integer',
00080                                                                 'default' => 0,
00081                                                                 'required' => true ),
00082                                          'vat_value' => array( 'name' => "VATValue",
00083                                                                'datatype' => 'float',
00084                                                                'default' => 0,
00085                                                                'required' => true ),
00086                                          'discount' => array( 'name' => "DiscountValue",
00087                                                               'datatype' => 'float',
00088                                                               'default' => 0,
00089                                                               'required' => true ),
00090                                          'name' => array( 'name' => 'Name',
00091                                                           'datatype' => 'string',
00092                                                           'default' => '',
00093                                                           'required' => true ) ),
00094                       'function_attributes' => array( 'contentobject' => 'contentObject',
00095                                                       'option_list' => 'optionList' ),
00096                       "keys" => array( "id" ),
00097                       'sort' => array( 'id' => 'asc' ),
00098                       "increment_key" => "id",
00099                       "relations" => array( "contentobject_id" => array( "class" => "ezcontentobject",
00100                                                                          "field" => "id" ),
00101                                             "productcollection_id" => array( "class" => "ezproductcollection",
00102                                                                              "field" => "id" ) ),
00103                       "class_name" => "eZProductCollectionItem",
00104                       "name" => "ezproductcollection_item" );
00105     }
00106 
00107     /*!
00108      Creates a new empty collection item which belongs to
00109      collection \a $collectionID and returns it.
00110     */
00111     static function create( $productCollectionID )
00112     {
00113         return new eZProductCollectionItem( array( "productcollection_id" => $productCollectionID ) );
00114     }
00115 
00116     /*!
00117      Clones the collection item object and returns it. The ID of the clone is erased.
00118     */
00119     function __clone()
00120     {
00121         $this->setAttribute( 'id', null );
00122     }
00123 
00124     /*!
00125      Copies the collection object item and the option,
00126      the new copy will point to the collection \a $collectionID.
00127      \return the new collection item object.
00128      \note The new collection item will already be present in the database.
00129      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00130      the calls within a db transaction; thus within db->begin and db->commit.
00131     */
00132     function copy( $collectionID )
00133     {
00134         $item = clone $this;
00135         $item->setAttribute( 'productcollection_id', $collectionID );
00136         $item->store();
00137         $oldItemOptionList = $this->optionList();
00138         foreach ( $oldItemOptionList as $oldItemOption )
00139         {
00140             $itemOption = $oldItemOption->copy( $item->attribute( 'id' ) );
00141         }
00142         return $item;
00143     }
00144 
00145     static function fetch( $id, $asObject = true )
00146     {
00147         return eZPersistentObject::fetchObject( eZProductCollectionItem::definition(),
00148                                                 null,
00149                                                 array( "id" => $id ),
00150                                                 $asObject );
00151     }
00152 
00153     static function fetchList( $conditions = null, $asObjects = true, $offset = false, $limit = false )
00154     {
00155         $limitation = null;
00156         if ( $offset !== false or $limit !== false )
00157             $limitation = array( 'offset' => $offset, 'length' => $limit );
00158 
00159         return eZPersistentObject::fetchObjectList( eZProductCollectionItem::definition(),
00160                                                     null,
00161                                                     $conditions,
00162                                                     null,
00163                                                     $limitation,
00164                                                     $asObjects );
00165     }
00166 
00167     /*!
00168      \return the discount percent for the current item
00169     */
00170     function discountPercent()
00171     {
00172         $discount = false;
00173         return $discount;
00174     }
00175 
00176     /*!
00177      \return Returns the content object defining the product.
00178     */
00179     function contentObject()
00180     {
00181         if ( $this->ContentObject === null )
00182         {
00183             if ( $this->ContentObjectID == 0 )
00184             {
00185                 return null;
00186             }
00187             $this->ContentObject = eZContentObject::fetch( $this->ContentObjectID );
00188         }
00189         return $this->ContentObject;
00190     }
00191 
00192     function optionList()
00193     {
00194         return eZProductCollectionItemOption::fetchList( $this->attribute( 'id' ) );
00195     }
00196 
00197     /*!
00198      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00199      the calls within a db transaction; thus within db->begin and db->commit.
00200      */
00201     function removeThis()
00202     {
00203         $itemOptionList = eZProductCollectionItemOption::fetchList( $this->attribute( 'id' ) );
00204 
00205         $db = eZDB::instance();
00206         $db->begin();
00207         foreach( $itemOptionList as $itemOption )
00208         {
00209             $itemOption->remove();
00210         }
00211         eZPersistentObject::remove();
00212         $db->commit();
00213     }
00214 
00215     /*!
00216      Goes trough all options and finds the attribute they points to and calls productOptionInformation()
00217      to fetch the option data.
00218      \return The total price of all options.
00219     */
00220     function calculatePriceWithOptions( $currency = false )
00221     {
00222         //include_once( 'kernel/shop/classes/ezshopfunctions.php' );
00223 
00224         $optionList = eZProductCollectionItemOption::fetchList( $this->attribute( 'id' ) );
00225         $contentObject = $this->contentObject();
00226         $contentObjectVersion = $contentObject->attribute( 'current_version' );
00227         $optionsPrice = 0.0;
00228         if ( count( $optionList ) > 0 )
00229         {
00230             $db = eZDB::instance();
00231             $db->begin();
00232             foreach( $optionList as $option )
00233             {
00234                 $objectAttribute = eZContentObjectAttribute::fetch( $option->attribute( 'object_attribute_id' ), $contentObjectVersion );
00235                 if ( $objectAttribute == null )
00236                 {
00237                     $optionsPrice += 0.0;
00238                     continue;
00239                 }
00240 
00241                 $dataType = $objectAttribute->dataType();
00242                 $optionData = $dataType->productOptionInformation( $objectAttribute, $option->attribute( 'option_item_id' ), $this );
00243 
00244                 if ( $optionData )
00245                 {
00246                     $optionData['additional_price'] = eZShopFunctions::convertAdditionalPrice( $currency, $optionData['additional_price'] );
00247                     $option->setAttribute( 'price', $optionData['additional_price'] );
00248                     $option->store();
00249 
00250                     $optionsPrice += $optionData['additional_price'];
00251                 }
00252 
00253             }
00254             $db->commit();
00255         }
00256         return $optionsPrice;
00257     }
00258 
00259     function verify( $currency = false )
00260     {
00261         $contentObject = $this->attribute( 'contentobject' );
00262         if ( $contentObject != null && $contentObject->attribute( 'main_node_id' ) > 0 )
00263         {
00264             //include_once( 'kernel/shop/classes/ezshopfunctions.php' );
00265 
00266             $attributes = $contentObject->contentObjectAttributes();
00267             $optionsPrice = $this->calculatePriceWithOptions( $currency );
00268 
00269             foreach ( $attributes as $attribute )
00270             {
00271                 $dataType = $attribute->dataType();
00272                 if ( eZShopFunctions::isProductDatatype( $dataType->isA() ) )
00273                 {
00274                     $priceObj = $attribute->content();
00275 
00276                     $price = $priceObj->attribute( 'price' );
00277                     $priceWithOptions = $price + $optionsPrice;
00278                     if ( $priceWithOptions != $this->attribute( 'price' ) )
00279                     {
00280                         return false;
00281                     }
00282                     if ( $priceObj->attribute( 'is_vat_included' ) != ( $this->attribute( 'is_vat_inc' ) > 0 ) )
00283                     {
00284                         return false;
00285                     }
00286                     if ( $priceObj->attribute( 'vat_percent' ) != $this->attribute( 'vat_value' ) )
00287                     {
00288                         return false;
00289                     }
00290                     if ( $priceObj->discount() != $this->attribute( 'discount' ) )
00291                     {
00292                         return false;
00293                     }
00294                     return true;
00295                 }
00296             }
00297 
00298         }
00299         return false;
00300     }
00301 
00302     /**
00303      * Removes all product collection items which related to the product
00304      * collections specified in the parameter array
00305      *
00306      * @param array $productCollectionIDList array of eZProductCollection IDs
00307      *
00308      * @return void
00309      **/
00310     static function cleanupList( $productCollectionIDList )
00311     {
00312         $db = eZDB::instance();
00313         $db->begin();
00314         $inText = $db->generateSQLINStatement( $productCollectionIDList, 'productcollection_id', false, false, 'int' );
00315         $rows = $db->arrayQuery( "SELECT id FROM ezproductcollection_item WHERE $inText" );
00316         if ( count( $rows ) > 0 )
00317         {
00318             $itemIDList = array();
00319             foreach ( $rows as $row )
00320             {
00321                 $itemIDList[] = $row['id'];
00322             }
00323             //include_once( 'kernel/classes/ezproductcollectionitemoption.php' );
00324             eZProductCollectionItemOption::cleanupList( $itemIDList );
00325         }
00326         $db->query( "DELETE FROM ezproductcollection_item WHERE $inText" );
00327         $db->commit();
00328     }
00329 
00330     /// Stores the content object
00331     public $ContentObject = null;
00332 }
00333 
00334 ?>