Created
August 2, 2013 11:53
-
-
Save javorszky/6139329 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This function is responsible to add multiple fields into the input. | |
* Without this the subfields won't even get mentioned in the form. | |
* Also, this is only true if the form does not have a field of this | |
* type yet. If it does, and you change this, the changes will NOT | |
* show up in the form. | |
* | |
* For reference, go to /plugins/gravityforms/js.php | |
* function SetDefaultValues (line 363 in version 1.7.5) | |
*/ | |
function add_custom_default_values () { | |
?> | |
case "amadeus" : | |
if(!field.label) | |
field.label = "<?php _e("Amadeus Delegate Info", "gravityforms"); ?>"; | |
field.inputs = [ | |
new Input( field.id + 0.11, '<?php echo esc_js(__("Title", "gravityforms")); ?>'), | |
new Input( field.id + 0.12, '<?php echo esc_js(__("First Name", "gravityforms")); ?>'), | |
new Input( field.id + 0.13, '<?php echo esc_js(__("Last Name", "gravityforms")); ?>'), | |
new Input( field.id + 0.14, '<?php echo esc_js(__("Telephone", "gravityforms")); ?>'), | |
new Input( field.id + 0.15, '<?php echo esc_js(__("Email", "gravityforms")); ?>'), | |
new Input( field.id + 0.16, '<?php echo esc_js(__("Company Name", "gravityforms")); ?>'), | |
new Input( field.id + 0.17, '<?php echo esc_js(__("Department", "gravityforms")); ?>'), | |
new Input( field.id + 0.18, '<?php echo esc_js(__("Address", "gravityforms")); ?>'), | |
new Input( field.id + 0.19, '<?php echo esc_js(__("Town", "gravityforms")); ?>'), | |
new Input( field.id + 0.21, '<?php echo esc_js(__("County", "gravityforms")); ?>'), | |
new Input( field.id + 0.22, '<?php echo esc_js(__("Postcode", "gravityforms")); ?>') | |
]; | |
break; | |
<?php | |
} | |
add_filter('gform_editor_js_set_default_values', 'add_custom_default_values'); | |
/** | |
* This adds the button to the standard fields list on the right side | |
* so people can add this. | |
* @param array $field_group The list of existing buttons | |
*/ | |
function add_es_namefield($field_group){ | |
foreach($field_group as &$group) { | |
if($group['name'] === 'standard_fields') { | |
$group['fields'][] = array( | |
"class" => "button", | |
"value" => __("Amadeus Delegate", "gravityforms"), | |
"onclick" => "StartAddField('amadeus');" | |
); | |
break; | |
} | |
} | |
return $field_group; | |
} | |
add_filter('gform_add_field_buttons', 'add_es_namefield'); | |
/** | |
* Modifies the title for the field in question | |
* @param string $type Field Type | |
* @return string New Field title | |
*/ | |
function title_name_field_title( $type ) { | |
if ( $type == 'amadeus' ) | |
return __( 'Amadeus Delegate' , 'gravityforms' ); | |
} | |
add_filter( 'gform_field_type_title' , 'title_name_field_title' ); | |
/** | |
* This is needed to add all the functionality to the settings section | |
* of the field window, like title, required, etc... | |
*/ | |
function title_name_editor_js(){ | |
?> | |
<script type='text/javascript'> | |
jQuery(document).ready(function($) { | |
fieldSettings["amadeus"] = ".label_setting, .description_setting, .admin_label_setting, .error_message_setting, .css_class_setting, .visibility_setting, .conditional_logic_field_setting, .rules_setting, .visibility_setting"; | |
}); | |
</script> | |
<?php | |
} | |
add_action( "gform_editor_js", "title_name_editor_js" ); | |
/** | |
* Adds the field markup | |
* @param string $content The existing content | |
* @param array $field The field in question | |
* @param string $value values (defaults) | |
* @param int $lead_id the ID of the entry | |
* @param int $form_id the ID of the form | |
*/ | |
function add_title_name_field($input, $field, $value, $lead_id, $form_id) { | |
if($field['type'] === 'amadeus' ) { | |
if(!empty($value)){ | |
foreach ($value as $key => $val) { | |
$newkey = str_replace('.', '_', $key); | |
$value[$newkey] = $val; | |
unset($value[$key]); | |
} | |
} | |
$formid = 'input_'.$form_id; | |
$input = sprintf("<div class='ginput_complex ginput_container row' id='%s'><div class='six columns'><label for='%s'>%s<span class='gfield_required'>*</span></label><span id='%s' class='%s'><input type='text' id='%s' name='%s' value='%s' tabindex='%s'/></span><span id='%s' class='%s'><input type='text' id='%s' name='%s' value='%s' tabindex='%s'/></span></div><div class='six columns'><label for='%s'>%s<span class='gfield_required'>*</span></label><span id='%s' class='%s'><input type='text' id='%s' name='%s' value='%s' tabindex='%s'/></span></div><div class='six columns'><label for='%s'>%s<span class='gfield_required'>*</span></label><span id='%s' class='%s'><input type='text' id='%s' name='%s' value='%s' tabindex='%s'/></span></div><div class='six columns'><label for='%s'>%s<span class='gfield_required'>*</span></label><span id='%s' class='%s'><input type='text' id='%s' name='%s' value='%s' tabindex='%s'/></span></div><div class='six columns'><label for='%s'>%s<span class='gfield_required'>*</span></label><span id='%s' class='%s'><input type='text' id='%s' name='%s' value='%s' tabindex='%s'/></span></div><div class='six columns'><label for='%s'>%s<span class='gfield_required'>*</span></label><span id='%s' class='%s'><input type='text' id='%s' name='%s' value='%s' tabindex='%s'/></span></div><div class='six columns textarea'><label for='%s'>%s<span class='gfield_required'>*</span></label><span id='%s' class='%s'><textarea id='%s' name='%s' tabindex='%s'/>%s</textarea></span></div><div class='six columns'><label for='%s'>%s<span class='gfield_required'>*</span></label><span id='%s' class='%s'><input type='text' id='%s' name='%s' value='%s' tabindex='%s'/></span><label for='%s'>%s<span class='gfield_required'>*</span></label><span id='%s' class='%s'><input type='text' id='%s' name='%s' value='%s' tabindex='%s'/></span><label for='%s'>%s<span class='gfield_required'>*</span></label><span id='%s' class='%s'><input type='text' id='%s' name='%s' value='%s' tabindex='%s'/></span></div></div><div class='sep'></div>", | |
$formid, //container id | |
// Prefix and First name | |
'input_' . str_replace('.', '_', $field['inputs'][0]['id']), // label for | |
$field['inputs'][1]['label'], // Label text | |
'input_' . str_replace('.', '_', $field['inputs'][0]['id']) . '_container', // span id | |
$field['type'].'_'._ss($field['inputs'][0]['label']), // span class | |
'input_' . str_replace('.', '_', $field['inputs'][0]['id']), // input id | |
'input_' . str_replace('.', '_', $field['inputs'][0]['id']), // input name | |
$value[str_replace('.', '_', $field['inputs'][0]['id'])], // input value | |
GFCommon::get_tabindex(), // tabindex | |
'input_' . str_replace('.', '_', $field['inputs'][1]['id']) . '_container', // span id | |
$field['type'].'_'._ss($field['inputs'][1]['label']), // span class | |
'input_' . str_replace('.', '_', $field['inputs'][1]['id']), // input id | |
'input_' . str_replace('.', '_', $field['inputs'][1]['id']), // input name | |
$value[str_replace('.', '_', $field['inputs'][1]['id'])], // input value | |
GFCommon::get_tabindex(), // tabindex | |
// Last name | |
'input_' . str_replace('.', '_', $field['inputs'][2]['id']), // label for | |
$field['inputs'][2]['label'], // Label text | |
'input_' . str_replace('.', '_', $field['inputs'][2]['id']) . '_container', // span id | |
$field['type'].'_'._ss($field['inputs'][2]['label']), // span class | |
'input_' . str_replace('.', '_', $field['inputs'][2]['id']), // input id | |
'input_' . str_replace('.', '_', $field['inputs'][2]['id']), // input name | |
$value[str_replace('.', '_', $field['inputs'][2]['id'])], // input value | |
GFCommon::get_tabindex(), // tabindex | |
// Telephone | |
'input_' . str_replace('.', '_', $field['inputs'][3]['id']), // label for | |
$field['inputs'][3]['label'], // Label text | |
'input_' . str_replace('.', '_', $field['inputs'][3]['id']) . '_container', // span id | |
$field['type'].'_'._ss($field['inputs'][3]['label']), // span class | |
'input_' . str_replace('.', '_', $field['inputs'][3]['id']), // input id | |
'input_' . str_replace('.', '_', $field['inputs'][3]['id']), // input name | |
$value[str_replace('.', '_', $field['inputs'][3]['id'])], // input value | |
GFCommon::get_tabindex(), // tabindex | |
'input_' . str_replace('.', '_', $field['inputs'][4]['id']), // label for | |
$field['inputs'][4]['label'], // Label text | |
'input_' . str_replace('.', '_', $field['inputs'][4]['id']) . '_container', // span id | |
$field['type'].'_'._ss($field['inputs'][4]['label']), // span class | |
'input_' . str_replace('.', '_', $field['inputs'][4]['id']), // input id | |
'input_' . str_replace('.', '_', $field['inputs'][4]['id']), // input name | |
$value[str_replace('.', '_', $field['inputs'][4]['id'])], // input value | |
GFCommon::get_tabindex(), // tabindex | |
// Company Name | |
'input_' . str_replace('.', '_', $field['inputs'][5]['id']), // label for | |
$field['inputs'][5]['label'], // Label text | |
'input_' . str_replace('.', '_', $field['inputs'][5]['id']) . '_container', // span id | |
$field['type'].'_'._ss($field['inputs'][5]['label']), // span class | |
'input_' . str_replace('.', '_', $field['inputs'][5]['id']), // input id | |
'input_' . str_replace('.', '_', $field['inputs'][5]['id']), // input name | |
$value[str_replace('.', '_', $field['inputs'][5]['id'])], // input value | |
GFCommon::get_tabindex(), // tabindex | |
// Department | |
'input_' . str_replace('.', '_', $field['inputs'][6]['id']), // label for | |
$field['inputs'][6]['label'], // Label text | |
'input_' . str_replace('.', '_', $field['inputs'][6]['id']) . '_container', // span id | |
$field['type'].'_'._ss($field['inputs'][6]['label']), // span class | |
'input_' . str_replace('.', '_', $field['inputs'][6]['id']), // input id | |
'input_' . str_replace('.', '_', $field['inputs'][6]['id']), // input name | |
$value[str_replace('.', '_', $field['inputs'][6]['id'])], // input value | |
GFCommon::get_tabindex(), // tabindex | |
// Address | |
'input_' . str_replace('.', '_', $field['inputs'][7]['id']), // label for | |
$field['inputs'][7]['label'], // Label text | |
'input_' . str_replace('.', '_', $field['inputs'][7]['id']) . '_container', // span id | |
$field['type'].'_'._ss($field['inputs'][7]['label']), // span class | |
'input_' . str_replace('.', '_', $field['inputs'][7]['id']), // input id | |
'input_' . str_replace('.', '_', $field['inputs'][7]['id']), // input name | |
GFCommon::get_tabindex(), // tabindex | |
$value[str_replace('.', '_', $field['inputs'][7]['id'])], // input value | |
// Town | |
'input_' . str_replace('.', '_', $field['inputs'][8]['id']), // label for | |
$field['inputs'][8]['label'], // Label text | |
'input_' . str_replace('.', '_', $field['inputs'][8]['id']) . '_container', // span id | |
$field['type'].'_'._ss($field['inputs'][8]['label']), // span class | |
'input_' . str_replace('.', '_', $field['inputs'][8]['id']), // input id | |
'input_' . str_replace('.', '_', $field['inputs'][8]['id']), // input name | |
$value[str_replace('.', '_', $field['inputs'][8]['id'])], // input value | |
GFCommon::get_tabindex(), // tabindex | |
// County | |
'input_' . str_replace('.', '_', $field['inputs'][9]['id']), // label for | |
$field['inputs'][9]['label'], // Label text | |
'input_' . str_replace('.', '_', $field['inputs'][9]['id']) . '_container', // span id | |
$field['type'].'_'._ss($field['inputs'][9]['label']), // span class | |
'input_' . str_replace('.', '_', $field['inputs'][9]['id']), // input id | |
'input_' . str_replace('.', '_', $field['inputs'][9]['id']), // input name | |
$value[str_replace('.', '_', $field['inputs'][9]['id'])], // input value | |
GFCommon::get_tabindex(), // tabindex | |
// Postcode | |
'input_' . str_replace('.', '_', $field['inputs'][10]['id']), // label for | |
$field['inputs'][10]['label'], // Label text | |
'input_' . str_replace('.', '_', $field['inputs'][10]['id']) . '_container', // span id | |
$field['type'].'_'._ss($field['inputs'][10]['label']), // span class | |
'input_' . str_replace('.', '_', $field['inputs'][10]['id']), // input id | |
'input_' . str_replace('.', '_', $field['inputs'][10]['id']), // input name | |
$value[str_replace('.', '_', $field['inputs'][10]['id'])], // input value | |
GFCommon::get_tabindex() | |
); | |
} | |
return $input; | |
} | |
add_filter("gform_field_input", "add_title_name_field", 10, 5); | |
/** | |
* Helper function to convert label to something dom-safe | |
* @param string $string Unserialized string | |
* @return string normalized string | |
*/ | |
function _ss($string){ | |
return str_replace(' ', '_', strtolower($string)); | |
} | |
/** | |
* Modifies the entry column content. | |
* @param int $form_id the form id | |
* @param int $field_id the field id | |
* @param array $value the default value of the field | |
* @param array !lead the value saved for the field | |
* @param string $query_string the query sting | |
*/ | |
function extract_from_array($fragment, $array){ | |
$test = $fragment . '.'; | |
$new = array(); | |
foreach ($array as $key => $value) { | |
if(strpos($key, $test) === 0) { | |
$new[] = $key; | |
} | |
} | |
return $new; | |
} | |
function mess_with_entry_content($form_id, $field_id, $value, $lead, $query_string) { | |
$map = array( | |
'delegate1' => 1, | |
'delegate2' => 13, | |
'delegate3' => 11, | |
'delegate4' => 12, | |
'delegate5' => 14, | |
'delegate6' => 15, | |
'delegate7' => 16, | |
'delegate8' => 17 | |
); | |
$titles = array( | |
'11' => 'Title', | |
'12' => 'First Name', | |
'13' => 'Last Name', | |
'14' => 'Telephone', | |
'15' => 'Email', | |
'16' => 'Company Name', | |
'17' => 'Department', | |
'18' => 'Address', | |
'19' => 'Town', | |
'21' => 'County', | |
'22' => 'Postcode' | |
); | |
if(strpos($field_id, 'delegate') !== false) { | |
$keys = extract_from_array($map[$field_id], $lead); | |
$html = ''; | |
$html = '<ul class="_es_amadeus_content">'; | |
foreach ($keys as $key) { | |
$titlekey = str_replace($map[$field_id].'.', '', $key); | |
if(!empty($lead[$key])) { | |
$html .= '<li><span class="title">'.$titles[$titlekey].':</span><span class="value"> '.nl2br($lead[$key]).'</span></li>'; | |
} | |
} | |
$html .= '</ul>'; | |
echo $html; | |
} | |
} | |
add_action('gform_entries_column', 'mess_with_entry_content', 10, 5); | |
/** | |
* This adds the custom columns to the entries detail page | |
* @param array $entry_meta the initials | |
* @param int $form_id the form is | |
* @return array the new entry meta | |
*/ | |
function custom_entry_meta($entry_meta, $form_id){ | |
if($form_id == 7) { | |
for ($i = 1; $i < 9; $i++) { | |
$entry_meta['delegate'.$i] = array( | |
'label' => 'Delegate '.$i, | |
'is_numeric' => false, | |
// 'update_entry_meta_callback' => 'update_entry_meta', | |
'is_default_column' => true | |
); | |
} | |
} | |
return $entry_meta; | |
} | |
add_filter( 'gform_entry_meta', 'custom_entry_meta', 10, 2); | |
/** | |
* Adds custom validation for the field. Always required. | |
* @param array $validation_result holds the form and the current validation result as valid | |
* @return array the form with messages (if any) and validation status | |
*/ | |
function custom_validation($validation_result){ | |
$form = $validation_result["form"]; | |
foreach ($form["fields"] as &$field) { | |
// Is it hidden? | |
$is_hidden = RGFormsModel::is_field_hidden($form, $field, array()); | |
if($is_hidden){ | |
continue; | |
} | |
if($field["type"] === "amadeus") { | |
$errors = ''; | |
foreach ($field["inputs"] as &$input) { | |
$key = str_replace('.', '_', "input_{$input['id']}"); | |
$value = rgpost($key); | |
if(strpos($input['label'], "Email") !== false) { | |
if(!filter_var($value, FILTER_VALIDATE_EMAIL)) { | |
$errors .= '<p>Please fill out a valid email address.</p>'; | |
} | |
} elseif(strpos($input['label'], "Telephone") !== false) { | |
if(!is_numeric(preg_replace('/(\(|\)|\+|\ )/', '', $value))) { | |
$errors .= '<p>Telephone must be a number. Permitted other characters are +, (, ) and space.</p>'; | |
} | |
} else { | |
if(strlen($value) < 2) { | |
$errors .= '<p>' . $input['label'] . ' needs to be at least 2 characters long</p>'; | |
} | |
} | |
} | |
if(!empty($errors)) { | |
$validation_result['is_valid'] = false; | |
$field['failed_validation'] = true; | |
$field['validation_message'] = $errors; | |
} | |
} | |
} | |
$validation_result["form"] = $form; | |
return $validation_result; | |
} | |
add_filter("gform_validation", "custom_validation", 10, 4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment