/**
  * Unit test for eZDiscountRule::fetchByUserIDArray()
  */
 public function testFetchByUserIDArray()
 {
     // Create 5 few discount rules
     $discountRules = array();
     for ($i = 0; $i < 5; $i++) {
         $row = array('name' => __FUNCTION__ . " #{$i}");
         $rule = new eZDiscountRule($row);
         $rule->store();
         $discountRules[] = $rule;
     }
     // Create 5 user discount rules for 3 different user IDs
     $usersDiscountRules = array();
     foreach (array(1, 3) as $userID) {
         $usersDiscountRules[$userID] = array();
         $userDiscountRules =& $usersDiscountRules[$userID];
         foreach ($discountRules as $discountRule) {
             $row = array('discountrule_id' => $discountRule->attribute('id'), 'contentobject_id' => $userID);
             $userDiscountRule = new eZUserDiscountRule($row);
             $userDiscountRule->store();
             $userDiscountRules[] = $userDiscountRule;
         }
     }
     // fetch the discount rules for user #1 and #2. This will match 10
     // eZUserDiscountRule, and return the 5 rules, since no duplicates will
     // be returned
     $rules = eZUserDiscountRule::fetchByUserIDArray(array(1, 2));
     $this->assertInternalType('array', $rules, "Return value should have been an array");
     $this->assertEquals(5, count($rules), "Return value should contain 5 items");
     foreach ($rules as $rule) {
         $this->assertInstanceOf('eZDiscountRule', $rule);
     }
 }
