Skip to content

Commit

Permalink
Support custom data and hooks for MembershipType entity
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed May 6, 2018
1 parent f62fff1 commit acfc2f2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CRM/Core/BAO/CustomGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,9 @@ public static function mapTableName($table) {
case 'Membership':
return 'civicrm_membership';

case 'MembershipType':
return 'civicrm_membership_type';

case 'Participant':
case 'ParticipantRole':
case 'ParticipantEventName':
Expand Down
1 change: 1 addition & 0 deletions CRM/Core/SelectValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ public static function customGroupExtends() {
'ContributionRecur' => ts('Recurring Contributions'),
'Group' => ts('Groups'),
'Membership' => ts('Memberships'),
'MembershipType' => ts('Membership Types'),
'Event' => ts('Events'),
'Participant' => ts('Participants'),
'ParticipantRole' => ts('Participants (Role)'),
Expand Down
42 changes: 28 additions & 14 deletions CRM/Member/BAO/MembershipType.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,24 @@ public static function setIsActive($id, $is_active) {
* @param array $ids
* Array contains the id (deprecated).
*
*
* @return object
* @return \CRM_Member_DAO_MembershipType
* @throws \CiviCRM_API3_Exception
*/
public static function add(&$params, $ids = array()) {
$id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('membershipType', $ids));
if (!$id) {
// DEPRECATED Check if membershipType ID was passed in via $ids
if (empty($params['id'])) {
if (isset($ids['membershipType'])) {
Civi::log()->warning('Deprecated: Passing membershipType by $ids array in CRM_Member_BAO_MembershipType::add');
}
$params['id'] = CRM_Utils_Array::value('membershipType', $ids);
}

$hook = empty($params['id']) ? 'create' : 'edit';
CRM_Utils_Hook::pre($hook, 'MembershipType', CRM_Utils_Array::value('id', $params), $params);

$membershipTypeId = CRM_Utils_Array::value('id', $params);

if (!$membershipTypeId) {
if (!isset($params['is_active'])) {
// do we need this?
$params['is_active'] = FALSE;
Expand All @@ -106,28 +118,30 @@ public static function add(&$params, $ids = array()) {
}
}

// action is taken depending upon the mode
$membershipType = new CRM_Member_DAO_MembershipType();
$membershipType->copyValues($params);
$membershipType->id = $id;

// $previousID is the old organization id for membership type i.e 'member_of_contact_id'. This is used when an organization is changed.
$previousID = NULL;
if ($id) {
$previousID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $id, 'member_of_contact_id');
if ($membershipTypeId) {
$previousID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $membershipTypeId, 'member_of_contact_id');
}

// action is taken depending upon the mode
$membershipType = new CRM_Member_DAO_MembershipType();
$membershipType->copyValues($params);
$membershipType->save();
if ($id) {

if ($membershipTypeId) {
// on update we may need to retrieve some details for the price field function - otherwise we get e-notices on attempts to retrieve
// name etc - the presence of previous id tells us this is an update
$params = array_merge(civicrm_api3('membership_type', 'getsingle', array('id' => $membershipType->id)), $params);
}
self::createMembershipPriceField($params, $previousID, $membershipType->id);
// update all price field value for quick config when membership type is set CRM-11718
if ($id) {
self::updateAllPriceFieldValue($id, $params);
if ($membershipTypeId) {
self::updateAllPriceFieldValue($membershipTypeId, $params);
}

CRM_Utils_Hook::post($hook, 'MembershipType', $membershipType->id, $membershipType);

self::flush();
return $membershipType;
}
Expand Down

0 comments on commit acfc2f2

Please sign in to comment.