Commit 95cfaaa6 authored by Mathieu Lutfy's avatar Mathieu Lutfy Committed by Aegir user

#79 Fix partner badge update to avoid Spark conflict.

parent 46d07268
<?php
//this module populates a custom field whenever a service provider becomes a partner or active contributor
//At the moment, it is set to run whenever a group or membership is created or edited.
// This module populates a custom field whenever a service provider becomes a partner or active contributor
// At the moment, it is set to run whenever a group or membership is created or edited.
// The reason we do this is because we need something we can add to a view,
// since it (IMHO) is impossible to do the view calculation in the view itself.
//The reason we do this is because we need something we can add to a view, since it (IMHO) is impossible to do the view calculation in the view itself
function civicrm_org_providers_civicrm_post($op, $objectName, $objectId, $ObjectRef){
if(!in_array($op, array('edit', 'create', 'delete'))){
function civicrm_org_providers_civicrm_post($op, $objectName, $objectId, $ObjectRef) {
if (!in_array($op, array('edit', 'create', 'delete'))) {
return;
}
if($objectName == 'Membership'){
if ($objectName == 'Membership') {
civicrm_org_providers_update_provider($ObjectRef->contact_id);
}
if($objectName == 'GroupContact' && in_array($objectId, array(131,140,267))){
if ($objectName == 'GroupContact' && in_array($objectId, array(131,140,267))) {
// $objectref is an array of contacts that are going to be added to the group, it seems...
// This is fething the last element?
foreach($ObjectRef as $contact);
if(is_object($contact)){
if (is_object($contact)) {
civicrm_org_providers_update_provider($contact->id);
}else{
}
else {
civicrm_org_providers_update_provider($contact);
}//$objectref is an array of contacts that are going to be added to the group, it seems...
}
}
}
function civicrm_org_providers_update_provider($contact_id){
civicrm_initialize();
function civicrm_org_providers_update_provider($contact_id) {
civicrm_initialize();
//check that the contact is a service provider contact subtype
$params = array('version' => 3, 'contact_id' => $contact_id);
$contact=civicrm_api('Contact', 'getsingle', $params);
if(!is_array($contact['contact_sub_type']) OR !in_array('Service_provider', $contact['contact_sub_type'])){
return;
};
//find out if they are in the founding members group
$params = array('version' => 3, 'contact_id' => $contact_id);
$founder = false;
$groups=civicrm_api('GroupContact', 'get', $params);
foreach($groups['values'] as $group){
if($group['group_id']==140){
$founder = true;
break;
}
}
// Check that the contact is a service provider contact subtype
$contact = civicrm_api3('Contact', 'getsingle', [
'contact_id' => $contact_id,
]);
//find out if they are in the active contributors group
$params = array('version' => 3, 'contact_id' => $contact_id);
$groups=civicrm_api('GroupContact', 'get', $params);
foreach($groups['values'] as $group){
switch ($group['group_id']) {
case 488:
$badges[] = 'contributor-supporting';
break;
case 489:
$badges[] = 'contributor-empowering';
break;
case 490:
$badges[] = 'contributor-sustaining';
break;
}
if (!is_array($contact['contact_sub_type']) || !in_array('Service_provider', $contact['contact_sub_type'])) {
return;
}
// Find out if they are in the founding members group
$founder = FALSE;
$groups = civicrm_api3('GroupContact', 'get', [
'contact_id' => $contact_id,
]);
foreach ($groups['values'] as $group) {
if ($group['group_id'] == 140) {
$founder = true;
break;
}
}
// Find out if they are in the active contributors group
$groups = civicrm_api3('GroupContact', 'get', [
'contact_id' => $contact_id,
]);
foreach($groups['values'] as $group) {
switch ($group['group_id']) {
case 488:
$badges[] = 'contributor-supporting';
break;
case 489:
$badges[] = 'contributor-empowering';
break;
case 490:
$badges[] = 'contributor-sustaining';
break;
}
}
//find out if they are in the technology partners group
// $params = array('version' => 3, 'contact_id' => $contact_id);
// $groups=civicrm_api('GroupContact', 'get', $params);
// foreach($groups['values'] as $group){
// if($group['group_id']==267){
// $badges[]='technology-sponsor';
// break;
// }
// }
// Find out if they members and what type of members they are
// Ignore the Spark membership types (23, 24, 25)
$memberships = CRM_Core_DAO::executeQuery("SELECT id FROM civicrm_membership WHERE contact_id = %1 AND membership_type_id NOT IN (23,24,25) ORDER BY start_date DESC LIMIT 1", array(1 => array($contact_id, 'Integer')));
$memberships->fetch();
$params = array('version' => 3, 'membership_id' => $memberships->id);
$membership=civicrm_api('Membership', 'getsingle', $params);
$membershipTypeBadgeTranslate = [
1 => 'partner-supporting',
2 => 'partner-empowering',
3 => 'partner-sustaining',
12 => 'partner-sustaining',
13 => 'partner-empowering',
14 => 'partner-supporting',
11 => 'sponsor-sustaining',
10 => 'sponsor-empowering',
18 => 'sponsor-supporting',
];
// 1=new, 2=current, 3=grace, 5=pending
if (in_array($membership['status_id'], [1,2,3,5]) AND in_array($membership['membership_type_id'], array_keys($membershipTypeBadgeTranslate))) {
$partnerText = $membershipTypeBadgeTranslate[$membership['membership_type_id']];
if ($founder) {
$partnerText = $partnerText.'-founding';
}
$badges[] = $partnerText;
}
//find out if they are in the technology partners group
// $params = array('version' => 3, 'contact_id' => $contact_id);
// $groups=civicrm_api('GroupContact', 'get', $params);
// foreach($groups['values'] as $group){
// if($group['group_id']==267){
// $badges[]='technology-sponsor';
// break;
// }
// }
//find out if they members and what type of members they are
$memberships = CRM_Core_DAO::executeQuery("SELECT id FROM civicrm_membership WHERE contact_id = %1 ORDER BY start_date DESC LIMIT 1", array(1 => array($contact_id, 'Integer')));
$memberships->fetch();
$params = array('version' => 3, 'membership_id' => $memberships->id);
$membership=civicrm_api('Membership', 'getsingle', $params);
$membershipTypeBadgeTranslate=array(
1 => 'partner-supporting',
2 => 'partner-empowering',
3 => 'partner-sustaining',
12 => 'partner-sustaining',
13 => 'partner-empowering',
14 => 'partner-supporting',
11 => 'sponsor-sustaining',
10 => 'sponsor-empowering',
18 => 'sponsor-supporting',
);
if(in_array($membership['status_id'], array(1,2,3,5)) AND in_array($membership['membership_type_id'], array_keys($membershipTypeBadgeTranslate))){ //1=new, 2=current, 3=grace, 5=pending
$updateParams = array('version' => 3, 'contact_id' => $contact_id, 'custom_160' => json_encode($badges));
$result = civicrm_api('contact', 'update', $updateParams);
$result = civicrm_api('involvement', 'update', array('contact_id' => $contact_id, 'version' => 3));
// print_r($membershipTypeBadgeTranslate[$membership['membership_type_id']]);exit;
$partnerText = $membershipTypeBadgeTranslate[$membership['membership_type_id']];
if($founder){
$partnerText = $partnerText.'-founding';
}
$badges[]=$partnerText;
}
$updateParams = array('version' => 3, 'contact_id' => $contact_id, 'custom_160' => json_encode($badges));
$result=civicrm_api('contact', 'update', $updateParams);
$result=civicrm_api('involvement', 'update', array('contact_id' =>$contact_id, 'version' => 3));
return;
return;
}
function civicrm_org_providers_block_info(){
function civicrm_org_providers_block_info() {
$blocks['update_links'] = array(
'info' => 'Update profiles via user dashboard',
'cache' => DRUPAL_NO_CACHE
......@@ -108,8 +123,7 @@ function civicrm_org_providers_block_info(){
return $blocks;
}
function civicrm_org_providers_block_view($block_name){
function civicrm_org_providers_block_view($block_name) {
switch($block_name){
case 'update_links':
......@@ -181,7 +195,7 @@ function civicrm_org_providers_block_view($block_name){
}
}
function civicrm_org_providers_batch_update($contact_ids=array()){
function civicrm_org_providers_batch_update($contact_ids=array()) {
civicrm_initialize();
if(count($contact_ids)==0){
$contacts = CRM_Core_DAO::executeQuery("SELECT contact_id FROM civicrm_group_contact WHERE group_id IN (131,140,267) UNION SELECT contact_id FROM civicrm_membership");
......
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