By Fred on Sunday, 13 July 2014
Posted in General Issues
Replies 15
Likes 0
Views 1.3K
Votes 0
So I'm taking a stab at Custom Badges because well... I kinda need them... :-\

Anyways I wanna know if I'm doing it right... There is a DB ID called plan_id for OS Membership Pro component, when someone purchases a membership they go into this table with the selected membership plan ID

	require_once( JPATH_ROOT . '/administrator/components/com_easysocial/includes/foundry.php' );
public function assignBadge( $rule , $message , $creatorId = null )
{
$planId = JRequest::getInt('plan_id');

if ($planId => 1)
{
Foundry::badges()->log( 'com_osmembership' , 'loose_cannon.donation' , $userId , JText::_( 'Obtained Loose Cannon Donation' ) );
}
}


and this would be the .badge file code I'm guessing..

[
{
"title" : "Loose Cannon",
"alias" : "loose-cannon",
"description" : "Getting the sheer enjoyment helping .Org",
"howto" : "To unlock this badge, you need to donate $10.00 or more.",
"command" : "loose_cannon.donation",
"extension" : "com_osmembership",
"avatar" : "media/badges/loose_cannon.png",
"frequency" : 1
}
]


So am I doing this right? I haven't tried it yet but I'm just looking for a little direction. I got the php stuff from one of the OS Membership Pros subscribers.php file which looks like this

class OSMembershipControllerSubscribers extends OSController
{

function export()
{
$config = OSMembershipHelper::getConfig();
$db = JFactory::getDbo();
$planId = JRequest::getInt('plan_id');
$published = JRequest::getInt('published', -1);
$where = array();
if ($planId > 0)
{
$where[] = ' a.plan_id=' . $planId;
}
if ($published != -1)
{
$where[] = ' a.published=' . $published;
}
$sql = 'SELECT a.*, b.username, c.title FROM #__osmembership_subscribers AS a
LEFT JOIN #__users AS b
ON a.user_id = b.id
LEFT JOIN #__osmembership_plans AS c
ON a.plan_id = c.id
';
if (count($where))
{
$sql .= ' WHERE ' . implode(' AND ', $where);
}
$db->setQuery($sql);
$rows = $db->loadObjectList();
for ($i = 0, $n = count($rows); $i < $n; $i++)
{
$row = $rows[$i];
switch ($row->published)
{
case 0:
$row->subscription_status = JText::_('OSM_PENDING');
break;
case 1:
$row->subscription_status = JText::_('OSM_ACTIVE');
break;
case 2:
$row->subscription_status = JText::_('OSM_EXPIRED');
break;
case 3:
$row->subscription_status = JText::_('OSM_CANCELLED_PENDING');
break;
case 4:
$row->subscription_status = JText::_('OSM_CANCELLED_REFUNDED');
break;
default:
$row->subscription_status = '';
break;
}
}

$sql = 'SELECT name, title FROM #__osmembership_plugins';
$db->setQuery($sql);
$plugins = $db->loadObjectList();
$pluginTitles = array();
foreach ($plugins as $plugin)
{
$pluginTitles[$plugin->name] = $plugin->title;
}
//Get list of custom fields
$sql = 'SELECT id, name, title, is_core FROM #__osmembership_fields WHERE published=1 ORDER BY ordering';
$db->setQuery($sql);
$rowFields = $db->loadObjectList();

$customFieldDatas = array();
if (count($where))
{
$sql = 'SELECT * FROM #__osmembership_field_value WHERE subscriber_id IN (SELECT id FROM #__osmembership_subscribers AS a WHERE ' .
implode(' AND ', $where) . ')';
}
else
{
$sql = 'SELECT * FROM #__osmembership_field_value';
}
$db->setQuery($sql);
$fieldDatas = $db->loadObjectList();
if (count($fieldDatas))
{
foreach ($fieldDatas as $fieldData)
{
$customFieldDatas[$fieldData->subscriber_id][$fieldData->field_id] = $fieldData->field_value;
}
}
if (count($rows))
{
$results_arr = array();
$results_arr[] = JText::_('OSM_PLAN');
$results_arr[] = JText::_('Username');
foreach ($rowFields as $rowField)
{
$results_arr[] = $rowField->title;
}
$results_arr[] = JText::_('OSM_SUBSCRIPTION_START_DATE');
$results_arr[] = JText::_('OSM_SUBSCRIPTION_END_DATE');
$results_arr[] = JText::_('OSM_SUBSCRIPTION_STATUS');
$results_arr[] = JText::_('OSM_DISCOUNT_AMOUNT');
$results_arr[] = JText::_('OSM_TAX_AMOUNT');
$results_arr[] = JText::_('OSM_GROSS_AMOUNT');
$results_arr[] = JText::_('OSM_PAYMENT_METHOD');
$results_arr[] = JText::_('OSM_TRANSACTION_ID');

$csv_output = "\"" . implode("\",\"", $results_arr) . "\"";

foreach ($rows as $r)
{
$results_arr = array();
$results_arr[] = $r->title;
$results_arr[] = $r->username;
foreach ($rowFields as $rowField)
{
if ($rowField->is_core)
{
$fieldName = $rowField->name;
$results_arr[] = $r->{$fieldName};
}
else
{
$fieldId = $rowField->id;
$results_arr[] = @$customFieldDatas[$r->id][$fieldId];
}
}
$results_arr[] = JHtml::_('date', $r->from_date, $config->date_format);
$results_arr[] = JHtml::_('date', $r->to_date, $config->date_format);
$results_arr[] = $r->subscription_status;
$results_arr[] = round($r->discount_amount, 2);
$results_arr[] = round($r->tax_amount, 2);
$results_arr[] = round($r->gross_amount, 2);
$results_arr[] = $pluginTitles[$r->payment_method];
$results_arr[] = $r->transaction_id;
$csv_output .= "\n\"" . implode("\",\"", $results_arr) . "\"";
}
$csv_output .= "\n";
if (ereg('Opera(/| )([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT']))
{
$UserBrowser = "Opera";
}
elseif (ereg('MSIE ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT']))
{
$UserBrowser = "IE";
}
else
{
$UserBrowser = '';
}
$mime_type = ($UserBrowser == 'IE' || $UserBrowser == 'Opera') ? 'application/octetstream' : 'application/octet-stream';
$filename = "order_list";
@ob_end_clean();
ob_start();
header('Content-Type: ' . $mime_type);
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
if ($UserBrowser == 'IE')
{
header('Content-Disposition: attachment; filename="' . $filename . '.csv"');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
}
else
{
header('Content-Disposition: attachment; filename="' . $filename . '.csv"');
header('Pragma: no-cache');
}
print $csv_output;
exit();
}
}
}
Ok, so I figured it out! I'm starting to learn more and more as I've been messing around with this. Here is how I got this to work


