diff --git a/Makefile b/Makefile index e2a6910cf6893baebd38749be3ca7bacbd0759ef..df3d01542f4cd01bbcc3a4713f53258cba8d2a31 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ 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 diff --git a/buyer_assign_group.php b/buyer_assign_group.php index 2fd5456077d8e6a30bb561714a48c347fe94836a..6a753895a2527e521d939e6115bdf10554738552 100644 --- a/buyer_assign_group.php +++ b/buyer_assign_group.php @@ -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 diff --git a/buyer_assign_group.xml b/buyer_assign_group.xml index c3f0b0d79e17ebc70a8277e7c2d2423370887b2b..6a58807c7a1ddb5b3dc166ff96ab7d3a97b51c06 100644 --- a/buyer_assign_group.xml +++ b/buyer_assign_group.xml @@ -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> diff --git a/releases/plg_opentools_vm2_buyer_assign_group_v1.4.zip b/releases/plg_opentools_vm2_buyer_assign_group_v1.4.zip new file mode 100644 index 0000000000000000000000000000000000000000..946926df1e6be9bfb72f3b581ef465dedb88315d Binary files /dev/null and b/releases/plg_opentools_vm2_buyer_assign_group_v1.4.zip differ