You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fulcrm_campaign/fulcrm_campaign.admin.inc

871 lines
42 KiB
PHP

<?php /* -*- php -*- */
function fulcrm_campaign_admin_campaign() {
$header = array( array( 'data' => t('ID'), 'field' => 'fulcrm_campaign.fcid' ),
array( 'data' => t('Name'), 'field' => 'fulcrm_campaign.name' ),
array( 'data' => t('Subject'), 'field' => 'fulcrm_campaign.subject' ),
array( 'data' => t('Actions') ),
);
$query =
db_select( 'fulcrm_campaign' );
$query = $query
->extend( 'TableSort' )
->fields( 'fulcrm_campaign', array( 'fcid', 'name', 'subject' ) );
$query = $query
->orderBy('fulcrm_campaign.fcid');
$result = $query->orderByHeader( $header )->execute();
$rows = array();
foreach ( $result as $row ) {
$rows[] = array( array( 'data' => '<a href="' . url('admin/content/fulcrm/campaign/' . $row->fcid) . '">' . $row->fcid . '</a>' ),
array( 'data' => $row->name ),
array( 'data' => $row->subject ),
array( 'data' => '<a href="' . url('admin/content/fulcrm/campaign/' . $row->fcid . '/edit') . '">arrange</a>' ),
);
}
$build[ 'tablesort_table' ] = array( '#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
return $build;
}
function fulcrm_campaign_admin_campaign_settings_form_submit( $form, &$form_state ) {
$fcid = intval( $form_state[ 'values' ][ 'fcid' ] );
db_update( 'fulcrm_campaign' )->fields( array( 'name' => $form_state[ 'values' ][ 'name' ],
'subject' => $form_state[ 'values' ][ 'subject' ],
) )->condition( 'fulcrm_campaign.fcid', $fcid )->execute();
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 ) {
$fcid = intval( $fcid );
$query = db_select( 'fulcrm_campaign' )->fields( 'fulcrm_campaign', array( 'fcid', 'name', 'subject' ) )->condition( 'fulcrm_campaign.fcid', $fcid );
foreach ( $query->execute() as $campaign ) {
$form[ 'fcid' ] = array( '#type' => 'hidden',
'#value' => $campaign->fcid,
);
}
$form[ 'name' ] = array( '#type' => 'textfield',
'#title' => 'Name (for internal use)',
'#default_value' => ( ( array_key_exists( 'values', $form_state ) && $form_state[ 'values' ][ 'name' ] ) ? $form_state[ 'values' ][ 'name' ] : $campaign->name ),
);
$form[ 'subject' ] = array( '#type' => 'textfield',
'#title' => 'Subject (of email)',
'#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' ][ 'submit' ] = array( '#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function fulcrm_campaign_admin_campaign_settings( $fcid ) {
return drupal_get_form( 'fulcrm_campaign_admin_campaign_settings_form', $fcid );
}
function fulcrm_campaign_admin_campaign_new_form_submit( $form, &$form_state ) {
$fcid = db_insert( 'fulcrm_campaign' )->fields( array( 'name' => $form_state[ 'values' ][ 'name' ],
'subject' => $form_state[ 'values' ][ 'subject' ],
) )->execute();
if ( $fcid ) {
$tid = intval( $form_state[ 'values' ][ 'template' ] );
if ( $tid )
fulcrm_campaign_create_campaign_variables_from_template( $fcid, $tid );
drupal_set_message( t( 'Mailshot created!' ) );
drupal_goto( 'admin/content/fulcrm/campaign/' . $fcid . '/edit' );
} else
drupal_set_message( t( 'There was an error creating the campaign.' ), $type = 'error' );
}
function fulcrm_campaign_admin_campaign_new_form( $form, &$form_state ) {
$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[ 'template' ] = array( '#type' => 'select',
'#title' => 'Template',
'#options' => $templates,
);
$form[ 'name' ] = array( '#type' => 'textfield',
'#default_value' => '',
'#title' => 'Name (for internal use)',
);
$form[ 'subject' ] = array( '#type' => 'textfield',
'#default_value' => '',
'#title' => 'Subject (of email)',
);
$form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit',
'#value' => t('Create'),
);
return $form;
}
function fulcrm_campaign_admin_campaign_new() {
return drupal_get_form( 'fulcrm_campaign_admin_campaign_new_form' );
}
function fulcrm_campaign_admin_campaign_node_form_submit( $form, &$form_state ) {
if ( $node = node_load( intval( $form_state[ 'values' ][ 'nid' ] ) ) ) {
$query = db_select( 'fulcrm_campaign_node' );
$query->condition( 'fulcrm_campaign_node.nid', $node->nid );
$query->addExpression( "MAX(weight)", 'max_weight' );
$max_weight = 0;
foreach ( $query->execute() as $row )
$max_weight = $row->max_weight;
if ( array_key_exists( 'add_to_campaigns', $form_state[ 'values' ] ) ) {
foreach ( $form_state[ 'values' ][ 'add_to_campaigns' ] as $fcid => $value ) {
if ( $value ) {
$max_weight++;
db_insert( 'fulcrm_campaign_node' )->fields( array( 'nid' => $node->nid,
'fcid' => $fcid,
'weight' => $max_weight,
) )->execute();
}
}
}
if ( array_key_exists( 'remove_from_campaigns', $form_state[ 'values' ] ) ) {
foreach ( $form_state[ 'values' ][ 'remove_from_campaigns' ] as $fcid => $value ) {
if ( $value ) {
db_delete( 'fulcrm_campaign_node' )->condition( 'fcid', $fcid )->condition( 'nid', $node->nid )->execute();
}
}
}
} else {
drupal_set_message( 'Could not load the correct node.', $type = 'error' );
}
}
function fulcrm_campaign_admin_campaign_node_form( $form, &$form_state, $node ) {
$campaigns = array();
$query = db_select( 'fulcrm_campaign' );
$query->fields( 'fulcrm_campaign', array( 'fcid', 'name', 'subject' ) );
foreach ( $query->execute() as $row )
$campaigns[ $row->fcid ] = array( 'name' => check_plain( $row->name ), 'subject' => check_plain( $row->subject ) );
$past_campaigns = array();
$query = db_select( 'fulcrm_campaign_node' );
$query->fields( 'fulcrm_campaign_node', array( 'nid', 'fcid' ) );
$query->join( 'fulcrm_campaign', 'fulcrm_campaign', 'fulcrm_campaign_node.fcid = fulcrm_campaign.fcid' );
$query->fields( 'fulcrm_campaign', array( 'fcid', 'name', 'subject' ) );
$query->leftJoin( 'fulcrm_webhook_entity_mapping', 'fwem', 'fwem.entity_type=\'fulcrm_campaign\' and fwem.fulcrm_type=\'campaign\' and fwem.entity_id = fulcrm_campaign.fcid' );
$query->fields( 'fwem', array( 'entity_id' ) );
$query->isNotNull( 'fwem.entity_id' );
$query->condition( 'fulcrm_campaign_node.nid', $node->nid );
foreach ( $query->execute() as $row )
$past_campaigns[ $row->fcid ] = $row->description;
$in_campaigns = array();
$query = db_select( 'fulcrm_campaign_node' );
$query->fields( 'fulcrm_campaign_node', array( 'nid', 'fcid' ) );
$query->join( 'fulcrm_campaign', 'fulcrm_campaign', 'fulcrm_campaign_node.fcid = fulcrm_campaign.fcid' );
$query->fields( 'fulcrm_campaign', array( 'fcid', 'name', 'subject' ) );
$query->leftJoin( 'fulcrm_webhook_entity_mapping', 'fwem', 'fwem.entity_type=\'fulcrm_campaign\' and fwem.fulcrm_type=\'campaign\' and fwem.entity_id = fulcrm_campaign.fcid' );
$query->fields( 'fwem', array( 'entity_id' ) );
$query->isNull( 'fwem.entity_id' );
$query->condition( 'fulcrm_campaign_node.nid', $node->nid );
foreach ( $query->execute() as $row ) {
$in_campaigns[ $row->fcid ] = array( 'name' => ( check_plain( $row->name ) . ' (<a href="' .
url('admin/content/fulcrm/campaign/' . $row->fcid . '/edit') .
'">configure</a>)' ),
'subject' => check_plain( $row->subject ) );
if ( array_key_exists( $row->fcid, $campaigns ) )
unset( $campaigns[ $row->fcid ] );
}
$form[ 'nid' ] = array( '#type' => 'hidden',
'#value' => $node->nid );
$header = array( 'name' => array( 'data' => t('Name (for internal use)') ), 'subject' => array( 'data' => t('Subject (of email)') ) );
if ( $campaigns ) {
$form[ 'add_to_campaigns' ] = array( '#type' => 'fieldset',
'#title' => 'Add Content to Open Mailshots',
);
$form[ 'add_to_campaigns' ][ 'add_to_campaigns' ] = array( '#type' => 'tableselect',
'#options' => $campaigns,
'#header' => $header,
);
}
if ( $in_campaigns ) {
$form[ 'remove_from_campaigns' ] = array( '#type' => 'fieldset',
'#title' => 'Remove Content from Pending Mailshots',
);
$form[ 'remove_from_campaigns' ][ 'remove_from_campaigns' ] = array( '#type' => 'tableselect',
'#options' => $in_campaigns,
'#header' => $header,
);
}
if ( $past_campaigns ) {
$form[ 'history_campaigns' ] = array( '#type' => 'fieldset',
'#title' => 'Content was Used in Mailshots',
);
foreach ( $past_campaigns as $fcid => $description ) {
$form[ 'history_campaigns' ][ $fcid ] = array( '#type' => 'item',
'#title' => $description );
}
}
if ( $past_campaigns || $in_campaigns || $campaigns ) {
$form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit',
'#value' => t('Save'),
);
} else {
$form[ 'nothing' ] = array( '#type' => 'markup',
'#markup' => 'This content does not appear in any campaigns, and currently no open/pending campaigns are available for you to add it to.',
);
}
return $form;
}
function fulcrm_campaign_admin_campaign_node( $node ) {
return drupal_get_form( 'fulcrm_campaign_admin_campaign_node_form', $node );
}
function fulcrm_campaign_admin_campaign_edit_form_submit( $form, &$form_state ) {
$fcid = intval( $form_state[ 'values' ][ 'fcid' ] );
if ( $fcid ) {
foreach ( $form_state[ 'values' ][ 'content' ] as $nid => $item ) {
db_update( 'fulcrm_campaign_node' )->condition( 'fcid', $fcid )->condition( 'nid', $nid )->fields( array( 'weight' => $item[ 'weight' ] ) )->execute();
}
}
}
function fulcrm_campaign_admin_campaign_edit_form( $form, &$form_state, $fcid ) {
$form[ 'fcid' ] = array( '#type' => 'hidden',
'#value' => $fcid,
);
$form[ 'content' ][ '#tree' ] = TRUE;
$query = db_select( 'fulcrm_campaign_node' );
$query->join( 'node', 'node', 'fulcrm_campaign_node.nid = node.nid' );
$query->condition( 'fulcrm_campaign_node.fcid', $fcid );
$query->fields( 'fulcrm_campaign_node', array( 'nid', 'weight' ) );
$query->fields( 'node', array( 'title' ) );
$query->orderBy( 'fulcrm_campaign_node.weight' );
foreach ( $query->execute() as $row ) {
$form[ 'content' ][ $row->nid ] = array( 'title' => array( '#markup' => check_plain( $row->title ),
),
'weight' => array( '#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => $row->weight,
'#delta' => 10,
'#title-display' => 'invisible',
),
);
}
$form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit',
'#value' => t('Save Order'),
);
return $form;
}
function theme_fulcrm_campaign_admin_campaign_edit_form( $variables ) {
$form = $variables['form'];
$rows = array();
foreach ( element_children( $form[ 'content' ] ) as $id ) {
$form[ 'content' ][ $id ][ 'weight' ][ '#attributes' ][ 'class' ] = array( 'fulcrm-campaign-content-item-weight' );
$rows[] = array( 'data' => array( drupal_render( $form[ 'content' ][ $id ][ 'title' ] ),
drupal_render( $form[ 'content' ][ $id ][ 'weight' ] ),
),
'class' => array( 'draggable' ),
);
}
$header = array( t('Title'), t('Weight') );
$output = theme( 'table', array( 'header' => $header, 'rows' => $rows, 'attributes' => array( 'id' => 'fulcrm-campaign-content-items-table' ) ) );
$output .= drupal_render_children( $form );
drupal_add_tabledrag( 'fulcrm-campaign-content-items-table', 'order', 'sibling', 'fulcrm-campaign-content-item-weight' );
return $output;
}
function fulcrm_campaign_admin_campaign_edit( $fcid ) {
return drupal_get_form( 'fulcrm_campaign_admin_campaign_edit_form', $fcid );
}
function fulcrm_campaign_admin_campaign_view_form_submit( $form, &$form_state ) {
$batch = array( 'title' => t('Building Content'),
'operations' => array(),
'finished' => 'fulcrm_campaign_batch_campaign_view_finished',
);
$fcid = intval( $form_state[ 'values' ][ 'fcid' ] );
$counter = 0;
// clear out this campaign's queue and related content
$qquery = db_select( 'fms_queue' )->fields( 'fms_queue', array( 'qid' ) )->condition( 'fcid', $fcid );
foreach ( $qquery->execute() as $row ) {
db_delete( 'fms_queue2content' )->condition( 'qid', $row->qid )->execute();
db_delete( 'fms_variable' )->condition( 'qid', $row->qid )->execute();
}
db_delete( 'fms_queue' )->condition( 'fcid', $fcid )->execute();
db_delete( 'fms_content' )->condition( 'fcid', $fcid )->execute();
db_update( 'fms_campaign' )->condition( 'fcid', $fcid )->fields( array( 'status' => null ) )->execute();
$themes = variable_get( 'fms_theme_cache' );
$theme = $themes[ $form_state[ 'values' ][ 'theme' ] ];
$module = $theme[ '#batch_module' ];
$hook = $theme[ '#batch_hook' ];
$args = $theme[ '#batch_arguments' ];
$nids = 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 )
$nids[] = $row->nid;
$headers = fulcrm_campaign_create_headers();
foreach ( module_invoke( $module, $hook, $fcid, $headers, $nids, $args ) as $operation ) {
$batch[ 'operations' ][] = array( 'fulcrm_campaign_batch_campaign', array( $fcid, $counter++, $operation[ '#module' ], $operation[ '#hook' ], $operation[ '#args' ] ) );
}
$batch[ 'operations' ][] = array( 'fulcrm_campaign_batch_campaign_last', array( $fcid ) );
batch_set( $batch );
}
function fulcrm_campaign_admin_campaign_view_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_view_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_view_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_view_form( $form, &$form_state, $fcid ) {
$form[ 'fcid' ] = array( '#type' => 'hidden',
'#value' => $fcid,
);
$form[ 'variables' ] = array( '#type' => 'fieldset',
'#title' => 'Mailshot 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_view_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_view_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_view_form_submit_new' ),
);
$form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit',
'#value' => t('Sync to fulcrm'),
);
return $form;
}
function fulcrm_campaign_admin_campaign_view( $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 ) {
if ( $fcid = intval( $form_state[ 'values' ][ 'fcid' ] ) ) {
$campaign = _fulcrm_campaign_admin_campaign_sync_build( $fcid );
$campaign_id = fulcrm_webhook_get_pk_for_entity_type( 'fulcrm_campaign', $fcid, 'campaign' );
if ( $campaign_id ) {
$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 );
$campaign_id = fulcrm_apiv2_url_to_pk( $url );
fulcrm_webhook_set_entity_mapping( $entity_type = 'fulcrm_campaign',
$bundle = 'fulcrm_campaign',
$entity_id = $fcid,
$fulcrm_type = $fulcrm_type,
$fulcrm_pk = $campaign_id
);
}
}
if ( $api_data[ 'success' ] ) {
drupal_set_message( '<a target="_blank" href="https://' . variable_get( 'fulcrm_apiv2_hostname' ) . '/post/campaign/' . $campaign_id . '/">Campaign ' . $fcid . '</a> synced to fulcrm.', 'status', FALSE );
} else {
drupal_set_message( '<a target="_blank" href="https://' . variable_get( 'fulcrm_apiv2_hostname' ) . '/post/campaign/' . $campaign_id . '/">Campaign ' . $fcid . '</a> 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 ) {
$form[ 'fcid' ] = array( '#type' => 'hidden',
'#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' ][ 'submit' ] = array( '#type' => 'submit',
'#value' => t('Sync to fulcrm'),
);
return $form;
}
function fulcrm_campaign_admin_campaign_sync( $fcid = 0 ) {
return drupal_get_form( 'fulcrm_campaign_admin_campaign_sync_form', $fcid );
}
function fulcrm_campaign_admin_campaign_delete_form_submit( $form, &$form_state ) {
if ( $fcid = intval( $form_state[ 'values' ][ 'fcid' ] ) ) {
db_delete( 'fulcrm_campaign_variable_value' )->condition( 'fcid', $fcid )->execute();
db_delete( 'fulcrm_campaign' )->condition( 'fcid', $fcid )->execute();
drupal_set_message( t('Campaign deleted.'), 'status', FALSE );
}
$form_state[ 'redirect' ] = 'admin/content/fulcrm/campaign';
}
function fulcrm_campaign_admin_campaign_delete_form( $form, &$form_state, $fcid ) {
$form[ 'fcid' ] = array( '#type' => 'hidden',
'#value' => $fcid,
);
$query = db_select( 'fulcrm_campaign' )->fields( 'fulcrm_campaign', array( 'fcid', 'name' ) )->condition( 'fulcrm_campaign.fcid', $fcid );
foreach ( $query->execute() as $row )
$name = $row->name;
$form[ 'confirm' ] = array( '#title' => 'Are you sure?',
'#type' => 'item',
'#markup' => 'This will delete the campaign <em>' . check_plain( $name ) . '</em>. There is no way to "undo" this operation! Also note that this will not delete any campaign it has created in fulcrm.',
);
$form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit',
'#value' => t('Delete'),
);
$form[ 'actions' ][ 'cancel' ] = array( '#type' => 'link',
'#href' => 'admin/content/fulcrm/campaign',
'#title' => 'Cancel',
);
return $form;
}
function fulcrm_campaign_admin_campaign_delete( $fcid ) {
return drupal_get_form( 'fulcrm_campaign_admin_campaign_delete_form', $fcid );
}
function fulcrm_campaign_admin_config_template_form( $form, &$form_state ) {
$header = array( array( 'data' => t('Template'), 'field' => 'fulcrm_campaign_variable_template.name' ),
array( 'data' => t('Actions') ),
);
$query = db_select( 'fulcrm_campaign_variable_template' )->fields( 'fulcrm_campaign_variable_template', array( 'tid', 'name' ) )->extend( 'TableSort' );
$result = $query->orderByHeader( $header )->execute();
$rows = array();
foreach ( $result as $row ) {
$rows[ $row->tid ] = array( array( 'data' => $row->name ),
array( 'data' => '<a href="' . url('admin/config/services/fulcrm/campaign/template/' . $row->tid) . '/edit">edit</a>' ),
);
}
$form[ 'tablesort_table' ] = array( '#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('No templates found.'),
'#attributes' => array(),
);
return $form;
}
function fulcrm_campaign_admin_config_template() {
return drupal_get_form( 'fulcrm_campaign_admin_config_template_form' );
}
function fulcrm_campaign_admin_config_template_add_form_submit( $form, &$form_state ) {
if ( $form_state[ 'values' ][ 'name' ] ) {
$tid = db_insert( 'fulcrm_campaign_variable_template' )->fields( array( 'name' => $form_state[ 'values' ][ 'name' ]) )->execute();
drupal_set_message( t('Template created!') );
drupal_goto( 'admin/config/services/fulcrm/campaign/template/' . $tid . '/edit' );
}
}
function fulcrm_campaign_admin_config_template_add_form( $form, &$form_state ) {
$form[ 'name' ] = array( '#type' => 'textfield',
'#title' => 'Template Name',
);
$form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit',
'#value' => t('Add'),
);
return $form;
}
function fulcrm_campaign_admin_config_template_add() {
return drupal_get_form( 'fulcrm_campaign_admin_config_template_add_form' );
}
function fulcrm_campaign_admin_config_template_edit_form_submit( $form, &$form_state ) {
if ( $tid = intval( $form_state[ 'values' ][ 'tid' ] ) ) {
db_update( 'fulcrm_campaign_variable_template' )->condition( 'tid', $tid )->fields( array( 'name' => $form_state[ 'values' ][ 'name' ],
) )->execute();
foreach ( $form_state[ 'values' ] as $name => $value ) {
if ( substr( $name , 0, 9 ) === 'variable_' )
db_update( 'fulcrm_campaign_template_value' )->condition( 'tid', $tid )->condition( 'name', substr( $name, 9 ) )->fields( array( 'value' => $value,
) )->execute();
}
drupal_set_message( 'Template updated.' );
}
}
function fulcrm_campaign_admin_config_template_edit_form_submit_new( $form, &$form_state ) {
if ( $tid = intval( $form_state[ 'values' ][ 'tid' ] ) ) {
db_insert( 'fulcrm_campaign_template_value' )->fields( array( 'tid' => $tid,
'name' => $form_state[ 'values' ][ 'new' ],
'value' => '' ) )->execute();
drupal_set_message( 'Variable added.' );
}
}
function fulcrm_campaign_admin_config_template_edit_form( $form, &$form_state, $tid ) {
$form[ 'tid' ] = array( '#type' => 'hidden',
'#value' => $tid,
);
$template_name = '';
$query = db_select( 'fulcrm_campaign_variable_template' );
$query->condition( 'fulcrm_campaign_variable_template.tid', $tid );
$query->fields( 'fulcrm_campaign_variable_template', array( 'name' ) );
foreach ( $query->execute() as $row )
$template_name = $row->name;
$form[ 'name' ] = array( '#type' => 'textfield',
'#title' => 'Template Name',
'#default_value' => $template_name,
);
$form[ 'variables' ] = array( '#type' => 'fieldset',
'#title' => 'Variables',
);
$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 ) {
$form[ 'variables' ][ 'values' ][ 'variable_' . $row->name ] = array( '#type' => 'textfield',
'#title' => check_plain( $row->name ),
'#default_value' => $row->value,
);
}
$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_config_template_edit_form_submit_new' ),
);
$form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function fulcrm_campaign_admin_config_template_edit( $tid = 0 ) {
return drupal_get_form( 'fulcrm_campaign_admin_config_template_edit_form', $tid );
}
function fulcrm_campaign_admin_config_settings_form_submit( $form, &$form_state ) {
drupal_set_message( t('The fulcrm Campaign settings have been saved.') );
}
function fulcrm_campaign_admin_config_settings_form( $form, &$form_state ) {
$form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function fulcrm_campaign_admin_config_settings() {
return drupal_get_form( 'fulcrm_campaign_admin_config_settings_form' );
}