Advertisement
Guest User

Untitled

a guest
Dec 11th, 2013
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.77 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Better Pre-submission Confirmation
  5. * http://gravitywiz.com/2012/08/04/better-pre-submission-confirmation/
  6. * How to get Gravity forms to generate dynamic confirmation message?
  7. */
  8.  
  9. class GWPreviewConfirmation {
  10.  
  11.     private static $lead;
  12.  
  13.     function init() {
  14.  
  15.         add_filter('gform_pre_render', array('GWPreviewConfirmation', 'replace_merge_tags'));
  16.         add_filter('gform_replace_merge_tags', array('GWPreviewConfirmation', 'product_summary_merge_tag'), 10, 3);
  17.  
  18.     }
  19.  
  20.     public static function replace_merge_tags($form) {
  21.  
  22.         $current_page = isset(GFFormDisplay::$submission[$form['id']]) ? GFFormDisplay::$submission[$form['id']]['page_number'] : 1;
  23.         $fields = array();
  24.  
  25.         // get all HTML fields on the current page
  26.         foreach($form['fields'] as &$field) {
  27.  
  28.             // skip all fields on the first page
  29.             if(rgar($field, 'pageNumber') <= 1)
  30.                 continue;
  31.  
  32.             $default_value = rgar($field, 'defaultValue');
  33.             preg_match_all('/{.+}/', $default_value, $matches, PREG_SET_ORDER);
  34.             if(!empty($matches)) {
  35.                 // if default value needs to be replaced but is not on current page, wait until on the current page to replace it
  36.                 if(rgar($field, 'pageNumber') != $current_page) {
  37.                     $field['defaultValue'] = '';
  38.                 } else {
  39.                     $field['defaultValue'] = self::preview_replace_variables($default_value, $form);
  40.                 }
  41.             }
  42.  
  43.             // only run 'content' filter for fields on the current page
  44.             if(rgar($field, 'pageNumber') != $current_page)
  45.                 continue;
  46.  
  47.             $html_content = rgar($field, 'content');
  48.             preg_match_all('/{.+}/', $html_content, $matches, PREG_SET_ORDER);
  49.             if(!empty($matches)) {
  50.                 $field['content'] = self::preview_replace_variables($html_content, $form);
  51.             }
  52.  
  53.         }
  54.  
  55.         return $form;
  56.     }
  57.  
  58.     /**
  59.     * Adds special support for file upload, post image and multi input merge tags.
  60.     */
  61.     public static function preview_special_merge_tags($value, $input_id, $merge_tag, $field) {
  62.        
  63.         // added to prevent overriding :noadmin filter (and other filters that remove fields)
  64.         if( !$value )
  65.             return $value;
  66.        
  67.         $input_type = RGFormsModel::get_input_type($field);
  68.        
  69.         $is_upload_field = in_array( $input_type, array('post_image', 'fileupload') );
  70.         $is_multi_input = is_array( rgar($field, 'inputs') );
  71.         $is_input = intval( $input_id ) != $input_id;
  72.        
  73.         if( !$is_upload_field && !$is_multi_input )
  74.             return $value;
  75.  
  76.         // if is individual input of multi-input field, return just that input value
  77.         if( $is_input )
  78.             return $value;
  79.            
  80.         $form = RGFormsModel::get_form_meta($field['formId']);
  81.         $lead = self::create_lead($form);
  82.         $currency = GFCommon::get_currency();
  83.  
  84.         if(is_array(rgar($field, 'inputs'))) {
  85.             $value = RGFormsModel::get_lead_field_value($lead, $field);
  86.             return GFCommon::get_lead_field_display($field, $value, $currency);
  87.         }
  88.  
  89.         switch($input_type) {
  90.         case 'fileupload':
  91.             $value = self::preview_image_value("input_{$field['id']}", $field, $form, $lead);
  92.             $value = self::preview_image_display($field, $form, $value);
  93.             break;
  94.         default:
  95.             $value = self::preview_image_value("input_{$field['id']}", $field, $form, $lead);
  96.             $value = GFCommon::get_lead_field_display($field, $value, $currency);
  97.             break;
  98.         }
  99.  
  100.         return $value;
  101.     }
  102.  
  103.     public static function preview_image_value($input_name, $field, $form, $lead) {
  104.  
  105.         $field_id = $field['id'];
  106.         $file_info = RGFormsModel::get_temp_filename($form['id'], $input_name);
  107.         $source = RGFormsModel::get_upload_url($form['id']) . "/tmp/" . $file_info["temp_filename"];
  108.  
  109.         if(!$file_info)
  110.             return '';
  111.  
  112.         switch(RGFormsModel::get_input_type($field)){
  113.  
  114.             case "post_image":
  115.                 list(,$image_title, $image_caption, $image_description) = explode("|:|", $lead[$field['id']]);
  116.                 $value = !empty($source) ? $source . "|:|" . $image_title . "|:|" . $image_caption . "|:|" . $image_description : "";
  117.                 break;
  118.  
  119.             case "fileupload" :
  120.                 $value = $source;
  121.                 break;
  122.  
  123.         }
  124.  
  125.         return $value;
  126.     }
  127.  
  128.     public static function preview_image_display($field, $form, $value) {
  129.  
  130.         // need to get the tmp $file_info to retrieve real uploaded filename, otherwise will display ugly tmp name
  131.         $input_name = "input_" . str_replace('.', '_', $field['id']);
  132.         $file_info = RGFormsModel::get_temp_filename($form['id'], $input_name);
  133.  
  134.         $file_path = $value;
  135.         if(!empty($file_path)){
  136.             $file_path = esc_attr(str_replace(" ", "%20", $file_path));
  137.             $value = "<a href='$file_path' target='_blank' title='" . __("Click to view", "gravityforms") . "'>" . $file_info['uploaded_filename'] . "</a>";
  138.         }
  139.         return $value;
  140.  
  141.     }
  142.  
  143.     /**
  144.     * Retrieves $lead object from class if it has already been created; otherwise creates a new $lead object.
  145.     */
  146.     public static function create_lead( $form ) {
  147.        
  148.         if( empty( self::$lead ) ) {
  149.             self::$lead = RGFormsModel::create_lead( $form );
  150.             self::clear_field_value_cache( $form );
  151.         }
  152.        
  153.         return self::$lead;
  154.     }
  155.  
  156.     public static function preview_replace_variables($content, $form) {
  157.  
  158.         $lead = self::create_lead($form);
  159.  
  160.         // add filter that will handle getting temporary URLs for file uploads and post image fields (removed below)
  161.         // beware, the RGFormsModel::create_lead() function also triggers the gform_merge_tag_filter at some point and will
  162.         // result in an infinite loop if not called first above
  163.         add_filter('gform_merge_tag_filter', array('GWPreviewConfirmation', 'preview_special_merge_tags'), 10, 4);
  164.  
  165.         $content = GFCommon::replace_variables($content, $form, $lead, false, false, false);
  166.  
  167.         // remove filter so this function is not applied after preview functionality is complete
  168.         remove_filter('gform_merge_tag_filter', array('GWPreviewConfirmation', 'preview_special_merge_tags'));
  169.  
  170.         return $content;
  171.     }
  172.  
  173.     public static function product_summary_merge_tag($text, $form, $lead) {
  174.  
  175.         if(empty($lead))
  176.             $lead = self::create_lead($form);
  177.  
  178.         $remove = array("<tr bgcolor=\"#EAF2FA\">\n                            <td colspan=\"2\">\n                                <font style=\"font-family: sans-serif; font-size:12px;\"><strong>Order</strong></font>\n                            </td>\n                       </tr>\n                       <tr bgcolor=\"#FFFFFF\">\n                            <td width=\"20\">&nbsp;</td>\n                            <td>\n                                ", "\n                            </td>\n                        </tr>");
  179.         $product_summary = str_replace($remove, '', GFCommon::get_submitted_pricing_fields($form, $lead, 'html'));
  180.  
  181.         return str_replace('{product_summary}', $product_summary, $text);
  182.     }
  183.    
  184.     public static function clear_field_value_cache( $form ) {
  185.        
  186.         if( ! class_exists( 'GFCache' ) )
  187.             return;
  188.            
  189.         foreach( $form['fields'] as &$field ) {
  190.             if( GFFormsModel::get_input_type( $field ) == 'total' )
  191.                 GFCache::delete( 'GFFormsModel::get_lead_field_value__' . $field['id'] );
  192.         }
  193.        
  194.     }
  195.  
  196. }
  197.  
  198. GWPreviewConfirmation::init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement