PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.08.06
E2Pdf – Export Pdf Tool for WordPress v1.08.06
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.08.06, at classes/extension/e2pdf-gravity.php

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