Page MenuHomeIn-Portal Phabricator

D447.id1153.diff
No OneTemporary

File Metadata

Created
Wed, Feb 26, 8:59 AM

D447.id1153.diff

Index: core/units/helpers/user_helper.php
===================================================================
--- core/units/helpers/user_helper.php
+++ core/units/helpers/user_helper.php
@@ -722,4 +722,32 @@
return $object->Update() ? '' : 'restore_impossible';
}
+
+ /**
+ * Returns user's primary group.
+ *
+ * @param integer $user_id User ID.
+ *
+ * @return integer
+ */
+ public function getPrimaryGroup($user_id)
+ {
+ if ( $user_id <= 0 ) {
+ return $this->Application->ConfigValue('User_LoggedInGroup');
+ }
+
+ $cache_key = 'user' . $user_id . '_primary_group[%UIDSerial:' . $user_id . '%]';
+ $cache_value = $this->Application->getCache($cache_key);
+
+ if ( $cache_value === false ) {
+ $sql = 'SELECT PrimaryGroupId
+ FROM ' . TABLE_PREFIX . 'Users
+ WHERE PortalUserId = ' . $user_id;
+ $cache_value = $this->Conn->GetOne($sql);
+ $this->Application->setCache($cache_key, $cache_value);
+ }
+
+ return $cache_value;
+ }
+
}
Index: core/units/users/users_event_handler.php
===================================================================
--- core/units/users/users_event_handler.php
+++ core/units/users/users_event_handler.php
@@ -1665,6 +1665,12 @@
WHERE PortalUserId IN (' . implode(',', $user_ids) . ')';
$this->Conn->Query($sql);
+ foreach ( $user_ids as $user_id ) {
+ $this->Application->incrementCacheSerial('u', $user_id);
+ }
+
+ $this->Application->incrementCacheSerial('u');
+
$sql = 'SELECT PortalUserId
FROM ' . $table_name . '
WHERE (GroupId = ' . $primary_group_id . ') AND (PortalUserId IN (' . implode(',', $user_ids) . '))';
Index: modules/in-commerce/units/orders/order_calculator.php
===================================================================
--- modules/in-commerce/units/orders/order_calculator.php
+++ modules/in-commerce/units/orders/order_calculator.php
@@ -527,7 +527,10 @@
if ($this->Application->ConfigValue('Comm_PriceBracketCalculation') == 1) {
// if we have to stick to primary group, then its pricing will go first,
// but if there is no pricing for primary group, then next optimal will be taken
- $primary_group = $this->getUserPrimaryGroup($user_id);
+
+ /** @var UserHelper $user_helper */
+ $user_helper = $this->Application->recallObject('UserHelper');
+ $primary_group = $user_helper->getPrimaryGroup($user_id);
return '( IF(GroupId = ' . $primary_group . ', 1, 2) ) ASC, pp.Price ASC';
}
@@ -787,24 +790,6 @@
}
/**
- * Returns primary group of given user
- *
- * @param int $user_id
- * @return int
- */
- protected function getUserPrimaryGroup($user_id)
- {
- if ($user_id > 0) {
- $sql = 'SELECT PrimaryGroupId
- FROM ' . TABLE_PREFIX . 'Users
- WHERE PortalUserId = ' . $user_id;
- return $this->Conn->GetOne($sql);
- }
-
- return $this->Application->ConfigValue('User_LoggedInGroup');
- }
-
- /**
* Returns ItemData associated with given order item
*
* @param Array $item
Index: modules/in-commerce/units/pricing/pricing_event_handler.php
===================================================================
--- modules/in-commerce/units/pricing/pricing_event_handler.php
+++ modules/in-commerce/units/pricing/pricing_event_handler.php
@@ -470,20 +470,19 @@
}
if ( $this->Application->ConfigValue('Comm_PriceBracketCalculation') == 1 ) {
- $sql = 'SELECT PrimaryGroupId
- FROM ' . TABLE_PREFIX . 'Users
- WHERE PortalUserId = ' . $this->Application->GetVar('u_id');
- $pricing_group = $this->Conn->GetOne($sql);
+ /** @var UserHelper $user_helper */
+ $user_helper = $this->Application->recallObject('UserHelper');
+ $pricing_group = $user_helper->getPrimaryGroup($this->Application->RecallVar('user_id'));
- if ( $pricing_group ) {
+ if ( $this->Application->LoggedIn() ) {
$sql = 'SELECT COUNT(*)
FROM ' . TABLE_PREFIX . 'ProductsPricing
WHERE ProductId = ' . $this->Application->GetVar('p_id') . ' AND GroupId = ' . $pricing_group . ' AND Price IS NOT NULL';
$pricing_for_group_exists = $this->Conn->GetOne($sql);
- }
- if ( !$pricing_group || !$pricing_for_group_exists ) {
- $pricing_group = $this->Application->ConfigValue('User_LoggedInGroup');
+ if ( !$pricing_for_group_exists ) {
+ $pricing_group = $this->Application->ConfigValue('User_LoggedInGroup');
+ }
}
}
else {
Index: modules/in-commerce/units/products/products_event_handler.php
===================================================================
--- modules/in-commerce/units/products/products_event_handler.php
+++ modules/in-commerce/units/products/products_event_handler.php
@@ -1444,12 +1444,9 @@
return ;
}
- $user_id = $this->Application->RecallVar('user_id');
-
- $sql = 'SELECT PrimaryGroupId
- FROM ' . TABLE_PREFIX . 'Users
- WHERE PortalUserId = ' . $user_id;
- $primary_group_id = $this->Conn->GetOne($sql);
+ /** @var UserHelper $user_helper */
+ $user_helper = $this->Application->recallObject('UserHelper');
+ $primary_group_id = $user_helper->getPrimaryGroup($this->Application->RecallVar('user_id'));
if (!$primary_group_id) {
return;
Index: modules/in-commerce/units/products/products_item.php
===================================================================
--- modules/in-commerce/units/products/products_item.php
+++ modules/in-commerce/units/products/products_item.php
@@ -39,7 +39,9 @@
if (!$this->Application->isAdmin && $this->Application->LoggedIn()) {
// also group based
- $primary_group = (int)$this->Application->Session->GetField('GroupId');
+ /** @var UserHelper $user_helper */
+ $user_helper = $this->Application->recallObject('UserHelper');
+ $primary_group = $user_helper->getPrimaryGroup($this->Application->RecallVar('user_id'));
$cache_key .= ':group=' . $primary_group;
}
@@ -82,4 +84,4 @@
return $price_info;
}
- }
\ No newline at end of file
+ }

Event Timeline