Exemple #2
0
 function discountPercent()
 {
     $discountPercent = 0;
     $user = eZUser::currentUser();
     $userID = $user->attribute('contentobject_id');
     $nodes = eZContentObjectTreeNode::fetchByContentObjectID($userID);
     $idArray = array();
     $idArray[] = $userID;
     foreach ($nodes as $node) {
         $parentNodeID = $node->attribute('parent_node_id');
         $idArray[] = $parentNodeID;
     }
     $rules = eZUserDiscountRule::fetchByUserIDArray($idArray);
     foreach ($rules as $rule) {
         $percent = $rule->attribute('discount_percent');
         if ($discountPercent < $percent) {
             $discountPercent = $percent;
         }
     }
     return $discountPercent;
 }
    // because we changed users, we have to remove content cache
    eZContentCacheManager::clearAllContentCache();
}
if ($http->hasPostVariable("RemoveCustomerButton")) {
    if ($http->hasPostVariable("CustomerIDArray")) {
        $customerIDArray = $http->postVariable("CustomerIDArray");
        $db = eZDB::instance();
        $db->begin();
        foreach ($customerIDArray as $customerID) {
            eZUserDiscountRule::removeUser($customerID);
        }
        $db->commit();
    }
    eZContentCacheManager::clearAllContentCache();
}
$membershipList = eZUserDiscountRule::fetchByRuleID($discountGroupID);
$customers = array();
foreach ($membershipList as $membership) {
    $customers[] = eZContentObject::fetch($membership->attribute('contentobject_id'));
}
$ruleList = eZDiscountSubRule::fetchByRuleID($discountGroupID);
$ruleArray = array();
foreach ($ruleList as $rule) {
    $name = $rule->attribute('name');
    $percent = $rule->attribute('discount_percent');
    $limitation = $rule->attribute('limitation');
    $discountRuleID = $rule->attribute('id');
    if ($limitation != '*') {
        $ruleValues = eZDiscountSubRuleValue::fetchBySubRuleID($discountRuleID);
        if ($ruleValues != null) {
            $limitation = ezpI18n::tr('kernel/shop', 'Classes') . ' ';
 /**
  * Generate cache content
  *
  * @since 5.3
  * @param int $userId
  * @return array|null
  */
 private static function generateUserCacheData($userId)
 {
     $user = eZUser::fetch($userId);
     if (!$user instanceof eZUser) {
         return null;
     }
     // user info (session: eZUserInfoCache)
     $data['info'][$userId] = array('contentobject_id' => $user->attribute('contentobject_id'), 'login' => $user->attribute('login'), 'email' => $user->attribute('email'), 'is_enabled' => $user->isEnabled(false), 'password_hash' => $user->attribute('password_hash'), 'password_hash_type' => $user->attribute('password_hash_type'));
     // user groups list (session: eZUserGroupsCache)
     $groups = $user->generateGroupIdList();
     $data['groups'] = $groups;
     // role list (session: eZRoleIDList)
     $groups[] = $userId;
     $data['roles'] = eZRole::fetchIDListByUser($groups);
     // role limitation list (session: eZRoleLimitationValueList)
     $limitList = $user->limitList(false);
     foreach ($limitList as $limit) {
         $data['role_limitations'][] = $limit['limit_value'];
     }
     // access array (session: AccessArray)
     $data['access_array'] = $user->generateAccessArray();
     // discount rules (session: eZUserDiscountRules<userId>)
     $data['discount_rules'] = eZUserDiscountRule::generateIDListByUserID($userId);
     return $data;
 }
Exemple #5
0
 /**
  * Callback which generates user cache for user
  *
  * @internal
  * @since 4.4
  * @see eZUser::getUserCacheByUserId()
  */
 static function generateUserCacheForFile($filePath, $userId)
 {
     $data = array('info' => array(), 'groups' => array(), 'roles' => array(), 'role_limitations' => array(), 'access_array' => array(), 'discount_rules' => array());
     $user = eZUser::fetch($userId);
     if ($user instanceof eZUser) {
         // user info (session: eZUserInfoCache)
         $data['info'][$userId] = array('contentobject_id' => $user->attribute('contentobject_id'), 'login' => $user->attribute('login'), 'email' => $user->attribute('email'), 'password_hash' => $user->attribute('password_hash'), 'password_hash_type' => $user->attribute('password_hash_type'));
         // user groups list (session: eZUserGroupsCache)
         $groups = $user->generateGroupIdList();
         $data['groups'] = $groups;
         // role list (session: eZRoleIDList)
         $groups[] = $userId;
         $data['roles'] = eZRole::fetchIDListByUser($groups);
         // role limitation list (session: eZRoleLimitationValueList)
         $limitList = $user->limitList(false);
         foreach ($limitList as $limit) {
             $data['role_limitations'][] = $limit['limit_value'];
         }
         // access array (session: AccessArray)
         $data['access_array'] = $user->generateAccessArray();
         // discount rules (session: eZUserDiscountRules<userId>)
         $data['discount_rules'] = eZUserDiscountRule::generateIDListByUserID($userId);
     }
     return array('content' => $data, 'scope' => 'user-info-cache', 'datatype' => 'php', 'store' => true);
 }
 static function generateViewCacheFile($user, $nodeID, $offset, $layout, $language, $viewMode, $viewParameters = false, $cachedViewPreferences = false, $viewCacheTweak = '')
 {
     $cacheNameExtra = '';
     $ini = eZINI::instance();
     if (!$language) {
         $language = false;
     }
     if (!$viewCacheTweak && $ini->hasVariable('ContentSettings', 'ViewCacheTweaks')) {
         $viewCacheTweaks = $ini->variable('ContentSettings', 'ViewCacheTweaks');
         if (isset($viewCacheTweaks[$nodeID])) {
             $viewCacheTweak = $viewCacheTweaks[$nodeID];
         } else {
             if (isset($viewCacheTweaks['global'])) {
                 $viewCacheTweak = $viewCacheTweaks['global'];
             }
         }
     }
     // should we use current siteaccess or let several siteaccesse share cache?
     if (strpos($viewCacheTweak, 'ignore_siteaccess_name') === false) {
         $currentSiteAccess = $GLOBALS['eZCurrentAccess']['name'];
     } else {
         $currentSiteAccess = $ini->variable('SiteSettings', 'DefaultAccess');
     }
     $cacheHashArray = array($nodeID, $viewMode, $language, $offset, $layout);
     // several user related cache tweaks
     if (strpos($viewCacheTweak, 'ignore_userroles') === false) {
         $cacheHashArray[] = implode('.', $user->roleIDList());
     }
     if (strpos($viewCacheTweak, 'ignore_userlimitedlist') === false) {
         $cacheHashArray[] = implode('.', $user->limitValueList());
     }
     if (strpos($viewCacheTweak, 'ignore_discountlist') === false) {
         $cacheHashArray[] = implode('.', eZUserDiscountRule::fetchIDListByUserID($user->attribute('contentobject_id')));
     }
     $cacheHashArray[] = eZSys::indexFile();
     // Add access type to cache hash if current access is uri type (so uri and host doesn't share cache)
     if (strpos($viewCacheTweak, 'ignore_siteaccess_type') === false && $GLOBALS['eZCurrentAccess']['type'] === eZSiteAccess::TYPE_URI) {
         $cacheHashArray[] = eZSiteAccess::TYPE_URI;
     }
     // Make the cache unique for every logged in user
     if (strpos($viewCacheTweak, 'pr_user') !== false and !$user->isAnonymous()) {
         $cacheNameExtra = $user->attribute('contentobject_id') . '-';
     }
     // Make the cache unique for every case of view parameters
     if (strpos($viewCacheTweak, 'ignore_viewparameters') === false && $viewParameters) {
         $vpString = '';
         ksort($viewParameters);
         foreach ($viewParameters as $key => $value) {
             if (!$key || $key === '_custom') {
                 continue;
             }
             $vpString .= 'vp:' . $key . '=' . $value;
         }
         $cacheHashArray[] = $vpString;
     }
     // Make the cache unique for every case of the preferences
     if ($cachedViewPreferences === false) {
         $depPreferences = $ini->variable('ContentSettings', 'CachedViewPreferences');
     } else {
         $depPreferences = $cachedViewPreferences;
     }
     if (strpos($viewCacheTweak, 'ignore_userpreferences') === false && isset($depPreferences[$viewMode])) {
         $depPreferences = explode(';', $depPreferences[$viewMode]);
         $pString = '';
         // Fetch preferences for the specified user
         $preferences = eZPreferences::values($user);
         foreach ($depPreferences as $pref) {
             $pref = explode('=', $pref);
             if (isset($pref[0])) {
                 if (isset($preferences[$pref[0]])) {
                     $pString .= 'p:' . $pref[0] . '=' . $preferences[$pref[0]] . ';';
                 } else {
                     if (isset($pref[1])) {
                         $pString .= 'p:' . $pref[0] . '=' . $pref[1] . ';';
                     }
                 }
             }
         }
         $cacheHashArray[] = $pString;
     }
     $cacheFile = $nodeID . '-' . $cacheNameExtra . md5(implode('-', $cacheHashArray)) . '.cache';
     $extraPath = eZDir::filenamePath($nodeID);
     $cacheDir = eZDir::path(array(eZSys::cacheDirectory(), $ini->variable('ContentSettings', 'CacheDir'), $currentSiteAccess, $extraPath));
     $cachePath = eZDir::path(array($cacheDir, $cacheFile));
     return array('cache_path' => $cachePath, 'cache_dir' => $cacheDir, 'cache_file' => $cacheFile);
 }
 /**
  * Returns a structure with information required for caching.
  *
  * @param $Params array
  * @return array
  */
 function cacheInfo( $Params )
 {
     $contentCacheInfo =& $GLOBALS['eZContentCacheInfo'];
     if ( !isset( $contentCacheInfo ) )
     {
         $user = eZUser::currentUser();
         $language = false;
         if ( isset( $Params['Language'] ) )
         {
             $language = $Params['Language'];
         }
         $roleList = $user->roleIDList();
         $discountList = eZUserDiscountRule::fetchIDListByUserID( $user->attribute( 'contentobject_id' ) );
         $contentCacheInfo = array( 'language' => $language,
                                    'role_list' => $roleList,
                                    'discount_list' => $discountList );
     }
     return $contentCacheInfo;
 }
 static function discountPercent($user, $params)
 {
     $bestMatch = 0.0;
     if (is_object($user)) {
         $groups = $user->groups();
         $idArray = array_merge($groups, array($user->attribute('contentobject_id')));
         // Fetch discount rules for the current user
         $rules = eZUserDiscountRule::fetchByUserIDArray($idArray);
         if (count($rules) > 0) {
             $db = eZDB::instance();
             $i = 1;
             $subRuleStr = '';
             foreach ($rules as $rule) {
                 $subRuleStr .= $rule->attribute('id');
                 if ($i < count($rules)) {
                     $subRuleStr .= ', ';
                 }
                 $i++;
             }
             // Fetch the discount sub rules
             $subRules = $db->arrayQuery("SELECT * FROM\n                                       ezdiscountsubrule\n                                       WHERE discountrule_id IN ( {$subRuleStr} )\n                                       ORDER BY discount_percent DESC");
             // Find the best matching discount rule
             foreach ($subRules as $subRule) {
                 if ($subRule['discount_percent'] > $bestMatch) {
                     // Rule has better discount, see if it matches
                     if ($subRule['limitation'] == '*') {
                         $bestMatch = $subRule['discount_percent'];
                     } else {
                         // Do limitation check
                         $limitationArray = $db->arrayQuery("SELECT * FROM\n                                       ezdiscountsubrule_value\n                                       WHERE discountsubrule_id='" . $subRule['id'] . "'");
                         $hasSectionLimitation = false;
                         $hasClassLimitation = false;
                         $hasObjectLimitation = false;
                         $objectMatch = false;
                         $sectionMatch = false;
                         $classMatch = false;
                         foreach ($limitationArray as $limitation) {
                             if ($limitation['issection'] == '1') {
                                 $hasSectionLimitation = true;
                                 if (isset($params['section_id']) && $params['section_id'] == $limitation['value']) {
                                     $sectionMatch = true;
                                 }
                             } elseif ($limitation['issection'] == '2') {
                                 $hasObjectLimitation = true;
                                 if (isset($params['contentobject_id']) && $params['contentobject_id'] == $limitation['value']) {
                                     $objectMatch = true;
                                 }
                             } else {
                                 $hasClassLimitation = true;
                                 if (isset($params['contentclass_id']) && $params['contentclass_id'] == $limitation['value']) {
                                     $classMatch = true;
                                 }
                             }
                         }
                         $match = true;
                         if ($hasClassLimitation == true and $classMatch == false) {
                             $match = false;
                         }
                         if ($hasSectionLimitation == true and $sectionMatch == false) {
                             $match = false;
                         }
                         if ($hasObjectLimitation == true and $objectMatch == false) {
                             $match = false;
                         }
                         if ($match == true) {
                             $bestMatch = $subRule['discount_percent'];
                         }
                     }
                 }
             }
         }
     }
     return $bestMatch;
 }
 function removeByID($id)
 {
     eZPersistentObject::removeObject(eZUserDiscountRule::definition(), array("id" => $id));
 }
Exemple #10
0
 /**
  *  Generate cache  key array based on current user roles, requested url, layout
  *
  * @param $userKeys Array
  * @return array
  */
 public function getCacheKeysArray($userKeys)
 {
     if (!is_array($userKeys)) {
         $userKeys = array($userKeys);
     }
     $user = eZUser::currentUser();
     $limitedAssignmentValueList = $user->limitValueList();
     $roleList = $user->roleIDList();
     $discountList = eZUserDiscountRule::fetchIDListByUserID($user->attribute('contentobject_id'));
     $currentSiteAccess = isset($GLOBALS['eZCurrentAccess']['name']) ? $GLOBALS['eZCurrentAccess']['name'] : false;
     $res = eZTemplateDesignResource::instance();
     $keys = $res->keys();
     $layout = isset($keys['layout']) ? $keys['layout'] : false;
     $uri = eZURI::instance(eZSys::requestURI());
     $actualRequestedURI = $uri->uriString();
     $userParameters = $uri->userParameters();
     $cacheKeysArray = array('spdf2png', $currentSiteAccess, $layout, $actualRequestedURI, implode('.', $userParameters), implode('.', $roleList), implode('.', $limitedAssignmentValueList), implode('.', $discountList), implode('.', $userKeys));
     return $cacheKeysArray;
 }