PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.07.11
E2Pdf – Export Pdf Tool for WordPress v1.07.11
1.32.49 1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 1.08.00 1.08.06 All 72 releases
e2pdf / classes / extension / e2pdf-gravity.php

e2pdf-gravity.php in E2Pdf – Export Pdf Tool for WordPress 1.07.11, at classes/extension/e2pdf-gravity.php

1,946 lines 82.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * E2pdf Gravity Forms Extension
5 *
6 * @copyright Copyright 2017 https://e2pdf.com
7 * @license GPL v2
8 * @version 1
9 * @link https://e2pdf.com
10 * @since 1.07.04
11 */
12 if (!defined('ABSPATH')) {
13 die('Access denied.');
14 }
15
16 class Extension_E2pdf_Gravity extends Model_E2pdf_Model {
17
18 private $options;
19 private $info = array(
20 'key' => 'gravity',
21 'title' => 'Gravity Forms'
22 );
23
24 function __construct() {
25 parent::__construct();
26 }
27
28 /**
29 * Get info about extension
30 *
31 * @param string $key - Key to get assigned extension info value
32 *
33 * @return array|string - Extension Key and Title or Assigned extension info value
34 */
35 public function info($key = false) {
36 if ($key && isset($this->info[$key])) {
37 return $this->info[$key];
38 } else {
39 return array(
40 $this->info['key'] => $this->info['title']
41 );
42 }
43 }
44
45 /**
46 * Check if needed plugin active
47 *
48 * @return bool - Activated/Not Activated plugin
49 */
50 public function active() {
51
52 if (!function_exists('is_plugin_active')) {
53 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
54 }
55
56 if (is_plugin_active('gravityforms/gravityforms.php')) {
57 return true;
58 }
59 return false;
60 }
61
62 /**
63 * Set option
64 *
65 * @param string $key - Key of option
66 * @param string $value - Value of option
67 *
68 * @return bool - Status of setting option
69 */
70 public function set($key, $value) {
71
72 if (!isset($this->options)) {
73 $this->options = new stdClass();
74 }
75
76 $this->options->$key = $value;
77 return true;
78 }
79
80 /**
81 * Get option by key
82 *
83 * @param string $key - Key to get assigned option value
84 *
85 * @return mixed
86 */
87 public function get($key) {
88 if (isset($this->options->$key)) {
89 $value = $this->options->$key;
90 return $value;
91 } elseif ($key == 'args') {
92 return array();
93 } else {
94 return false;
95 }
96 }
97
98 /**
99 * Get items to work with
100 *
101 * @return array() - List of available items
102 */
103 public function items() {
104
105 $content = array();
106
107 if (class_exists('GFFormsModel')) {
108 $forms = GFFormsModel::get_forms(null, 'title');
109 if ($forms) {
110 foreach ($forms as $key => $value) {
111 $content[] = $this->item($value->id);
112 }
113 }
114 }
115
116 return $content;
117 }
118
119 /**
120 * Get entries for export
121 *
122 * @param int $item - Form ID
123 * @param string $name - Entries names
124 *
125 * @return array() - Entries list
126 */
127 public function datasets($item = false, $name = false) {
128
129 $item = (int) $item;
130 $datasets = array();
131
132 if (class_exists('GFAPI') && $item) {
133 $datasets_tmp = GFAPI::get_entries($item);
134 if ($datasets_tmp) {
135 foreach ($datasets_tmp as $key => $dataset) {
136 $this->set('item', $item);
137 $this->set('dataset', $dataset['id']);
138
139 $dataset_title = $this->render($name);
140 if (!$dataset_title) {
141 $dataset_title = $dataset['id'];
142 }
143 $datasets[] = array(
144 'key' => $dataset['id'],
145 'value' => $dataset_title
146 );
147 }
148 }
149 }
150
151 return $datasets;
152 }
153
154 /**
155 * Get dataset
156 *
157 * @param int $dataset - Dataset ID
158 *
159 * @return object - Dataset
160 */
161 public function dataset($dataset = false) {
162
163 $dataset = (int) $dataset;
164
165 if (!$dataset) {
166 return false;
167 }
168
169 $entry = GFFormsModel::get_entry($dataset);
170 $item = $entry && isset($entry['form_id']) ? $entry['form_id'] : '0';
171
172 $data = new stdClass();
173 $data->url = $this->helper->get_url(array('page' => 'gf_entries', 'view' => 'entry', 'id' => $item, 'lid' => $dataset));
174
175 return $data;
176 }
177
178 /**
179 * Get item
180 *
181 * @param int $item - Item ID
182 *
183 * @return object - Item
184 */
185 public function item($item = false) {
186
187 $item = (int) $item;
188 if (!$item && $this->get('item')) {
189 $item = $this->get('item');
190 }
191
192 $gravity_form = false;
193 if (class_exists('GFFormsModel')) {
194 $gravity_form = GFFormsModel::get_form_meta($item);
195 }
196
197 $form = new stdClass();
198 if ($gravity_form) {
199 $form->id = (string) $item;
200 $form->url = $this->helper->get_url(array('page' => 'gf_edit_forms', 'id' => $item));
201 $form->name = $gravity_form['title'];
202 } else {
203 $form->id = '';
204 $form->url = 'javascript:void(0);';
205 $form->name = '';
206 }
207 return $form;
208 }
209
210 /**
211 * Render value according to content
212 *
213 * @param string $value - Content
214 * @param string $type - Type of rendering value
215 * @param array $field - Field details
216 *
217 * @return string - Fully rendered value
218 */
219 public function render($value, $type = false, $field = array()) {
220
221 $value = $this->render_shortcodes($value, $type, $field);
222 $value = $this->strip_shortcodes($value);
223
224 if ($type === 'value' && isset($field['type']) && $field['type'] === 'e2pdf-checkbox' && isset($field['properties']['option'])) {
225 $option = $this->render($field['properties']['option']);
226 $options = explode(', ', $value);
227 $option_options = explode(', ', $option);
228 if (is_array($options) && is_array($option_options) && !array_diff($option_options, $options)) {
229 return $option;
230 } else {
231 return "";
232 }
233 }
234
235 if ($type === 'value') {
236 $value = $this->convert_shortcodes($value, true);
237 }
238
239 return $value;
240 }
241
242 /**
243 * Load actions for this extension
244 */
245 public function load_actions() {
246 add_action('gform_after_email', array($this, 'action_gform_after_email'), 30, 1);
247 }
248
249 /**
250 * Load filters for this extension
251 */
252 public function load_filters() {
253 add_filter('gform_confirmation', array($this, 'filter_gform_confirmation'), 30, 4);
254 add_filter('gform_notification', array($this, 'filter_gform_notification'), 30, 3);
255 }
256
257 /**
258 * Render shortcodes which available in this extension
259 *
260 * @param string $value - Content
261 * @param string $type - Type of rendering value
262 * @param array $field - Field details
263 *
264 * @return string - Value with rendered shortcodes
265 */
266 public function render_shortcodes($value, $type = false, $field = array()) {
267
268 $dataset = $this->get('dataset');
269 $item = $this->get('item');
270 $user_id = $this->get('user_id');
271 $args = $this->get('args');
272 $template_id = $this->get('template_id') ? $this->get('template_id') : '0';
273 $element_id = isset($field['element_id']) ? $field['element_id'] : '0';
274
275 $form = false;
276 $entry = false;
277
278 if ($this->verify()) {
279
280 $args = apply_filters('e2pdf_extension_render_shortcodes_args', $args, $element_id, $template_id, $item, $dataset);
281
282 $form = GFFormsModel::get_form_meta($item);
283 $entry = GFFormsModel::get_entry($dataset);
284
285 if (false !== strpos($value, '[')) {
286
287 $shortcode_tags = array(
288 'e2pdf-user',
289 'e2pdf-arg'
290 );
291 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
292 $tagnames = array_intersect($shortcode_tags, $matches[1]);
293
294 if (!empty($tagnames)) {
295
296 $pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames);
297
298 preg_match_all("/$pattern/", $value, $shortcodes);
299
300 foreach ($shortcodes[0] as $key => $shortcode_value) {
301 $shortcode = array();
302 $shortcode[1] = $shortcodes[1][$key];
303 $shortcode[2] = $shortcodes[2][$key];
304 $shortcode[3] = $shortcodes[3][$key];
305 $shortcode[4] = $shortcodes[4][$key];
306 $shortcode[5] = $shortcodes[5][$key];
307 $shortcode[6] = $shortcodes[6][$key];
308
309 $atts = shortcode_parse_atts($shortcode[3]);
310
311 if ($shortcode['2'] == 'e2pdf-user') {
312 if (!isset($atts['id']) && $user_id) {
313 $shortcode[3] .= " id=\"" . $user_id . "\"";
314 $value = str_replace($shortcode_value, "[" . $shortcode['2'] . $shortcode['3'] . "]", $value);
315 }
316 } elseif ($shortcode['2'] == 'e2pdf-arg') {
317 if (isset($atts['key']) && isset($args[$atts['key']])) {
318 $sub_value = $this->strip_shortcodes($args[$atts['key']]);
319 $value = str_replace($shortcode_value, $sub_value, $value);
320 } else {
321 $value = str_replace($shortcode_value, '', $value);
322 }
323 }
324 }
325 }
326
327 $shortcode_tags = array(
328 'e2pdf-format-number',
329 'e2pdf-format-date',
330 'e2pdf-format-output',
331 );
332 $shortcode_tags = apply_filters('e2pdf_extension_render_shortcodes_tags', $shortcode_tags);
333 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
334 $tagnames = array_intersect($shortcode_tags, $matches[1]);
335
336 if (!empty($tagnames)) {
337
338 $pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames);
339
340 preg_match_all("/$pattern/", $value, $shortcodes);
341 foreach ($shortcodes[0] as $key => $shortcode_value) {
342 $shortcode = array();
343 $shortcode[1] = $shortcodes[1][$key];
344 $shortcode[2] = $shortcodes[2][$key];
345 $shortcode[3] = $shortcodes[3][$key];
346 $shortcode[4] = $shortcodes[4][$key];
347 $shortcode[5] = $shortcodes[5][$key];
348 $shortcode[6] = $shortcodes[6][$key];
349
350 if (!$shortcode['5']) {
351 $sub_value = '';
352 } elseif (isset($field['type']) && ($field['type'] === 'e2pdf-image' || $field['type'] === 'e2pdf-signature')) {
353 $sub_value = $this->render($shortcode['5'], $type);
354 } else {
355 $sub_value = $this->render($shortcode['5'], $type, $field);
356 }
357 $value = str_replace($shortcode_value, "[" . $shortcode['2'] . $shortcode['3'] . "]" . $sub_value . "[/" . $shortcode['2'] . "]", $value);
358 }
359 }
360 }
361
362 $value = do_shortcode($value);
363
364 if (class_exists('GFCommon')) {
365 add_filter('gform_merge_tag_filter', array($this, 'filter_gform_merge_tag_filter'), 30, 5);
366 $value = GFCommon::replace_variables($value, $form, $entry, false, false, false, 'text');
367 remove_filter('gform_merge_tag_filter', array($this, 'filter_gform_merge_tag_filter'), 30, 5);
368 }
369
370 if (isset($field['type']) && ($field['type'] === 'e2pdf-image' || $field['type'] === 'e2pdf-signature')) {
371 $esig = isset($field['properties']['esig']) && $field['properties']['esig'] ? true : false;
372 if ($esig) {
373 //process e-signature
374 $value = "";
375 } else {
376
377 if (!$this->helper->load('image')->get_image($value)) {
378 $value = $this->strip_shortcodes($value);
379 if (
380 $value &&
381 trim($value) != "" &&
382 extension_loaded('gd') &&
383 function_exists('imagettftext')
384 ) {
385 if (isset($field['properties']['text_color']) && $field['properties']['text_color']) {
386 $penColour = $this->helper->load('convert')->to_hex_color($field['properties']['text_color']);
387 } else {
388 $penColour = array(0x14, 0x53, 0x94);
389 }
390
391 $default_options = array(
392 'imageSize' => array(isset($field['width']) ? $field['width'] : '400', isset($field['height']) ? $field['height'] : '150'),
393 'bgColour' => 'transparent',
394 'penColour' => $penColour
395 );
396
397 $options = array();
398 $options = apply_filters('e2pdf_image_sig_output_options', $options, $element_id, $template_id);
399 $options = array_merge($default_options, $options);
400
401 $model_e2pdf_font = new Model_E2pdf_Font();
402
403 $font = false;
404 if (isset($field['properties']['text_font']) && $field['properties']['text_font']) {
405 $font = $model_e2pdf_font->get_font_path($field['properties']['text_font']);
406 }
407
408 if (!$font) {
409 $font = $model_e2pdf_font->get_font_path('Noto Sans');
410 }
411
412 $size = 150;
413 if (isset($field['properties']['text_font_size']) && $field['properties']['text_font_size']) {
414 $size = $field['properties']['text_font_size'];
415 }
416
417 $model_e2pdf_signature = new Model_E2pdf_Signature();
418 $value = $model_e2pdf_signature->ttf_signature($value, $size, $font, $options);
419 } else {
420 $value = "";
421 }
422 }
423 }
424 } else {
425 $value = $this->convert_shortcodes($value);
426 }
427 }
428
429 $value = apply_filters('e2pdf_extension_render_shortcodes_value', $value, $element_id, $template_id, $item, $dataset);
430
431 return $value;
432 }
433
434 /**
435 * Strip unused shortcodes
436 *
437 * @param string $value - Content
438 *
439 * @return string - Value with removed unused shortcodes
440 */
441 public function strip_shortcodes($value) {
442 $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', "", $value);
443 $value = preg_replace('~(?:{/?)[^/}]+/?}~s', "", $value);
444 return $value;
445 }
446
447 /**
448 * Convert "shortcodes" inside value string
449 *
450 * @param string $value - Value string
451 * @param bool $to - Convert From/To
452 *
453 * @return string - Converted value
454 */
455 public function convert_shortcodes($value, $to = false) {
456 if ($value) {
457 if ($to) {
458 $value = str_replace('&#91;', '[', $value);
459 $value = str_replace('&#93;', ']', $value);
460 } else {
461 $value = str_replace('[', '&#91;', $value);
462 $value = str_replace(']', '&#93;', $value);
463 $value = str_replace('&#091;', '&#91;', $value);
464 $value = str_replace('&#093;', '&#93;', $value);
465 }
466 }
467 return $value;
468 }
469
470 /**
471 * Auto Generate of Template for this extension
472 *
473 * @return array - List of elements
474 */
475 public function auto() {
476
477 $item = $this->get('item');
478
479 $response = array();
480 $elements = array();
481 $merged_tags = array();
482 $form = false;
483
484 if (class_exists('GFFormsModel')) {
485 $form = GFFormsModel::get_form_meta($item);
486 }
487
488 if ($form) {
489 if (class_exists('GFCommon')) {
490 foreach ($form['fields'] as $field) {
491 $tags = GFCommon::get_field_merge_tags($field);
492 foreach ($tags as $tag) {
493 if (isset($tag['tag'])) {
494 if ($field->type == 'list') {
495 $field_id = preg_replace('/\{(?:.*)\:(.*)\:\}/', '${1}', $tag['tag']);
496 } else {
497 $field_id = preg_replace('/\{(?:.*)\:(.*)\}/', '${1}', $tag['tag']);
498 }
499 if ($field_id) {
500 $merged_tags[$field_id] = $tag['tag'];
501 }
502 }
503 }
504 }
505 }
506
507 foreach ($form['fields'] as $field) {
508 if ($field->type == 'product' || $field->type == 'shipping') {
509 if ($field->inputType == 'select') {
510 $field->type = 'select';
511 } elseif ($field->inputType == 'radio') {
512 $field->type = 'radio';
513 } elseif ($field->inputType == 'price') {
514 $field->type = 'text';
515 }
516 }
517
518 switch ($field->type) {
519 case 'text':
520 case 'number':
521 case 'date':
522 case 'time':
523 case 'phone':
524 case 'website':
525 case 'email':
526 case 'fileupload':
527 case 'post_title':
528 case 'post_excerpt':
529 case 'post_tags':
530 case 'post_custom_field':
531 case 'quantity':
532 case 'shipping':
533 case 'total':
534 $value = isset($merged_tags[$field->id]) ? $merged_tags[$field->id] : '';
535 $elements[] = $this->auto_field($field, array(
536 'type' => 'e2pdf-html',
537 'block' => true,
538 'properties' => array(
539 'top' => '20',
540 'left' => '20',
541 'right' => '20',
542 'width' => '100%',
543 'height' => 'auto',
544 'value' => $field->label,
545 'pass' => $field->enablePasswordInput ? '1' : '0'
546 )
547 ));
548
549 $elements[] = $this->auto_field($field, array(
550 'type' => 'e2pdf-input',
551 'properties' => array(
552 'top' => '5',
553 'width' => '100%',
554 'height' => 'auto',
555 'value' => $value,
556 )
557 ));
558 break;
559 case 'list':
560 $elements[] = $this->auto_field($field, array(
561 'type' => 'e2pdf-html',
562 'block' => true,
563 'properties' => array(
564 'top' => '20',
565 'left' => '20',
566 'right' => '20',
567 'width' => '100%',
568 'height' => 'auto',
569 'value' => $field->label,
570 )
571 ));
572
573 if ($field->enableColumns) {
574
575 $width = number_format(floor((100 / count($field->choices)) * 100) / 100, 2);
576
577 foreach ($field->choices as $key => $choice) {
578
579 $field_id = (int) $key + 1;
580 $value = isset($merged_tags[$field->id]) ? $merged_tags[$field->id] : '';
581 if (substr($value, -1) == '}') {
582 $value = substr($value, 0, -1) . '1_' . $field_id . '}';
583 }
584
585 $float = true;
586 if ($key == '0') {
587 $float = false;
588 }
589 $elements[] = $this->auto_field($field, array(
590 'type' => 'e2pdf-html',
591 'block' => true,
592 'float' => $float,
593 'properties' => array(
594 'top' => '5',
595 'left' => '20',
596 'right' => '20',
597 'width' => $width . '%',
598 'height' => 'auto',
599 'value' => $choice['text'],
600 )
601 ));
602
603 $elements[] = $this->auto_field($field, array(
604 'type' => 'e2pdf-input',
605 'properties' => array(
606 'top' => '5',
607 'width' => '100%',
608 'height' => 'auto',
609 'value' => $value,
610 )
611 ));
612 }
613 } else {
614 $value = isset($merged_tags[$field->id]) ? $merged_tags[$field->id] : '';
615 if (substr($value, -1) == '}') {
616 $value = substr($value, 0, -1) . '1}';
617 }
618
619 $elements[] = $this->auto_field($field, array(
620 'type' => 'e2pdf-input',
621 'properties' => array(
622 'top' => '5',
623 'width' => '100%',
624 'height' => 'auto',
625 'value' => $value,
626 )
627 ));
628 }
629 break;
630 case 'textarea':
631 case 'post_content':
632 $elements[] = $this->auto_field($field, array(
633 'type' => 'e2pdf-html',
634 'block' => true,
635 'properties' => array(
636 'top' => '20',
637 'left' => '20',
638 'right' => '20',
639 'width' => '100%',
640 'height' => 'auto',
641 'value' => $field->label,
642 )
643 ));
644
645 $elements[] = $this->auto_field($field, array(
646 'type' => 'e2pdf-textarea',
647 'properties' => array(
648 'top' => '5',
649 'width' => '100%',
650 'height' => 'auto',
651 'value' => isset($merged_tags[$field->id]) ? $merged_tags[$field->id] : '',
652 )
653 ));
654 break;
655 case 'select':
656 case 'multiselect':
657 case 'post_category':
658 case 'option':
659 $elements[] = $this->auto_field($field, array(
660 'type' => 'e2pdf-html',
661 'block' => true,
662 'properties' => array(
663 'top' => '20',
664 'left' => '20',
665 'right' => '20',
666 'width' => '100%',
667 'height' => 'auto',
668 'value' => $field->label,
669 )
670 ));
671
672 $options_tmp = array();
673 if (isset($field->choices) && is_array($field->choices)) {
674 foreach ($field->choices as $opt_key => $option) {
675 $options_tmp[] = isset($option['value']) ? $option['value'] : '';
676 }
677 }
678
679 $value = isset($merged_tags[$field->id]) ? $merged_tags[$field->id] : '';
680 if ($field->enableChoiceValue && $value) {
681 if (substr($value, -1) == '}') {
682 $value = substr($value, 0, -1) . ':value}';
683 }
684 }
685
686 $elements[] = $this->auto_field($field, array(
687 'type' => 'e2pdf-select',
688 'properties' => array(
689 'top' => '5',
690 'width' => '100%',
691 'height' => 'auto',
692 'options' => implode("\n", $options_tmp),
693 'value' => $value,
694 'height' => $field->type == 'multiselect' ? '80' : 'auto',
695 'multiline' => $field->type == 'multiselect' ? '1' : '0',
696 )
697 ));
698 break;
699 case 'checkbox':
700 $elements[] = $this->auto_field($field, array(
701 'type' => 'e2pdf-html',
702 'block' => true,
703 'properties' => array(
704 'top' => '20',
705 'left' => '20',
706 'right' => '20',
707 'width' => '100%',
708 'height' => 'auto',
709 'value' => $field->label,
710 )
711 ));
712
713 if (isset($field->choices) && is_array($field->choices)) {
714
715 $value = isset($merged_tags[$field->id]) ? $merged_tags[$field->id] : '';
716 if ($field->enableChoiceValue && $value) {
717 if (substr($value, -1) == '}') {
718 $value = substr($value, 0, -1) . ':value}';
719 }
720 }
721
722 foreach ($field->choices as $opt_key => $option) {
723 $elements[] = $this->auto_field($field, array(
724 'type' => 'e2pdf-checkbox',
725 'properties' => array(
726 'top' => '5',
727 'width' => 'auto',
728 'height' => 'auto',
729 'value' => $value,
730 'option' => isset($option['value']) ? $option['value'] : ''
731 )
732 ));
733 $elements[] = $this->auto_field($field, array(
734 'type' => 'e2pdf-html',
735 'float' => true,
736 'properties' => array(
737 'left' => '5',
738 'width' => '100%',
739 'height' => 'auto',
740 'value' => isset($option['text']) ? $option['text'] : ''
741 )
742 ));
743 }
744 }
745 break;
746 case 'radio':
747 $elements[] = $this->auto_field($field, array(
748 'type' => 'e2pdf-html',
749 'block' => true,
750 'properties' => array(
751 'top' => '20',
752 'left' => '20',
753 'right' => '20',
754 'width' => '100%',
755 'height' => 'auto',
756 'value' => $field->label,
757 )
758 ));
759
760 if (isset($field->choices) && is_array($field->choices)) {
761
762 $value = isset($merged_tags[$field->id]) ? $merged_tags[$field->id] : '';
763 if ($field->enableChoiceValue && $value) {
764 if (substr($value, -1) == '}') {
765 $value = substr($value, 0, -1) . ':value}';
766 }
767 }
768
769 foreach ($field->choices as $opt_key => $option) {
770 $elements[] = $this->auto_field($field, array(
771 'type' => 'e2pdf-radio',
772 'properties' => array(
773 'top' => '5',
774 'width' => 'auto',
775 'height' => 'auto',
776 'value' => $value,
777 'option' => isset($option['value']) ? $option['value'] : '',
778 'group' => $value
779 )
780 ));
781 $elements[] = $this->auto_field($field, array(
782 'type' => 'e2pdf-html',
783 'float' => true,
784 'properties' => array(
785 'left' => '5',
786 'width' => '100%',
787 'height' => 'auto',
788 'value' => isset($option['text']) ? $option['text'] : ''
789 )
790 ));
791 }
792 }
793 break;
794 case 'name':
795 foreach ($field['inputs'] as $key => $input) {
796 if ($input->isHidden) {
797 unset($field['inputs'][$key]);
798 }
799 }
800
801 $width = '100%';
802 if (count($field['inputs']) == '3') {
803 $width = '33.3%';
804 } else {
805 $width = 100 / count($field['inputs']) . '%';
806 }
807
808 foreach ($field['inputs'] as $key => $sub_field) {
809
810 $elements[] = $this->auto_field($sub_field, array(
811 'type' => 'e2pdf-html',
812 'block' => true,
813 'float' => $key == '0' ? false : true,
814 'properties' => array(
815 'top' => '20',
816 'left' => '20',
817 'right' => '20',
818 'width' => $width,
819 'height' => 'auto',
820 'value' => isset($sub_field['label']) && $sub_field['label'] ? $sub_field['label'] : '',
821 )
822 ));
823
824 if (isset($sub_field['choices']) && is_array($sub_field['choices'])) {
825
826 $options_tmp = array();
827 foreach ($sub_field['choices'] as $opt_key => $option) {
828 $options_tmp[] = isset($option['value']) ? $option['value'] : '';
829 }
830
831 $elements[] = $this->auto_field($field, array(
832 'type' => 'e2pdf-select',
833 'properties' => array(
834 'top' => '5',
835 'width' => '100%',
836 'height' => 'auto',
837 'options' => implode("\n", $options_tmp),
838 'value' => isset($merged_tags[$sub_field['id']]) ? $merged_tags[$sub_field['id']] : '',
839 'height' => 'auto',
840 'multiline' => '0',
841 )
842 ));
843 } else {
844 $elements[] = $this->auto_field($sub_field, array(
845 'type' => 'e2pdf-input',
846 'properties' => array(
847 'top' => '5',
848 'width' => '100%',
849 'height' => 'auto',
850 'value' => isset($merged_tags[$sub_field['id']]) ? $merged_tags[$sub_field['id']] : '',
851 )
852 ));
853 }
854 }
855 break;
856 case 'address':
857 $index = 0;
858 foreach ($field['inputs'] as $key => $sub_field) {
859
860 if (isset($sub_field['isHidden']) && $sub_field['isHidden']) {
861
862 } else {
863 $elements[] = $this->auto_field($sub_field, array(
864 'type' => 'e2pdf-html',
865 'block' => true,
866 'float' => $index == '0' ? false : true,
867 'properties' => array(
868 'top' => '20',
869 'left' => '20',
870 'right' => '20',
871 'width' => $key == '0' || $key == '1' ? '100%' : '50%',
872 'height' => 'auto',
873 'value' => isset($sub_field['label']) && $sub_field['label'] ? $sub_field['label'] : '',
874 )
875 ));
876
877 $elements[] = $this->auto_field($sub_field, array(
878 'type' => 'e2pdf-input',
879 'properties' => array(
880 'top' => '5',
881 'width' => '100%',
882 'height' => 'auto',
883 'value' => isset($merged_tags[$sub_field['id']]) ? $merged_tags[$sub_field['id']] : '',
884 )
885 ));
886 $index++;
887 }
888 }
889 break;
890 case 'consent':
891 $elements[] = $this->auto_field($field, array(
892 'type' => 'e2pdf-html',
893 'block' => true,
894 'properties' => array(
895 'top' => '20',
896 'left' => '20',
897 'right' => '20',
898 'width' => '100%',
899 'height' => 'auto',
900 'value' => $field->label,
901 )
902 ));
903
904 $elements[] = $this->auto_field($field, array(
905 'type' => 'e2pdf-checkbox',
906 'properties' => array(
907 'top' => '5',
908 'width' => 'auto',
909 'height' => 'auto',
910 'value' => isset($merged_tags[$field->id . '.1']) ? $merged_tags[$field->id . '.1'] : '',
911 'option' => '1'
912 )
913 ));
914 $elements[] = $this->auto_field($field, array(
915 'type' => 'e2pdf-html',
916 'float' => true,
917 'properties' => array(
918 'left' => '5',
919 'width' => '100%',
920 'height' => 'auto',
921 'value' => $field->checkboxLabel
922 )
923 ));
924 break;
925 case 'post_image':
926
927 $value = isset($merged_tags[$field->id]) ? $merged_tags[$field->id] : '';
928
929 $elements[] = $this->auto_field($field, array(
930 'type' => 'e2pdf-html',
931 'block' => true,
932 'properties' => array(
933 'top' => '20',
934 'left' => '20',
935 'right' => '20',
936 'width' => '100%',
937 'height' => 'auto',
938 'value' => $field->label,
939 )
940 ));
941
942 $elements[] = $this->auto_field($field, array(
943 'type' => 'e2pdf-image',
944 'properties' => array(
945 'top' => '5',
946 'width' => '100',
947 'height' => '100',
948 'value' => $value,
949 'dimension' => '1'
950 )
951 ));
952
953 if ($field->displayTitle) {
954
955 $elements[] = $this->auto_field($field, array(
956 'type' => 'e2pdf-html',
957 'block' => true,
958 'properties' => array(
959 'top' => '20',
960 'left' => '20',
961 'right' => '20',
962 'width' => '100%',
963 'height' => 'auto',
964 'value' => __('Title', 'gravityforms'),
965 )
966 ));
967
968 $elements[] = $this->auto_field($field, array(
969 'type' => 'e2pdf-input',
970 'properties' => array(
971 'top' => '5',
972 'width' => '100%',
973 'height' => 'auto',
974 'value' => $value && substr($value, -1) == '}' ? substr($value, 0, -1) . ':title}' : '',
975 'dimension' => '1'
976 )
977 ));
978 }
979
980 if ($field->displayCaption) {
981 $elements[] = $this->auto_field($field, array(
982 'type' => 'e2pdf-html',
983 'block' => true,
984 'properties' => array(
985 'top' => '20',
986 'left' => '20',
987 'right' => '20',
988 'width' => '100%',
989 'height' => 'auto',
990 'value' => __('Caption', 'gravityforms'),
991 )
992 ));
993
994 $elements[] = $this->auto_field($field, array(
995 'type' => 'e2pdf-input',
996 'properties' => array(
997 'top' => '5',
998 'width' => '100%',
999 'height' => 'auto',
1000 'value' => $value && substr($value, -1) == '}' ? substr($value, 0, -1) . ':caption}' : '',
1001 'dimension' => '1'
1002 )
1003 ));
1004 }
1005
1006 if ($field->displayDescription) {
1007 $elements[] = $this->auto_field($field, array(
1008 'type' => 'e2pdf-html',
1009 'block' => true,
1010 'properties' => array(
1011 'top' => '20',
1012 'left' => '20',
1013 'right' => '20',
1014 'width' => '100%',
1015 'height' => 'auto',
1016 'value' => __('Description', 'gravityforms'),
1017 )
1018 ));
1019
1020 $elements[] = $this->auto_field($field, array(
1021 'type' => 'e2pdf-input',
1022 'properties' => array(
1023 'top' => '5',
1024 'width' => '100%',
1025 'height' => 'auto',
1026 'value' => $value && substr($value, -1) == '}' ? substr($value, 0, -1) . ':description}' : '',
1027 'dimension' => '1'
1028 )
1029 ));
1030 }
1031
1032 break;
1033 case 'html':
1034 $elements[] = $this->auto_field($field, array(
1035 'type' => 'e2pdf-html',
1036 'block' => true,
1037 'properties' => array(
1038 'top' => '20',
1039 'left' => '20',
1040 'right' => '20',
1041 'width' => '100%',
1042 'height' => 'auto',
1043 'value' => $field->content,
1044 )
1045 ));
1046 break;
1047 case 'product':
1048
1049 if ($field->inputType != 'hiddenproduct') {
1050
1051 $elements[] = $this->auto_field($field, array(
1052 'type' => 'e2pdf-html',
1053 'block' => true,
1054 'properties' => array(
1055 'top' => '20',
1056 'left' => '20',
1057 'right' => '20',
1058 'width' => '100%',
1059 'height' => 'auto',
1060 'value' => $field->label,
1061 )
1062 ));
1063
1064 if ($field->inputType == 'singleproduct' || $field->inputType == 'calculation') {
1065
1066 if ($field->disableQuantity) {
1067 $width = '50%';
1068 } else {
1069 $width = '33.3%';
1070 }
1071
1072 foreach ($field['inputs'] as $key => $sub_field) {
1073
1074 if ($field->disableQuantity && $key == '2') {
1075
1076 } else {
1077 $elements[] = $this->auto_field($sub_field, array(
1078 'type' => 'e2pdf-html',
1079 'block' => true,
1080 'float' => $key == '0' ? false : true,
1081 'properties' => array(
1082 'top' => '5',
1083 'left' => '20',
1084 'right' => '20',
1085 'width' => $width,
1086 'height' => 'auto',
1087 'value' => isset($sub_field['label']) && $sub_field['label'] ? $sub_field['label'] : '',
1088 )
1089 ));
1090
1091 $elements[] = $this->auto_field($sub_field, array(
1092 'type' => 'e2pdf-input',
1093 'properties' => array(
1094 'top' => '5',
1095 'width' => '100%',
1096 'height' => 'auto',
1097 'value' => isset($merged_tags[$sub_field['id']]) ? $merged_tags[$sub_field['id']] : '',
1098 )
1099 ));
1100 }
1101 }
1102 }
1103 }
1104 break;
1105
1106 case 'signature':
1107 $elements[] = $this->auto_field($field, array(
1108 'type' => 'e2pdf-html',
1109 'block' => true,
1110 'properties' => array(
1111 'top' => '20',
1112 'left' => '20',
1113 'right' => '20',
1114 'width' => '100%',
1115 'height' => 'auto',
1116 'value' => $field->label,
1117 )
1118 ));
1119
1120 $elements[] = $this->auto_field($field, array(
1121 'type' => 'e2pdf-signature',
1122 'properties' => array(
1123 'top' => '5',
1124 'width' => '100%',
1125 'height' => '150',
1126 'scale' => '1',
1127 'dimension' => '1',
1128 'value' => isset($merged_tags[$field->id]) ? $merged_tags[$field->id] : ''
1129 )
1130 ));
1131 break;
1132 default:
1133 //non-supported fields
1134 break;
1135 }
1136 }
1137 }
1138
1139 $response['page'] = array(
1140 'bottom' => '20',
1141 'top' => '20',
1142 'left' => '20',
1143 'right' => '20'
1144 );
1145
1146 $response['elements'] = $elements;
1147 return $response;
1148 }
1149
1150 public function auto_field($field = false, $element = array()) {
1151
1152 if (!$field) {
1153 return false;
1154 }
1155
1156 if (!isset($element['block'])) {
1157 $element['block'] = false;
1158 }
1159
1160 if (!isset($element['float'])) {
1161 $element['float'] = false;
1162 }
1163
1164 return $element;
1165 }
1166
1167 /**
1168 * Verify if item and dataset exists
1169 *
1170 * @return bool - item and dataset exists
1171 */
1172 public function verify() {
1173 $item = $this->get('item');
1174 $dataset = $this->get('dataset');
1175
1176 if ($item && $dataset && class_exists('GFFormsModel')) {
1177 $entry = GFFormsModel::get_entry($dataset);
1178 if ($entry && isset($entry['form_id']) && $entry['form_id'] == $item) {
1179 return true;
1180 }
1181 }
1182
1183 return false;
1184 }
1185
1186 /**
1187 * Create Form based on uploaded PDF
1188 *
1189 * @param object $template - Template Object to work with
1190 * @param array $data - Settings to create labels/shortcodes
1191 *
1192 * @return object - Mapped Template Object
1193 */
1194 public function auto_form($template, $data = array()) {
1195
1196 if ($template->get('ID')) {
1197
1198 $auto_form_label = isset($data['auto_form_label']) && $data['auto_form_label'] ? $data['auto_form_label'] : false;
1199 $auto_form_shortcode = isset($data['auto_form_shortcode']) ? true : false;
1200
1201 if (class_exists('GFAPI')) {
1202 $confirmation_id = uniqid();
1203 $form = array(
1204 'title' => $template->get('title'),
1205 'fields' => array(
1206 ),
1207 'confirmations' => array()
1208 );
1209
1210 $form['confirmations'][$confirmation_id] = array(
1211 'id' => $confirmation_id,
1212 'name' => __('Default Confirmation', 'gravityforms'),
1213 'isDefault' => true,
1214 'type' => 'message',
1215 'message' => sprintf(__('Thanks for contacting us! We will get in touch with you shortly. [e2pdf-download id="%s"]', 'gravityforms'), $template->get('ID')),
1216 'url' => '',
1217 'pageId' => '',
1218 'queryString' => '',
1219 );
1220
1221 $pages = $template->get('pages');
1222 $checkboxes = array();
1223 $radios = array();
1224
1225 $field_id = 1;
1226
1227 foreach ($pages as $page_key => $page) {
1228 if (isset($page['elements']) && !empty($page['elements'])) {
1229 foreach ($page['elements'] as $element_key => $element) {
1230 $type = false;
1231 $labels = array();
1232 $label = '';
1233
1234 if ($auto_form_shortcode) {
1235 $labels[] = $field_id;
1236 }
1237 if ($auto_form_label && $auto_form_label == 'value' && isset($element['value']) && $element['value']) {
1238 $labels[] = $element['value'];
1239 } elseif ($auto_form_label && $auto_form_label == 'name' && isset($element['name']) && $element['name']) {
1240 $labels[] = $element['name'];
1241 }
1242
1243 if ($element['type'] == 'e2pdf-input' || $element['type'] == 'e2pdf-signature') {
1244 $type = 'text';
1245 $label = !empty($labels) ? implode(' ', $labels) : __('Text', 'e2pdf');
1246 } elseif ($element['type'] == 'e2pdf-textarea') {
1247
1248 $type = 'textarea';
1249 $label = !empty($labels) ? implode(' ', $labels) : __('Textarea', 'e2pdf');
1250 } elseif ($element['type'] == 'e2pdf-select') {
1251 $type = 'select';
1252 $label = !empty($labels) ? implode(' ', $labels) : __('Select', 'e2pdf');
1253
1254 $choices = array();
1255 $field_options = array();
1256
1257 if (isset($element['properties']['options'])) {
1258 $field_options = explode("\n", $element['properties']['options']);
1259 foreach ($field_options as $option) {
1260 $choices[] = array(
1261 'text' => $option,
1262 'value' => $option
1263 );
1264 }
1265 }
1266 } elseif ($element['type'] == 'e2pdf-checkbox') {
1267 $field_key = array_search($element['name'], array_column($checkboxes, 'name'));
1268 if ($field_key !== false) {
1269 $checkbox = array_search($checkboxes[$field_key]['id'], array_column($form['fields'], 'id'));
1270 if ($checkbox !== false) {
1271 $form['fields'][$checkbox]['choices'][] = array(
1272 'text' => $element['properties']['option'],
1273 'value' => $element['properties']['option'],
1274 );
1275
1276 $num = count($form['fields'][$checkbox]['inputs']) + 1;
1277 $form['fields'][$checkbox]['inputs'][] = array(
1278 'id' => $field_id . '.' . $num,
1279 'label' => $element['properties']['option'],
1280 );
1281 $pages[$page_key]['elements'][$element_key]['value'] = '{' . $form['fields'][$checkbox]['label'] . ':' . $form['fields'][$checkbox]['id'] . '}';
1282 }
1283 } else {
1284 $label = !empty($labels) ? implode(' ', $labels) : __('Checkbox', 'e2pdf');
1285 $type = 'checkbox';
1286 $checkboxes[] = array(
1287 'id' => $field_id,
1288 'name' => $element['name'],
1289 );
1290 $choices = array(
1291 array(
1292 'text' => $element['properties']['option'],
1293 'value' => $element['properties']['option'],
1294 )
1295 );
1296
1297 $inputs = array(
1298 array(
1299 'id' => $field_id . '.1',
1300 'label' => $element['properties']['option'],
1301 )
1302 );
1303 }
1304 } elseif ($element['type'] == 'e2pdf-radio') {
1305 if (isset($element['properties']['group']) && $element['properties']['group']) {
1306 $element['name'] = $element['properties']['group'];
1307 } else {
1308 $element['name'] = $element['element_id'];
1309 }
1310
1311 $field_key = array_search($element['name'], array_column($radios, 'name'));
1312 if ($field_key !== false) {
1313 $radio = array_search($radios[$field_key]['id'], array_column($form['fields'], 'id'));
1314 if ($radio !== false) {
1315 $form['fields'][$radio]['choices'][] = array(
1316 'text' => $element['properties']['option'],
1317 'value' => $element['properties']['option'],
1318 );
1319
1320 $pages[$page_key]['elements'][$element_key]['value'] = '{' . $form['fields'][$checkbox]['label'] . ':' . $form['fields'][$checkbox]['id'] . '}';
1321 }
1322 } else {
1323 $label = !empty($labels) ? implode(' ', $labels) : __('Radio', 'e2pdf');
1324 $type = 'radio';
1325 $radios[] = array(
1326 'id' => $field_id,
1327 'name' => $element['name'],
1328 );
1329 $choices = array(
1330 array(
1331 'text' => $element['properties']['option'],
1332 'value' => $element['properties']['option'],
1333 )
1334 );
1335 }
1336 }
1337
1338 if ($type) {
1339 $field = array(
1340 'id' => $field_id,
1341 'type' => $type,
1342 'label' => $label,
1343 );
1344 if ($type == 'select' || $type == 'radio' || $type == 'checkbox') {
1345 $field['choices'] = $choices;
1346 }
1347
1348 if ($type == 'checkbox') {
1349 $field['inputs'] = $inputs;
1350 }
1351
1352 $form['fields'][] = $field;
1353 $pages[$page_key]['elements'][$element_key]['value'] = '{' . $label . ':' . $field_id . '}';
1354
1355 if (isset($element['properties']['esig'])) {
1356 unset($pages[$page_key]['elements'][$element_key]['properties']['esig']);
1357 }
1358 $field_id++;
1359 }
1360 }
1361 }
1362 }
1363
1364 $item = GFAPI::add_form($form);
1365 if (!is_wp_error($item)) {
1366 $template->set('item', $item);
1367 $template->set('pages', $pages);
1368 }
1369 }
1370 }
1371
1372 return $template;
1373 }
1374
1375 /**
1376 * Init Visual Mapper data
1377 *
1378 * @return bool|string - HTML data source for Visual Mapper
1379 */
1380 public function visual_mapper() {
1381
1382 $item = $this->get('item');
1383 $html = '';
1384 $source = '';
1385
1386 if ($item && function_exists('gravity_form')) {
1387
1388 $form = false;
1389 if (class_exists('GFFormsModel')) {
1390 $form = GFFormsModel::get_form_meta($item);
1391 }
1392
1393 if (!$form) {
1394 return __('Form could not be found', 'e2pdf');
1395 }
1396
1397 add_filter('gform_pre_render', array($this, 'filter_gform_pre_render'), 30, 1);
1398 $source = gravity_form($item, true, true, false, null, false, 0, false);
1399 remove_filter('gform_pre_render', array($this, 'filter_gform_pre_render'), 30);
1400
1401 if ($source) {
1402 libxml_use_internal_errors(true);
1403 $dom = new DOMDocument();
1404 if (function_exists('mb_convert_encoding')) {
1405 $html = $dom->loadHTML(mb_convert_encoding($source, 'HTML-ENTITIES', 'UTF-8'));
1406 } else {
1407 $html = $dom->loadHTML('<?xml encoding="UTF-8">' . $source);
1408 }
1409 libxml_clear_errors();
1410 }
1411
1412 if (!$source) {
1413 return __('Form source is empty', 'e2pdf');
1414 } elseif (!$html) {
1415 return __('Form could not be parsed due incorrect HTML', 'e2pdf');
1416 } else {
1417 $xpath = new DomXPath($dom);
1418
1419 $remove_by_class = array(
1420 'gf_progressbar_wrapper',
1421 'gform_previous_button',
1422 'gform_next_button',
1423 'gform_button button'
1424 );
1425 foreach ($remove_by_class as $key => $class) {
1426 $elements = $xpath->query("//*[contains(@class, '{$class}')]");
1427 foreach ($elements as $element) {
1428 $element->parentNode->removeChild($element);
1429 }
1430 }
1431
1432 $remove_by_tag = array(
1433 'script'
1434 );
1435 foreach ($remove_by_tag as $key => $tag) {
1436 $elements = $xpath->query("//{$tag}");
1437 foreach ($elements as $element) {
1438 $element->parentNode->removeChild($element);
1439 }
1440 }
1441
1442 //replace time fields
1443 $elements = $xpath->query("//*[contains(@class, 'gfield_time_hour')]");
1444 foreach ($elements as $element) {
1445
1446 $sub_elements = $xpath->query(".//*[self::input or self::select]", $element->parentNode);
1447 foreach ($sub_elements as $sub_element) {
1448 if ($sub_element->attributes->getNamedItem("class")->nodeValue) {
1449 $sub_element->attributes->getNamedItem("class")->nodeValue = $sub_element->attributes->getNamedItem("class")->nodeValue . " e2pdf-no-vm";
1450 } else {
1451 $attr = $dom->createAttribute('class');
1452 $attr->value = 'e2pdf-no-vm';
1453 $sub_element->appendChild($attr);
1454 }
1455 }
1456
1457 if ($element->parentNode->attributes->getNamedItem("class")->nodeValue) {
1458 $element->parentNode->attributes->getNamedItem("class")->nodeValue = $element->parentNode->attributes->getNamedItem("class")->nodeValue . ' e2pdf-vm-field-wrapper';
1459 } else {
1460 $attr = $dom->createAttribute('class');
1461 $attr->value = 'e2pdf-vm-field-wrapper';
1462 $element->parentNode->appendChild($attr);
1463 }
1464
1465 $input = $xpath->query("./input", $element)->item(0);
1466 $field = $dom->createElement('input');
1467 $field_atts = array(
1468 'type' => 'text',
1469 'class' => 'e2pdf-vm-field',
1470 'name' => $input->attributes->getNamedItem("name")->nodeValue
1471 );
1472
1473 foreach ($field_atts as $key => $value) {
1474 $attr = $dom->createAttribute($key);
1475 $attr->value = $value;
1476 $field->appendChild($attr);
1477 }
1478 $element->parentNode->appendChild($field);
1479 }
1480
1481 //replace multiupload field
1482 $elements = $xpath->query("//*[contains(@class, 'gform_drop_area')]");
1483 foreach ($elements as $element) {
1484 $element->attributes->getNamedItem("class")->nodeValue = $element->attributes->getNamedItem("class")->nodeValue . ' e2pdf-vm-field-wrapper';
1485
1486 $field_id = '0';
1487 if ($element->attributes->getNamedItem("id")->nodeValue) {
1488 $field_id = preg_replace('/(?:.*)_([\d]+)/', '${1}', $element->attributes->getNamedItem("id")->nodeValue);
1489 }
1490
1491 $field = $dom->createElement('input');
1492 $field_atts = array(
1493 'type' => 'text',
1494 'class' => 'e2pdf-vm-field',
1495 'name' => 'input_' . $field_id
1496 );
1497
1498 foreach ($field_atts as $key => $value) {
1499 $attr = $dom->createAttribute($key);
1500 $attr->value = $value;
1501 $field->appendChild($attr);
1502 }
1503
1504 $element->appendChild($field);
1505 }
1506
1507 //replace single product
1508 $elements = $xpath->query("//*[contains(@class, 'ginput_container_singleproduct') or contains(@class, 'ginput_container_product_calculation') or contains(@class, 'ginput_container_singleshipping') or contains(@class, 'ginput_container_total') or contains(@class, 'gfield_signature_container')]");
1509 foreach ($elements as $element) {
1510 $spans = $xpath->query(".//span", $element);
1511 foreach ($spans as $key => $sub_element) {
1512 $sub_element->parentNode->removeChild($sub_element);
1513 }
1514
1515 $inputs = $xpath->query(".//input", $element);
1516 foreach ($inputs as $key => $sub_element) {
1517 $sub_element->attributes->getNamedItem("type")->nodeValue = 'text';
1518 $sub_element->attributes->getNamedItem("class")->nodeValue = '';
1519 }
1520 }
1521
1522 $merged_tags = array();
1523 if (class_exists('GFCommon')) {
1524 foreach ($form['fields'] as $field) {
1525 $tags = GFCommon::get_field_merge_tags($field);
1526 foreach ($tags as $tag) {
1527 if (isset($tag['tag'])) {
1528 if ($field->type == 'list') {
1529 $field_id = preg_replace('/\{(?:.*)\:(.*)\:\}/', '${1}', $tag['tag']);
1530 } else {
1531 $field_id = preg_replace('/\{(?:.*)\:(.*)\}/', '${1}', $tag['tag']);
1532 }
1533 if ($field_id) {
1534 $merged_tags[$field_id] = $tag['tag'];
1535 }
1536 }
1537 }
1538 }
1539 }
1540
1541 $fields = array();
1542 foreach ($form['fields'] as $field) {
1543 $fields[$field->id] = $field;
1544 }
1545
1546 $replace_by_types = array(
1547 "//input",
1548 "//textarea",
1549 "//select"
1550 );
1551
1552 foreach ($replace_by_types as $replace_by_type) {
1553 $inputs = $xpath->query($replace_by_type);
1554 foreach ($inputs as $element) {
1555 $type = $element->attributes->getNamedItem("type")->nodeValue;
1556 if ($element->attributes->getNamedItem("name")) {
1557
1558 $field_id = false;
1559 $field = false;
1560 $sub_field_id = preg_replace('/input_([^\[]+)(?:.*)/', '${1}', $element->attributes->getNamedItem("name")->nodeValue);
1561
1562 if ($sub_field_id) {
1563
1564 if (substr($sub_field_id, -6) == '_valid') {
1565 $field_id = preg_replace('/(?:.*)\_([\d]+)\_valid/', '${1}', $sub_field_id);
1566 } else {
1567 $field_id = preg_replace('/([\d]+)\.(?:.*)/', '${1}', $sub_field_id);
1568 }
1569
1570 if (isset($fields[$field_id])) {
1571 $field = $fields[$field_id];
1572 }
1573 }
1574
1575 if ($field) {
1576 if (
1577 $field->type == 'name' ||
1578 $field->type == 'address' ||
1579 ($field->type == 'product' && $field->inputType && ($field->inputType == 'singleproduct' || $field->inputType == 'calculation'))
1580 ) {
1581 $value = $merged_tags[$sub_field_id];
1582 $element->attributes->getNamedItem("name")->nodeValue = $value;
1583 } elseif ($field->type == 'consent') {
1584 $value = isset($merged_tags[$field_id . '.1']) ? $merged_tags[$field_id . '.1'] : '';
1585 $element->attributes->getNamedItem("name")->nodeValue = $value;
1586 } else {
1587 if (isset($merged_tags[$field_id])) {
1588 $value = $merged_tags[$field_id];
1589 if (substr($value, -1) == '}') {
1590 if ($element->parentNode->attributes->getNamedItem("class") && (false !== strpos($element->parentNode->attributes->getNamedItem("class")->nodeValue, 'ginput_post_image_'))) {
1591 if (false !== strpos($element->parentNode->attributes->getNamedItem("class")->nodeValue, 'ginput_post_image_title')) {
1592 $value = substr($value, 0, -1) . ':title}';
1593 } elseif (false !== strpos($element->parentNode->attributes->getNamedItem("class")->nodeValue, 'ginput_post_image_caption')) {
1594 $value = substr($value, 0, -1) . ':caption}';
1595 } elseif (false !== strpos($element->parentNode->attributes->getNamedItem("class")->nodeValue, 'ginput_post_image_description')) {
1596 $value = substr($value, 0, -1) . ':description}';
1597 }
1598 } elseif ($field->type == 'list') {
1599 if ($field->enableColumns) {
1600 $parent_class = $element->parentNode->attributes->getNamedItem("class")->nodeValue;
1601 $index = 1;
1602 $td = $xpath->query("./td[contains(@class, '{$parent_class}')]/preceding-sibling::td", $element->parentNode->parentNode);
1603 if ($td && isset($td->length)) {
1604 $index = (int) $td->length + 1;
1605 }
1606 $value = substr($value, 0, -1) . '1_' . $index . '}';
1607 } else {
1608 $value = substr($value, 0, -1) . '1}';
1609 }
1610 } elseif ($field->enableChoiceValue && $value) {
1611 $value = substr($value, 0, -1) . ':value}';
1612 }
1613 }
1614
1615 $element->attributes->getNamedItem("name")->nodeValue = $value;
1616 }
1617 }
1618 }
1619 }
1620 }
1621 }
1622
1623 return $dom->saveHTML();
1624 }
1625 }
1626
1627 return false;
1628 }
1629
1630 /**
1631 * Convert Field name to Value
1632 * @since 0.01.34
1633 *
1634 * @param string $name - Field name
1635 *
1636 * @return bool|string - Converted value or false
1637 */
1638 public function auto_map($name = false) {
1639
1640 $item = $this->get('item');
1641
1642 $merged_tags = array();
1643 $form = false;
1644 $sub_field_id = false;
1645
1646 if ($name) {
1647 $sub_field_id = preg_replace('/\{(?:.*)\:(.*)\:\}/', '${1}', $name);
1648 }
1649
1650 if ($sub_field_id) {
1651 if (class_exists('GFFormsModel')) {
1652 $form = GFFormsModel::get_form_meta($item);
1653 }
1654
1655 if ($form) {
1656 if (class_exists('GFCommon')) {
1657 foreach ($form['fields'] as $field) {
1658 $tags = GFCommon::get_field_merge_tags($field);
1659 foreach ($tags as $tag) {
1660 if (isset($tag['tag'])) {
1661 if ($field->type == 'list') {
1662 $field_id = preg_replace('/\{(?:.*)\:(.*)\:\}/', '${1}', $tag['tag']);
1663 } else {
1664 $field_id = preg_replace('/\{(?:.*)\:(.*)\}/', '${1}', $tag['tag']);
1665 }
1666 if ($field_id) {
1667 $merged_tags[$field_id] = $tag['tag'];
1668 }
1669 }
1670 }
1671 }
1672 }
1673
1674 if (isset($merged_tags[$sub_field_id])) {
1675 return $merged_tags[$sub_field_id];
1676 }
1677 }
1678 }
1679
1680 return false;
1681 }
1682
1683 /**
1684 * Load additional shortcodes for this extension
1685 */
1686 public function load_shortcodes() {
1687
1688 }
1689
1690 public function filter_gform_confirmation($content, $form, $dataset, $ajax) {
1691
1692 if (false === strpos($content, '[')) {
1693 return $content;
1694 }
1695
1696 $shortcode_tags = array(
1697 'e2pdf-download',
1698 'e2pdf-save',
1699 'e2pdf-view',
1700 'e2pdf-adobesign'
1701 );
1702
1703 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
1704 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1705
1706 if (!empty($tagnames)) {
1707
1708 $pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames);
1709
1710 preg_match_all("/$pattern/", $content, $shortcodes);
1711 foreach ($shortcodes[0] as $key => $shortcode_value) {
1712
1713 $shortcode = array();
1714 $shortcode[1] = $shortcodes[1][$key];
1715 $shortcode[2] = $shortcodes[2][$key];
1716 $shortcode[3] = $shortcodes[3][$key];
1717 $shortcode[4] = $shortcodes[4][$key];
1718 $shortcode[5] = $shortcodes[5][$key];
1719 $shortcode[6] = $shortcodes[6][$key];
1720
1721 $atts = shortcode_parse_atts($shortcode[3]);
1722
1723 if (($shortcode[2] === 'e2pdf-save' && isset($atts['attachment']) && $atts['attachment'] == 'true') || $shortcode[2] === 'e2pdf-attachment') {
1724
1725 } else {
1726 if (!isset($atts['dataset']) && isset($atts['id'])) {
1727 $template = new Model_E2pdf_Template();
1728 $template->load($atts['id']);
1729 if ($template->get('extension') === 'gravity') {
1730 $entry_id = $dataset && isset($dataset['id']) ? $dataset['id'] : false;
1731 if ($entry_id) {
1732 $atts['dataset'] = $entry_id;
1733 $shortcode[3] .= " dataset=\"{$entry_id}\"";
1734 }
1735 }
1736 }
1737
1738 if (!isset($atts['apply'])) {
1739 $shortcode[3] .= " apply=\"true\"";
1740 }
1741
1742 if (!isset($atts['filter'])) {
1743 $shortcode[3] .= " filter=\"true\"";
1744 }
1745
1746 $new_shortcode = "[" . $shortcode[2] . $shortcode[3] . "]";
1747 $content = str_replace($shortcode_value, $new_shortcode, $content);
1748 }
1749 }
1750 }
1751
1752 return $content;
1753 }
1754
1755 public function filter_gform_notification($notification, $form, $dataset) {
1756
1757 $content = isset($notification['message']) && $notification['message'] ? $notification['message'] : '';
1758
1759 if (false === strpos($content, '[')) {
1760 return $notification;
1761 }
1762
1763 $shortcode_tags = array(
1764 'e2pdf-download',
1765 'e2pdf-save',
1766 'e2pdf-view',
1767 'e2pdf-adobesign',
1768 'e2pdf-attachment'
1769 );
1770
1771 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
1772 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1773
1774 if (!empty($tagnames)) {
1775
1776 $pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames);
1777
1778 preg_match_all("/$pattern/", $content, $shortcodes);
1779
1780 foreach ($shortcodes[0] as $key => $shortcode_value) {
1781
1782 $shortcode = array();
1783 $shortcode[1] = $shortcodes[1][$key];
1784 $shortcode[2] = $shortcodes[2][$key];
1785 $shortcode[3] = $shortcodes[3][$key];
1786 $shortcode[4] = $shortcodes[4][$key];
1787 $shortcode[5] = $shortcodes[5][$key];
1788 $shortcode[6] = $shortcodes[6][$key];
1789
1790 $atts = shortcode_parse_atts($shortcode[3]);
1791
1792 if (!isset($atts['dataset']) && isset($atts['id'])) {
1793 $template = new Model_E2pdf_Template();
1794 $template->load($atts['id']);
1795 if ($template->get('extension') === 'gravity') {
1796 $entry_id = $dataset && isset($dataset['id']) ? $dataset['id'] : false;
1797 if ($entry_id) {
1798 $atts['dataset'] = $entry_id;
1799 $shortcode[3] .= " dataset=\"{$entry_id}\"";
1800 }
1801 }
1802 }
1803
1804 if (!isset($atts['apply'])) {
1805 $shortcode[3] .= " apply=\"true\"";
1806 }
1807
1808 if (!isset($atts['filter'])) {
1809 $shortcode[3] .= " filter=\"true\"";
1810 }
1811
1812 $file = false;
1813
1814 if (($shortcode[2] === 'e2pdf-save' && isset($atts['attachment']) && $atts['attachment'] == 'true') || $shortcode[2] === 'e2pdf-attachment') {
1815
1816 if (class_exists('GFCommon')) {
1817 $shortcode[3] = GFCommon::replace_variables($shortcode[3], $form, $dataset, false, false, false, 'text');
1818 }
1819
1820 $file = do_shortcode_tag($shortcode);
1821 if ($file) {
1822 if ($shortcode[2] != 'e2pdf-save' && !isset($atts['pdf'])) {
1823 $this->helper->add('gravity_attachments', $file);
1824 }
1825
1826 $notification['attachments'] = ( is_array(rgget('attachments', $notification)) ) ? rgget('attachments', $notification) : array();
1827 $notification['attachments'][] = $file;
1828 }
1829
1830 $notification['message'] = str_replace($shortcode_value, '', $notification['message']);
1831 } else {
1832 $new_shortcode = "[" . $shortcode[2] . $shortcode[3] . "]";
1833 $notification['message'] = str_replace($shortcode_value, $new_shortcode, $notification['message']);
1834 }
1835 }
1836 }
1837
1838 return $notification;
1839 }
1840
1841 public function filter_gform_pre_render($form) {
1842
1843 if (isset($form['fields'])) {
1844 foreach ($form['fields'] as $key => $field) {
1845 if ($field->pageNumber != '1') {
1846 $field->pageNumber = '1';
1847 $form['fields'][$key] = $field;
1848 }
1849 }
1850 }
1851
1852 return $form;
1853 }
1854
1855 public function filter_gform_merge_tag_filter($value, $merge_tag, $modifier, $field, $raw_value) {
1856
1857 if ($field && $value) {
1858 if ($field->type == 'consent') {
1859 $mod = explode('.', $merge_tag);
1860 if (isset($mod[1]) && $mod[1] == '1') {
1861 $value = '1';
1862 }
1863 } elseif ($field->type == 'list') {
1864
1865 if ($modifier && $modifier != 'text') {
1866
1867 $list_id = false;
1868 $field_id = false;
1869
1870 if (false !== strpos($modifier, '_')) {
1871 $mod = explode('_', $modifier);
1872 if (isset($mod[0]) && is_numeric($mod[0])) {
1873 $list_id = $mod[0] - 1;
1874 }
1875 if (isset($mod[1]) && is_numeric($mod[1])) {
1876 $field_id = $mod[1] - 1;
1877 }
1878 } elseif (is_numeric($modifier)) {
1879 $list_id = $modifier - 1;
1880 }
1881
1882 if ($list_id !== false) {
1883 $value = '';
1884 $list = maybe_unserialize($raw_value);
1885 if (is_array($list)) {
1886 if (isset($list[$list_id])) {
1887 if ($field_id !== false) {
1888 if (is_array($list[$list_id]) && isset(array_values($list[$list_id])[$field_id])) {
1889 $value = array_values($list[$list_id])[$field_id];
1890 }
1891 } else {
1892 if (is_array($list[$list_id])) {
1893 $value = implode(',', $list[$list_id]);
1894 } else {
1895 $value = $list[$list_id];
1896 }
1897 }
1898 }
1899 }
1900 }
1901 }
1902 }
1903 }
1904
1905 return $value;
1906 }
1907
1908 /**
1909 * Delete attachments that were sent by email
1910 */
1911 public function action_gform_after_email($is_success) {
1912
1913 $files = $this->helper->get('gravity_attachments');
1914 if (is_array($files) && !empty($files)) {
1915 foreach ($files as $key => $file) {
1916 $this->helper->delete_dir(dirname($file) . '/');
1917 }
1918 $this->helper->deset('gravity_attachments');
1919 }
1920 }
1921
1922 /**
1923 * Get styles for generating Map Field function
1924 *
1925 * @return array - List of css files to load
1926 */
1927 public function styles() {
1928 $styles = array();
1929
1930 if (class_exists('GFCommon') && class_exists('GFForms')) {
1931 $base_url = GFCommon::get_base_url();
1932 $version = GFForms::$version;
1933 $min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
1934 $styles[] = $base_url . "/css/formreset{$min}.css?ver=" . $version;
1935 $styles[] = $base_url . "/css/datepicker{$min}.css?ver=" . $version;
1936 $styles[] = $base_url . "/css/formsmain{$min}.css?ver=" . $version;
1937 $styles[] = $base_url . "/css/readyclass{$min}.css?ver=" . $version;
1938 $styles[] = $base_url . "/css/browsers{$min}.css?ver=" . $version;
1939 $styles[] = $base_url . "/css/rtl{$min}.css?ver=" . $version;
1940 }
1941
1942 return $styles;
1943 }
1944
1945 }
1946