<?php
// $Id: mailbuild.inc,v 1.15 2006/09/15 05:59:01 quicksketch Exp $
/** 
 * function webform_edit_mailbuild
 * Create a set of form items to be displayed on the form for editing this component.
 * Use care naming the form items, as this correlates directly to the database schema.
 * The component "Name" and "Description" fields are added to every component type and
 * are not necessary to specify here (although they may be overridden if desired).
 * @returns An array of form items to be displayed on the edit component page
 **/
function _webform_edit_mailbuild ($currfield) {
  $editFields = array();
  if (isset($_POST['edit']['webformcomponents'])) {
    $components = _webform_components_decode($_POST['edit']['webformcomponents']);
  }
  $name_options = array();
  $email_options = array();
  foreach($components as $cid=>$c){
    if($c['type'] == 'email'){
      $email_options[$cid] = $c['name'];
    }else{
      $name_options[$cid] = $c['name'];
    }
  }
  
  $editFields['object'] = array(
    '#type' => 'fieldset',
    '#title' => 'form object',
    '#collapsible' => true,
    '#collapsed' => true,
    '#weight' => -100,
    '#value' => '<pre>'.print_r($components,true).'</pre>',
  );

  $editFields['extra']['name_field'] = array(
    '#type' => 'select',
    '#title' => t('Name Field'),
    '#default_value' => $currfield['extra']['name_field'],
    '#description' => t('Select the field from this form that represents the name to use when subscribing.'),
    '#options' => $name_options,
  );

  $editFields['extra']['email_field'] = array(
    '#type' => 'select',
    '#title' => t('Email Field'),
    '#default_value' => $currfield['extra']['email_field'],
    '#description' => t('Select the field from this form that represents the email address to use when subscribing.'),
    '#options' => $email_options,
  );

  return $editFields;
}

/** 
 * function webform_render_mailbuild
 * Build a form item array containing all the properties of this component
 * @param $component An array of information describing the component, directly correlating to the webform_component database schema
 * @returns An array of a form item to be displayed on the client-side webform
 **/
function _webform_render_mailbuild ($component) {
  $formItem = array (
    '#type'          => 'checkbox',
    '#title'         => htmlspecialchars($component['extra']['description'], ENT_QUOTES),
    '#required'      => $component['mandatory'],
    '#weight'        => $component['weight'],
    '#default_value' => 1,
//    '#description'   => _webform_filtervalues($component['extra']['description']),
    '#prefix'        => '<div class="webform-component-'.$component['type'].'" id="webform-component-'._webform_safe_name($component['name']).'">',
    '#suffix'        => '</div>',
  );
  
  return $formItem;
}


/** 
 * function _webform_submission_display_mailbuild
 * Display the result of a textfield submission. The output of this function will be displayed under the "results" tab then "submissions"
 * @param $data An array of information containing the submission result, directly correlating to the webform_submitted_data database schema
 * @param $component An array of information describing the component, directly correlating to the webform_component database schema
 * @returns Textual output formatted for human reading.
 **/
function _webform_submission_display_mailbuild ($data,$component) {
  $formItem = _webform_render_mailbuild($component);
  $formItem['#default_value'] = $data['value'][0];
  $formItem['#attributes'] = array("disabled" => "disabled");
  return $formItem;
}


/** 
 * function webform_submit_mailbuild
 * Translates the submitted 'safe' form values back into their un-edited original form
 * @param $data The POST data associated with the component
 * @param $component An array of information describing the component, directly correlating to the webform_component database schema
 * @returns Nothing
 **/
function _webform_submit_mailbuild (&$data,$component) {
  // flatten form_values so fieldsets don't get in the way
  $form_values = $_POST['edit'];
  $node = node_load(array('nid'=>arg(1)));
  _webform_client_submit_flatten($node, $form_values['submitted'], $form_values['submitted']);

  $name = $form_values['submitted'][$component['extra']['name_field']];
  $email = $form_values['submitted'][$component['extra']['email_field']];
  if($data){
    mailbuild_subscribe($name,$email);
  }
}

/** 
 * function _webform_help_mailbuild
 * Module specific instance of hook_help
 **/
function _webform_help_mailbuild($section) {
  switch ($section) {
    case 'admin/settings/webform#mailbuild_description':
      $output = t("Allows creation of a 'Subscribe to Newsletter' checkbox which will subscribe the user to a Mailbuild or Campaign Monitor subscription list.");
      break;
  }
  return $output;
}

/** 
 * function _webform_analysis_view_mailbuild
 * Calculate and returns statistics about results for this component from all submission to this webform. The output of this function will be displayed under the "results" tab then "analysis"
 * @param $component An array of information describing the component, directly correlating to the webform_component database schema
 * @returns An array of data rows, each containing a statistic for this component's submissions.
 **/
function _webform_analysis_rows_mailbuild ($component) {
    $query = 'SELECT data, count(data) as datacount '.
      ' FROM {webform_submitted_data} '.
      ' WHERE nid = %d '.
      ' AND cid = %d '.
      " AND data != '' ".
      ' GROUP BY data ';
    $result = db_query($query, $component['nid'], $component['cid']);
    $rows = array();
    while ($data = db_fetch_array($result)) {
      $rows[] = array($data['data']==1?'Yes':'No', $data['datacount']);
    }
    return $rows;
}

/** 
 * function _webform_table_data_mailbuild
 * Return the result of this component's submission for display in a table. The output of this function will be displayed under the "results" tab then "table"
 * @param $data An array of information containing the submission result, directly correlating to the webform_submitted_data database schema
 * @returns Textual output formatted for human reading.
 **/
function _webform_table_data_mailbuild ($data) {
  return $data['value'][0] ? 'Yes' : 'No';
}


/** 
 * function _webform_csv_headers_mailbuild
 * Return the header information for this component to be displayed in a comma seperated value file. The output of this function will be displayed under the "results" tab then "download"
 * @param $component An array of information describing the component, directly correlating to the webform_component database schema
 * @returns An array of data to be displayed in the first three rows of a CSV file, not including either prefixed or trailing commas
 **/
function _webform_csv_headers_mailbuild ($component) {
  return array('','',$component['name']);
}

/** 
 * function _webform_csv_data_mailbuild
 * Return the result of a textfield submission. The output of this function will be displayed under the "results" tab then "submissions"
 * @param $data An array of information containing the submission result, directly correlating to the webform_submitted_data database schema
 * @returns Textual output formatted for CSV, not including either prefixed or trailing commas
 **/
function _webform_csv_data_mailbuild ($data,$component) {
  return $data['value'][0] ? 'Yes' : 'No';
}
