Commit 22227d01 authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

V1.4: Fix changing VM shoppergroups for good (need to use VmTable rather than...

V1.4: Fix changing VM shoppergroups for good (need to use VmTable rather than the model, and reset the cache...)
parent cbf48aa8
BASE=buyer_assign_group
PLUGINTYPE=vmcustom
ZIPBASE=opentools_vm2
VERSION=1.3
VERSION=1.4
PLUGINFILES=$(BASE).php $(BASE).script.php $(BASE).xml index.html
......
......@@ -30,7 +30,6 @@ class plgVmCustomBuyer_Assign_Group extends vmCustomPlugin {
} else {
$this->setConfigParameterable ('customfield_params', $varsToPush);
}
// $this->onStoreInstallPluginTable($this->_psType);
}
/**
......@@ -46,7 +45,6 @@ class plgVmCustomBuyer_Assign_Group extends vmCustomPlugin {
} else {
$paramName = 'customfield_params';
}
$html .= '<table class="admintable">';
$html .= VmHTML::row (array('JHTML', '_'),'VMCUSTOM_BUYER_GROUP_JOOMLA', 'access.usergroup', $paramName.'['.$row.'][joomla_groups][]', $field->joomla_groups, ' multiple data-placeholder=" "', false);
$html .= VmHTML::row (array('JHTML', '_'),'VMCUSTOM_BUYER_GROUP_JOOMLA_REMOVE', 'access.usergroup', $paramName.'['.$row.'][joomla_groups_remove][]', $field->joomla_groups_remove, ' multiple data-placeholder=" "', true);
......@@ -111,10 +109,49 @@ class plgVmCustomBuyer_Assign_Group extends vmCustomPlugin {
return $SQLfields;
}
/* This function does NOT load the whole user, it just loads enough to have the Joomla
groups and the VM shopper groups, i.e. it loads all data from the vmusers and the
vmuser_shoppergroups table, and the Joomla user information, but nothing else */
function loadUserGroupData($uid) {
if (empty($uid)) return null;
$db = JFactory::getDBO();
$userModel = VmModel::getModel('user');
$user = $userModel->getTable('vmusers');
$user->load((int)$uid);
$user->JUser = JUser::getInstance($uid);
// Add the virtuemart_shoppergroup_ids
$xrefTable = $userModel->getTable('vmuser_shoppergroups');
$user->shopper_groups = $xrefTable->load($uid);
if(empty($user->shopper_groups)) $user->shopper_groups = array();
$site = JFactory::getApplication ()->isSite ();
if ($site) {
$shoppergroupmodel = VmModel::getModel('ShopperGroup');
$shoppergroupmodel->appendShopperGroups($user->shopper_groups,$user->JUser,$site);
}
$user->shopper_groups = (array)$user->shopper_groups;
return $user;
}
/** This functions makes sure the user model clears the cache of the given user, because the
user data was modified directly in the database. In VM2 there is only the last user
cached, so make sure to change the user id by first setting it to 0 and then to the real
uid. Otherwise the cache would not be erased if the uid was the last requested. */
function clearUserCache($uid) {
$userModel = VmModel::getModel('user');
$userModel->_data = null;
$userModel->setId(0); // <- Make sure the cache is really, really erased in VM2!
$userModel->setId($uid);
}
function setShopperGroups($uid, $groups) {
$this->clearUserCache($uid);
$noError = true;
$userModel = VmModel::getModel('user');
$userModel->setId($uid);
$shoppergroupmodel = VmModel::getModel('ShopperGroup');
$defaultgroup = $shoppergroupmodel->getDefault(0);
......@@ -133,6 +170,7 @@ class plgVmCustomBuyer_Assign_Group extends vmCustomPlugin {
vmError('Set shoppergroup '.$error);
$noError = false;
}
$user_shoppergroups_table->emptyCache();
return $noError;
}
......@@ -181,9 +219,7 @@ class plgVmCustomBuyer_Assign_Group extends vmCustomPlugin {
foreach ($orderEntries as $e) {
$uid = $e['virtuemart_user_id'];
if (!isset($users[$uid])) {
$userModel->setId($uid);
$users[$uid] = $userModel->getUser();
$users[$uid]->shopper_groups = (array)$users[$uid]->shopper_groups;
$users[$uid] = $this->loadUserGroupData($uid);
}
$cid = $e['virtuemart_custom_id'];
if (!isset($customs[$cid])) {
......@@ -203,8 +239,8 @@ class plgVmCustomBuyer_Assign_Group extends vmCustomPlugin {
$purchased = in_array($order_status, $pstates[$cid]) && !in_array($old_order_status, $pstates[$cid]);
$unpurchased = !in_array($order_status, $pstates[$cid]) && in_array($old_order_status, $pstates[$cid]);
$modified = false;
if ($purchased) {
$modified = false;
if ($e['group_type']==0) { // Joomla User Group
if ($e['group_add']==1 && !in_array($e['group_id'], $users[$uid]->JUser->groups)) { // Add to Joomla user group
$modified = JUserHelper::addUserToGroup ($uid, $e['group_id']);
......@@ -220,13 +256,10 @@ class plgVmCustomBuyer_Assign_Group extends vmCustomPlugin {
}
if ($modified) {
$this->setModifiedFlag ($e, $modified);
// clear the user model cache and update update the $users array so the next step in the loop has the correct groups if the user is added to another group
$this->clearUserCache($uid);
$users[$uid] = $this->loadUserGroupData($uid);
}
// Update the $users array so the next step in the loop has the correct groups if the user is added to another group
$userModel->_data = null;
$userModel->setId(0); // <- This makes sure to invalidate the cache
$userModel->setId($uid);
$users[$uid] = $userModel->getUser();
$users[$uid]->shopper_groups = (array)$users[$uid]->shopper_groups;
} elseif ($unpurchased && $e['modified']) {
// Undo the addition/removal when a product purchase is cancelled
if ($e['group_type']==0) { // Joomla User Group
......@@ -246,11 +279,8 @@ class plgVmCustomBuyer_Assign_Group extends vmCustomPlugin {
$this->setModifiedFlag ($e, 0);
}
// Reload the user data, because shopper groups are NOT automatically updated in the data structure in memory!
$userModel->_data = null;
$userModel->setId(0); // <- This makes sure to invalidate the cache
$userModel->setId($uid);
$users[$uid] = $userModel->getUser();
$users[$uid]->shopper_groups = (array)$users[$uid]->shopper_groups;
$this->clearUserCache($uid);
$users[$uid] = $this->loadUserGroupData($uid);
}
catch (Exception $ex) {
JFactory::getApplication()->enqueueMessage("ERROR: <pre>".$ex->getMessage()."</pre>", 'info');
......@@ -261,7 +291,7 @@ class plgVmCustomBuyer_Assign_Group extends vmCustomPlugin {
// VM2.x compat:
function plgVmOnUpdateOrder($data, $old_order_status) {
return $this->plgVmOnUpdateOrderShipment($data, $old_order_status);
return $this->updateOrderStatus($data->virtuemart_order_id, $data->order_status, $old_order_status);
}
// VM3.x
function plgVmOnUpdateOrderShipment($data, $old_order_status) {
......@@ -325,6 +355,17 @@ class plgVmCustomBuyer_Assign_Group extends vmCustomPlugin {
$this->updateOrderStatus($order_id, $order['details']['BT']->order_status, '');
}
}
// function plgVmOnSelfCallBE($type, $name, &$output) {
// if ($name != $this->_name || $type != 'vmcustom') return false;
// vmDebug('plgVmOnSelfCallBE');
//
// $db = JFactory::getDBO();
// $db->setQuery("SELECT * FROM `".$this->_tablename.";");
// $val = $db->loadAssocList();
//
// JFactory::getApplication()->enqueueMessage("<pre>plgVmOnSelfCallBE, type=$type, name=$name, full database contents:".print_r($val,1)."</pre>", 'error');
// }
}
// No closing tag
\ No newline at end of file
......@@ -6,7 +6,7 @@
<authorUrl>http://www.open-tools.net/</authorUrl>
<copyright>Copyright (C) 2013-2014 Reinhold Kainhofer. All rights reserved.</copyright>
<license>http://www.gnu.org/licenses/gpl.html GNU/GPL v3+</license>
<version>1.3</version>
<version>1.4</version>
<description>VMCUSTOM_BUYER_GROUP_DESC</description>
<files>
<filename plugin="buyer_assign_group">buyer_assign_group.php</filename>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment