form.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. add_filter('gform_field_content', 'boot_gf', 10, 5);
  3. function boot_gf ($content, $field, $value, $lead_id, $form_id) {
  4. if($field["type"] != 'hidden' && $field["type"] != 'list' && $field["type"] != 'multiselect' && $field["type"] != 'checkbox' && $field["type"] != 'fileupload' && $field["type"] != 'date' && $field["type"] != 'html' && $field["type"] != 'address') {
  5. $content = str_replace('class=\'medium', 'class=\'form-control medium', $content);
  6. }
  7. if($field["type"] == 'name' || $field["type"] == 'address') {
  8. //$content = str_replace('<input ', '<input class=\'form-control\' ', $content);
  9. }
  10. if($field["type"] == 'textarea') {
  11. $content = str_replace('class=\'textarea', 'class=\'form-control medium textarea', $content);
  12. }
  13. if($field["type"] == 'checkbox') {
  14. $content = str_replace('li class=\'', 'li class=\'checkbox ', $content);
  15. $content = str_replace('<input ', '<input style=\'margin-left:1px;\' ', $content);
  16. }
  17. if($field["type"] == 'radio') {
  18. $content = str_replace('li class=\'', 'li class=\'radio ', $content);
  19. $content = str_replace('<input ', '<input style=\'margin-left:1px;\' ', $content);
  20. }
  21. return $content;
  22. }
  23. add_filter('gform_validation_message', 'change_validation_message', 10, 2);
  24. function change_validation_message($message, $form)
  25. {
  26. return "<div class='validation_error'><strong>Oops!</strong> Looks like something is missing.</div>";
  27. }
  28. add_filter( 'gform_submit_button', 'form_submit_button', 10, 5 );
  29. function form_submit_button ( $button, $form ){
  30. $button = str_replace( "input", "button", $button );
  31. $button = str_replace( "/", "", $button );
  32. $button .= "{$form['button']['text']}</button>";
  33. return $button;
  34. }
  35. /*============================================
  36. GF- Date Assist
  37. ==============================================*/
  38. class DW_GF_Date {
  39. public $_args;
  40. public function __construct( $args = array() ) {
  41. $this->_args = wp_parse_args( $args, array(
  42. 'form_id' => false,
  43. 'target_field_id' => false,
  44. 'source_field_id' => false,
  45. 'format' => 'Y-m-d',
  46. 'modifier' => false
  47. ) );
  48. if( ! $this->_args['form_id'] || ! $this->_args['target_field_id'] ) {
  49. return;
  50. }
  51. add_action( 'init', array( $this, 'init' ) );
  52. }
  53. public function init() {
  54. if( $this->_args['source_field_id'] ) {
  55. add_action( 'gform_pre_submission', array( $this, 'populate_date_on_pre_submission' ) );
  56. } else {
  57. add_filter( 'gform_pre_render', array( $this, 'populate_date_on_pre_render' ) );
  58. }
  59. }
  60. public function populate_date_on_pre_render( $form ) {
  61. foreach( $form['fields'] as &$field ) {
  62. if( $field['id'] == $this->_args['target_field_id'] ) {
  63. $key = sprintf( 'gwpd_%d_%d', $form['id'], $field['id'] );
  64. $value = $this->get_modified_date( $field );
  65. $field['allowsPrepopulate'] = true;
  66. $field['inputName'] = $key;
  67. add_filter("gform_field_value_{$key}", create_function( '', 'return \'' . $value . '\';' ) );
  68. }
  69. }
  70. return $form;
  71. }
  72. public function populate_date_on_pre_submission( $form ) {
  73. foreach( $form['fields'] as &$field ) {
  74. if( $field['id'] == $this->_args['target_field_id'] ) {
  75. $timestamp = strtotime( rgpost( 'input_' . $this->_args['source_field_id'] ) );
  76. $value = $this->get_modified_date( $field, $timestamp );
  77. $_POST[ "input_{$field['id']}" ] = $value;
  78. }
  79. }
  80. }
  81. public function get_modified_date( $field, $timestamp = false ) {
  82. if( ! $timestamp ) {
  83. $timestamp = current_time( 'timestamp' );
  84. }
  85. if( GFFormsModel::get_input_type( $field ) == 'date' ) {
  86. list( $format, $divider ) = $field['dateFormat'] ? array_pad( explode( '_', $field['dateFormat' ] ), 2, 'slash' ) : array( 'mdy', 'slash' );
  87. $dividers = array( 'slash' => '/', 'dot' => '.', 'dash' => '-' );
  88. $format = str_replace( 'y', 'Y', $format );
  89. $divider = $dividers[$divider];
  90. $format = implode( $divider, str_split( $format ) );
  91. } else {
  92. $format = $this->_args['format'];
  93. }
  94. if( $this->_args['modifier'] ) {
  95. $date = date( $format, strtotime( $this->_args['modifier'], $timestamp ) );
  96. } else {
  97. $date = date( $format, $timestamp );
  98. }
  99. return $date;
  100. }
  101. }
  102. new DW_GF_Date( array(
  103. 'form_id' => 1,
  104. 'target_field_id' => 17,
  105. 'modifier' => '+4 months'
  106. ) );