function onMembershipActive($row) {
if ($row->user_id) {
$user = JFactory::getUser($row->user_id);
$currentGroups = $user->get('groups') ;
$plan = JTable::getInstance('Osmembership','Plan');
$plan->load($row->plan_id);
$params = new JRegistry($plan->params);
$groups = explode(',', $params->get('joomla_group_ids'));
$currentGroups = array_unique(array_merge($currentGroups, $groups)) ;
$user->set('groups', $currentGroups);
$user->save(true);
switch ($row->plan_id) {
case 1:
Foundry::badges()->log( 'com_osmembership' , 'loose_cannon.donation' , $row->user_id , JText::_( 'Obtained Loose Cannon Donation' ) );
break;
case 2:
Foundry::badges()->log( 'com_osmembership' , 'stone_mason.donation' , $row->user_id , JText::_( 'Obtained Stone Mason Donation' ) );
break;
case 3:
Foundry::badges()->log( 'com_osmembership' , 'forge_worker.donation' , $row->user_id , JText::_( 'Obtained Forge Worker Donation' ) );
break;
case 4:
Foundry::badges()->log( 'com_osmembership' , 'voxel_shifter.donation' , $row->user_id , JText::_( 'Obtained Voxel Shifter Donation' ) );
break;
case 5:
Foundry::badges()->log( 'com_osmembership' , 'allspark.donation' , $row->user_id , JText::_( 'Obtained Allspark Donation' ) );
break;
case 6:
Foundry::badges()->log( 'com_osmembership' , 'grand_kodite_master.donation' , $row->user_id , JText::_( 'Obtained Grand Kodite Master Donation' ) );
break;
default:
Foundry::badges()->log( 'com_osmembership' , 'default.donation' , $row->user_id , JText::_( 'Obtained Default Donation' ) );
break;
}
}
}
·
Wednesday, 16 July 2014 22:08
·
0 Likes
·
0 Votes
·
0 Comments
·
Hello Fred,

I am really sorry for the delay of this reply as it is a weekend for us here. There's a lot of codes here so I didn't really looked at the last one as that seems to be logics of your app but your badge declaration and assigning does seem to look alright to me
·
Sunday, 13 July 2014 13:04
·
0 Likes
·
0 Votes
·
0 Comments
·
nope, that didn't seem to work... maybe I have the assignBadge code in the wrong place...
·
Sunday, 13 July 2014 20:37
·
0 Likes
·
0 Votes
·
0 Comments
·
Your codes for the badges are correct already However, I am not really sure about the logic behind it?
·
Sunday, 13 July 2014 21:52
·
0 Likes
·
0 Votes
·
0 Comments
·
I dont know either... I so need to go back to school for coding...

