diff --git a/fulcrm_campaign.admin.inc b/fulcrm_campaign.admin.inc new file mode 100644 index 0000000..35f7c84 --- /dev/null +++ b/fulcrm_campaign.admin.inc @@ -0,0 +1,664 @@ + 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' => '' . $row->fcid . '' ), + array( 'data' => $row->name ), + array( 'data' => $row->subject ), + array( 'data' => 'arrange' ), + ); + } + + $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( $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 (on email)', + '#default_value' => ( ( array_key_exists( 'values', $form_state ) && $form_state[ 'values' ][ 'subject' ] ) ? $form_state[ 'values' ][ 'subject' ] : $campaign->subject ), + ); + + $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/config/services/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 (on 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 ) . ' (configure)' ), + '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') ), 'subject' => array( 'data' => t('Subject') ) ); + + 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( 'fms-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' => 'fms-content-items-table' ) ) ); + $output .= drupal_render_children( $form ); + drupal_add_tabledrag( 'fms-content-items-table', 'order', 'sibling', 'fms-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' => check_plain( $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_form_submit( $form, &$form_state ) { + if ( $fcid = intval( $form_state[ 'values' ][ 'fcid' ] ) ) { + // XXX + // XXX if ( fulcrm_campaign_schedule_campaign( $fcid, $start = $form_state[ 'values' ][ 'start' ], $force = true, $finalised = true ) ) { + drupal_set_message( 'Campaign ' . $fcid . ' synced to fulcrm.' ); + // XXX drupal_goto( 'admin/config/content/fms' ); + // XXX } + } +} + +function fulcrm_campaign_admin_campaign_sync_form( $form, &$form_state, $fcid ) { + $form[ 'fcid' ] = array( '#type' => 'hidden', + '#value' => $fcid, + ); + + $form[ 'actions' ] = array( '#type' => 'actions' ); + $form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit', + '#value' => t('Schedule and Send'), + ); + + + 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/config/services/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 ' . check_plain( $name ) . '. 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/config/services/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' => 'edit' ), + ); + } + + $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_variable_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_variable_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_variable_value' )->fields( 'fulcrm_campaign_variable_value', array( 'name', 'value' ) )->condition( 'fulcrm_campaign_variable_value.tid', $tid )->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' => check_plain( $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' ); +} diff --git a/fulcrm_campaign.info b/fulcrm_campaign.info new file mode 100644 index 0000000..1dae9d0 --- /dev/null +++ b/fulcrm_campaign.info @@ -0,0 +1,9 @@ +name = fulcrm campaign +description = fulcrm campaign +core = 7.x +package = fulcrm + +configure = admin/config/services/fulcrm/campaign + +dependencies[] = fulcrm_apiv2 +dependencies[] = fulcrm_webhook diff --git a/fulcrm_campaign.install b/fulcrm_campaign.install new file mode 100644 index 0000000..008ef9b --- /dev/null +++ b/fulcrm_campaign.install @@ -0,0 +1,73 @@ + 'The campaign itself, and a reference to fulcrm.', + 'fields' => array( 'fcid' => array( 'type' => 'serial', + 'not null' => TRUE, + 'default' => 0, + 'description' => 'Unique ID' ), + 'name' => array( 'type' => 'text', + 'description' => 'Campaign name.' ), + 'subject' => array( 'type' => 'text', + 'description' => 'Campaign subject.' ), + ), + 'primary key' => array( 'fcid' ), + ); + + $schema[ 'fulcrm_campaign_node' ] = array( 'description' => 'Which nodes are to be build into a mailshot, and the order.', + 'fields' => array( 'fcid' => array( 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'description' => 'fulcrm_campaign.fcid' ), + 'nid' => array( 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'description' => 'node.nid' ), + 'weight' => array( 'type' => 'int', + 'default' => 0, + 'description' => 'sort order for content' ), + ), + 'primary key' => array( 'nid', 'fcid' ), + ); + $schema[ 'fulcrm_campaign_variable_template' ] = array( 'description' => 'FMS variable templates, to be used as presets.', + 'fields' => array( 'tid' => array( 'type' => 'serial', + 'not null' => TRUE, + 'default' => 0, + 'description' => 'Unique ID' ), + 'name' => array( 'type' => 'text', + 'not null' => TRUE, + 'description' => 'Name of the template.' ), + ), + 'primary key' => array( 'tid' ), + ); + $schema[ 'fulcrm_campaign_template_value' ] = array( 'description' => 'Names and values of variables for each template.', + 'fields' => array( 'tid' => array( 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'description' => 'fulcrm_campaign_variable_template.tid' ), + 'name' => array( 'type' => 'varchar', + 'length' => 64, + 'not null' => TRUE, + 'description' => 'Variable name.' ), + 'value' => array( 'type' => 'text', + 'description' => 'Variable value.' ), + ), + 'primary key' => array( 'tid', 'name' ), + ); + $schema[ 'fulcrm_campaign_variable_value' ] = array( 'description' => 'Names and values of variables for each campaign.', + 'fields' => array( 'fcid' => array( 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'description' => 'fulcrm_campaign.fcid' ), + 'name' => array( 'type' => 'varchar', + 'length' => 64, + 'not null' => TRUE, + 'description' => 'Variable name.' ), + 'value' => array( 'type' => 'text', + 'description' => 'Variable value.' ), + ), + 'primary key' => array( 'fcid', 'name' ), + ); + + return $schema; +} diff --git a/fulcrm_campaign.module b/fulcrm_campaign.module new file mode 100644 index 0000000..bbb2bfd --- /dev/null +++ b/fulcrm_campaign.module @@ -0,0 +1,132 @@ + array( 'title' => t('Administer fulcrm campaign'), + 'description' => t('Configure fulcrm Campaign.'), + ), + 'administer fulcrm campaign templates' => array( 'title' => t('Administer fulcrm Campaign Templates'), + 'description' => t('Configure theme templates and variable templates for FMS.'), + ), + 'build fulcrm campaign' => array( 'title' => t('Build fulcrm Campaign'), + 'description' => t('Assemble content together for a Campaign.'), + ), + 'set fulcrm campaign variables' => array( 'title' => t('Set FMS Mailshot Variables'), + 'description' => t('Set/override variables on a Campaign.'), + ), + ); +} + +function fulcrm_campaign_menu() { + $items[ 'admin/config/services/fulcrm/campaign' ] = array( 'page callback' => 'fulcrm_campaign_admin_campaign', + 'title' => 'fulcrm Campaigns', + 'description' => 'Administer campaigns created in Drupal and pushed to fulcrm.', + 'access arguments' => array('build fulcrm campaign'), + 'file' => 'fulcrm_campaign.admin.inc', + ); + $items[ 'admin/config/services/fulcrm/campaign/new' ] = array( 'page callback' => 'fulcrm_campaign_admin_campaign_new', + 'type' => MENU_LOCAL_ACTION, + 'title' => 'Create a Campaign', + 'description' => 'Create an fulcrm Campaign.', + 'access arguments' => array('build fulcrm campaign'), + 'file' => 'fulcrm_campaign.admin.inc', + 'weight' => 1, + ); + $items[ 'admin/config/services/fulcrm/campaign/%' ] = array( 'page callback' => 'fulcrm_campaign_admin_campaign_settings', + 'title' => 'Settings', + 'page arguments' => array( 5 ), + 'access arguments' => array('build fulcrm campaign'), + 'file' => 'fulcrm_campaign.admin.inc', + 'weight' => 1, + ); + $items[ 'admin/config/services/fulcrm/campaign/%/settings' ] = array( 'page callback' => 'fulcrm_campaign_admin_campaign_settings', + 'title' => 'Settings', + 'type' => MENU_DEFAULT_LOCAL_TASK, + 'access arguments' => array('build fulcrm campaign'), + 'page arguments' => array( 5 ), + 'weight' => 1, + ); + $items[ 'admin/config/services/fulcrm/campaign/%/sync' ] = array( 'page callback' => 'fulcrm_campaign_admin_campaign_sync', + 'title' => 'Sync to fulcrm', + 'type' => MENU_DEFAULT_LOCAL_TASK, + 'access arguments' => array('build fulcrm campaign'), + 'file' => 'fulcrm_campaign.admin.inc', + 'page arguments' => array( 5 ), + 'weight' => 10, + ); + $items[ 'admin/config/services/fulcrm/campaign/%/edit' ] = array( 'page callback' => 'fulcrm_campaign_admin_campaign_edit', + 'title' => 'Edit', + 'type' => MENU_LOCAL_TASK, + 'page arguments' => array( 5 ), + 'access arguments' => array('build fulcrm campaign'), + 'file' => 'fulcrm_campaign.admin.inc', + 'weight' => 2, + ); + $items[ 'admin/config/services/fulcrm/campaign/%/delete' ] = array( 'page callback' => 'fulcrm_campaign_admin_campaign_delete', + 'title' => 'Delete', + 'type' => MENU_LOCAL_TASK, + 'page arguments' => array( 5 ), + 'access arguments' => array('build fulcrm campaign'), + 'file' => 'fulcrm_campaign.admin.inc', + 'weight' => 5, + ); + + $items[ 'node/%node/fulcrm/campaign' ] = array( 'title' => 'Mailshot', + 'page callback' => 'fulcrm_campaign_admin_campaign_node', + 'page arguments' => array( 1 ), + 'access arguments' => array('build fulcrm campaign'), + 'file' => 'fulcrm_campaign.admin.inc', + 'weight' => 5, + 'type' => MENU_LOCAL_TASK + ); + + $items[ 'admin/config/services/fulcrm/campaign/settings' ] = array( 'page callback' => 'fulcrm_campaign_admin_config_settings', + 'title' => 'fulcrm Campaign Settings', + 'description' => 'fulcrm campaign system settings.', + 'type' => MENU_LOCAL_TASK, + 'access arguments' => array('administer fulcrm campaign'), + 'file' => 'fulcrm_campaign.admin.inc', + ); + $items[ 'admin/config/services/fulcrm/campaign/template' ] = array( 'page callback' => 'fulcrm_campaign_admin_config_template', + 'title' => 'Template Configuration', + 'description' => 'fulcrm Campaign template settings.', + 'type' => MENU_LOCAL_TASK, + 'access arguments' => array('administer fulcrm campaign templates'), + 'file' => 'fulcrm_campaign.admin.inc', + ); + $items[ 'admin/config/services/fulcrm/campaign/template/add' ] = array( 'page callback' => 'fulcrm_campaign_admin_config_template_add', + 'title' => 'Create Template', + 'description' => 'fulcrm Campaign template settings.', + 'type' => MENU_LOCAL_ACTION, + 'access arguments' => array('administer fulcrm campaign templates'), + 'file' => 'fulcrm_campaign.admin.inc', + ); + $items[ 'admin/config/services/fulcrm/campaign/template/%/edit' ] = array( 'page callback' => 'fulcrm_campaign_admin_config_template_edit', + 'title' => 'Edit Template', + 'description' => 'fulcrm Campaign template settings.', + 'access arguments' => array('administer fulcrm campaign templates'), + 'page arguments' => array( 6 ), + 'file' => 'fulcrm_campaign.admin.inc', + ); + return $items; +} + +function fulcrm_campaign_theme() { + return array( 'fulcrm_campaign_admin_campaign_edit_form' => array( 'render element' => 'form', + 'file' => 'fulcrm_campaign.admin.inc', + ), + ); +} + +function fulcrm_campaign_create_campaign_variables_from_template( $fcid, $tid ) { + 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 ); + foreach ( $query->execute() as $row ) { + db_insert( 'fulcrm_campaign_variable_value' )->fields( array( 'fcid' => $fcid, + 'name' => $row->name, + 'value' => $row->value, + ) )->execute(); + } + module_invoke_all( 'fulcrm_campaign_prepared', $fcid ); +} +