GFPDFViewView_PDF

View_PDF.

Defined (1)

The class is defined in the following location(s).

/src/view/View_PDF.php  
  1. class View_PDF extends Helper_Abstract_View { 
  2.  
  3. /** 
  4. * Set the view's name 
  5. * @var string 
  6. * @since 4.0 
  7. */ 
  8. protected $view_type = 'PDF'; 
  9.  
  10. /** 
  11. * Holds the abstracted Gravity Forms API specific to Gravity PDF 
  12. * @var \GFPDF\Helper\Helper_Form 
  13. * @since 4.0 
  14. */ 
  15. protected $gform; 
  16.  
  17. /** 
  18. * Holds our log class 
  19. * @var \Monolog\Logger|LoggerInterface 
  20. * @since 4.0 
  21. */ 
  22. protected $log; 
  23.  
  24. /** 
  25. * Holds our Helper_Abstract_Options / Helper_Options_Fields object 
  26. * Makes it easy to access global PDF settings and individual form PDF settings 
  27. * @var \GFPDF\Helper\Helper_Abstract_Options 
  28. * @since 4.0 
  29. */ 
  30. protected $options; 
  31.  
  32. /** 
  33. * Holds our Helper_Data object 
  34. * which we can autoload with any data needed 
  35. * @var \GFPDF\Helper\Helper_Data 
  36. * @since 4.0 
  37. */ 
  38. protected $data; 
  39.  
  40. /** 
  41. * Holds our Helper_Misc object 
  42. * Makes it easy to access common methods throughout the plugin 
  43. * @var \GFPDF\Helper\Helper_Misc 
  44. * @since 4.0 
  45. */ 
  46. protected $misc; 
  47.  
  48. /** 
  49. * Holds our Helper_Templates object 
  50. * used to ease access to our PDF templates 
  51. * @var \GFPDF\Helper\Helper_Templates 
  52. * @since 4.0 
  53. */ 
  54. protected $templates; 
  55.  
  56. /** 
  57. * Setup our class by injecting all our dependancies 
  58. * @param array $data_cache An array of data to pass to the view 
  59. * @param \GFPDF\Helper\Helper_Form|Helper_Abstract_Form $gform Our abstracted Gravity Forms helper functions 
  60. * @param \Monolog\Logger|LoggerInterface $log Our logger class 
  61. * @param \GFPDF\Helper\Helper_Abstract_Options $options Our options class which allows us to access any settings 
  62. * @param \GFPDF\Helper\Helper_Data $data Our plugin data store 
  63. * @param \GFPDF\Helper\Helper_Misc $misc Our miscellanious methods 
  64. * @param \GFPDF\Helper\Helper_Templates $templates 
  65. * @since 4.0 
  66. */ 
  67. public function __construct( $data_cache = [], Helper_Abstract_Form $gform, LoggerInterface $log, Helper_Abstract_Options $options, Helper_Data $data, Helper_Misc $misc, Helper_Templates $templates ) { 
  68.  
  69. /** Call our parent constructor */ 
  70. parent::__construct( $data_cache ); 
  71.  
  72. /** Assign our internal variables */ 
  73. $this->gform = $gform; 
  74. $this->log = $log; 
  75. $this->options = $options; 
  76. $this->data = $data; 
  77. $this->misc = $misc; 
  78. $this->templates = $templates; 
  79.  
  80. /** 
  81. * Our PDF Generator 
  82. * @param array $entry The Gravity Forms Entry to process 
  83. * @param array $settings The Gravity Form PDF Settings 
  84. * @return void 
  85. * @since 4.0 
  86. */ 
  87. public function generate_pdf( $entry, $settings ) { 
  88.  
  89. $controller = $this->getController(); 
  90. $model = $controller->model; 
  91. $form = $this->gform->get_form( $entry['form_id'] ); 
  92.  
  93. $this->fix_wp_external_links_plugin_conflict(); 
  94.  
  95. /** 
  96. * Load our arguments that should be accessed by our PDF template 
  97. * @var array 
  98. */ 
  99. $args = $this->templates->get_template_arguments( 
  100. $form,  
  101. $this->misc->get_fields_sorted_by_id( $form['id'] ),  
  102. $entry,  
  103. $model->get_form_data( $entry ),  
  104. $settings,  
  105. $this->templates->get_config_class( $settings['template'] ),  
  106. $this->misc->get_legacy_ids( $entry['id'], $settings ) 
  107. ); 
  108.  
  109. /** 
  110. * Show $form_data array if requested 
  111. */ 
  112. if ( isset( $_GET['data'] ) && $this->gform->has_capability( 'gravityforms_view_settings' ) && isset( $args['form_data'] ) ) { 
  113. echo '<pre>'; 
  114. print_r( $args['form_data'] ); 
  115. echo '</pre>'; 
  116. exit; 
  117.  
  118. /** Enable Multicurrency support */ 
  119. $this->misc->maybe_add_multicurrency_support(); 
  120.  
  121. /** 
  122. * Set out our PDF abstraction class 
  123. */ 
  124. $pdf = new Helper_PDF( $entry, $settings, $this->gform, $this->data, $this->misc, $this->templates ); 
  125. $pdf->set_filename( $model->get_pdf_name( $settings, $entry ) ); 
  126.  
  127. try { 
  128.  
  129. /** Initialise our PDF helper class */ 
  130. $pdf->init(); 
  131. $pdf->set_template(); 
  132.  
  133. /** Increment our rudimentary PDF counter */ 
  134. $this->options->increment_pdf_count(); 
  135.  
  136. /** Set display type and allow user to override the behaviour */ 
  137. $settings['pdf_action'] = apply_filters( 'gfpdfe_pdf_output_type', $settings['pdf_action'] ); /** Backwards compat */ 
  138. if ( $settings['pdf_action'] == 'download' ) { 
  139. $pdf->set_output_type( 'download' ); 
  140.  
  141. /** Add Backwards compatibility support for our v3 Tier 2 Add-on */ 
  142. if ( isset( $settings['advanced_template'] ) && strtolower( $settings['advanced_template'] ) == 'yes' ) { 
  143.  
  144. /** Check if we should process this document using our legacy system */ 
  145. if ( $model->handle_legacy_tier_2_processing( $pdf, $entry, $settings, $args ) ) { 
  146. return true; 
  147.  
  148. /** Determine if we should show the print dialog box */ 
  149. if ( isset( $_GET['print'] ) ) { 
  150. $pdf->set_print_dialog( true ); 
  151.  
  152. /** Render the PDF template HTML */ 
  153. $pdf->render_html( $args ); 
  154.  
  155. /** Generate PDF */ 
  156. $pdf->generate(); 
  157.  
  158. } catch ( Exception $e ) { 
  159.  
  160. $this->log->addError( 'PDF Generation Error', [ 
  161. 'entry' => $entry,  
  162. 'settings' => $settings,  
  163. 'exception' => $e->getMessage(),  
  164. ] ); 
  165.  
  166. if ( $this->gform->has_capability( 'gravityforms_view_entries' ) ) { 
  167. wp_die( $e->getMessage() ); 
  168.  
  169. wp_die( esc_html__( 'There was a problem generating your PDF', 'gravity-forms-pdf-extended' ) ); 
  170.  
  171. /** 
  172. * The WP External Links plugin conflicts with Gravity PDF when trying to display the PDF 
  173. * This method disables the \WPEL_Front::scan() method which was causes the problem 
  174. * See https://github.com/GravityPDF/gravity-pdf/issues/386 
  175. * @since 4.1 
  176. */ 
  177. private function fix_wp_external_links_plugin_conflict() { 
  178. if ( function_exists( 'wpel_init' ) ) { 
  179. add_filter( 'wpel_apply_settings', function () { 
  180. return false; 
  181. } ); 
  182.  
  183.  
  184. /** 
  185. * Ensure a PHP extension is added to the end of the template name 
  186. * @param string $name The PHP template 
  187. * @return string 
  188. * @since 4.0 
  189. */ 
  190. public function get_template_filename( $name ) { 
  191. if ( substr( $name, -4 ) !== '.php' ) { 
  192. $name = $name . '.php'; 
  193.  
  194. return $name; 
  195.  
  196. /** 
  197. * Start the PDF HTML Generation Process 
  198. * @param array $entry The Gravity Forms Entry Array 
  199. * @param \GFPDF\Helper\Helper_Abstract_Model $model 
  200. * @param array $config Any configuration data passed in 
  201. * @return string The generated HTML 
  202. * @since 4.0 
  203. */ 
  204. public function process_html_structure( $entry, Helper_Abstract_Model $model, $config = [] ) { 
  205. /** Determine whether we should output or return the results */ 
  206. $config['meta'] = ( isset( $config['meta'] ) ) ? $config['meta'] : []; 
  207. $echo = ( isset( $config['meta']['echo'] ) ) ? $config['meta']['echo'] : true; /** whether to output or return the generated markup. Default is echo */ 
  208.  
  209. if ( ! $echo ) { 
  210. ob_start(); 
  211.  
  212. /** Generate the markup */ 
  213. ?> 
  214.  
  215. <div id="container"> 
  216. <?php 
  217. /** 
  218. * See https://gravitypdf.com/documentation/v4/gfpdf_pre_html_fields/ for more details about this action 
  219. * @since 4.1 
  220. */ 
  221. do_action( 'gfpdf_pre_html_fields', $entry, $config ); 
  222. ?> 
  223.  
  224. <?php $this->generate_html_structure( $entry, $model, $config ); ?> 
  225.  
  226. <?php 
  227. /** 
  228. * See https://gravitypdf.com/documentation/v4/gfpdf_post_html_fields/ for more details about this action 
  229. * @since 4.1 
  230. */ 
  231. do_action( 'gfpdf_post_html_fields', $entry, $config ); 
  232. ?> 
  233. </div> 
  234.  
  235. <?php 
  236.  
  237. if ( ! $echo ) { 
  238. return ob_get_clean(); 
  239.  
  240. return null; 
  241.  
  242. /** 
  243. * Build our HTML structure 
  244. * @param array $entry The Gravity Forms Entry Array 
  245. * @param array $config Any configuration data passed in 
  246. * @return string The generated HTML 
  247. * @since 4.0 
  248. */ 
  249. public function generate_html_structure( $entry, Helper_Abstract_Model $model, $config = [] ) { 
  250.  
  251. /** Set up required variables */ 
  252. $form = $this->gform->get_form( $entry['form_id'] ); 
  253. $products = new Field_Products( new GF_Field(), $entry, $this->gform, $this->misc ); 
  254. $page_number = 0; 
  255. $container = ( isset( $config['meta']['enable_css_ready_classes'] ) && false === $config['meta']['enable_css_ready_classes'] ) ? new Helper_Field_Container_Void() : new Helper_Field_Container(); 
  256.  
  257. /** Allow the config to be changed through a filter */ 
  258. $config['meta'] = ( isset( $config['meta'] ) ) ? $config['meta'] : []; 
  259. $config = apply_filters( 'gfpdf_pdf_configuration', $config, $entry, $form ); 
  260.  
  261. /** Get the user configuration values */ 
  262. $skip_marked_fields = ( isset( $config['meta']['exclude'] ) ) ? $config['meta']['exclude'] : true; /** whether we should exclude fields with a CSS value of 'exclude'. Default to true */ 
  263. $skip_conditional_fields = ( isset( $config['meta']['conditional'] ) ) ? $config['meta']['conditional'] : true; /** whether we should skip fields hidden with conditional logic. Default to true. */ 
  264. $show_title = ( isset( $config['meta']['show_title'] ) ) ? $config['meta']['show_title'] : false; /** whether we should show the form title. Default to true */ 
  265. $show_page_names = ( isset( $config['meta']['page_names'] ) ) ? $config['meta']['page_names'] : false; /** whether we should show the form's page names. Default to false */ 
  266. $show_html_fields = ( isset( $config['meta']['html_field'] ) ) ? $config['meta']['html_field'] : false; /** whether we should show the form's html fields. Default to false */ 
  267. $show_individual_product_fields = ( isset( $config['meta']['individual_products'] ) ) ? $config['meta']['individual_products'] : false; /** Whether to show individual fields in the entry. Default to false - they are grouped together at the end of the form */ 
  268.  
  269. /** Skip over any of the following blacklisted fields */ 
  270. $blacklisted = apply_filters( 'gfpdf_blacklisted_fields', [ 'captcha', 'password', 'page' ] ); 
  271.  
  272. /** Display the form title, if needed */ 
  273. $this->show_form_title( $show_title, $form ); 
  274.  
  275. /** Loop through the fields and output or skip if needed */ 
  276. foreach ( $form['fields'] as $key => $field ) { 
  277.  
  278. /** Load our page name, if needed */ 
  279. if ( $show_page_names === true && $field->pageNumber !== $page_number ) { 
  280. $this->display_page_name( $page_number, $form, $container ); 
  281. $page_number++; 
  282.  
  283. /** 
  284. * @TODO Create middleware filter to check if the field should be skipped. 
  285. * This will reduce code duplication and increase plugin flexibility 
  286. */ 
  287.  
  288. /** Skip any fields with the css class 'exclude', if needed */ 
  289. if ( $skip_marked_fields !== false && strpos( $field->cssClass, 'exclude' ) !== false ) { 
  290. /** To prevent display issues we will output the column markup needed */ 
  291. $container->maybe_display_faux_column( $field ); 
  292.  
  293. continue; 
  294.  
  295. /** Skip over any hidden fields (usually by conditional logic), if needed */ 
  296. if ( $skip_conditional_fields === true && GFFormsModel::is_field_hidden( $form, $field, [], $entry ) ) { 
  297. /** To prevent display issues we will output the column markup needed */ 
  298. $container->maybe_display_faux_column( $field ); 
  299.  
  300. continue; 
  301.  
  302. /** Skip over any product fields, if needed */ 
  303. if ( $show_individual_product_fields === false && GFCommon::is_product_field( $field->type ) ) { 
  304. /** To prevent display issues we will output the column markup needed */ 
  305. $container->maybe_display_faux_column( $field ); 
  306. continue; 
  307.  
  308. /** Skip HTML fields, if needed */ 
  309. if ( $show_html_fields === false && $field->type == 'html' ) { 
  310. /** To prevent display issues we will output the column markup needed */ 
  311. $container->maybe_display_faux_column( $field ); 
  312.  
  313. continue; 
  314.  
  315. /** Skip over any fields we don't want to include */ 
  316. if ( in_array( $field->type, $blacklisted ) ) { 
  317. /** To prevent display issues we will output the column markup needed */ 
  318. $container->maybe_display_faux_column( $field ); 
  319.  
  320. continue; 
  321.  
  322. /** Let's output our field */ 
  323. $this->process_field( $field, $entry, $form, $config, $products, $container, $model ); 
  324.  
  325. /** correctly close / cleanup the HTML container if needed */ 
  326. $container->close(); 
  327.  
  328. /** Output product table, if needed */ 
  329. if ( $show_individual_product_fields === false && ! $products->is_empty() ) { 
  330. echo $products->html(); 
  331.  
  332.  
  333. /** 
  334. * Handle our field loader and display the generated HTML 
  335. * @param GF_Field $field The field to process 
  336. * @param array $entry The Gravity Form Entry 
  337. * @param array $form The Gravity Form Field 
  338. * @param array $config The user-passed configuration data 
  339. * @param \GFPDF\Helper\Fields\Field_Products $products A Field_Products Object 
  340. * @param \GFPDF\Helper\Helper_Field_Container $container 
  341. * @return void 
  342. * @since 4.0 
  343. */ 
  344. public function process_field( GF_Field $field, $entry, $form, $config, Field_Products $products, Helper_Field_Container $container, Helper_Abstract_Model $model ) { 
  345.  
  346. /** 
  347. * Set up our configuration variables 
  348. */ 
  349. $config['meta'] = ( isset( $config['meta'] ) ) ? $config['meta'] : []; /** ensure we have a meta key */ 
  350. $show_empty_fields = ( isset( $config['meta']['empty'] ) ) ? $config['meta']['empty'] : false; /** whether to show empty fields or not. Default is false */ 
  351. $load_legacy_css = ( isset( $config['meta']['legacy_css'] ) ) ? $config['meta']['legacy_css'] : false; /** whether we should add our legacy field class names (v3.x.x) to our fields. Default to false */ 
  352. $show_section_description = ( isset( $config['meta']['section_content'] ) ) ? $config['meta']['section_content'] : false; /** whether we should include a section breaks content. Default to false */ 
  353.  
  354. $class = $model->get_field_class( $field, $form, $entry, $products ); 
  355.  
  356. /** Try and display our HTML */ 
  357. try { 
  358.  
  359. /** Only load our HTML if the field is NOT empty, or the $empty config option is true */ 
  360. if ( ! $class->is_empty() || $show_empty_fields === true ) { 
  361. /** Load our legacy CSS class names */ 
  362. if ( $load_legacy_css === true ) { 
  363. $this->load_legacy_css( $field ); 
  364.  
  365. /** 
  366. * Add CSS Ready Class Float Support to mPDF 
  367. * Open a HTML container if needed 
  368. */ 
  369. $container->generate( $field ); 
  370.  
  371. echo ( $field->type !== 'section' ) ? $class->html() : $class->html( $show_section_description ); 
  372. } else { 
  373. /** To prevent display issues we will output the column markup needed */ 
  374. $container->maybe_display_faux_column( $field ); 
  375. } catch ( Exception $e ) { 
  376. $this->log->addError( 'PDF Generation Error', [ 
  377. 'field' => $field,  
  378. 'entry' => $entry,  
  379. 'config' => $config,  
  380. 'form_id' => $form['id'],  
  381. 'exception' => $e->getMessage(),  
  382. ] ); 
  383.  
  384. /** 
  385. * If enabled, we'll show the Gravity Form Title in the document 
  386. * @param boolean $show_title Whether or not to show the title 
  387. * @param array $form The Gravity Form array 
  388. * @return void 
  389. * @since 4.0 
  390. */ 
  391. public function show_form_title( $show_title, $form ) { 
  392. /** Show the form title, if needed */ 
  393. if ( $show_title !== false ) { 
  394.  
  395. /** Load our HTML */ 
  396. $html = $this->load( 'form_title', [ 'form' => $form ], false ); 
  397.  
  398. /** Run it through a filter and output */ 
  399. echo apply_filters( 'gfpdf_pdf_form_title_html', $html, $form ); 
  400.  
  401. /** 
  402. * Output the current page name HTML 
  403. * @param integer $page The current page number 
  404. * @param array $form The form array 
  405. * @param Helper_Field_Container $container 
  406. * @return string The page HTML output 
  407. * @since 4.0 
  408. */ 
  409. public function display_page_name( $page, $form, Helper_Field_Container $container ) { 
  410.  
  411. /** Only display the current page name if it exists */ 
  412. if ( isset( $form['pagination']['pages'][ $page ] ) && strlen( trim( $form['pagination']['pages'][ $page ] ) ) > 0 ) { 
  413.  
  414. /** correctly close / cleanup the HTML container if needed */ 
  415. $container->close(); 
  416.  
  417. /** Load our HTML */ 
  418. $html = $this->load( 'page_title', [ 'form' => $form, 'page' => $page ], false ); 
  419.  
  420. /** Run it through a filter and output */ 
  421. echo apply_filters( 'gfpdf_field_page_name_html', $html, $page, $form ); 
  422.  
  423. /** 
  424. * Automatically render our core PDF fields and add styles in templates to simplify there usage for users 
  425. * @param string $html The current HTML template being processed 
  426. * @param array $form 
  427. * @param array $entry 
  428. * @param array $settings 
  429. * @return string 
  430. * @since 4.0 
  431. */ 
  432. public function autoprocess_core_template_options( $html, $form, $entry, $settings ) { 
  433. /** Prevent core styles loading if a v3 template or using our legacy Tier 2 add-on */ 
  434. $template_info = $this->templates->get_template_info_by_id( $settings['template'] ); 
  435. if ( 
  436. ( esc_html__( 'Legacy', 'gravity-forms-pdf-extended' ) !== $template_info['group'] ) && 
  437. ( empty( $settings['advanced_template'] ) || 'Yes' !== $settings['advanced_template'] ) 
  438. ) { 
  439. $html = $this->get_core_template_styles( $settings, $entry ) . $html; 
  440.  
  441. return $html; 
  442.  
  443. /** 
  444. * Loads the core template styles and runs it through a filter 
  445. * @param array $entry The Gravity Form entry being processed 
  446. * @param array $settings The current PDF settings 
  447. * @return string 
  448. * @since 4.0 
  449. */ 
  450. public function get_core_template_styles( $settings, $entry ) { 
  451. $form = $this->gform->get_form( $entry['form_id'] ); 
  452.  
  453. $html = $this->load_core_template_styles( $settings ); 
  454.  
  455. $html = apply_filters( 'gfpdf_pdf_core_template_html_output', $html, $form, $entry, $settings ); 
  456. $html = apply_filters( 'gfpdf_pdf_core_template_html_output_' . $form['id'], $html, $form, $entry, $settings ); 
  457.  
  458. return $html; 
  459.  
  460. /** 
  461. * Load our core PDF template settings 
  462. * @param $settings 
  463. * @return string|\WP_Error 
  464. * @since 4.0 
  465. */ 
  466. public function load_core_template_styles( $settings ) { 
  467. $controller = $this->getController(); 
  468. $model = $controller->model; 
  469.  
  470. /** Run our settings through the preprocessor which requires an array with a 'settings' key */ 
  471. $args = $model->preprocess_template_arguments( [ 'settings' => $settings ] ); 
  472. $settings = $args['settings']; 
  473.  
  474. return $this->load( 'core_template_styles', [ 'settings' => $settings ], false );