@ -2,6 +2,14 @@
function fulcrm_membership_menu() {
function fulcrm_membership_menu() {
$items = array();
$items = array();
$items[ 'admin/config/services/fulcrm/membership' ] = array( 'page callback' => 'fulcrm_membership_admin',
'file' => 'fulcrm_membership.admin.inc',
'title' => 'fulcrm Membership',
'description' => 'Configure Membership integration with fulcrm.org.',
'access callback' => 'user_access',
'access arguments' => array('administer fulcrm membership'),
);
$items[ 'user/fulcrm/optin/%' ] = array( 'page callback' => 'fulcrm_membership_optin',
$items[ 'user/fulcrm/optin/%' ] = array( 'page callback' => 'fulcrm_membership_optin',
'page arguments' => array(3),
'page arguments' => array(3),
'title' => 'Subscription Opt-In',
'title' => 'Subscription Opt-In',
@ -9,6 +17,12 @@ function fulcrm_membership_menu() {
'access callback' => 'user_access',
'access callback' => 'user_access',
'access arguments' => array('access content'),
'access arguments' => array('access content'),
);
);
$items[ 'user/fulcrm/membership' ] = array( 'page callback' => 'fulcrm_membership_self_set',
'title' => 'Choose Your Settings',
'description' => 'Opt-In Membership Settings.',
'access callback' => 'user_access',
'access arguments' => array('access content'),
);
return $items;
return $items;
}
}
@ -18,8 +32,40 @@ function fulcrm_membership_optin_form_submit( $form, &$form_state ) {
if ( $p2m_id ) {
if ( $p2m_id ) {
$api_data = fulcrm_apiv2_PATCH( 'person2membership/' . $p2m_id . '/',
$api_data = fulcrm_apiv2_PATCH( 'person2membership/' . $p2m_id . '/',
$data = array( 'archived' => NULL ),
$data = array( 'archived' => NULL ),
$query = array( 'expand' => implode( ',', array( 'person', 'membership' ) ) ) );
$query = array( 'expand' => implode( ',', array( 'person', 'membership', 'timescale' ) ) ) );
if ( $api_data[ 'success' ] ) {
if ( $api_data[ 'success' ] ) {
$api1_data = fulcrm_apiv2_POST( 'n/',
$data = array( 'note' => 'Subscription added through opt-in via website.',
'person' => $api_data[ 'data' ][ 'person' ][ 'url' ],
'content_object' => $api_data[ 'data' ][ 'url' ],
) );
$now = new DateTime();
$now->setTimestamp( time() );
$now->setTimeZone( new DateTimeZone('GMT') );
$now_str = $now->format('Y-m-d\TH:i:s\Z');
if ( $api_data[ 'data' ][ 'timescale' ] ) {
$api3_data = fulcrm_apiv2_PATCH( $api_data[ 'data' ][ 'timescale' ][ 'url' ],
$data = array( 'start' => $now_str ) );
if ( $api3_data[ 'success' ] ) {
} else {
drupal_set_message( t('There was a problem confirming your subscription (update start date).'), 'error', FALSE );
}
} else {
$api3_data = fulcrm_apiv2_POST( 'timescale/',
$data = array( 'start' => $now_str ) );
if ( $api3_data[ 'success' ] ) {
$api4_data = fulcrm_apiv2_PATCH( 'person2membership/' . $p2m_id . '/',
$data = array( 'timescale' => $api3_data[ 'data' ][ 'url' ] ) );
if ( $api4_data[ 'success' ] ) {
} else {
drupal_set_message( t('There was a problem confirming your subscription (associate start date).'), 'error', FALSE );
}
} else {
drupal_set_message( t('There was a problem confirming your subscription (create start date).'), 'error', FALSE );
}
}
if ( $api_data[ 'data' ][ 'person' ][ 'archived' ] ) {
if ( $api_data[ 'data' ][ 'person' ][ 'archived' ] ) {
$api2_data = fulcrm_apiv2_PATCH( $api_data[ 'data' ][ 'person' ][ 'url' ],
$api2_data = fulcrm_apiv2_PATCH( $api_data[ 'data' ][ 'person' ][ 'url' ],
$data = array( 'archived' => NULL ) );
$data = array( 'archived' => NULL ) );
@ -82,3 +128,251 @@ function fulcrm_membership_optin( $passport ) {
return drupal_get_form( 'fulcrm_membership_optin_form', $api_data[ 'data' ][ 'results' ] );
return drupal_get_form( 'fulcrm_membership_optin_form', $api_data[ 'data' ][ 'results' ] );
}
}
}
}
function fulcrm_membership_person2membership_is_current( $p2m ) {
if ( $p2m[ 'timescale' ] ) {
$tz = new DateTimeZone('UTC');
$now = new DateTime();
$ts_start = $p2m[ 'timescale' ][ 'start' ];
if ( $ts_start ) {
$start = DateTime::createFromFormat( 'Y-m-d\TH:i:s.u\Z', $ts_start, $timezone = $tz );
$start = $start ? $start : DateTime::createFromFormat( 'Y-m-d\TH:i:s\Z', $ts_start, $timezone = $tz );
} else {
$start = NULL;
}
$ts_finish = $p2m[ 'timescale' ][ 'finish' ];
if ( $ts_finish ) {
$finish = DateTime::createFromFormat( 'Y-m-d\TH:i:s.u\Z', $ts_finish, $timezone = $tz );
$finish = $finish ? $finish : DateTime::createFromFormat( 'Y-m-d\TH:i:s\Z', $ts_finish, $timezone = $tz );
} else {
$finish = NULL;
}
return ( ( is_null( $start ) || ( $start <= $now ) ) && ( is_null( $finish ) || ( $finish > $now ) ) );
} else {
return true;
}
}
function fulcrm_membership_can_self_set_person2membership( $p2m ) {
if ( array_key_exists( 'membership', $p2m ) ) {
if ( is_array( $p2m[ 'membership' ] ) )
$membership_id = $p2m[ 'membership' ][ 'id' ];
else
$membership_id = fulcrm_apiv2_url_to_pk( $p2m[ 'membership' ] );
$self_set_membership_ids = variable_get( 'fulcrm_membership_self_set_membership_ids', array() );
if ( array_key_exists( $membership_id, $self_set_membership_ids ) ) {
if ( $self_set_membership_ids[ $membership_id ] )
return true;
}
}
return false;
}
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[ 'timescale' ][ '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 ) {
global $user;
$form[ 'title' ] = array( '#type' => 'item',
'#title' => 'Subscription Preferences',
'#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' );
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() );
$membership_map = array();
$options = array();
foreach ( $possible_ids as $membership_id => $is_possible ) {
if ( $is_possible ) {
$membership_map[ $membership_id ] = 0;
if ( array_key_exists( $membership_id, $membership_name ) )
$options[ $membership_id ] = $membership_name[ $membership_id ];
}
}
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 ) ) {
$membership_map[ $membership_id ] = $membership_id;
}
}
}
$form[ 'memberships' ] = array( '#type' => 'checkboxes',
'#title' => 'Your Current Subscriptions',
'#options' => $options,
'#default_value' => $membership_map,
);
} 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 );
}
}
$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;
}
function fulcrm_membership_self_set() {
//if ( user_is_logged_in() ) {
return drupal_get_form( 'fulcrm_membership_self_set_form' );
//} else {
//drupal_goto( 'user?destination=user/fulcrm/membership' );
//}
}