form.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 function __construct( $args = array() ) {
  40. $this->_args = wp_parse_args( $args, array(
  41. 'form_id' => false,
  42. 'target_field_id' => false,
  43. 'source_field_id' => false,
  44. 'format' => 'Y-m-d',
  45. 'modifier' => false
  46. ) );
  47. if( ! $this->_args['form_id'] || ! $this->_args['target_field_id'] ) {
  48. return;
  49. }
  50. add_action( 'init', array( $this, 'init' ) );
  51. }
  52. public function init() {
  53. if( $this->_args['source_field_id'] ) {
  54. add_action( 'gform_pre_submission', array( $this, 'populate_date_on_pre_submission' ) );
  55. } else {
  56. add_filter( 'gform_pre_render', array( $this, 'populate_date_on_pre_render' ) );
  57. }
  58. }
  59. public function populate_date_on_pre_render( $form ) {
  60. foreach( $form['fields'] as &$field ) {
  61. if( $field['id'] == $this->_args['target_field_id'] ) {
  62. $key = sprintf( 'gwpd_%d_%d', $form['id'], $field['id'] );
  63. $value = $this->get_modified_date( $field );
  64. $field['allowsPrepopulate'] = true;
  65. $field['inputName'] = $key;
  66. add_filter("gform_field_value_{$key}", create_function( '', 'return \'' . $value . '\';' ) );
  67. }
  68. }
  69. return $form;
  70. }
  71. public function populate_date_on_pre_submission( $form ) {
  72. foreach( $form['fields'] as &$field ) {
  73. if( $field['id'] == $this->_args['target_field_id'] ) {
  74. $timestamp = strtotime( rgpost( 'input_' . $this->_args['source_field_id'] ) );
  75. $value = $this->get_modified_date( $field, $timestamp );
  76. $_POST[ "input_{$field['id']}" ] = $value;
  77. }
  78. }
  79. }
  80. public function get_modified_date( $field, $timestamp = false ) {
  81. if( ! $timestamp ) {
  82. $timestamp = current_time( 'timestamp' );
  83. }
  84. if( GFFormsModel::get_input_type( $field ) == 'date' ) {
  85. list( $format, $divider ) = $field['dateFormat'] ? array_pad( explode( '_', $field['dateFormat' ] ), 2, 'slash' ) : array( 'mdy', 'slash' );
  86. $dividers = array( 'slash' => '/', 'dot' => '.', 'dash' => '-' );
  87. $format = str_replace( 'y', 'Y', $format );
  88. $divider = $dividers[$divider];
  89. $format = implode( $divider, str_split( $format ) );
  90. } else {
  91. $format = $this->_args['format'];
  92. }
  93. if( $this->_args['modifier'] ) {
  94. $date = date( $format, strtotime( $this->_args['modifier'], $timestamp ) );
  95. } else {
  96. $date = date( $format, $timestamp );
  97. }
  98. return $date;
  99. }
  100. }
  101. new DW_GF_Date( array(
  102. 'form_id' => 1,
  103. 'target_field_id' => 17,
  104. 'modifier' => '+4 months'
  105. ) );