Commit d6c88240 authored by Mathieu Lutfy's avatar Mathieu Lutfy Committed by Aegir user

#111 import code from civicrm_org_providers.

parent 713988c2
<?php
class CRM_Badgesync_BAO_Badgesync {
/**
* Updates the partner badges on the contact record, based on their membership.
*
* - They must have a contact_sub_type = Service_provider (why?)
* - It checks the "founding members" group
* - Also handles the "Active contributor", based on their smartgroups.
*/
public static function updateProvider($contact_id) {
// Check that the contact is a service provider contact subtype
$contact = civicrm_api3('Contact', 'getsingle', [
'contact_id' => $contact_id,
]);
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 are members and what type of members they are
// Ignore the Spark membership types (23, 24, 25)
$membership_id = CRM_Core_DAO::singleValueQuery("
SELECT m.id
FROM civicrm_membership m
INNER JOIN civicrm_membership_type mt ON (mt.id = m.membership_type_id AND mt.member_of_contact_id = 19332)
WHERE m.contact_id = %1
ORDER BY m.start_date
DESC LIMIT 1", [
1 => [$contact_id, 'Integer'],
]
);
if (!$membership_id) {
return;
}
$membership = civicrm_api3('Membership', 'getsingle', [
'membership_id' => $membership_id,
]);
$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;
}
$updateParams = array('version' => 3, 'contact_id' => $contact_id, 'custom_160' => json_encode($badges));
$result = civicrm_api3('contact', 'update', $updateParams);
$result = civicrm_api('involvement', 'update', array('contact_id' => $contact_id, 'version' => 3));
}
}
......@@ -136,15 +136,16 @@ function badgesync_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
* @param $op string operation create, edit, remove, etc
* @param $objectName string name of the object
* @param $groupId int object id such as group id
* @param $contactIds array reference to the object, in our case contact ids
* @param $objectRef array reference to the object, in our case contact ids
*/
function badgesync_civicrm_post($op, $objectName, $groupId, $contactIds) {
function badgesync_civicrm_post($op, $objectName, $groupId, &$objectRef) {
// step 1: trap when the contact is added to the group
// group id's specific to groups that we are interested.
// 488 supporting, 489 - enpowering, 490 sustaining
// 488 supporting, 489 - empowering, 490 sustaining
$groupIds = array(488, 489, 490);
if ($objectName == 'GroupContact' && in_array($groupId, $groupIds) && !empty($contactIds)) {
// FIXME: this might duplicate code in CRM_Badgesync_BAO_Badgesync::updateProvider().
if ($objectName == 'GroupContact' && in_array($groupId, $groupIds) && !empty($objectRef)) {
// get the badge image based on the group
// image format follows the pattern: civibadge-contributor-supporting.png
switch ($groupId) {
......@@ -162,7 +163,7 @@ function badgesync_civicrm_post($op, $objectName, $groupId, $contactIds) {
// step 2: Fetch the existing value custom data: Service Provide - Badges
// loop through contacts that are updated and fetch the existing value for badge
// custom_160 is the custom field id for badges
foreach ($contactIds as $contactId) {
foreach ($objectRef as $contactId) {
$result = civicrm_api3('Contact', 'get', array(
'sequential' => 1,
'return' => array("custom_160"),
......@@ -211,4 +212,7 @@ function badgesync_civicrm_post($op, $objectName, $groupId, $contactIds) {
}
}
if ($objectName == 'Membership' && in_array($op, ['edit', 'create', 'delete'])) {
CRM_Badgesync_BAO_Badgesync::updateProvider($objectRef->contact_id);
}
}
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