can now begin a membership; but cease needs a fix

master
Marek Isalski 6 years ago
parent f47111a85e
commit 29e742394b

@ -132,8 +132,129 @@ function fulcrm_membership_optin( $passport ) {
}
}
function fulcrm_membership_cease_person2membership( $p2m, $now = NULL ) {
if ( is_null( $now ) )
$now = new DateTime();
$tz = new DateTimeZone('UTC');
$now->setTimeZone( $tz );
$finish = $now->format( 'Y-m-d\TH:i:s.u\Z' );
if ( $p2m[ 'timescale' ] ) {
$api_data = fulcrm_apiv2_PATCH( $p2m[ 'url' ], $data = array( 'finish' => $finish ) );
if ( $api_data[ 'success' ] ) {
return true;
} else {
watchdog('fulcrm_membership', 'could not patch person2membership %id cease', array('%id'=>$p2m['id']), WATCHDOG_ERROR);
}
} else {
$timescale_data = fulcrm_apiv2_POST( 'timescale/', $data = array( 'finish' => $finish ) );
if ( $timescale_data[ 'success' ] ) {
$api_data = fulcrm_apiv2_PATCH( $p2m[ 'url' ], $data = array( 'timescale' => $timescale_data[ 'data' ][ 'url' ] ) );
if ( $api_data[ 'success' ] ) {
return true;
} else {
watchdog('fulcrm_membership', 'could not patch person2membership %id to add timescale %tid', array('%id'=>$p2m['id'],'%tid'=>$timescale_data['data']['id']), WATCHDOG_ERROR);
}
} else {
watchdog('fulcrm_membership', 'could not create timescale to cease membership %id', array('%id'=>$p2m['id']), WATCHDOG_ERROR);
}
}
return false;
}
function fulcrm_membership_begin_person2membership( $person_id, $membership_id, $now = NULL ) {
if ( is_null( $now ) )
$now = new DateTime();
$tz = new DateTimeZone('UTC');
$now->setTimeZone( $tz );
$start = $now->format( 'Y-m-d\TH:i:s.u\Z' );
$timescale_data = fulcrm_apiv2_POST( 'timescale/', $data = array( 'start' => $start ) );
if ( $timescale_data[ 'success' ] ) {
$api_data = fulcrm_apiv2_POST( 'person2membership/', $data = array( 'person' => '/api/v2/person/' . $person_id . '/',
'membership' => '/api/v2/membership/' . $membership_id . '/',
'timescale' => $timescale_data[ 'data' ][ 'url' ],
) );
if ( $api_data[ 'success' ] ) {
return true;
} else {
watchdog('fulcrm_membership', 'could not post person2membership to create membership %mid for %pid', array('%mid'=>$membership_id,'%pid'=>$person_id), WATCHDOG_ERROR);
}
} else {
watchdog('fulcrm_membership', 'could not post timescale to create membership %mid for %pid', array('%mid'=>$membership_id,'%pid'=>$person_id), WATCHDOG_ERROR);
}
}
function fulcrm_membership_self_set_form_submit( $form, &$form_state ) {
global $user;
$person_id = fulcrm_webhook_get_pk_for_entity_type( 'user', $user->uid, 'person' );
if ( $person_id ) {
$person_data = fulcrm_apiv2_GET( 'person/' . $person_id . '/',
$query = array( 'expand' => implode( ',', array( 'person2memberships',
'person2memberships.membership',
'person2memberships.timescale' ) ) ) );
if ( $person_data[ 'success' ] ) {
$membership_data = fulcrm_apiv2_GET( 'membership/',
$query = array( 'page_size' => 1000,
) );
if ( $membership_data[ 'success' ] ) {
$membership_name = array();
foreach ( $membership_data[ 'data' ][ 'results' ] as $membership )
$membership_name[ $membership[ 'id' ] ] = $membership[ 'name' ];
$possible_ids = variable_get( 'fulcrm_membership_self_set_membership_ids', array() );
$current_map = array();
$new_map = array();
foreach ( $possible_ids as $membership_id => $is_possible ) {
if ( $is_possible ) {
$current_map[ $membership_id ] = false;
$new_map[ $membership_id ] = array_key_exists( $membership_id, $form_state[ 'values' ][ 'memberships' ] ) ? $form_state[ 'values' ][ 'memberships' ][ $membership_id ] : 0;
}
}
foreach ( $person_data[ 'data' ][ 'person2memberships' ] as $p2m ) {
$membership_id = $p2m[ 'membership' ][ 'id' ];
if ( array_key_exists( $membership_id, $possible_ids ) && $possible_ids[ $membership_id ] ) {
if ( fulcrm_membership_person2membership_is_current( $p2m ) ) {
$current_map[ $membership_id ] = true;
}
}
}
// figure out the differences
foreach ( $possible_ids as $membership_id => $is_possible ) {
if ( $is_possible ) {
if ( $current_map[ $membership_id ] && $new_map[ $membership_id ] ) {
// do nothing
} elseif ( !$current_map[ $membership_id ] && !$new_map[ $membership_id ] ) {
// do nothing
} elseif ( $current_map[ $membership_id ] && !$new_map[ $membership_id ] ) {
// REMOVE MEMBERSHIP $membership_id
foreach ( $person_data[ 'data' ][ 'person2memberships' ] as $p2m ) {
if ( $p2m[ 'membership' ][ 'id' ] == $membership_id ) {
if ( fulcrm_membership_person2membership_is_current( $p2m ) ) {
if ( fulcrm_membership_cease_person2membership( $p2m ) )
drupal_set_message( t('Ceased your "' . $p2m[ 'membership' ][ 'name' ] . '" membership.'), 'status', FALSE );
}
}
}
} elseif ( !$current_map[ $membership_id ] && $new_map[ $membership_id ] ) {
// ADD MEMBERSHIP $membership_id
if ( fulcrm_membership_begin_person2membership( $person_id, $membership_id ) ) {
drupal_set_message( t('You have begun your "' . $membership_name[ $membership_id ] . '" membership.'), 'status', FALSE );
}
}
}
}
} else {
drupal_set_message( t('There was a problem fetching membership information.'), 'error', FALSE );
}
} else {
drupal_set_message( t('There was a problem fetching your subscription information.'), 'error', FALSE );
}
}
}
function fulcrm_membership_self_set_form( $form, &$form_state ) {
@ -141,7 +262,7 @@ function fulcrm_membership_self_set_form( $form, &$form_state ) {
$form[ 'title' ] = array( '#type' => 'item',
'#title' => 'Subscription Preferences',
'#description' => '',
'#description' => 'This page lets you choose which subscriptions you will receive. Simply tick or untick the boxes as required.',
);
$person_id = fulcrm_webhook_get_pk_for_entity_type( 'user', $user->uid, 'person' );
@ -183,7 +304,7 @@ function fulcrm_membership_self_set_form( $form, &$form_state ) {
}
$form[ 'memberships' ] = array( '#type' => 'checkboxes',
'#title' => 'Subscriptions',
'#title' => 'Your Current Subscriptions',
'#options' => $options,
'#default_value' => $membership_map,
);
@ -196,11 +317,13 @@ function fulcrm_membership_self_set_form( $form, &$form_state ) {
}
$form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'cancel' ] = array( '#markup' => l(t('Cancel'), 'user' ) );
$form[ 'actions' ][ 'save' ] = array( '#type' => 'submit',
'#value' => t('Save'),
'#submit' => array( 'fulcrm_membership_self_set_form_submit' ),
);
return $form;
}

Loading…
Cancel
Save