Commit 43928984 authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

Version 0.1: Adding/removing to/from Joomla/VM groups finally works

parent c8928381
......@@ -13,7 +13,7 @@ defined('_JEXEC') or die( 'Direct Access to ' . basename( __FILE__ ) . ' is not
if (!class_exists('vmCustomPlugin')) require(JPATH_VM_PLUGINS . DS . 'vmcustomplugin.php');
class plgVmCustomBuyer_Assign_Group extends vmCustomPlugin {
var $counter=0;
function __construct(& $subject, $config) {
parent::__construct($subject, $config);
$this->_tablepkey = 'id';
......@@ -38,8 +38,6 @@ class plgVmCustomBuyer_Assign_Group extends vmCustomPlugin {
$html = '';
$this->parseCustomParams($field);
// $html .= "<pre>States: ".join(", ", $this->purchased_status)."</pre>";
$html .= "<pre>Field: ".print_r($this, 1)."</pre>";
$html .= '<table class="admintable">';
$html .= VmHTML::row (array('JHTML', '_'),'VMCUSTOM_BUYER_GROUP_JOOMLA', 'access.usergroup', 'custom_param['.$row.'][joomla_groups][]', $field->joomla_groups, ' multiple data-placeholder=" "', false);
$html .= VmHTML::row (array('JHTML', '_'),'VMCUSTOM_BUYER_GROUP_JOOMLA_REMOVE', 'access.usergroup', 'custom_param['.$row.'][joomla_groups_remove][]', $field->joomla_groups_remove, ' multiple data-placeholder=" "', true);
......@@ -97,22 +95,53 @@ class plgVmCustomBuyer_Assign_Group extends vmCustomPlugin {
return $SQLfields;
}
function setShopperGroups($uid, $groups) {
$noError = true;
$userModel = VmModel::getModel('user');
$userModel->setId($uid);
$shoppergroupmodel = VmModel::getModel('ShopperGroup');
$defaultgroup = $shoppergroupmodel->getDefault(0);
if(empty($groups) or ($groups == array($defaultgroup->virtuemart_shoppergroup_id))) {
$groups = array();
}
// Bind the form fields to the table
$shoppergroupData = array('virtuemart_user_id'=>$uid,
'virtuemart_shoppergroup_id'=>$groups);
$user_shoppergroups_table = $userModel->getTable('vmuser_shoppergroups');
$shoppergroupData = $user_shoppergroups_table -> bindChecknStore($shoppergroupData);
$errors = $user_shoppergroups_table->getErrors();
foreach($errors as $error){
$this->setError($error);
vmError('Set shoppergroup '.$error);
$noError = false;
}
return $noError;
}
function addShopperGroup($user, $group) {
# TODO
$groups = $user->shopper_groups;
if (!in_array($group, $groups)) {
$groups[] = $group;
return $this->setShopperGroups($user->virtuemart_user_id, $groups);
}
return false;
}
function removeShopperGroup($user, $group) {
# TODO
$groups = $user->shopper_groups;
if (in_array($group, $groups)) {
$groups=array_diff($groups, array($group));
return $this->setShopperGroups($user->virtuemart_user_id, $groups);
}
return false;
}
function setModifiedFlag ($entry, $value) {
JFactory::getApplication()->enqueueMessage("setModifiedFlag: entry=<pre>".print_r($entry,1)."</pre>", 'error');
$db = JFactory::getDbo();
$db->setQuery("UPDATE `".$this->_tablename."` SET `modified`=".(int)$value." WHERE `id`=".$entry['id']);
if (!$db->query()) {
JFactory::getApplication()->enqueueMessage("SQL query FAILED: Message: ".$db->getErrorMsg().", SQL: <pre>".$db->getQuery()."</pre>", 'error');
}
$this->updateOrderStatus($order_id, $order['details']['BT']->order_status, '');
# TODO
$db = JFactory::getDbo();
$db->setQuery("UPDATE `".$this->_tablename."` SET `modified`=".(int)$value." WHERE `id`=".$entry['id']);
if (!$db->query()) {
JFactory::getApplication()->enqueueMessage("SQL query FAILED: Message: ".$db->getErrorMsg().", SQL: <pre>".$db->getQuery()."</pre>", 'error');
}
}
function updateOrderStatus ($order_id, $order_status, $old_order_status) {
......@@ -148,78 +177,58 @@ JFactory::getApplication()->enqueueMessage("setModifiedFlag: entry=<pre>".print_
$pstates[$cid] = $c->purchased_status;
}
}
foreach ($users as $u) {
JFactory::getApplication()->enqueueMessage("User: Joomla groups <pre>".print_r($u->JUser->groups,1)."</pre>, Shopper Groups: <pre>".print_r($u->shopper_groups,1)."</pre>", 'info');
}
// JFactory::getApplication()->enqueueMessage("User: <pre>".print_r($users,1)."</pre>", 'info');
// JFactory::getApplication()->enqueueMessage("User: <pre>".print_r($users,1)."</pre>", 'info');
// 3) Check which custom field entries need some change
foreach ($orderEntries as $e) {
try {
$uid = $e['virtuemart_user_id'];
$cid = $e['virtuemart_custom_id'];
$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]);
try {
$uid = $e['virtuemart_user_id'];
$cid = $e['virtuemart_custom_id'];
$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]);
JFactory::getApplication()->enqueueMessage("Order status: $order_status, old order status: $old_order_status, pstates: ".print_r($pstates[$cid],1), 'info');
JFactory::getApplication()->enqueueMessage("Purchased: $purchased, Unpurchased: $unpurchased", 'info');
if ($purchased) {
$modified = false;
if ($e['group_type']==0) { // Joomla User Group
// $modified = ($e['group_add'] != );
if ($e['group_add']==1 && !in_array($e['group_id'], $users[$uid]->JUser->groups)) { // Add to Joomla user group
JFactory::getApplication()->enqueueMessage("Trying to add user $uid to group ".$e['group_id'], 'info');
$res = JUserHelper::addUserToGroup ($uid, $e['group_id']);
} elseif ($e['group_add']==0 && in_array($e['group_id'], $users[$uid]->JUser->groups)) { // Remove from Joomla user group
JFactory::getApplication()->enqueueMessage("Trying to remove user $uid from group ".$e['group_id'], 'info');
$res = JUserHelper::removeUserFromGroup ($uid, $e['group_id']);
}
$modified = $res;
JFactory::getApplication()->enqueueMessage("Result: ".print_r($res,1), 'info');
} elseif ($e['group_type']=1) { // Virtuemart Shopper Group
if ($e['group_add']==1) { // Add to Virtuemart Shopper group
$res = $this->addShopperGroup($users[$uid], $e['group_id']);
} elseif ($e['group_add']==0) { // Remove from Virtuemart Shopper group
$res = $this->removeShopperGroup ($users[$uid], $e['group_id']);
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']);
} elseif ($e['group_add']==0 && in_array($e['group_id'], $users[$uid]->JUser->groups)) { // Remove from Joomla user group
$modified = JUserHelper::removeUserFromGroup ($uid, $e['group_id']);
}
} elseif ($e['group_type']=1) { // Virtuemart Shopper Group
if ($e['group_add']==1) { // Add to Virtuemart Shopper group
$modified = $this->addShopperGroup($users[$uid], $e['group_id']);
} elseif ($e['group_add']==0) { // Remove from Virtuemart Shopper group
$modified = $this->removeShopperGroup ($users[$uid], $e['group_id']);
}
}
}
if ($modified) {
$this->setModifiedFlag ($e, $modified);
}
} elseif ($unpurchased && $e['modified']) {
// Undo the addition/removal when a product purchase is cancelled
if ($e['group_type']==0) { // Joomla User Group
JFactory::getApplication()->enqueueMessage("Trying to Undo add (".$e['group_add'].") user $uid to group ".$e['group_id'], 'info');
if ($e['group_add']==1) { // Remove from Joomla user group again
$res = JUserHelper::removeUserFromGroup ($uid, $e['group_id']);
} elseif ($e['group_add']==0) { // Add to Joomla user group again
$res = JUserHelper::addUserToGroup ($uid, $e['group_id']);
if ($modified) {
$this->setModifiedFlag ($e, $modified);
}
JFactory::getApplication()->enqueueMessage("Result: ".print_r($res,1), 'info');
} elseif ($e['group_type']==1) { // Virtuemart Shopper Group
if ($e['group_add']==1) { // Remove from Virtuemart Shopper group again
$modified = $this->removeShopperGroup($users[$uid], $e['group_id']);
} elseif ($e['group_add']==0) { // Add from Virtuemart Shopper group again
$modified = $this->addShopperGroup ($users[$uid], $e['group_id']);
} elseif ($unpurchased && $e['modified']) {
// Undo the addition/removal when a product purchase is cancelled
if ($e['group_type']==0) { // Joomla User Group
if ($e['group_add']==1) { // Remove from Joomla user group again
$modified = JUserHelper::removeUserFromGroup ($uid, $e['group_id']);
} elseif ($e['group_add']==0) { // Add to Joomla user group again
$modified = JUserHelper::addUserToGroup ($uid, $e['group_id']);
}
} elseif ($e['group_type']==1) { // Virtuemart Shopper Group
if ($e['group_add']==1) { // Remove from Virtuemart Shopper group again
$modified = $this->removeShopperGroup($users[$uid], $e['group_id']);
} elseif ($e['group_add']==0) { // Add from Virtuemart Shopper group again
$modified = $this->addShopperGroup ($users[$uid], $e['group_id']);
}
}
// Clear the modified flag in the database
$this->setModifiedFlag ($e, 0);
}
// Clear the modified flag in the database
$this->setModifiedFlag ($e, 0);
}
}
catch (Exception $ex) {
JFactory::getApplication()->enqueueMessage("ERROR: <pre>".$ex->getMessage()."</pre>", 'info');
$ex->getMessage();
JLog::add('Unable to add/remove user to/from group');
}
catch (Exception $ex) {
JFactory::getApplication()->enqueueMessage("ERROR: <pre>".$ex->getMessage()."</pre>", 'info');
JLog::add('Unable to add/remove user to/from group: '.$ex->getMessage());
}
}
// JFactory::getApplication()->enqueueMessage("Order Entries: <pre>".print_r($orderEntries,1)."</pre>", 'info');
// JFactory::getApplication()->enqueueMessage("Purchased States: <pre>".print_r($pstates,1)."</pre>", 'info');
}
function plgVmCouponUpdateOrderStatus($data, $old_order_status) {
......@@ -238,6 +247,8 @@ JFactory::getApplication()->enqueueMessage("ERROR: <pre>".$ex->getMessage()."</
$uid = $user->JUser->id;
$order_id = $order['details']['BT']->virtuemart_order_id;
$orderModel = VmModel::getModel('orders');
$order = $orderModel->getOrder($order_id);
$values = array();
$customModel = VmModel::getModel('customfields');
......
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