[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/kernel/shop/ -> discountruleedit.php (source)

   1  <?php
   2  //
   3  // Created on: <25-Nov-2002 15:40:10 wy>
   4  //
   5  // SOFTWARE NAME: eZ publish
   6  // SOFTWARE RELEASE: 3.9.0
   7  // BUILD VERSION: 17785
   8  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
   9  // SOFTWARE LICENSE: GNU General Public License v2.0
  10  // NOTICE: >
  11  //   This program is free software; you can redistribute it and/or
  12  //   modify it under the terms of version 2.0  of the GNU General
  13  //   Public License as published by the Free Software Foundation.
  14  //
  15  //   This program is distributed in the hope that it will be useful,
  16  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  17  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18  //   GNU General Public License for more details.
  19  //
  20  //   You should have received a copy of version 2.0 of the GNU General
  21  //   Public License along with this program; if not, write to the Free
  22  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  23  //   MA 02110-1301, USA.
  24  //
  25  //
  26  
  27  // TODO: it was not in the original code, but we may consider to add support for "folder with products",
  28  //       not only products (i.e. objects with attribute of the ezprice datatype).
  29  
  30  include_once ( 'kernel/common/template.php' );
  31  include_once ( 'kernel/classes/ezcontentobject.php' );
  32  include_once ( 'kernel/classes/ezdiscountrule.php' );
  33  include_once ( 'kernel/classes/ezdiscountsubrule.php' );
  34  include_once ( 'kernel/classes/ezdiscountsubrulevalue.php' );
  35  include_once ( 'kernel/classes/ezcontentbrowse.php' );
  36  include_once ( 'lib/ezutils/classes/ezhttppersistence.php' );
  37  include_once ( 'kernel/shop/classes/ezshopfunctions.php' );
  38  
  39  $module =& $Params['Module'];
  40  
  41  if ( !isset( $Params['DiscountGroupID'] ) )
  42  {
  43      return $Module->handleError( EZ_ERROR_KERNEL_NOT_AVAILABLE, 'kernel' );
  44  }
  45  else
  46  {
  47      $discountGroupID = $Params['DiscountGroupID'];
  48  }
  49  
  50  $discountRuleID = false;
  51  
  52  if ( isset( $Params['DiscountRuleID'] ) )
  53  {
  54      $discountRuleID = $Params['DiscountRuleID'];
  55  }
  56  
  57  $http =& eZHTTPTool::instance();
  58  
  59  if ( $http->hasPostVariable( 'DiscardButton' ) )
  60  {
  61      return $module->redirectTo( $module->functionURI( 'discountgroupview' ) . '/' . $discountGroupID );
  62  }
  63  
  64  
  65  if ( $http->hasPostVariable( 'BrowseProductButton' ) )
  66  {
  67      eZContentBrowse::browse( array( 'action_name' => 'FindProduct',
  68                                      'description_template' => 'design:shop/browse_discountproduct.tpl',
  69                                      'keys' => array( 'discountgroup_id' => $discountGroupID,
  70                                                       'discountrule_id' => $discountRuleID ),
  71                                      'content' => array( 'discountgroup_id' => $discountGroupID,
  72                                                          'discountrule_id' => $discountRuleID ),
  73                                      'persistent_data' => array( 'discountrule_name' => $http->postVariable( 'discountrule_name' ),
  74                                                                  'discountrule_percent' => $http->postVariable( 'discountrule_percent' ),
  75                                                                  'Contentclasses' => ( $http->hasPostVariable( 'Contentclasses' ) )? serialize( $http->postVariable( 'Contentclasses' ) ): '',
  76                                                                  'Sections' => ( $http->hasPostVariable( 'Sections' ) )? serialize( $http->postVariable( 'Sections' ) ): '',
  77                                                                  'Products' => ( $http->hasPostVariable( 'Products' ) )? serialize( $http->postVariable( 'Products' ) ): '' ),
  78                                      'from_page' => "/shop/discountruleedit/$discountGroupID/$discountRuleID" ),
  79                               $module );
  80      return;
  81  }
  82  
  83  if ( $http->hasPostVariable( 'discountrule_name' ) )
  84  {
  85      // if it has post variables, the values will be taken from POST variables instead of object itself
  86      include_once ( 'lib/ezlocale/classes/ezlocale.php' );
  87      $locale =& eZLocale::instance();
  88  
  89      $discountRuleName = $http->postVariable( 'discountrule_name' );
  90      $discountRulePercent = $locale->internalNumber( $http->postVariable( 'discountrule_percent' ) );
  91  
  92      $discountRuleSelectedClasses = array();
  93      if ( $http->hasPostVariable( 'Contentclasses' ) && $http->postVariable( 'Contentclasses' ) )
  94      {
  95          $discountRuleSelectedClasses = $http->postVariable( 'Contentclasses' );
  96          if ( !is_array( $discountRuleSelectedClasses ) )
  97          {
  98              $discountRuleSelectedClasses = unserialize( $discountRuleSelectedClasses );
  99          }
 100      }
 101  
 102      $discountRuleSelectedSections = array();
 103      if ( $http->hasPostVariable( 'Sections' ) && $http->postVariable( 'Sections' ) )
 104      {
 105          $discountRuleSelectedSections = $http->postVariable( 'Sections' );
 106          if ( !is_array( $discountRuleSelectedSections ) )
 107          {
 108              $discountRuleSelectedSections = unserialize( $discountRuleSelectedSections );
 109          }
 110      }
 111  
 112      $discountRuleSelectedProducts = array();
 113      if ( $http->hasPostVariable( 'Products' ) && $http->postVariable( 'Products' ) )
 114      {
 115          $discountRuleSelectedProducts = $http->postVariable( 'Products' );
 116          if ( !is_array( $discountRuleSelectedProducts ) )
 117          {
 118              $discountRuleSelectedProducts = unserialize( $discountRuleSelectedProducts );
 119          }
 120      }
 121  
 122      $discountRule = array( 'id' => $discountRuleID ,
 123                             'name' => $discountRuleName,
 124                             'discount_percent' => $discountRulePercent );
 125  }
 126  else
 127  {
 128      // read variables from object, if it exists, if not, create new one...
 129      if ( $discountRuleID )
 130      {
 131          // exists => read needed info from db
 132          $discountRule = eZDiscountSubRule::fetch( $discountRuleID );
 133          if ( !$discountRule )
 134          {
 135              return $Module->handleError( EZ_ERROR_KERNEL_NOT_AVAILABLE, 'kernel' );
 136          }
 137  
 138          $discountRuleSelectedClasses = array();
 139          $discountRuleSelectedClassesValues = eZDiscountSubRuleValue::fetchBySubRuleID( $discountRuleID, 0, false );
 140          foreach( $discountRuleSelectedClassesValues as $value )
 141          {
 142              $discountRuleSelectedClasses[] = $value['value'];
 143          }
 144          if ( count( $discountRuleSelectedClasses ) == 0 )
 145          {
 146              $discountRuleSelectedClasses[] = -1;
 147          }
 148  
 149          $discountRuleSelectedSections = array();
 150          $discountRuleSelectedSectionsValues = eZDiscountSubRuleValue::fetchBySubRuleID( $discountRuleID, 1, false );
 151          foreach( $discountRuleSelectedSectionsValues as $value )
 152          {
 153              $discountRuleSelectedSections[] = $value['value'];
 154          }
 155          if ( count( $discountRuleSelectedSections ) == 0 )
 156          {
 157              $discountRuleSelectedSections[] = -1;
 158          }
 159  
 160          $discountRuleSelectedProductsValues = eZDiscountSubRuleValue::fetchBySubRuleID( $discountRuleID, 2, false );
 161          foreach( $discountRuleSelectedProductsValues as $value )
 162          {
 163              $discountRuleSelectedProducts[] = $value['value'];
 164          }
 165      }
 166      else
 167      {
 168          // does not exist => create new one, but do not store...
 169          $discountRuleName = ezi18n( 'design/admin/shop/discountruleedit', 'New discount rule' );
 170          $discountRulePercent = 0.0;
 171          $discountRuleSelectedClasses = array( -1 );
 172          $discountRuleSelectedSections = array( -1 );
 173          $discountRuleSelectedProducts = array();
 174  
 175          $discountRule = array( 'id' => 0,
 176                                 'name' => $discountRuleName,
 177                                 'discount_percent' => $discountRulePercent );
 178      }
 179  }
 180  
 181  if ( $module->isCurrentAction( 'FindProduct' ) )
 182  {
 183      // returning from browse; add products to product list
 184      $result = eZContentBrowse::result( 'FindProduct' );
 185      if ( $result )
 186      {
 187          $discountRuleSelectedProducts = array_merge( $discountRuleSelectedProducts, $result );
 188          $discountRuleSelectedProducts = array_unique( $discountRuleSelectedProducts );
 189      }
 190  }
 191  
 192  if ( $http->hasPostVariable( 'DeleteProductButton' ) )
 193  {
 194      // remove products from list:
 195      if ( $http->hasPostVariable( 'DeleteProductIDArray' ) )
 196      {
 197          $deletedIDList = $http->postVariable( 'DeleteProductIDArray' );
 198          $arrayKeys = array_keys( $discountRuleSelectedProducts );
 199  
 200          foreach( $arrayKeys as $key )
 201          {
 202              if ( in_array( $discountRuleSelectedProducts[$key], $deletedIDList ) )
 203              {
 204                  unset( $discountRuleSelectedProducts[$key] );
 205              }
 206          }
 207      }
 208  }
 209  
 210  $productList = array();
 211  foreach ( $discountRuleSelectedProducts as $productID )
 212  {
 213      $object =& eZContentObject::fetch( $productID );
 214      if ( eZShopFunctions::isProductObject( $object ) )
 215          $productList[] = $object;
 216  }
 217  
 218  if ( $http->hasPostVariable( 'StoreButton' ) )
 219  {
 220      // remove products stored in the database and store them again
 221      $db =& eZDB::instance();
 222      $db->begin();
 223      if ( $discountRuleID )
 224      {
 225          $discountRule = eZDiscountSubRule::fetch( $discountRuleID );
 226          eZDiscountSubRuleValue::removeBySubRuleID ( $discountRuleID );
 227      }
 228      else
 229      {
 230          $discountRule = eZDiscountSubRule::create( $discountGroupID );
 231          $discountRule->store();
 232          $discountRuleID = $discountRule->attribute( 'id' );
 233      }
 234  
 235      $discountRule->setAttribute( 'name', trim( $http->postVariable( 'discountrule_name' ) ) );
 236      $discountRule->setAttribute( 'discount_percent', $http->postVariable( 'discountrule_percent' ) );
 237      $discountRule->setAttribute( 'limitation', '*' );
 238  
 239      if ( $http->hasPostVariable( 'Products' ) && $http->postVariable( 'Products' ) )
 240      {
 241          foreach( $productList as $product )
 242          {
 243              $ruleValue = eZDiscountSubRuleValue::create( $discountRuleID, $product->attribute( 'id' ), 2 );
 244              $ruleValue->store();
 245          }
 246          $discountRule->setAttribute( 'limitation', false );
 247      }
 248      else
 249      {
 250          if ( $discountRuleSelectedClasses && !in_array( -1, $discountRuleSelectedClasses ) )
 251          {
 252              foreach( $discountRuleSelectedClasses as $classID )
 253              {
 254                  $ruleValue = eZDiscountSubRuleValue::create( $discountRuleID, $classID, 0 );
 255                  $ruleValue->store();
 256              }
 257              $discountRule->setAttribute( 'limitation', false );
 258          }
 259          if ( $discountRuleSelectedSections && !in_array( -1, $discountRuleSelectedSections ) )
 260          {
 261              foreach( $discountRuleSelectedSections as $sectionID )
 262              {
 263                  $ruleValue = eZDiscountSubRuleValue::create( $discountRuleID, $sectionID, 1 );
 264                  $ruleValue->store();
 265              }
 266              $discountRule->setAttribute( 'limitation', false );
 267          }
 268      }
 269  
 270      $discountRule->store();
 271      $db->commit();
 272  
 273      // we changed prices => remove content cache
 274      include_once ( 'kernel/classes/ezcontentcachemanager.php' );
 275      eZContentCacheManager::clearAllContentCache();
 276  
 277      return $module->redirectTo( $module->functionURI( 'discountgroupview' ) . '/' . $discountGroupID );
 278  }
 279  
 280  $classList = eZContentClass::fetchList();
 281  $productClassList = array();
 282  foreach ( $classList as $class )
 283  {
 284      if ( eZShopFunctions::isProductClass( $class ) )
 285          $productClassList[] = $class;
 286  }
 287  
 288  $sectionList = eZSection::fetchList();
 289  
 290  $tpl =& templateInit();
 291  
 292  $tpl->setVariable( 'module', $module );
 293  $tpl->setVariable( 'discountgroup_id', $discountGroupID );
 294  $tpl->setVariable( 'discountrule', $discountRule );
 295  
 296  $tpl->setVariable( 'product_class_list', $productClassList );
 297  $tpl->setVariable( 'section_list', $sectionList );
 298  
 299  $tpl->setVariable( 'class_limitation_list', $discountRuleSelectedClasses );
 300  $tpl->setVariable( 'section_limitation_list', $discountRuleSelectedSections );
 301  $tpl->setVariable( 'product_list', $productList );
 302  
 303  $tpl->setVariable( 'class_any_selected', in_array( -1, $discountRuleSelectedClasses ) );
 304  $tpl->setVariable( 'section_any_selected', in_array( -1, $discountRuleSelectedSections ) );
 305  
 306  $Result = array();
 307  $Result['content'] =& $tpl->fetch( 'design:shop/discountruleedit.tpl' );
 308  $Result['path'] = array( array( 'url' => false,
 309                                  'text' => ezi18n( 'kernel/shop', 'Editing rule' ) ) );
 310  
 311  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7