Don't like ads? PRO users don't see any ads ;-)

gravity form preview shortcode

By: chrismccoy on Jun 3rd, 2012  |  syntax: PHP  |  size: 4.25 KB  |  hits: 84  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. add_shortcode('gform_preview', 'do_gform_preview');
  4.  
  5. function do_gform_preview($atts, $content = null) {
  6.     extract( shortcode_atts( array(
  7.         'field' => '',
  8.         'raw' => false,
  9.     ), $atts ) );
  10.    
  11.     $form_id = rgpost('gform_submit');
  12.     $field_id = $field;
  13.    
  14.     if(!$field_id || !$form_id)
  15.         return '';
  16.    
  17.     $form = RGFormsModel::get_form_meta($form_id);
  18.     $field = RGFormsModel::get_field($form, $field_id);
  19.     $is_hidden = RGFormsModel::is_field_hidden($form, $field, array());
  20.    
  21.     if($is_hidden)
  22.         return '';
  23.    
  24.     if(is_array($field['inputs']) && intval($field_id) == $field_id) {
  25.         $value = array();
  26.         foreach($field['inputs'] as $input) {
  27.             $value[(string)$input['id']] = gform_get_input_value($input['id'], $field, $form);
  28.         }
  29.     } else {
  30.         $value = gform_get_input_value($field_id, $field, $form);
  31.     }
  32.    
  33.     if(!$raw) {
  34.         if(!$field) {
  35.             $display_value = rgpost($field_id);
  36.         } else if(in_array(RGFormsModel::get_input_type($field), array('fileupload'))) {
  37.             $display_value = gform_preview_image_display($field, $value, $form);
  38.         } else {
  39.             $currency = GFCommon::get_currency();
  40.             $display_value = GFCommon::get_lead_field_display($field, $value, $currency);
  41.         }
  42.        
  43.         $display_value = !$display_value && !array_empty($value) ? $value : $display_value;
  44.        
  45.     } else {
  46.         $display_value = $value;
  47.     }
  48.    
  49.     if($content && $display_value)
  50.         $display_value = sprintf($content, $display_value);
  51.    
  52.     $display_value = apply_filters('gws_preview_value', $display_value, $value, $content);
  53.    
  54.     return do_shortcode($display_value);
  55. }
  56.  
  57. function gform_get_input_value($input_id, $field, $form) {
  58.    
  59.     $input_name = "input_" . str_replace('.', '_', $input_id);
  60.     $value = rgpost($input_name);
  61.    
  62.     /*if(!$value)
  63.         $value = RGFormsModel::get_default_value($field, $input_id);*/
  64.    
  65.     if(in_array(RGFormsModel::get_input_type($field), array('fileupload', 'post_image'))) {
  66.         return gform_preview_image_value($input_name, $field, $form);
  67.     }
  68.    
  69.     return RGFormsModel::prepare_value($form, $field, $value, $input_name, false);
  70. }
  71.  
  72. function gform_preview_image_value($input_name, $field, $form, $raw = false) {
  73.    
  74.     $file_info = RGFormsModel::get_temp_filename($form['id'], $input_name);
  75.     $source = RGFormsModel::get_upload_url($form['id']) . "/tmp/" . $file_info["temp_filename"];
  76.    
  77.     if(!$file_info)
  78.         return '';
  79.    
  80.     switch(RGFormsModel::get_input_type($field)){
  81.        
  82.         case "post_image":
  83.             $image_title = isset($_POST["{$input_name}_1"]) ? strip_tags($_POST["{$input_name}_1"]) : "";
  84.             $image_caption = isset($_POST["{$input_name}_4"]) ? strip_tags($_POST["{$input_name}_4"]) : "";
  85.             $image_description = isset($_POST["{$input_name}_7"]) ? strip_tags($_POST["{$input_name}_7"]) : "";
  86.  
  87.             $value = !empty($source) ? $source . "|:|" . $image_title . "|:|" . $image_caption . "|:|" . $image_description : "";
  88.             break;
  89.  
  90.         case "fileupload" :
  91.             $value = $source;
  92.             break;
  93.            
  94.     }
  95.    
  96.     return $value;
  97. }
  98.  
  99. function gform_preview_image_display($field, $value, $form) {
  100.    
  101.     switch(RGFormsModel::get_input_type($field)){
  102.  
  103.         case "fileupload" :
  104.            
  105.             $input_name = "input_" . str_replace('.', '_', $field['id']);
  106.             $file_info = RGFormsModel::get_temp_filename($form['id'], $input_name);
  107.            
  108.             $file_path = $value;
  109.             if(!empty($file_path)){
  110.                 $file_path = esc_attr(str_replace(" ", "%20", $file_path));
  111.                 $value = "<a href='$file_path' target='_blank' title='" . __("Click to view", "gravityforms") . "'>" . $file_info['uploaded_filename'] . "</a>";
  112.             }
  113.             return $value;
  114.             break;
  115.            
  116.     }
  117.    
  118.     return $value;
  119. }
  120.  
  121. if(!function_exists('array_empty')) {
  122.     function array_empty($mixed) {
  123.         if (is_array($mixed)) {
  124.             foreach ($mixed as $value) {
  125.                 if (!array_empty($value)) {
  126.                     return false;
  127.                 }
  128.             }
  129.         }
  130.         elseif (!empty($mixed)) {
  131.             return false;
  132.         }
  133.         return true;
  134.     }
  135. }