now starting to sync to fulcrm

master
Marek Isalski 7 years ago
parent 889d625e37
commit 361c76d024

@ -41,6 +41,38 @@ function fulcrm_campaign_admin_campaign_settings_form_submit( $form, &$form_stat
drupal_set_message( t( 'Campaign information updated.' ) ); drupal_set_message( t( 'Campaign information updated.' ) );
} }
function fulcrm_campaign_admin_campaign_settings_form_submit_template( $form, &$form_state ) {
if ( $fcid = intval( $form_state[ 'values' ][ 'fcid' ] ) ) {
$tid = intval( $form_state[ 'values' ][ 'template' ] );
fulcrm_campaign_create_campaign_variables_from_template( $fcid, $tid );
drupal_set_message( 'Template variables preloaded.' );
}
}
function fulcrm_campaign_admin_campaign_settings_form_submit_save( $form, &$form_state ) {
if ( $fcid = intval( $form_state[ 'values' ][ 'fcid' ] ) ) {
foreach ( $form_state[ 'values' ] as $name => $value ) {
if ( substr( $name , 0, 9 ) === 'variable_' )
db_update( 'fulcrm_campaign_variable_value' )
->condition( 'fcid', $fcid )
->condition( 'name', substr( $name, 9 ) )
->fields( array( 'value' => $value ) )
->execute();
}
module_invoke_all( 'fulcrm_campaign_prepared', $fcid );
drupal_set_message( 'Variables saved.' );
}
}
function fulcrm_campaign_admin_campaign_settings_form_submit_new( $form, &$form_state ) {
if ( $fcid = intval( $form_state[ 'values' ][ 'fcid' ] ) ) {
db_insert( 'fulcrm_campaign_variable_value' )->fields( array( 'fcid' => $fcid,
'name' => $form_state[ 'values' ][ 'new' ],
'value' => '' ) )->execute();
drupal_set_message( 'Variable added.' );
}
}
function fulcrm_campaign_admin_campaign_settings_form( $form, &$form_state, $fcid ) { function fulcrm_campaign_admin_campaign_settings_form( $form, &$form_state, $fcid ) {
$fcid = intval( $fcid ); $fcid = intval( $fcid );
$query = db_select( 'fulcrm_campaign' )->fields( 'fulcrm_campaign', array( 'fcid', 'name', 'subject' ) )->condition( 'fulcrm_campaign.fcid', $fcid ); $query = db_select( 'fulcrm_campaign' )->fields( 'fulcrm_campaign', array( 'fcid', 'name', 'subject' ) )->condition( 'fulcrm_campaign.fcid', $fcid );
@ -59,6 +91,63 @@ function fulcrm_campaign_admin_campaign_settings_form( $form, &$form_state, $fci
'#default_value' => ( ( array_key_exists( 'values', $form_state ) && $form_state[ 'values' ][ 'subject' ] ) ? $form_state[ 'values' ][ 'subject' ] : $campaign->subject ), '#default_value' => ( ( array_key_exists( 'values', $form_state ) && $form_state[ 'values' ][ 'subject' ] ) ? $form_state[ 'values' ][ 'subject' ] : $campaign->subject ),
); );
$form[ 'variables' ] = array( '#type' => 'fieldset',
'#title' => 'Campaign Variables',
);
$templates = array( 0 => '' );
$query = db_select( 'fulcrm_campaign_variable_template' )->fields( 'fulcrm_campaign_variable_template', array( 'tid', 'name' ) )->orderBy( 'fulcrm_campaign_variable_template.name' );
foreach ( $query->execute() as $row ) {
$templates[ $row->tid ] = check_plain( $row->name );
}
$form[ 'variables' ][ 'template' ] = array( '#type' => 'select',
'#title' => 'Load from template',
'#options' => $templates,
);
$form[ 'variables' ][ 'template_submit' ] = array( '#type' => 'submit',
'#value' => t('Use Template'),
'#description' => t('Preload variables from this template.'),
'#submit' => array( 'fulcrm_campaign_admin_campaign_settings_form_submit_template' ), // If no javascript action.
/*
'#ajax' => array(
'callback' => 'poll_choice_js',
'wrapper' => 'poll-choices',
'method' => 'replace',
'effect' => 'fade',
),
*/
);
$form[ 'variables' ][ 'values' ] = array( '#type' => 'fieldset',
'#title' => 'Current Values',
);
$query = db_select( 'fulcrm_campaign_variable_value' )
->fields( 'fulcrm_campaign_variable_value', array( 'name', 'value' ) )
->condition( 'fulcrm_campaign_variable_value.fcid', $fcid )
->orderBy( 'fulcrm_campaign_variable_value.name' );
foreach ( $query->execute() as $row ) {
$form[ 'variables' ][ 'values' ][ 'variable_' . $row->name ] = array( '#type' => 'textfield',
'#title' => check_plain( $row->name ),
'#default_value' => $row->value,
);
}
$form[ 'variables' ][ 'values' ][ 'save' ] = array( '#type' => 'submit',
'#value' => 'Save Variables',
'#submit' => array( 'fulcrm_campaign_admin_campaign_settings_form_submit_save' ),
);
$form[ 'variables' ][ 'new' ] = array( '#type' => 'textfield',
'#title' => 'New Variable Name',
);
$form[ 'variables' ][ 'new_submit' ] = array( '#type' => 'submit',
'#value' => t('Add Variable'),
'#description' => t('Add a new variable to this template.'),
'#submit' => array( 'fulcrm_campaign_admin_campaign_settings_form_submit_new' ),
);
$form[ 'actions' ] = array( '#type' => 'actions' ); $form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit', $form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit',
'#value' => t('Save'), '#value' => t('Save'),
@ -418,7 +507,7 @@ function fulcrm_campaign_admin_campaign_view_form( $form, &$form_state, $fcid )
foreach ( $query->execute() as $row ) { foreach ( $query->execute() as $row ) {
$form[ 'variables' ][ 'values' ][ 'variable_' . $row->name ] = array( '#type' => 'textfield', $form[ 'variables' ][ 'values' ][ 'variable_' . $row->name ] = array( '#type' => 'textfield',
'#title' => check_plain( $row->name ), '#title' => check_plain( $row->name ),
'#default_value' => check_plain( $row->value ), '#default_value' => $row->value,
); );
} }
$form[ 'variables' ][ 'values' ][ 'save' ] = array( '#type' => 'submit', $form[ 'variables' ][ 'values' ][ 'save' ] = array( '#type' => 'submit',
@ -449,14 +538,124 @@ function fulcrm_campaign_admin_campaign_view( $fcid ) {
return drupal_get_form( 'fulcrm_campaign_admin_campaign_view_form', $fcid ); return drupal_get_form( 'fulcrm_campaign_admin_campaign_view_form', $fcid );
} }
function _fulcrm_campaign_admin_campaign_sync_build( $fcid, $fetch = false, $errors = false, $quick = false ) {
global $user;
$campaign = array( 'd' => array() );
if ( $fetch ) {
$campaign_id = fulcrm_webhook_get_pk_for_entity_type( 'fulcrm_campaign', $fcid, 'campaign' );
if ( $campaign_id ) {
$api_data = fulcrm_apiv2_GET( 'campaign/' . $campaign_id . '/',
$query = array( 'expand' => implode( ',', array( 'd',
) ) ) );
if ( $api_data[ 'success' ] ) {
$campaign = $api_data[ 'data' ];
} else {
if ( $errors )
drupal_set_message( t('Error fetching existing campaign data from fulcrm.'), 'error', FALSE );
}
} else {
if ( $errors )
drupal_set_message( t('This campaign has never been synced with fulcrm before.'), 'warning', FALSE );
}
}
$query = db_select( 'fulcrm_campaign' )->fields( 'fulcrm_campaign', array( 'fcid', 'name', 'subject') )->condition( 'fulcrm_campaign.fcid', $fcid );
foreach ( $query->execute() as $row ) {
$campaign[ 'name' ] = $row->name;
$campaign[ 'subject' ] = $row->subject;
}
$campaign[ 'd' ][ 'name' ] = $campaign[ 'name' ];
$query = db_select( 'fulcrm_campaign_variable_value' )
->fields( 'fulcrm_campaign_variable_value', array( 'name', 'value' ) )
->condition( 'fulcrm_campaign_variable_value.fcid', $fcid )
->orderBy( 'fulcrm_campaign_variable_value.name' );
foreach ( $query->execute() as $row ) {
$campaign[ $row->name ] = $row->value;
}
if ( $quick ) {
$campaign[ 'd' ][ 'fms_content' ] = '...Drupal content data goes here...';
$campaign[ 'd' ][ 'fms_content_list' ] = '...Drupal content references go here...';
} else {
$old_user = $user;
$user = user_load( variable_get( 'fulcrm_uid', 0 ) );
$fms_content = array();
$query = db_select( 'fulcrm_campaign_node' )
->fields( 'fulcrm_campaign_node', array( 'nid' ) )
->condition( 'fulcrm_campaign_node.fcid', $fcid )
->orderBy( 'fulcrm_campaign_node.weight' );
foreach ( $query->execute() as $row ) {
$node = node_load( $row->nid );
$node_view = node_view( $node, 'full' );
$node_data = array( 'title' => $node->title,
//'date' => $node->date,
//'node_url' => $node->url,
//'submitted' => $submitted,
//'nid' => $node->nid,
//'type' => $node->type,
( 'is_node_type_' . $node->type . '?' ) => true,
);
$fms_content[ 'node_' . $node->nid ] = $node_data;
$campaign[ $row->name ] = $row->value;
}
$campaign[ 'd' ][ 'fms_content' ] = $fms_content;
$campaign[ 'd' ][ 'fms_content_list' ] = array_keys( $fms_content );
$user = $old_user;
}
return $campaign;
}
function fulcrm_campaign_admin_campaign_sync_form_submit( $form, &$form_state ) { function fulcrm_campaign_admin_campaign_sync_form_submit( $form, &$form_state ) {
if ( $fcid = intval( $form_state[ 'values' ][ 'fcid' ] ) ) { if ( $fcid = intval( $form_state[ 'values' ][ 'fcid' ] ) ) {
// XXX $campaign = _fulcrm_campaign_admin_campaign_sync_build( $fcid );
// XXX if ( fulcrm_campaign_schedule_campaign( $fcid, $start = $form_state[ 'values' ][ 'start' ], $force = true, $finalised = true ) ) {
drupal_set_message( 'Campaign ' . $fcid . ' synced to fulcrm.' ); $campaign_id = fulcrm_webhook_get_pk_for_entity_type( 'fulcrm_campaign', $fcid, 'campaign' );
// XXX drupal_goto( 'admin/config/content/fms' ); if ( $campaign_id ) {
// XXX } $api_data = fulcrm_apiv2_PATCH( 'campaign/' . $campaign_id . '/', $campaign, $query = array( 'expand' => 'd' ) );
} else {
$api_data = fulcrm_apiv2_POST( 'campaign/', $campaign, $query = array( 'expand' => 'd' ) );
if ( $api_data[ 'success' ] ) {
$url = $api_data[ 'headers' ][ 'Location' ];
$fulcrm_type = fulcrm_apiv2_url_to_type( $url );
$fulcrm_pk = fulcrm_apiv2_url_to_pk( $url );
fulcrm_webhook_set_mapping( 'fulcrm_campaign', 'fulcrm_campaign', $fcid, $fulcrm_type, $fulcrm_pk );
}
} }
if ( $api_data[ 'success' ] ) {
drupal_set_message( 'Campaign ' . $fcid . ' synced to fulcrm.', 'status', FALSE );
} else {
drupal_set_message( 'Campaign ' . $fcid . ' could not be synced to fulcrm.', 'error', FALSE );
}
}
}
function _fulcrm_campaign_admin_array_copy( array $array ) {
$result = array();
foreach ( $array as $key => $val ) {
if ( is_array( $val ) ) {
$result[ $key ] = _fulcrm_campaign_admin_array_copy( $val );
} elseif ( is_object( $val ) ) {
$result[ $key ] = clone $val;
} else {
$result[ $key ] = $val;
}
}
return $result;
} }
function fulcrm_campaign_admin_campaign_sync_form( $form, &$form_state, $fcid ) { function fulcrm_campaign_admin_campaign_sync_form( $form, &$form_state, $fcid ) {
@ -464,14 +663,18 @@ function fulcrm_campaign_admin_campaign_sync_form( $form, &$form_state, $fcid )
'#value' => $fcid, '#value' => $fcid,
); );
$campaign = _fulcrm_campaign_admin_campaign_sync_build( $fcid, $fetch = true, $errors = true, $quick = true );
$form[ 'obj' ] = array( '#type' => 'markup',
'#markup' => '<pre>' . check_plain( print_r( $campaign, 1 ) ) . '</pre>',
);
$form[ 'actions' ] = array( '#type' => 'actions' ); $form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit', $form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit',
'#value' => t('Schedule and Send'), '#value' => t('Sync to fulcrm'),
); );
return $form; return $form;
} }
function fulcrm_campaign_admin_campaign_sync( $fcid = 0 ) { function fulcrm_campaign_admin_campaign_sync( $fcid = 0 ) {
@ -509,8 +712,6 @@ function fulcrm_campaign_admin_campaign_delete_form( $form, &$form_state, $fcid
'#href' => 'admin/content/fulcrm/campaign', '#href' => 'admin/content/fulcrm/campaign',
'#title' => 'Cancel', '#title' => 'Cancel',
); );
return $form; return $form;
} }
@ -579,7 +780,7 @@ function fulcrm_campaign_admin_config_template_edit_form_submit( $form, &$form_s
foreach ( $form_state[ 'values' ] as $name => $value ) { foreach ( $form_state[ 'values' ] as $name => $value ) {
if ( substr( $name , 0, 9 ) === 'variable_' ) if ( substr( $name , 0, 9 ) === 'variable_' )
db_update( 'fulcrm_campaign_variable_value' )->condition( 'tid', $tid )->condition( 'name', substr( $name, 9 ) )->fields( array( 'value' => $value, db_update( 'fulcrm_campaign_template_value' )->condition( 'tid', $tid )->condition( 'name', substr( $name, 9 ) )->fields( array( 'value' => $value,
) )->execute(); ) )->execute();
} }
@ -589,7 +790,7 @@ function fulcrm_campaign_admin_config_template_edit_form_submit( $form, &$form_s
function fulcrm_campaign_admin_config_template_edit_form_submit_new( $form, &$form_state ) { function fulcrm_campaign_admin_config_template_edit_form_submit_new( $form, &$form_state ) {
if ( $tid = intval( $form_state[ 'values' ][ 'tid' ] ) ) { if ( $tid = intval( $form_state[ 'values' ][ 'tid' ] ) ) {
db_insert( 'fulcrm_campaign_variable_value' )->fields( array( 'tid' => $tid, db_insert( 'fulcrm_campaign_template_value' )->fields( array( 'tid' => $tid,
'name' => $form_state[ 'values' ][ 'new' ], 'name' => $form_state[ 'values' ][ 'new' ],
'value' => '' ) )->execute(); 'value' => '' ) )->execute();
drupal_set_message( 'Variable added.' ); drupal_set_message( 'Variable added.' );
@ -617,11 +818,11 @@ function fulcrm_campaign_admin_config_template_edit_form( $form, &$form_state, $
'#title' => 'Variables', '#title' => 'Variables',
); );
$query = db_select( 'fulcrm_campaign_variable_value' )->fields( 'fulcrm_campaign_variable_value', array( 'name', 'value' ) )->condition( 'fulcrm_campaign_variable_value.tid', $tid )->orderBy( 'fulcrm_campaign_variable_value.name' ); $query = db_select( 'fulcrm_campaign_template_value' )->fields( 'fulcrm_campaign_template_value', array( 'name', 'value' ) )->condition( 'fulcrm_campaign_template_value.tid', $tid )->orderBy( 'fulcrm_campaign_template_value.name' );
foreach ( $query->execute() as $row ) { foreach ( $query->execute() as $row ) {
$form[ 'variables' ][ 'values' ][ 'variable_' . $row->name ] = array( '#type' => 'textfield', $form[ 'variables' ][ 'values' ][ 'variable_' . $row->name ] = array( '#type' => 'textfield',
'#title' => check_plain( $row->name ), '#title' => check_plain( $row->name ),
'#default_value' => check_plain( $row->value ), '#default_value' => $row->value,
); );
} }

@ -121,7 +121,7 @@ function fulcrm_campaign_theme() {
function fulcrm_campaign_create_campaign_variables_from_template( $fcid, $tid ) { function fulcrm_campaign_create_campaign_variables_from_template( $fcid, $tid ) {
db_delete( 'fulcrm_campaign_variable_value' )->condition( 'fulcrm_campaign_variable_value.fcid', $fcid )->execute(); db_delete( 'fulcrm_campaign_variable_value' )->condition( 'fulcrm_campaign_variable_value.fcid', $fcid )->execute();
$query = db_select( 'fms_variable_template_value' )->fields( 'fms_variable_template_value', array( 'name', 'value' ) )->condition( 'fms_variable_template_value.tid', $tid ); $query = db_select( 'fulcrm_campaign_template_value' )->fields( 'fulcrm_campaign_template_value', array( 'name', 'value' ) )->condition( 'fulcrm_campaign_template_value.tid', $tid );
foreach ( $query->execute() as $row ) { foreach ( $query->execute() as $row ) {
db_insert( 'fulcrm_campaign_variable_value' )->fields( array( 'fcid' => $fcid, db_insert( 'fulcrm_campaign_variable_value' )->fields( array( 'fcid' => $fcid,
'name' => $row->name, 'name' => $row->name,

Loading…
Cancel
Save