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

Start implementing group assignment

parent a07f4537
......@@ -29,20 +29,6 @@ class plgVmCustomBuyer_Assign_Group extends vmCustomPlugin {
$this->onStoreInstallPluginTable($this->_psType);
}
function addJoomlaGroup($user, $group) {
# TODO
}
function removeJoomlaGroup($user, $group) {
# TODO
}
function addShopperGroup($user, $group) {
# TODO
}
function removeShopperGroup($user, $group) {
# TODO
}
/**
* @see Form displayed in the product edit page in the BE, configure the download file
* @author Reinhold Kainhofer
......@@ -111,10 +97,28 @@ class plgVmCustomBuyer_Assign_Group extends vmCustomPlugin {
return $SQLfields;
}
function addShopperGroup($user, $group) {
# TODO
}
function removeShopperGroup($user, $group) {
# TODO
}
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
}
function updateOrderStatus ($order_id, $order_status, $old_order_status) {
// 1) Load all entries associated with this order
$db = JFactory::getDbo();
$db->setQuery("SELeCT * FROM `".$this->_tablename."` WHERE `order_id` = ".(int)$order_id . ";");
$db->setQuery("SELECT * FROM `".$this->_tablename."` WHERE `order_id` = ".(int)$order_id . ";");
$orderEntries = $db->loadAssocList();
if (is_null($orderEntries)) {
JFactory::getApplication()->enqueueMessage("SQL query FAILED: Message: ".$db->getErrorMsg(), 'error');
......@@ -134,6 +138,7 @@ class plgVmCustomBuyer_Assign_Group extends vmCustomPlugin {
if (!isset($users[$uid])) {
$userModel->setId($uid);
$users[$uid] = $userModel->getUser();
$users[$uid]->shopper_groups = (array)$users[$uid]->shopper_groups;
}
$cid = $e['virtuemart_custom_id'];
if (!isset($customs[$cid])) {
......@@ -143,26 +148,77 @@ class plgVmCustomBuyer_Assign_Group extends vmCustomPlugin {
$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]);
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) {
// TODO: Check if user is alreay part of the group. If not, add him and set the modified flag
} elseif ($unpurchased && $e['modified'] {
$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 ($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']);
}
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']);
}
}
// 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');
}
}
JFactory::getApplication()->enqueueMessage("Order Entries: <pre>".print_r($orderEntries,1)."</pre>", 'info');
JFactory::getApplication()->enqueueMessage("Purchased States: <pre>".print_r($pstates,1)."</pre>", 'info');
JFactory::getApplication()->enqueueMessage("Customs: <pre>".print_r($customs,1)."</pre>", 'info');
// TODO:
// JFactory::getApplication()->enqueueMessage("Order Entries: <pre>".print_r($orderEntries,1)."</pre>", 'info');
// JFactory::getApplication()->enqueueMessage("Purchased States: <pre>".print_r($pstates,1)."</pre>", 'info');
}
......@@ -185,6 +241,7 @@ JFactory::getApplication()->enqueueMessage("Customs: <pre>".print_r($customs,1).
$values = array();
$customModel = VmModel::getModel('customfields');
// Collect all group additions/removals that need to be stored in the database; Will be used on order change
foreach ($order['items'] as $item) {
$customs = $customModel->getproductCustomslist ($item->virtuemart_product_id);
foreach ($customs as $field) {
......
Supports Markdown
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