But I'm trying to do is when someone purchases a subscription through Membership Pro it will give a badge for that assigned Subscription ID. But what I think I'm missing is maybe an action for when its purchased it runs that log script... I dont know...
·
Sunday, 13 July 2014 23:59
·
0 Likes
·
0 Votes
·
0 Comments
·
Yeah perhaps the code where it logs the badge did not get executed at all.
·
Monday, 14 July 2014 02:15
·
0 Likes
·
0 Votes
·
0 Comments
·
Well I have figured out where it needs to go... it needs to be with this function onMembershipActive but everything I've tried doesn't work...


/**
* Run when a membership activated
* @param PlanOsMembership $row
*/

function onMembershipActive($row) {
if ($row->user_id) {
$user = JFactory::getUser($row->user_id);
$currentGroups = $user->get('groups') ;
$plan = JTable::getInstance('Osmembership','Plan');
$plan->load($row->plan_id);
$params = new JRegistry($plan->params);
$groups = explode(',', $params->get('joomla_group_ids'));
$currentGroups = array_unique(array_merge($currentGroups, $groups)) ;
$user->set('groups', $currentGroups);
$user->save(true);
}
}
·
Monday, 14 July 2014 12:41
·
0 Likes
·
0 Votes
·
0 Comments
·
Sorry but I can't help you out with this as I am not really sure what their codes are doing
·
Monday, 14 July 2014 12:48
·
0 Likes
·
0 Votes
·
0 Comments
·
Thanks for updating Fred Glad that your issues are resolved now.
·
Wednesday, 16 July 2014 23:35
·
0 Likes
·
0 Votes
·
0 Comments
·
I'm facing the same problem, have the badges created and installed, but I am having a problem locating the proper location for the logic. I too have OSMembership, this thread is a couple years old now, so it seems things have changed a bit. What was the file path for the correct location of subscribers.php?

Fred wrote:

Ok, so I figured it out! I'm starting to learn more and more as I've been messing around with this. Here is how I got this to work


function onMembershipActive($row) {
if ($row->user_id) {
$user = JFactory::getUser($row->user_id);
$currentGroups = $user->get('groups') ;
$plan = JTable::getInstance('Osmembership','Plan');
$plan->load($row->plan_id);
$params = new JRegistry($plan->params);
$groups = explode(',', $params->get('joomla_group_ids'));
$currentGroups = array_unique(array_merge($currentGroups, $groups)) ;
$user->set('groups', $currentGroups);
$user->save(true);
switch ($row->plan_id) {
case 1:
Foundry::badges()->log( 'com_osmembership' , 'loose_cannon.donation' , $row->user_id , JText::_( 'Obtained Loose Cannon Donation' ) );
break;
case 2:
Foundry::badges()->log( 'com_osmembership' , 'stone_mason.donation' , $row->user_id , JText::_( 'Obtained Stone Mason Donation' ) );
break;
case 3:
Foundry::badges()->log( 'com_osmembership' , 'forge_worker.donation' , $row->user_id , JText::_( 'Obtained Forge Worker Donation' ) );
break;
case 4:
Foundry::badges()->log( 'com_osmembership' , 'voxel_shifter.donation' , $row->user_id , JText::_( 'Obtained Voxel Shifter Donation' ) );
break;
case 5:
Foundry::badges()->log( 'com_osmembership' , 'allspark.donation' , $row->user_id , JText::_( 'Obtained Allspark Donation' ) );
break;
case 6:
Foundry::badges()->log( 'com_osmembership' , 'grand_kodite_master.donation' , $row->user_id , JText::_( 'Obtained Grand Kodite Master Donation' ) );
break;
default:
Foundry::badges()->log( 'com_osmembership' , 'default.donation' , $row->user_id , JText::_( 'Obtained Default Donation' ) );
break;
}
}
}
·
Friday, 27 May 2016 12:07
·
0 Likes
·
0 Votes
·
0 Comments
·
Hey Spartan,

I am not too sure if Fred is around but I would suggest that you try creating a ticket on OSMembership to ask them about this method
·
Friday, 27 May 2016 12:28
·
0 Likes
·
0 Votes
·
0 Comments
·
Indeed, that is the next step, now I have a new issue, will create the appropriate thread for. Thanks!
·
Friday, 27 May 2016 12:37
·
0 Likes
·
0 Votes
·
0 Comments
·
No problem Spartan
·
Friday, 27 May 2016 12:38
·
0 Likes
·
0 Votes
·
0 Comments
·
we would love to have custom badges on your site, what can i do ? iam not a programmer. is there a plan in future to add it ? pls advise
NK
·
Tuesday, 31 May 2016 16:51
·
0 Likes
·
0 Votes
·
0 Comments
·
Hello NK,

This thread is mostly discussing about the custom code created by Fred to integrate with OSMembership. You might want to start a new thread about your issue/request. Perhaps, if it is a feature request, you can directly add it in our voices page here: http://stackideas.com/voices/easysocial
·
Tuesday, 31 May 2016 18:43
·
0 Likes
·
0 Votes
·
0 Comments
·
View Full Post