eZ Publish  [4.0]
ezwishlist.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZWishList class
00004 //
00005 // Created on: <01-Aug-2002 10:22:02 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 /*!
00033   \class eZWishList ezwishlist.php
00034   \brief eZWishList handles shopping wish lists
00035   \ingroup eZKernel
00036 
00037   \sa eZProductCollection
00038 */
00039 
00040 //include_once( "kernel/classes/ezpersistentobject.php" );
00041 //include_once( "kernel/classes/ezproductcollection.php" );
00042 //include_once( "kernel/classes/ezproductcollectionitem.php" );
00043 //include_once( "kernel/classes/datatypes/ezuser/ezuser.php" );
00044 //include_once( "kernel/classes/ezuserdiscountrule.php" );
00045 //include_once( "kernel/classes/ezcontentobjecttreenode.php" );
00046 
00047 class eZWishList extends eZPersistentObject
00048 {
00049     /*!
00050     */
00051     function eZWishList( $row = array() )
00052     {
00053         $this->eZPersistentObject( $row );
00054     }
00055 
00056     /*!
00057      \return the persistent object definition for the eZCard class.
00058     */
00059     static function definition()
00060     {
00061         return array( "fields" => array( "id" => array( 'name' => 'ID',
00062                                                         'datatype' => 'integer',
00063                                                         'default' => 0,
00064                                                         'required' => true ),
00065                                          "user_id" => array( 'name' => "UserID",
00066                                                              'datatype' => 'integer',
00067                                                              'default' => 0,
00068                                                              'required' => true,
00069                                                              'foreign_class' => 'eZUser',
00070                                                              'foreign_attribute' => 'contentobject_id',
00071                                                              'multiplicity' => '1..*' ),
00072                                          "productcollection_id" => array( 'name' => "ProductCollectionID",
00073                                                                           'datatype' => 'integer',
00074                                                                           'default' => 0,
00075                                                                           'required' => true,
00076                                                                           'foreign_class' => 'eZProductCollection',
00077                                                                          'foreign_attribute' => 'id',
00078                                                                          'multiplicity' => '1..*' ) ),
00079                       "keys" => array( "id" ),
00080                       'function_attributes' => array( 'items' => 'items' ),
00081                       "increment_key" => "id",
00082                       "class_name" => "eZWishList",
00083                       "name" => "ezwishlist" );
00084     }
00085 
00086 
00087     function discountPercent()
00088     {
00089         $discountPercent = 0;
00090         $user = eZUser::currentUser();
00091         $userID = $user->attribute( 'contentobject_id' );
00092         $nodes = eZContentObjectTreeNode::fetchByContentObjectID( $userID );
00093         $idArray = array();
00094         $idArray[] = $userID;
00095         foreach ( $nodes as $node )
00096         {
00097             $parentNodeID = $node->attribute( 'parent_node_id' );
00098             $idArray[] = $parentNodeID;
00099         }
00100         $rules = eZUserDiscountRule::fetchByUserIDArray( $idArray );
00101         foreach ( $rules as $rule )
00102         {
00103             $percent = $rule->attribute( 'discount_percent' );
00104             if ( $discountPercent < $percent )
00105                 $discountPercent = $percent;
00106         }
00107         return $discountPercent;
00108     }
00109 
00110     function itemCount( $alternativeProductionID = false )
00111     {
00112         $countRes = eZPersistentObject::fetchObjectList( eZProductCollectionItem::definition(),
00113                                                          array(),
00114                                                          array( "productcollection_id" => ( $alternativeProductionID === false ) ? $this->ProductCollectionID : $alternativeProductionID ),
00115                                                          false,
00116                                                          null,
00117                                                          false,
00118                                                          false,
00119                                                          array( array( 'operation' => 'count( id )',
00120                                                                        'name' => 'count' ) ) );
00121         return $countRes[0]['count'];
00122     }
00123 
00124     function items( $asObject = true, $alternativeProductionID = false, $offset = false, $limit = false )
00125     {
00126         $productItems = eZPersistentObject::fetchObjectList( eZProductCollectionItem::definition(),
00127                                                        null,
00128                                                        array( 'productcollection_id' => ( $alternativeProductionID === false ) ? $this->ProductCollectionID : $alternativeProductionID ),
00129                                                        null,
00130                                                        array( 'offset' => $offset,
00131                                                               'length' => $limit ),
00132                                                        $asObject );
00133 //        $discountPercent = $this->discountPercent();
00134         $addedProducts = array();
00135         foreach ( $productItems as  $productItem )
00136         {
00137             $discountPercent = 0.0;
00138             $isVATIncluded = true;
00139             $id = $productItem->attribute( 'id' );
00140             $contentObject = $productItem->attribute( 'contentobject' );
00141 
00142             if ( $contentObject !== null )
00143             {
00144                 $vatValue = $productItem->attribute( 'vat_value' );
00145                 $count = $productItem->attribute( 'item_count' );
00146                 $discountPercent = $productItem->attribute( 'discount' );
00147                 $nodeID = $contentObject->attribute( 'main_node_id' );
00148                 $objectName = $contentObject->attribute( 'name' );
00149 
00150                 $isVATIncluded = $productItem->attribute( 'is_vat_inc' );
00151                 $price = $productItem->attribute( 'price' );
00152 
00153                 if ( $isVATIncluded )
00154                 {
00155                     $priceExVAT = $price / ( 100 + $vatValue ) * 100;
00156                     $priceIncVAT = $price;
00157                 }
00158                 else
00159                 {
00160                     $priceExVAT = $price;
00161                     $priceIncVAT = $price * ( 100 + $vatValue ) / 100;
00162                 }
00163 
00164                 $totalPriceExVAT = $count * $priceExVAT  * ( 100 - $discountPercent ) / 100;
00165                 $totalPriceIncVAT = $count * $priceIncVAT * ( 100 - $discountPercent ) / 100 ;
00166 
00167                 $addedProduct = array( "id" => $id,
00168                                        "vat_value" => $vatValue,
00169                                        "item_count" => $count,
00170                                        "node_id" => $nodeID,
00171                                        "object_name" => $objectName,
00172                                        "price_ex_vat" => $priceExVAT,
00173                                        "price_inc_vat" => $priceIncVAT,
00174                                        "discount_percent" => $discountPercent,
00175                                        "total_price_ex_vat" => $totalPriceExVAT,
00176                                        "total_price_inc_vat" => $totalPriceIncVAT,
00177                                        'item_object' =>$productItem );
00178                 $addedProducts[] = $addedProduct;
00179             }
00180         }
00181         return $addedProducts;
00182     }
00183 
00184     /*!
00185      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00186      the calls within a db transaction; thus within db->begin and db->commit.
00187      */
00188     static function removeItem( $itemID )
00189     {
00190         $item = eZProductCollectionItem::fetch( $itemID );
00191         $item->remove();
00192     }
00193 
00194     /*!
00195      Will return the wish list for the current user. If a wish list does not exist one will be created.
00196      \return current eZWishList object
00197      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00198      the calls within a db transaction; thus within db->begin and db->commit.
00199     */
00200     static function currentWishList( $asObject=true )
00201     {
00202         $http = eZHTTPTool::instance();
00203 
00204         $user = eZUser::currentUser();
00205         $userID = $user->attribute( 'contentobject_id' );
00206         $WishListArray = eZPersistentObject::fetchObjectList( eZWishList::definition(),
00207                                                           null, array( "user_id" => $userID
00208                                                                        ),
00209                                                           null, null,
00210                                                           $asObject );
00211 
00212         $currentWishList = false;
00213         if ( count( $WishListArray ) == 0 )
00214         {
00215             $collection = eZProductCollection::create();
00216             $collection->store();
00217 
00218             $currentWishList = new eZWishList( array( "user_id" => $userID,
00219                                               "productcollection_id" => $collection->attribute( "id" ) ) );
00220             $currentWishList->store();
00221         }
00222         else
00223         {
00224             $currentWishList = $WishListArray[0];
00225         }
00226         return $currentWishList;
00227     }
00228 
00229     /*!
00230      \static
00231      Removes all wishlists from the database.
00232      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00233      the calls within a db transaction; thus within db->begin and db->commit.
00234     */
00235     static function cleanup()
00236     {
00237         $db = eZDB::instance();
00238         $db->begin();
00239         $rows = $db->arrayQuery( "SELECT productcollection_id FROM ezwishlist" );
00240         if ( count( $rows ) > 0 )
00241         {
00242             $productCollectionIDList = array();
00243             foreach ( $rows as $row )
00244             {
00245                 $productCollectionIDList[] = $row['productcollection_id'];
00246             }
00247             eZProductCollection::cleanupList( $productCollectionIDList );
00248         }
00249         $db->query( "DELETE FROM ezwishlist" );
00250         $db->commit();
00251     }
00252 
00253     /*!
00254      Remove wish list entries belonging to user \a $userID
00255     */
00256     static function removeByUserID( $userID )
00257     {
00258         eZPersistentObject::removeObject( eZWishList::definition(),
00259                                           array( 'user_id' => $userID ) );
00260     }
00261 }
00262 
00263 ?>