eZ Publish  [4.0]
ezproductcollection.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZProductCollection class
00004 //
00005 // Created on: <04-Jul-2002 13:40:41 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  * eZProductCollection is a container class which handles groups of products
00033  **/
00034 class eZProductCollection extends eZPersistentObject
00035 {
00036     function eZProductCollection( $row )
00037     {
00038         $this->eZPersistentObject( $row );
00039     }
00040 
00041     static function definition()
00042     {
00043         return array( "fields" => array( "id" => array( 'name' => 'ID',
00044                                                         'datatype' => 'integer',
00045                                                         'default' => 0,
00046                                                         'required' => true ),
00047                                          "created" => array( 'name' => "Created",
00048                                                              'datatype' => 'integer',
00049                                                              'default' => 0,
00050                                                              'required' => true ),
00051                                          'currency_code' => array( 'name' => 'CurrencyCode',
00052                                                                    'datatype' => 'string',
00053                                                                    'default' => '',
00054                                                                    'required' => true ) ),
00055                       "keys" => array( "id" ),
00056                       "increment_key" => "id",
00057                       "class_name" => "eZProductCollection",
00058                       "name" => "ezproductcollection" );
00059     }
00060 
00061     /**
00062      * Creates a new empty collection and returns it.
00063      *
00064      * @return eZProductCollection
00065      **/
00066     static function create( )
00067     {
00068         $row = array( "created" => time() );
00069         return new eZProductCollection( $row );
00070     }
00071 
00072     /**
00073      * Clones the collection object and returns it.
00074      * The ID of the clone is erased.
00075      **/
00076     function __clone()
00077     {
00078         $this->setAttribute( 'id', null );
00079     }
00080 
00081     /**
00082      * Copies the collection object, the collection items and options.
00083      *
00084      * @note The new collection will already be present in the database.
00085      *
00086      * @return eZProductCollection The new collection object.
00087      **/
00088     function copy()
00089     {
00090         $collection = clone $this;
00091 
00092         $db = eZDB::instance();
00093         $db->begin();
00094         $collection->store();
00095 
00096         $oldItems = $this->itemList();
00097         foreach ( $oldItems as $oldItem )
00098         {
00099             $item = $oldItem->copy( $collection->attribute( 'id' ) );
00100         }
00101         $db->commit();
00102         return $collection;
00103     }
00104 
00105     /**
00106      * Fetches an eZProductCollection based on its ID
00107      *
00108      * @param int $productCollectionID
00109      * @param bool $asObject
00110      *        If true, return an object. if false, returns an array
00111      *
00112      * @return array|eZProductCollection
00113      **/
00114     static function fetch( $productCollectionID, $asObject = true )
00115     {
00116         return eZPersistentObject::fetchObject( eZProductCollection::definition(),
00117                                                 null,
00118                                                 array( 'id' => $productCollectionID ),
00119                                                 $asObject );
00120     }
00121 
00122     /**
00123      * Returns all production collection items as an array.
00124      *
00125      * @param bool $asObject
00126      *        If true, return an object. if false, returns an array
00127      *
00128      * @return array(eZProductCollection|array)
00129      **/
00130     function itemList( $asObject = true )
00131     {
00132         return eZPersistentObject::fetchObjectList( eZProductCollectionItem::definition(),
00133                                                     null, array( "productcollection_id" => $this->ID ),
00134                                                     null,
00135                                                     null,
00136                                                     $asObject );
00137     }
00138 
00139     static function verify( $id )
00140     {
00141         $invalidItemArray = array();
00142         $collection = eZProductCollection::fetch( $id );
00143         if ( !is_object( $collection ) )
00144              return $invalidItemArray;
00145 
00146         $currency = $collection->attribute( 'currency_code' );
00147         $productItemList = eZPersistentObject::fetchObjectList( eZProductCollectionItem::definition(),
00148                                                                  null, array( "productcollection_id" => $id ),
00149                                                                  null,
00150                                                                  null,
00151                                                                  true );
00152         $isValid = true;
00153 
00154         foreach ( $productItemList as $productItem )
00155         {
00156             if ( !$productItem->verify( $currency ) )
00157             {
00158                 $invalidItemArray[] = $productItem;
00159                 $isValid = false;
00160             }
00161         }
00162         if ( !$isValid )
00163         {
00164             return $invalidItemArray;
00165         }
00166         return $isValid;
00167     }
00168 
00169     /**
00170      * Removes all product collections based on a product collection ID list
00171      * Will also remove the product collection items.
00172      *
00173      * @param array $productCollectionIDList array of eZProductCollection IDs
00174      *
00175      * @return void
00176      **/
00177     static function cleanupList( $productCollectionIDList )
00178     {
00179         $db = eZDB::instance();
00180         $db->begin();
00181 
00182         // Purge shipping information associated with product collections being removed.
00183         require_once( 'kernel/classes/ezshippingmanager.php' );
00184         foreach ( $productCollectionIDList as $productCollectionID )
00185             eZShippingManager::purgeShippingInfo( $productCollectionID );
00186 
00187         //include_once( 'kernel/classes/ezproductcollectionitem.php' );
00188         eZProductCollectionItem::cleanupList( $productCollectionIDList );
00189         $where = $db->generateSQLINStatement( $productCollectionIDList, 'id', false, false, 'int' );
00190         $db->query( "DELETE FROM ezproductcollection WHERE {$where}" );
00191         $db->commit();
00192     }
00193 }
00194 
00195 ?>