eZ Publish  [4.0]
ezdiscount.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZDiscount class
00004 //
00005 // Created on: <04-Nov-2005 12:26:52 dl>
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 /*! \file ezdiscount.php
00032 */
00033 
00034 class eZDiscount
00035 {
00036     function eZDiscount()
00037     {
00038     }
00039 
00040     /*!
00041      \static
00042      params = array( 'contentclass_id' => classID,
00043                      'contentobject_id' => objectID,
00044                      'section_id' => sectionID );
00045 
00046     */
00047     static function discountPercent( $user, $params )
00048     {
00049         //include_once( 'lib/ezdb/classes/ezdb.php' );
00050         //include_once( 'kernel/classes/ezuserdiscountrule.php' );
00051         //include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
00052 
00053         $bestMatch = 0.0;
00054 
00055         if ( is_object( $user ) )
00056         {
00057             $groups = $user->groups();
00058             $idArray = array_merge( $groups, array( $user->attribute( 'contentobject_id' ) ) );
00059 
00060             // Fetch discount rules for the current user
00061             $rules = eZUserDiscountRule::fetchByUserIDArray( $idArray );
00062 
00063             if ( count( $rules ) > 0 )
00064             {
00065                 $db = eZDB::instance();
00066 
00067                 $i = 1;
00068                 $subRuleStr = '';
00069                 foreach ( $rules as $rule )
00070                 {
00071                     $subRuleStr .= $rule->attribute( 'id' );
00072                     if ( $i < count( $rules ) )
00073                         $subRuleStr .= ', ';
00074                     $i++;
00075                 }
00076 
00077                 // Fetch the discount sub rules
00078                 $subRules = $db->arrayQuery( "SELECT * FROM
00079                                        ezdiscountsubrule
00080                                        WHERE discountrule_id IN ( $subRuleStr )
00081                                        ORDER BY discount_percent DESC" );
00082 
00083                 // Find the best matching discount rule
00084                 foreach ( $subRules as $subRule )
00085                 {
00086                     if ( $subRule['discount_percent'] > $bestMatch )
00087                     {
00088                         // Rule has better discount, see if it matches
00089                         if ( $subRule['limitation'] == '*' )
00090                             $bestMatch = $subRule['discount_percent'];
00091                         else
00092                         {
00093                             // Do limitation check
00094                             $limitationArray = $db->arrayQuery( "SELECT * FROM
00095                                        ezdiscountsubrule_value
00096                                        WHERE discountsubrule_id='" . $subRule['id']. "'" );
00097 
00098                             $hasSectionLimitation = false;
00099                             $hasClassLimitation = false;
00100                             $hasObjectLimitation = false;
00101                             $objectMatch = false;
00102                             $sectionMatch = false;
00103                             $classMatch = false;
00104                             foreach ( $limitationArray as $limitation )
00105                             {
00106                                 if ( $limitation['issection'] == '1' )
00107                                 {
00108                                     $hasSectionLimitation = true;
00109 
00110                                     if ( isset( $params['section_id'] ) && $params['section_id'] == $limitation['value'] )
00111                                         $sectionMatch = true;
00112                                 }
00113                                 elseif ( $limitation['issection'] == '2' )
00114                                 {
00115                                     $hasObjectLimitation = true;
00116 
00117                                     if ( isset( $params['contentobject_id'] ) && $params['contentobject_id'] == $limitation['value'] )
00118                                         $objectMatch = true;
00119                                 }
00120                                 else
00121                                 {
00122                                     $hasClassLimitation = true;
00123                                     if ( isset( $params['contentclass_id'] ) && $params['contentclass_id'] == $limitation['value'] )
00124                                         $classMatch = true;
00125                                 }
00126                             }
00127 
00128                             $match = true;
00129                             if ( ( $hasClassLimitation == true ) and ( $classMatch == false ) )
00130                                 $match = false;
00131 
00132                             if ( ( $hasSectionLimitation == true ) and ( $sectionMatch == false ) )
00133                                 $match = false;
00134 
00135                             if ( ( $hasObjectLimitation == true ) and ( $objectMatch == false ) )
00136                                 $match = false;
00137 
00138                             if ( $match == true  )
00139                                 $bestMatch = $subRule['discount_percent'];
00140                         }
00141                     }
00142                 }
00143             }
00144         }
00145         return $bestMatch;
00146     }
00147 }
00148 
00149 ?>