From a89aa9d62a6d13b267fffd7adcc8605362b0d8be Mon Sep 17 00:00:00 2001 From: Marek Isalski Date: Fri, 1 Feb 2019 10:40:09 +0000 Subject: [PATCH] some work towards getting donations to be buyable --- fulcrm_shoppingcart.admin.inc | 2 +- fulcrm_shoppingcart.module | 252 +++++++++++++++++++++++++--------- 2 files changed, 185 insertions(+), 69 deletions(-) diff --git a/fulcrm_shoppingcart.admin.inc b/fulcrm_shoppingcart.admin.inc index 1a42275..d2222c7 100644 --- a/fulcrm_shoppingcart.admin.inc +++ b/fulcrm_shoppingcart.admin.inc @@ -65,7 +65,7 @@ function fulcrm_shoppingcart_admin_form( $form, &$form_state ) { if ( $product_data[ 'success' ] ) { if ( $product_data[ 'data' ][ 'results' ] ) { foreach ( $product_data[ 'data' ][ 'results' ] as $product ) { - $products[ $product[ 'url' ] ] = $product[ 'name' ]; + $products[ $product[ 'id' ] ] = $product[ 'name' ]; } } } else { diff --git a/fulcrm_shoppingcart.module b/fulcrm_shoppingcart.module index 45443c5..25d5e0e 100644 --- a/fulcrm_shoppingcart.module +++ b/fulcrm_shoppingcart.module @@ -81,6 +81,11 @@ function fulcrm_shoppingcart_menu() { 'access callback' => 'user_access', 'access arguments' => array('access content'), ); + $items[ 'fulcrm/donate' ] = array( 'page callback' => 'fulcrm_shoppingcart_donate', + 'type' => MENU_CALLBACK, + 'access callback' => 'user_access', + 'access arguments' => array('access content'), + ); $items[ 'fulcrm/checkout' ] = array( 'page callback' => 'fulcrm_shoppingcart_checkout', 'type' => MENU_CALLBACK, 'access callback' => 'user_access', @@ -677,6 +682,95 @@ function fulcrm_shoppingcart_buy_eventbookingtype( $person, $eventbookingtype, $ return drupal_render( $form ); } +function fulcrm_shoppingcart_buy_donation_product_form_submit( $form, &$form_state ) { + // fulcrm_shoppingcart_buy_generic_product_form_submit( $form, $form_state ); + + $product_id = $form_state[ 'fulcrm_shoppingcart' ][ 'product' ][ 'id' ]; + + $person_uuid = NULL; + if ( array_key_exists( 'person', $form_state[ 'fulcrm_shoppingcart' ] ) && + array_key_exists( 'uuid', $form_state[ 'fulcrm_shoppingcart' ][ 'person' ] ) ) + $person_uuid = $form_state[ 'fulcrm_shoppingcart' ][ 'person' ][ 'uuid' ]; + + $cart = fulcrm_shoppingcart_get_session_cart(); + + if ( is_array( $cart ) ) { + if ( $person_uuid ) { + $patch = FALSE; + if ( !array_key_exists( 'fulcrm', $cart[ 'd' ] ) || + !array_key_exists( 'person', $cart[ 'd' ][ 'fulcrm' ] ) || + !array_key_exists( $person_uuid, $cart[ 'd' ][ 'fulcrm' ][ 'person' ] ) ) { + $cart[ 'd' ][ 'fulcrm' ][ 'person' ][ $person_uuid ] = array( 'uuid' => $person_uuid ); + $patch = TRUE; + } + if ( !array_key_exists( 'transaction', $cart[ 'd' ][ 'fulcrm' ] ) || + !array_key_exists( 'person', $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ] ) ) { + $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ][ 'person' ] = $person_uuid; + $patch = TRUE; + } + if ( $patch ) { + $api_data = fulcrm_apiv2_PATCH( $cart[ 'url' ], array( 'd' => $cart[ 'd' ] ), + $query = array( 'expand' => 'd' ) ); + fulcrm_shoppingcart_invalidate_cache(); + } + } + + $shoppingitem_name = ( $form_state[ 'fulcrm_shoppingcart' ][ 'product' ][ 'name' ] ); + + $item = array( 'shoppingcart' => $cart[ 'url' ], + 'product' => fulcrm_apiv2_make_url( 'product', $product_id ), + 'quantity' => 1.0, + 'd' => array( 'name' => $shoppingitem_name, + ), + ); + + $api_data = fulcrm_apiv2_POST( 'shoppingitem/', $item, $query = array( 'expand' => 'd' ) ); + fulcrm_shoppingcart_invalidate_cache(); + if ( $api_data[ 'success' ] ) { + drupal_set_message( t('Added "%item" to shopping cart.', array( '%item' => $item[ 'd' ][ 'name' ] ) ), 'status' ); + $form_state[ 'redirect' ] = 'fulcrm/cart'; + } else { + drupal_set_message( t('There was a problem adding your item to the shopping cart.'), 'error' ); + } + } else { + drupal_set_message( t('There was a problem adding your item to the shopping cart.'), 'error' ); + } +} + +function fulcrm_shoppingcart_buy_donation_product_form( $form, &$form_state, $person, $product ) { + $form_state[ 'fulcrm_shoppingcart' ][ 'person' ] = $person; + $form_state[ 'fulcrm_shoppingcart' ][ 'product' ] = $product; + + $form = array(); + + $form[ 'title_product' ] = array( '#type' => 'markup', + '#markup' => '

' . check_plain( $product[ 'name' ] ) . '

', + ); + + drupal_set_title( check_plain( $product[ 'data' ][ 'name' ] ) ); + + $form[ 'actions' ] = array( '#type' => 'actions' ); + $form[ 'actions' ][ 'save' ] = array( '#type' => 'submit', + '#value' => t('Add to Cart'), + '#submit' => array( 'fulcrm_shoppingcart_buy_donation_product_form_submit' ), + ); + + return $form; +} + +function fulcrm_shoppingcart_buy_donation( $person, $product ) { + $person_id = NULL; + if ( array_key_exists( 'id', $person ) ) + $person_id = $person[ 'id' ]; + + $fulcrm_shoppingcart_buy_eventbookingtype_form = drupal_get_form( 'fulcrm_shoppingcart_buy_donation_form', $person, $product ); + + return theme( 'fulcrm_shoppingcart_buy_donation', array( 'fulcrm_shoppingcart_buy_donation_form' => $fulcrm_shoppingcart_buy_donation_form ) ); + + $form = drupal_get_form( 'fulcrm_shoppingcart_buy_eventbookingtype_form', $person, $eventbookingtype, $product ); + return drupal_render( $form ); +} + function fulcrm_shoppingcart_buy_generic_product_form_submit( $form, &$form_state ) { $product_id = $form_state[ 'fulcrm_shoppingcart' ][ 'product' ][ 'id' ]; @@ -751,84 +845,97 @@ function fulcrm_shoppingcart_buy_generic_product_form( $form, &$form_state, $per return $form; } -function fulcrm_shoppingcart_buy( $product_id, $hmac ) { - global $user; +function _fulcrm_shoppingcart_buy( $product_id ) { + $product_data = fulcrm_apiv2_GET( 'product/' . $product_id . '/', + $query = array( 'expand' => implode( ',', array( 'content_object', + 'selector_object', + 'person_ddatacollection', + 'organisation_ddatacollection', + 'thing_ddatacollection', + 'transaction_ddatacollection', + 'lineitem_ddatacollection', + ) ) ) ); + if ( !$product_data[ 'success' ] ) { + return 'error while fetching information about this product!'; + } - if ( fulcrm_crypto_check_object_id_hmac( 'product', $product_id, $hmac ) ) { - $product_data = fulcrm_apiv2_GET( 'product/' . $product_id . '/', - $query = array( 'expand' => implode( ',', array( 'content_object', - 'selector_object', - 'person_ddatacollection', - 'organisation_ddatacollection', - 'thing_ddatacollection', - 'transaction_ddatacollection', - 'lineitem_ddatacollection', - ) ) ) ); - if ( !$product_data[ 'success' ] ) { - return 'error while fetching information about this product!'; - } + if ( user_is_logged_in() ) { + $person_id = fulcrm_webhook_get_pk_for_entity_type( 'user', $user->uid, 'person' ); + } else { + $person_id = null; + } - if ( user_is_logged_in() ) { - $person_id = fulcrm_webhook_get_pk_for_entity_type( 'user', $user->uid, 'person' ); - } else { - $person_id = null; + if ( $person_id ) { + $person_data = fulcrm_apiv2_GET( 'person/' . $person_id . '/', + $query = array( 'expand' => implode( ',', array( 'd', + 'related_from_person2persons', + 'related_from_person2persons.related_from', + 'related_from_person2persons.related_from.d', + 'related_from_person2persons.relationship', + 'related_to_person2persons', + 'related_to_person2persons.related_to', + 'related_to_person2persons.related_to.d', + 'related_to_person2persons.relationship', + ) ) ) ); + if ( !$person_data[ 'success' ] ) { + return 'error while fetching your details!'; } + } else { + $person_data = array( 'data' => array( 'uuid' => NULL, // initialise as NULL so first item added takes over + 'name' => 'website visitor', + 'd' => array( 'name' => 'website visitor', + ), + ) ); + } - if ( $person_id ) { - $person_data = fulcrm_apiv2_GET( 'person/' . $person_id . '/', - $query = array( 'expand' => implode( ',', array( 'd', - 'related_from_person2persons', - 'related_from_person2persons.related_from', - 'related_from_person2persons.related_from.d', - 'related_from_person2persons.relationship', - 'related_to_person2persons', - 'related_to_person2persons.related_to', - 'related_to_person2persons.related_to.d', - 'related_to_person2persons.relationship', - ) ) ) ); - if ( !$person_data[ 'success' ] ) { - return 'error while fetching your details!'; - } - } else { - $person_data = array( 'data' => array( 'uuid' => NULL, // initialise as NULL so first item added takes over - 'name' => 'website visitor', - 'd' => array( 'name' => 'website visitor', - ), - ) ); + $cart = fulcrm_shoppingcart_get_session_cart( $create = true ); + if ( !$cart ) { + return 'error while fetching the contents of your shopping cart!'; + } + + $hook_results = module_invoke_all( 'fulcrm_shoppingcart_pre_add_to_cart', $cart, $person_data[ 'data' ], $product_data[ 'data' ] ); + foreach ( $hook_results as $hook_result ) + return $hook_result; + + if ( variable_get( 'fulcrm_shoppingcart_donations', false ) ) { + switch ( $product_data[ 'data' ][ 'id' ] ) { + case variable_set( 'fulcrm_shoppingcart_donation_checkout_product', '' ): + case variable_set( 'fulcrm_shoppingcart_donation_solicited_product', '' ): + return fulcrm_shoppingcart_buy_donation( $person_data[ 'data' ], $product_data[ 'data' ] ); } + } - $cart = fulcrm_shoppingcart_get_session_cart( $create = true ); - if ( !$cart ) { - return 'error while fetching the contents of your shopping cart!'; + switch ( $product_data[ 'data' ][ 'content_object' ][ 'url' ] ? fulcrm_apiv2_url_to_type( $product_data[ 'data' ][ 'content_object' ][ 'url' ] ) : null ) { + case 'eventbookingtype': + $eventbookingtype_data = fulcrm_apiv2_GET( $product_data[ 'data' ][ 'content_object' ][ 'url' ], + $query = array( 'expand' => implode( ',', array( 'event', + 'person_ddatacollection', + 'eventbooking_ddatacollection', + 'eventbookingtypeslots', + 'eventbookingtypeslots.eventslot', + 'event', + 'event.eventparts', + 'event.eventparts.eventslots' ) ) ) ); + if ( !$eventbookingtype_data[ 'success' ] ) { + return 'error while fetching information about your booking!'; } - $hook_results = module_invoke_all( 'fulcrm_shoppingcart_pre_add_to_cart', $cart, $person_data[ 'data' ], $product_data[ 'data' ] ); - foreach ( $hook_results as $hook_result ) - return $hook_result; + return fulcrm_shoppingcart_buy_eventbookingtype( $person_data[ 'data' ], $eventbookingtype_data[ 'data' ], $product_data[ 'data' ] ); + break; + case null: + return drupal_get_form( 'fulcrm_shoppingcart_buy_generic_product_form', $person_data[ 'data' ], $product_data[ 'data' ] ); + default: + return 'cannot handle these products yet'; + } + return 'ok!'; - switch ( $product_data[ 'data' ][ 'content_object' ][ 'url' ] ? fulcrm_apiv2_url_to_type( $product_data[ 'data' ][ 'content_object' ][ 'url' ] ) : null ) { - case 'eventbookingtype': - $eventbookingtype_data = fulcrm_apiv2_GET( $product_data[ 'data' ][ 'content_object' ][ 'url' ], - $query = array( 'expand' => implode( ',', array( 'event', - 'person_ddatacollection', - 'eventbooking_ddatacollection', - 'eventbookingtypeslots', - 'eventbookingtypeslots.eventslot', - 'event', - 'event.eventparts', - 'event.eventparts.eventslots' ) ) ) ); - if ( !$eventbookingtype_data[ 'success' ] ) { - return 'error while fetching information about your booking!'; - } +} - return fulcrm_shoppingcart_buy_eventbookingtype( $person_data[ 'data' ], $eventbookingtype_data[ 'data' ], $product_data[ 'data' ] ); - break; - case null: - return drupal_get_form( 'fulcrm_shoppingcart_buy_generic_product_form', $person_data[ 'data' ], $product_data[ 'data' ] ); - default: - return 'cannot handle these products yet'; - } - return 'ok!'; +function fulcrm_shoppingcart_buy( $product_id, $hmac ) { + global $user; + + if ( fulcrm_crypto_check_object_id_hmac( 'product', $product_id, $hmac ) ) { + return _fulcrm_shoppingcart_buy( $product_id ); } else { drupal_not_found(); } @@ -963,6 +1070,15 @@ function fulcrm_shoppingcart_coupon( $transaction_id, $hmac ) { } } +function fulcrm_shoppingcart_donate() { + if ( variable_get( 'fulcrm_shoppingcart_donations', false ) ) { + $product_id = variable_get( 'fulcrm_shoppingcart_donation_solicited_product', null ); + if ( $product_id ) + return _fulcrm_shoppingcart_buy( $product_id ); + } + return drupal_goto( 'fulcrm/checkout' ); +} + function fulcrm_shoppingcart_checkout() { $cart = fulcrm_shoppingcart_get_session_cart( false ); if ( $cart && array_key_exists( 'url', $cart ) ) {