00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 include_once( "kernel/classes/ezpersistentobject.php" );
00041 include_once( "kernel/classes/ezdiscountrule.php" );
00042
00043 class eZUserDiscountRule extends eZPersistentObject
00044 {
00045
00046
00047
00048 function eZUserDiscountRule( $row )
00049 {
00050 $this->eZPersistentObject( $row );
00051 }
00052
00053 function definition()
00054 {
00055 return array( "fields" => array( "id" => array( 'name' => 'ID',
00056 'datatype' => 'integer',
00057 'default' => 0,
00058 'required' => true ),
00059 "discountrule_id" => array( 'name' => "DiscountRuleID",
00060 'datatype' => 'integer',
00061 'default' => 0,
00062 'required' => true,
00063 'foreign_class' => 'eZDiscountRule',
00064 'foreign_attribute' => 'id',
00065 'multiplicity' => '1..*' ),
00066 "contentobject_id" => array( 'name' => "ContentobjectID",
00067 'datatype' => 'integer',
00068 'default' => 0,
00069 'required' => true,
00070 'foreign_class' => 'eZContentObject',
00071 'foreign_attribute' => 'id',
00072 'multiplicity' => '1..*' ) ),
00073 "keys" => array( "id" ),
00074 "increment_key" => "id",
00075 "relations" => array( "discountrule_id" => array( "class" => "ezdiscountrule",
00076 "field" => "id" ),
00077 "contentobject_id" => array( "class" => "ezcontentobject",
00078 "field" => "id" ) ),
00079 "class_name" => "eZUserDiscountRule",
00080 "name" => "ezuser_discountrule" );
00081 }
00082
00083 function store()
00084 {
00085 include_once( 'lib/ezutils/classes/ezexpiryhandler.php' );
00086 $handler =& eZExpiryHandler::instance();
00087 $handler->setTimestamp( 'user-discountrules-cache', mktime() );
00088 $handler->store();
00089 eZPersistentObject::store();
00090 }
00091
00092 function fetch( $id, $asObject = true )
00093 {
00094 return eZPersistentObject::fetchObject( eZUserDiscountRule::definition(),
00095 null,
00096 array( "id" => $id
00097 ),
00098 $asObject );
00099 }
00100
00101 function fetchByUserID( $userID, $asObject = true )
00102 {
00103 return eZPersistentObject::fetchObjectList( eZUserDiscountRule::definition(),
00104 null,
00105 array( "contentobject_id" => $userID ),
00106 null,
00107 null,
00108 $asObject );
00109 }
00110
00111 function fetchIDListByUserID( $userID )
00112 {
00113 $http =& eZHTTPTool::instance();
00114
00115 include_once( 'lib/ezutils/classes/ezexpiryhandler.php' );
00116 $handler =& eZExpiryHandler::instance();
00117 $expiredTimeStamp = 0;
00118 if ( $handler->hasTimestamp( 'user-discountrules-cache' ) )
00119 $expiredTimeStamp = $handler->timestamp( 'user-discountrules-cache' );
00120
00121 $ruleTimestamp =& $http->sessionVariable( 'eZUserDiscountRulesTimestamp' );
00122
00123 $ruleArray = false;
00124
00125 if ( $ruleTimestamp > $expiredTimeStamp )
00126 {
00127 if ( $http->hasSessionVariable( 'eZUserDiscountRules' . $userID ) )
00128 {
00129 $ruleArray =& $http->sessionVariable( 'eZUserDiscountRules' . $userID );
00130 }
00131 }
00132
00133 if ( !is_array( $ruleArray ) )
00134 {
00135 $userID = (int)$userID;
00136 $db =& eZDB::instance();
00137 $query = "SELECT DISTINCT ezdiscountrule.id
00138 FROM ezdiscountrule,
00139 ezuser_discountrule
00140 WHERE ezuser_discountrule.contentobject_id = $userID AND
00141 ezuser_discountrule.discountrule_id = ezdiscountrule.id";
00142 $ruleArray = $db->arrayQuery( $query );
00143 $http->setSessionVariable( 'eZUserDiscountRules' . $userID, $ruleArray );
00144 $http->setSessionVariable( 'eZUserDiscountRulesTimestamp', mktime() );
00145 }
00146
00147 $rules = array();
00148 foreach ( $ruleArray as $ruleRow )
00149 {
00150 $rules[] = $ruleRow['id'];
00151 }
00152 return $rules;
00153 }
00154
00155 function &fetchByUserIDArray( $idArray )
00156 {
00157 $db =& eZDB::instance();
00158 $groupString = $db->implodeWithTypeCast( ',', $idArray, 'int' );
00159 $query = "SELECT DISTINCT ezdiscountrule.id,
00160 ezdiscountrule.name
00161 FROM ezdiscountrule,
00162 ezuser_discountrule
00163 WHERE ezuser_discountrule.contentobject_id IN ( $groupString ) AND
00164 ezuser_discountrule.discountrule_id = ezdiscountrule.id";
00165 $ruleArray = $db->arrayQuery( $query );
00166
00167 $rules = array();
00168 foreach ( $ruleArray as $ruleRow )
00169 {
00170 $rules[] = new eZDiscountRule( $ruleRow );
00171 }
00172 return $rules;
00173 }
00174
00175 function &fetchUserID( $discountRuleID )
00176 {
00177 $userList = eZPersistentObject::fetchObjectList( eZUserDiscountRule::definition(),
00178 null,
00179 array( "discountrule_id" => $discountRuleID ),
00180 null,
00181 null,
00182 false );
00183 $idArray = array();
00184 foreach ( $userList as $user )
00185 {
00186
00187 $idArray[] = $user['contentobject_id'];
00188 }
00189 return $idArray;
00190 }
00191
00192 function &fetchByRuleID( $discountRuleID, $asObject = true )
00193 {
00194 $objectList = eZPersistentObject::fetchObjectList( eZUserDiscountRule::definition(),
00195 null,
00196 array( "discountrule_id" => $discountRuleID ),
00197 null,
00198 null,
00199 $asObject );
00200 return $objectList;
00201 }
00202
00203 function create( $discountRuleID, $contentobjectID )
00204 {
00205 $row = array(
00206 "id" => null,
00207 "discountrule_id" => $discountRuleID,
00208 "contentobject_id" => $contentobjectID );
00209 return new eZUserDiscountRule( $row );
00210 }
00211
00212 function removeUser( $userID )
00213 {
00214 eZPersistentObject::removeObject( eZUserDiscountRule::definition(),
00215 array( "contentobject_id" => $userID ) );
00216 }
00217 function remove( $id )
00218 {
00219 eZPersistentObject::removeObject( eZUserDiscountRule::definition(),
00220 array( "id" => $id ) );
00221 }
00222 }
00223 ?>