/*
* Check to see if the form is in the sources table
*/
- $result = $this->wpdb->get_row(
+ $source_id = $this->wpdb->get_var(
$this->wpdb->prepare(
"SELECT id
FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "sources
WHERE form_id = %d",
$form['id']
- ),
- ARRAY_A
+ )
);
foreach ( $form['fields'] as $k => $v ) {
$inputs = $choices = array();
}
break;
case 'checkbox':
+ $form_enabled = true;
+ // check the inputName to see if it matches a group
+ if ( $groups ) {
+ foreach ( $groups as $group ) {
+ if ( $v['inputName'] == $group['title'] ) {
+ $interestsData = new GlmDataInterests( $this->wpdb, $this->config );
+ $sql = "
+ SELECT *
+ FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "interests
+ WHERE group_id = %d";
+ $interests = $this->wpdb->get_results(
+ $this->wpdb->prepare(
+ $sql,
+ $group['id']
+ ),
+ ARRAY_A
+ );
+ //echo '<pre>$interests: ' . print_r( $interests, true ) . '</pre>';
+ if ( $interests ) {
+ foreach ( $interests as $interest ) {
+ $gf_field_id = $this->wpdb->get_var(
+ $this->wpdb->prepare(
+ "SELECT gf_field_id
+ FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "gf_interests
+ WHERE form_id = %d
+ AND interest_id = %d",
+ $form['id'],
+ $interest['id']
+ )
+ );
+ // @TODO If not found then add to the gf_interests
+ $inputs[] = array(
+ 'id' => (
+ ( $gf_field_id )
+ ? $gf_field_id
+ : $v['id'] . '.' . $interest['id']
+ ),
+ 'label' => $interest['title']
+ );
+ $choices[] = array(
+ 'text' => $interest['title'],
+ 'value' => $interest['title'],
+ 'isSelected' => false,
+ 'price' => ''
+ );
+ }
+ }
+ $form['fields'][$k]['inputs'] = $inputs;
+ $form['fields'][$k]['choices'] = $choices;
+ }
+ }
+ }
break;
}
}
- if ( !$result ) {
+ $source_data = array(
+ 'title' => $form['title'],
+ 'form_id' => $form['id'],
+ 'enabled' => $form_enabled
+ );
+ $source_data_format = array( '%s', '%d', '%d' );
+ if ( !$source_id ) {
$this->wpdb->insert(
GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'sources',
- array(
- 'title' => $form['title'],
- 'form_id' => $form['id'],
- 'enabled' => $form_enabled
- ),
- array( '%s', '%d', '%d' )
+ $source_data,
+ $source_data_format
);
+ } else {
+ // should only update if it will enable the form in sources table
+ if ( $form_enabled ) {
+ $this->wpdb->update(
+ GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'sources',
+ $source_data,
+ array( 'id' => $source_id ),
+ $source_data_format,
+ '%d'
+ );
+ }
}
// Return the form.
return $form;
* made by Gravity Forms.
*/
add_action( 'gform_after_submission', function( $entry, $form ){
+ $has_member_ok_field = false;
/*
* Get the source id for the form.
*/
- $result = $this->wpdb->get_row(
+ $source_id = $this->wpdb->get_var(
$this->wpdb->prepare(
"SELECT id
FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "sources
WHERE form_id = %d",
$form['id']
+ )
+ );
+ $leads_fields = array();
+ $mapped_keys = $this->config['mapped_keys'];
+ // get all the interests for this form
+ $gf_interests = array();
+ $gfInterestsData = $this->wpdb->get_results(
+ $this->wpdb->prepare(
+ "SELECT *
+ FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "gf_interests
+ WHERE form_id = %d",
+ $form['id']
),
ARRAY_A
);
- if ( $result ) {
- $source_id = $result['id'];
- } else {
- $this->wpdb->insert(
- GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'sources',
- array(
- 'title' => $form['title'],
- 'form_id' => $form['id'],
- 'enabled' => $form_enabled
- ),
- array( '%s', '%d', '%d' )
- );
- $source_id = $this->wpdb->insert_id;
+ if ( $gfInterestsData ) {
+ foreach ( $gfInterestsData as $gf_vals ) {
+ $gf_interests[$gf_vals['gf_field_id']] = $gf_vals['interest_id'];
+ }
+ }
+ if ( $gf_interests ) {
+ foreach ( $gf_interests as $gf_field_id => $interest_id ) {
+ if ( isset( $entry[$gf_field_id] ) && $entry[$gf_field_id] ) {
+ $leads_fields[] = $interest_id;
+ }
+ }
}
- $leads_fields = array();
- /*
- $mapped_keys = array(
- 'fname',
- 'lname',
- 'org',
- 'addr1',
- 'addr2',
- 'city',
- 'state',
- 'zip',
- 'country',
- 'email',
- 'phone',
- 'phone2',
- 'fax',
- 'mail_ok',
- 'member_ok',
- );
- */
- $mapped_keys = $this->config['mapped_keys'];
// have to go through the $form array to see what field we need from post
$glm_leads_entry = array();
if ( $form['fields'] ) {
switch ( $field['type'] ) {
case 'checkbox':
if ( in_array( $field['inputName'], $mapped_keys ) ) {
+ if ( $field['inputName'] === 'member_ok' ) {
+ $has_member_ok_field = true;
+ }
if ( isset( $_POST['input_' . $field['id'] . '_1'] ) && $_POST['input_' . $field['id'] . '_1'] ) {
$glm_leads_entry[$field['inputName']] = 1;
}
+ } else if ( $field['inputName'] ) {
+ // Here the field_id didn't match because it is new for
+ // this form
+ foreach ( $_POST as $key => $value ) {
+ if ( preg_match( '/input_' . $field['id'] . '_(\d*)/', $key, $matches ) ) {
+ $field_id = "{$field['id']}.{$matches[1]}";
+ // Look for no matches for this field_id in
+ // gf_interests table
+ $interest_id = $this->wpdb->get_var(
+ $this->wpdb->prepare(
+ "SELECT interest_id
+ FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "gf_interests
+ WHERE form_id = %d
+ AND gf_field_id = %s",
+ $form['id'],
+ $field_id
+ )
+ );
+ if ( !$interest_id ) {
+ $interest_id = $this->wpdb->get_var(
+ $this->wpdb->prepare(
+ "SELECT id
+ FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "interests
+ WHERE title = %s",
+ htmlspecialchars( $value )
+ )
+ );
+ if ( $interest_id ) {
+ $this->wpdb->insert(
+ GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'gf_interests',
+ array(
+ 'interest_id' => $interest_id,
+ 'gf_field_id' => $field_id,
+ 'form_id' => $form['id']
+ ),
+ array( '%d', '%s', '%d' )
+ );
+ $leads_fields[] = $interest_id;
+ }
+ } else {
+ if ( !in_array( $interest_id, $leads_fields ) ) {
+ $leads_fields[] = $interest_id;
+ }
+ }
+ }
+ }
}
break;
case 'website':
case 'phone':
case 'email':
+ case 'select':
case 'text':
if ( in_array( $field['inputName'], $mapped_keys ) ) {
$glm_leads_entry[$field['inputName']] = $_POST['input_' . $field['id']];
}
// create the lead
// check first to see if the lead exists by matching email address
- $result = $this->wpdb->get_row(
+ $lead_id = $this->wpdb->get_var(
$this->wpdb->prepare(
"SELECT id
FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "leads
WHERE email = %s",
$glm_leads_entry['email']
- ),
- ARRAY_A
+ )
);
- if ( $result['id'] ) {
- $lead_id = $result['id'];
+ if ( $has_member_ok_field === true ) {
+ $glm_lead_data = array(
+ 'email' => $glm_leads_entry['email'],
+ 'created' => $entry['date_created'],
+ 'mail_ok' => ( isset( $glm_leads_entry['mail_ok'] ) && $glm_leads_entry['mail_ok'] ) ? 1: 0,
+ 'member_ok' => ( isset( $glm_leads_entry['member_ok'] ) && $glm_leads_entry['member_ok'] ) ? 1: 0,
+ );
+ } else {
+ $glm_lead_data = array(
+ 'email' => $glm_leads_entry['email'],
+ 'created' => $entry['date_created'],
+ 'mail_ok' => ( isset( $glm_leads_entry['mail_ok'] ) && $glm_leads_entry['mail_ok'] ) ? 1: 0,
+ );
+ }
+ if ( $lead_id ) {
// will need to update the mail_ok or member fields
+ $this->wpdb->update(
+ GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'leads',
+ $glm_lead_data,
+ array( 'id' => $lead_id ),
+ '%s'
+ );
} else {
// this will need mail_ok and members to be setup also
$result = $this->wpdb->insert(
- GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'leads', // table
- array(
- 'email' => $glm_leads_entry['email'],
- 'created' => $entry['date_created'],
- 'mail_ok' => ( isset( $glm_leads_entry['mail_ok'] ) && $glm_leads_entry['mail_ok'] ) ? 1: 0,
- 'member_ok' => ( isset( $glm_leads_entry['member_ok'] ) && $glm_leads_entry['member_ok'] ) ? 1: 0,
- ), // data
- array( '%s', '%s', '%s', '%s' ) // data format
+ GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'leads',
+ $glm_lead_data,
+ '%s'
);
if ( $result === false ) {
die( 'lead error given' );
if ( !$lead_id ) {
return;
}
- //echo '<pre>$glm_leads_entry: ' . print_r( $glm_leads_entry, true ) . '</pre>';
- // create the lead entry
- $lead_entry = $this->wpdb->insert(
- GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'lead_entry', // table
- array(
- 'source_id' => $source_id,
- 'lead_id' => $lead_id,
- 'fname' => ( isset( $glm_leads_entry['fname'] ) ) ? $glm_leads_entry['fname'] : '',
- 'lname' => ( isset( $glm_leads_entry['lname'] ) ) ? $glm_leads_entry['lname'] : '',
- 'lname' => ( isset( $glm_leads_entry['lname'] ) ) ? $glm_leads_entry['lname'] : '',
- 'addr1' => ( isset( $glm_leads_entry['addr1'] ) ) ? $glm_leads_entry['addr1'] : '',
- 'org' => ( isset( $glm_leads_entry['org'] ) ) ? $glm_leads_entry['org'] : '',
- 'city' => ( isset( $glm_leads_entry['city'] ) ) ? $glm_leads_entry['city'] : '',
- 'state' => ( isset( $glm_leads_entry['state'] ) ) ? $glm_leads_entry['state'] : '',
- 'zip' => ( isset( $glm_leads_entry['zip'] ) ) ? $glm_leads_entry['zip'] : '',
- 'country' => ( isset( $glm_leads_entry['country'] ) ) ? $glm_leads_entry['country'] : '',
- 'phone' => ( isset( $glm_leads_entry['phone'] ) ) ? $glm_leads_entry['phone'] : '',
- 'phone2' => ( isset( $glm_leads_entry['phone2'] ) ) ? $glm_leads_entry['phone2'] : '',
- 'fax' => ( isset( $glm_leads_entry['fax'] ) ) ? $glm_leads_entry['fax'] : '',
- 'user_trace_info' => $entry['ip'],
- 'date_submitted' => $entry['date_created']
- ), // data
- array(
- '%d',
- '%d',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- '%s',
- ) // data format
+ /*
+ * Create the lead entry
+ * Remember to update the Lead Data Format array if you add or delete from
+ * this array
+ */
+ $lead_data = array(
+ 'gf_entry_id' => $entry['id'],
+ 'source_id' => $source_id,
+ 'lead_id' => $lead_id,
+ 'fname' => ( isset( $glm_leads_entry['fname'] ) ) ? $glm_leads_entry['fname'] : '',
+ 'lname' => ( isset( $glm_leads_entry['lname'] ) ) ? $glm_leads_entry['lname'] : '',
+ 'lname' => ( isset( $glm_leads_entry['lname'] ) ) ? $glm_leads_entry['lname'] : '',
+ 'addr1' => ( isset( $glm_leads_entry['addr1'] ) ) ? $glm_leads_entry['addr1'] : '',
+ 'addr2' => ( isset( $glm_leads_entry['addr2'] ) ) ? $glm_leads_entry['addr2'] : '',
+ 'org' => ( isset( $glm_leads_entry['org'] ) ) ? $glm_leads_entry['org'] : '',
+ 'city' => ( isset( $glm_leads_entry['city'] ) ) ? $glm_leads_entry['city'] : '',
+ 'state' => ( isset( $glm_leads_entry['state'] ) ) ? $glm_leads_entry['state'] : '',
+ 'zip' => ( isset( $glm_leads_entry['zip'] ) ) ? $glm_leads_entry['zip'] : '',
+ 'country' => ( isset( $glm_leads_entry['country'] ) ) ? $glm_leads_entry['country'] : '',
+ 'phone' => ( isset( $glm_leads_entry['phone'] ) ) ? $glm_leads_entry['phone'] : '',
+ 'phone2' => ( isset( $glm_leads_entry['phone2'] ) ) ? $glm_leads_entry['phone2'] : '',
+ 'fax' => ( isset( $glm_leads_entry['fax'] ) ) ? $glm_leads_entry['fax'] : '',
+ 'user_trace_info' => $entry['ip'],
+ 'date_submitted' => $entry['date_created']
+ );
+ $this->wpdb->insert(
+ GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'lead_entry',
+ $lead_data,
+ array( '%d', '%d', '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )
);
- if ( $lead_entry === false ) {
- die( 'lead_entry error given' );
- }
$lead_entry_id = $this->wpdb->insert_id;
- // grab the interest
- $interestsData = new GlmDataInterests( $this->wpdb, $this->config );
- $interests = $interestsData->getList();
-
- $ints = array();
+ // Grab the interest.
+ $interestsData = new GlmDataInterests( $this->wpdb, $this->config );
+ $interests = $interestsData->getList();
+ $ints = array();
if ( $interests ) {
foreach ( $interests as $interest ) {
$ints[$interest['id']] = htmlspecialchars_decode( $interest['title'] );
}
}
- // create the lead to interest entries
- // using $leads_fields
- // first delete any they current have
+ // Create the lead to interest entries.
+ // Using $leads_fields.
+ // First delete any they current have.
if ( is_array( $leads_fields) && !empty( $leads_fields ) ) {
foreach ( $leads_fields as $interest_val ) {
- $int_key = array_search( $interest_val, $ints );
- if ( $int_key ) {
- $this->wpdb->insert(
- GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'lead_interests', // table
+ $int_key = array_search( $interest_val, array_keys( $ints ) );
+ if ( $int_key !== false ) {
+ $isGood = $this->wpdb->insert(
+ GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'lead_interests',
array(
- 'interest_id' => $int_key,
+ 'interest_id' => $interest_val,
'lead_entry_id' => $lead_entry_id
- ), // data
- array( '%d', '%d' ) // data format
+ ),
+ array( '%d', '%d' )
);
+ if ( !$isGood ) {
+ die( 'We did not get lead_interests id' );
+ }
}
}
}