/kernel/classes/ezuserdiscountrule.php

https://github.com/zerustech/ezpublish · PHP · 220 lines · 173 code · 19 blank · 28 comment · 8 complexity · 0558cfb14f4663236ea72209f5f40554 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the eZUserDiscountRule class.
  4. *
  5. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6. * @license For full copyright and license information view LICENSE file distributed with this source code.
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. /*!
  11. \class eZUserDiscountRule ezuserdiscountrule.php
  12. \brief The class eZUserDiscountRule does
  13. */
  14. class eZUserDiscountRule extends eZPersistentObject
  15. {
  16. static function definition()
  17. {
  18. return array( "fields" => array( "id" => array( 'name' => 'ID',
  19. 'datatype' => 'integer',
  20. 'default' => 0,
  21. 'required' => true ),
  22. "discountrule_id" => array( 'name' => "DiscountRuleID",
  23. 'datatype' => 'integer',
  24. 'default' => 0,
  25. 'required' => true,
  26. 'foreign_class' => 'eZDiscountRule',
  27. 'foreign_attribute' => 'id',
  28. 'multiplicity' => '1..*' ),
  29. "contentobject_id" => array( 'name' => "ContentobjectID",
  30. 'datatype' => 'integer',
  31. 'default' => 0,
  32. 'required' => true,
  33. 'foreign_class' => 'eZContentObject',
  34. 'foreign_attribute' => 'id',
  35. 'multiplicity' => '1..*' ) ),
  36. "keys" => array( "id" ),
  37. "increment_key" => "id",
  38. "relations" => array( "discountrule_id" => array( "class" => "eZDiscountRule",
  39. "field" => "id" ),
  40. "contentobject_id" => array( "class" => "eZContentObject",
  41. "field" => "id" ) ),
  42. "class_name" => "eZUserDiscountRule",
  43. "name" => "ezuser_discountrule" );
  44. }
  45. function store( $fieldFilters = null )
  46. {
  47. if ( $this->ContentobjectID == eZUser::anonymousId() )
  48. {
  49. eZUser::purgeUserCacheByAnonymousId();
  50. }
  51. else
  52. {
  53. $handler = eZExpiryHandler::instance();
  54. $handler->setTimestamp( 'user-discountrules-cache', time() );
  55. $handler->store();
  56. }
  57. parent::store( $fieldFilters );
  58. }
  59. static function fetch( $id, $asObject = true )
  60. {
  61. return eZPersistentObject::fetchObject( eZUserDiscountRule::definition(),
  62. null,
  63. array( "id" => $id
  64. ),
  65. $asObject );
  66. }
  67. static function fetchByUserID( $userID, $asObject = true )
  68. {
  69. return eZPersistentObject::fetchObjectList( eZUserDiscountRule::definition(),
  70. null,
  71. array( "contentobject_id" => $userID ),
  72. null,
  73. null,
  74. $asObject );
  75. }
  76. static function fetchIDListByUserID( $userID )
  77. {
  78. if ( $userID == eZUser::anonymousId() )
  79. {
  80. $userCache = eZUSer::getUserCacheByAnonymousId();
  81. $ruleArray = $userCache['discount_rules'];
  82. }
  83. else
  84. {
  85. $http = eZHTTPTool::instance();
  86. $handler = eZExpiryHandler::instance();
  87. $expiredTimeStamp = 0;
  88. if ( $handler->hasTimestamp( 'user-discountrules-cache' ) )
  89. $expiredTimeStamp = $handler->timestamp( 'user-discountrules-cache' );
  90. $ruleTimestamp =& $http->sessionVariable( 'eZUserDiscountRulesTimestamp' );
  91. $ruleArray = false;
  92. // check for cached version in session
  93. if ( $ruleTimestamp > $expiredTimeStamp )
  94. {
  95. if ( $http->hasSessionVariable( 'eZUserDiscountRules' . $userID ) )
  96. {
  97. $ruleArray =& $http->sessionVariable( 'eZUserDiscountRules' . $userID );
  98. }
  99. }
  100. if ( !is_array( $ruleArray ) )
  101. {
  102. $ruleArray = self::generateIDListByUserID( (int) $userID );
  103. $http->setSessionVariable( 'eZUserDiscountRules' . $userID, $ruleArray );
  104. $http->setSessionVariable( 'eZUserDiscountRulesTimestamp', time() );
  105. }
  106. }
  107. $rules = array();
  108. foreach ( $ruleArray as $ruleRow )
  109. {
  110. $rules[] = $ruleRow['id'];
  111. }
  112. return $rules;
  113. }
  114. /**
  115. * Get raw list of discount rules
  116. *
  117. * @internal
  118. * @param int $userId
  119. * @return array
  120. */
  121. static public function generateIDListByUserID( $userId )
  122. {
  123. $db = eZDB::instance();
  124. $query = "SELECT DISTINCT ezdiscountrule.id
  125. FROM ezdiscountrule,
  126. ezuser_discountrule
  127. WHERE ezuser_discountrule.contentobject_id = $userId AND
  128. ezuser_discountrule.discountrule_id = ezdiscountrule.id";
  129. return $db->arrayQuery( $query );
  130. }
  131. /**
  132. * Fetches the eZDiscountRules matching an array of eZUserID
  133. *
  134. * @param array(eZUserID) $idArray Array of user ID
  135. *
  136. * @return array(eZDiscountRule)
  137. */
  138. static function &fetchByUserIDArray( $idArray )
  139. {
  140. $db = eZDB::instance();
  141. $inString = $db->generateSQLINStatement( $idArray, 'ezuser_discountrule.contentobject_id', false, false, 'int' );
  142. $query = "SELECT DISTINCT ezdiscountrule.id,
  143. ezdiscountrule.name
  144. FROM ezdiscountrule,
  145. ezuser_discountrule
  146. WHERE $inString AND
  147. ezuser_discountrule.discountrule_id = ezdiscountrule.id";
  148. $ruleArray = $db->arrayQuery( $query );
  149. $rules = array();
  150. foreach ( $ruleArray as $ruleRow )
  151. {
  152. $rules[] = new eZDiscountRule( $ruleRow );
  153. }
  154. return $rules;
  155. }
  156. static function &fetchUserID( $discountRuleID )
  157. {
  158. $userList = eZPersistentObject::fetchObjectList( eZUserDiscountRule::definition(),
  159. null,
  160. array( "discountrule_id" => $discountRuleID ),
  161. null,
  162. null,
  163. false );
  164. $idArray = array();
  165. foreach ( $userList as $user )
  166. {
  167. $idArray[] = $user['contentobject_id'];
  168. }
  169. return $idArray;
  170. }
  171. static function &fetchByRuleID( $discountRuleID, $asObject = true )
  172. {
  173. $objectList = eZPersistentObject::fetchObjectList( eZUserDiscountRule::definition(),
  174. null,
  175. array( "discountrule_id" => $discountRuleID ),
  176. null,
  177. null,
  178. $asObject );
  179. return $objectList;
  180. }
  181. static function create( $discountRuleID, $contentobjectID )
  182. {
  183. $row = array(
  184. "id" => null,
  185. "discountrule_id" => $discountRuleID,
  186. "contentobject_id" => $contentobjectID );
  187. return new eZUserDiscountRule( $row );
  188. }
  189. static function removeUser( $userID )
  190. {
  191. eZPersistentObject::removeObject( eZUserDiscountRule::definition(),
  192. array( "contentobject_id" => $userID ) );
  193. }
  194. function removeByID( $id )
  195. {
  196. eZPersistentObject::removeObject( eZUserDiscountRule::definition(),
  197. array( "id" => $id ) );
  198. }
  199. }
  200. ?>