add page for users to edit their memberships

master
Marek Isalski 6 years ago
parent e3375f0159
commit f47111a85e

@ -25,9 +25,42 @@ function fulcrm_membership_menu() {
'access callback' => 'user_access',
'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;
}
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_optin_form_submit( $form, &$form_state ) {
foreach ( $form_state[ 'values' ][ 'person2memberships' ] as $p2m_id ) {
$p2m_id = fulcrm_crypto_get_object_id_form_value( 'person2membership-optin', $p2m_id );
@ -98,3 +131,83 @@ function fulcrm_membership_optin( $passport ) {
return drupal_get_form( 'fulcrm_membership_optin_form', $api_data[ 'data' ][ 'results' ] );
}
}
function fulcrm_membership_self_set_form_submit( $form, &$form_state ) {
}
function fulcrm_membership_self_set_form( $form, &$form_state ) {
global $user;
$form[ 'title' ] = array( '#type' => 'item',
'#title' => 'Subscription Preferences',
'#description' => '',
);
$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' => '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' ][ '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' );
//}
}

Loading…
Cancel
Save