eZ Publish  [4.0]
ezproductcategory.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Created on: <17-Feb-2006 15:20:15 vs>
00004 //
00005 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00006 // SOFTWARE NAME: eZ Publish
00007 // SOFTWARE RELEASE: 4.0.x
00008 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00009 // SOFTWARE LICENSE: GNU General Public License v2.0
00010 // NOTICE: >
00011 //   This program is free software; you can redistribute it and/or
00012 //   modify it under the terms of version 2.0  of the GNU General
00013 //   Public License as published by the Free Software Foundation.
00014 //
00015 //   This program is distributed in the hope that it will be useful,
00016 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //   GNU General Public License for more details.
00019 //
00020 //   You should have received a copy of version 2.0 of the GNU General
00021 //   Public License along with this program; if not, write to the Free
00022 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00023 //   MA 02110-1301, USA.
00024 //
00025 //
00026 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00027 //
00028 
00029 /*!
00030   \class eZProductCategory ezproductcategory.php
00031   \brief Handles product categories used by the default VAT handler.
00032   \ingroup eZKernel
00033 */
00034 
00035 //include_once( "kernel/classes/ezpersistentobject.php" );
00036 
00037 class eZProductCategory extends eZPersistentObject
00038 {
00039     /*!
00040     */
00041     function eZProductCategory( $row )
00042     {
00043         $this->eZPersistentObject( $row );
00044     }
00045 
00046     static function definition()
00047     {
00048         return array( "fields" => array( "id" => array( 'name' => 'ID',
00049                                                         'datatype' => 'integer',
00050                                                         'default' => 0,
00051                                                         'required' => true ),
00052                                          "name" => array( 'name' => "Name",
00053                                                           'datatype' => 'string',
00054                                                           'default' => '',
00055                                                           'required' => true ) ),
00056                       "keys" => array( "id" ),
00057                       "increment_key" => "id",
00058                       "class_name" => "eZProductCategory",
00059                       "name" => "ezproductcategory" );
00060     }
00061 
00062     static function fetch( $id, $asObject = true )
00063     {
00064         return eZPersistentObject::fetchObject( eZProductCategory::definition(),
00065                                                 null,
00066                                                 array( "id" => $id ),
00067                                                 $asObject );
00068     }
00069 
00070     static function fetchByName( $name, $asObject = true )
00071     {
00072         return eZPersistentObject::fetchObject( eZProductCategory::definition(),
00073                                                 null,
00074                                                 array( "name" => $name ),
00075                                                 $asObject );
00076     }
00077 
00078     static function fetchList( $asObject = true )
00079     {
00080         return eZPersistentObject::fetchObjectList( eZProductCategory::definition(),
00081                                                     null, null, array( 'name' => 'asc' ), null,
00082                                                     $asObject );
00083     }
00084 
00085     /**
00086      * Returns number of products belonging to the given category.
00087      *
00088      * \public
00089      * \static
00090      */
00091     static function fetchProductCountByCategory( $categoryID )
00092     {
00093         $ini = eZINI::instance( 'shop.ini' );
00094         if ( !$ini->hasVariable( 'VATSettings', 'ProductCategoryAttribute' ) ||
00095              !$categoryAttrName = $ini->variable( 'VATSettings', 'ProductCategoryAttribute' ) )
00096             return 0;
00097 
00098         require_once( 'lib/ezdb/classes/ezdb.php' );
00099         $db = eZDB::instance();
00100         $categoryID =(int) $categoryID;
00101         $categoryAttrName = $db->escapeString( $categoryAttrName );
00102         $query = "SELECT COUNT(*) AS count " .
00103                  " FROM ezcontentobject_attribute coa, ezcontentclass_attribute cca, ezcontentobject co " .
00104                  "WHERE " .
00105                  " cca.id=coa.contentclassattribute_id " .
00106                  " AND coa.contentobject_id=co.id " .
00107                  " AND cca.data_type_string='ezproductcategory' " .
00108                  " AND cca.identifier='$categoryAttrName' " .
00109                  " AND coa.version=co.current_version " .
00110                  " AND coa.data_int=$categoryID";
00111         $rows = $db->arrayQuery( $query );
00112         return $rows[0]['count'];
00113     }
00114 
00115     static function create()
00116     {
00117         $row = array(
00118             "id" => null,
00119             "name" => ezi18n( 'kernel/shop/productcategories', 'Product category' ) );
00120         return new eZProductCategory( $row );
00121     }
00122 
00123     /**
00124      * Remove the given category and all references to it.
00125      *
00126      * \public
00127      * \static
00128      */
00129     static function removeByID( $id )
00130     {
00131         $id = (int) $id;
00132 
00133         $db = eZDB::instance();
00134         $db->begin();
00135 
00136         // Delete references to the category from VAT charging rules.
00137         require_once( 'kernel/classes/ezvatrule.php' );
00138         eZVatRule::removeReferencesToProductCategory( $id );
00139 
00140         // Reset product category attribute for all products
00141         // that have been referencing the category.
00142         $ini = eZINI::instance( 'shop.ini' );
00143         if ( $ini->hasVariable( 'VATSettings', 'ProductCategoryAttribute' ) &&
00144              $categoryAttrName = $ini->variable( 'VATSettings', 'ProductCategoryAttribute' ) )
00145         {
00146             $categoryAttrName = $db->escapeString( $categoryAttrName );
00147             $query = "SELECT coa.id FROM ezcontentobject_attribute coa, ezcontentclass_attribute cca, ezcontentobject co " .
00148                      "WHERE " .
00149                      " cca.id=coa.contentclassattribute_id " .
00150                      " AND coa.contentobject_id=co.id " .
00151                      " AND cca.data_type_string='ezproductcategory' " .
00152                      " AND cca.identifier='$categoryAttrName' " .
00153                      " AND coa.version=co.current_version " .
00154                      " AND coa.data_int=$id";
00155 
00156             $rows = $db->arrayQuery( $query );
00157 
00158             foreach ( $rows as $row )
00159             {
00160                 $query = "UPDATE ezcontentobject_attribute SET data_int=0, sort_key_int=0 WHERE id=" . (int) $row['id'];
00161                 $db->query( $query );
00162             }
00163         }
00164 
00165         // Remove the category itself.
00166         eZPersistentObject::removeObject( eZProductCategory::definition(),
00167                                           array( "id" => $id ) );
00168 
00169         $db->commit();
00170     }
00171 }
00172 
00173 ?>