PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.01.01
E2Pdf – Export Pdf Tool for WordPress v1.01.01
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-formidable.php

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

2,387 lines 94.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * E2pdf Formidable Extension
5 *
6 * @copyright Copyright 2017 https://e2pdf.com
7 * @license GPL v2
8 * @version 1
9 * @link https://e2pdf.com
10 * @since 0.00.01
11 */
12 if (!defined('ABSPATH')) {
13 die('Access denied.');
14 }
15
16 class Extension_E2pdf_Formidable extends Model_E2pdf_Model {
17
18 private $options;
19
20 function __construct() {
21 parent::__construct();
22 }
23
24 /**
25 * Get info about extension
26 *
27 * @return array() - Extension key and name
28 */
29 public function info() {
30 return array(
31 'formidable' => __('Formidable', 'e2pdf')
32 );
33 }
34
35 /**
36 * Check if export item function available
37 *
38 * @return bool - Item export available/not available
39 */
40 public function export() {
41 return true;
42 }
43
44 /**
45 * Check if needed plugin active
46 *
47 * @return bool - Activated/Not Activated plugin
48 */
49 public function active() {
50
51 if (!function_exists('is_plugin_active')) {
52 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
53 }
54
55 if (is_plugin_active('formidable/formidable.php')) {
56 return true;
57 }
58 return false;
59 }
60
61 /**
62 * Set option
63 *
64 * @param string $key - Key of option
65 * @param string $value - Value of option
66 *
67 * @return bool - Status of setting option
68 */
69 public function set($key, $value) {
70
71 if (!isset($this->options)) {
72 $this->options = new stdClass();
73 }
74
75 $this->options->$key = $value;
76 return true;
77 }
78
79 /**
80 * Get option by key
81 *
82 * @param string $key - Key to get assigned option value
83 *
84 * @return mixed
85 */
86 public function get($key) {
87
88 if (isset($this->options->$key)) {
89 $value = $this->options->$key;
90 return $value;
91 } else {
92 return false;
93 }
94 }
95
96 /**
97 * Get items to work with
98 *
99 * @return array() - List of available items
100 */
101 public function get_items() {
102
103 $content = array();
104
105 if (class_exists('FrmForm')) {
106 $where = array(
107 'is_template' => 0,
108 'status' => 'published'
109 );
110
111 $forms = FrmForm::getAll($where, 'name');
112 if ($forms) {
113 foreach ($forms as $key => $value) {
114 $content[] = array(
115 'key' => $value->id,
116 'value' => $value->name
117 );
118 }
119 }
120 }
121
122 return $content;
123 }
124
125 /**
126 * Get entries for export
127 *
128 * @param int $item - Form ID
129 * @param string $name - Entries names
130 *
131 * @return array() - Entries list
132 */
133 public function get_datasets($item = false, $name = false) {
134
135 $item = (int) $item;
136 $datasets = array();
137
138 if (class_exists('FrmEntry') && $item) {
139 $where = array(
140 'it.form_id' => $item
141 );
142
143 $datasets_tmp = FrmEntry::getAll($where, ' ORDER BY id DESC');
144
145 if ($datasets_tmp) {
146 foreach ($datasets_tmp as $key => $dataset) {
147 $this->set('item', $item);
148 $this->set('dataset', $dataset->id);
149
150 $dataset_title = $this->render($name);
151 if (!$dataset_title) {
152 $dataset_title = $dataset->item_key;
153 }
154 $datasets[] = array(
155 'key' => $dataset->id,
156 'value' => $dataset_title
157 );
158 }
159 }
160 }
161
162
163 return $datasets;
164 }
165
166 /**
167 * Get dataset
168 *
169 * @param int $dataset - Dataset ID
170 *
171 * @return object - Dataset
172 */
173 public function get_dataset($dataset = false) {
174
175 $dataset = (int) $dataset;
176
177 if (!$dataset) {
178 return false;
179 }
180
181 $data = new stdClass();
182 $data->url = $this->helper->get_url(array('page' => 'formidable-entries', 'frm_action' => 'show', 'id' => $dataset));
183
184 return $data;
185 }
186
187 /**
188 * Get item
189 *
190 * @param int $item - Item ID
191 *
192 * @return object - Item
193 */
194 public function get_item($item = false) {
195
196 $item = (int) $item;
197 $form = new stdClass();
198
199 $formidable_form = false;
200 if (class_exists('FrmForm')) {
201 $formidable_form = FrmForm::getOne($item);
202 }
203
204 if ($formidable_form) {
205 $form->url = $this->helper->get_url(array('page' => 'formidable', 'frm_action' => 'edit', 'id' => $item));
206 $form->name = $formidable_form->name;
207 } else {
208 $form->url = 'javascript:void(0);';
209 $form->name = '';
210 }
211 return $form;
212 }
213
214 /**
215 * Render value according to content
216 *
217 * @param string $value - Content
218 * @param string $type - Type of rendering value
219 * @param array $field - Field details
220 *
221 * @return string - Fully rendered value
222 */
223 public function render($value, $type = false, $field = array()) {
224
225 $value = $this->render_shortcodes($value, $type, $field);
226 $value = $this->strip_shortcodes($value);
227
228 if ($type === 'value' && isset($field['type']) && $field['type'] === 'e2pdf-checkbox' && isset($field['properties']['option'])) {
229 $option = $this->render($field['properties']['option']);
230 $options = explode(', ', $value);
231 $option_options = explode(', ', $option);
232 if (is_array($options) && is_array($option_options) && !array_diff($option_options, $options)) {
233 return $option;
234 } else {
235 return "";
236 }
237 }
238
239 $value = str_replace("&#91;", "[", $value);
240
241 return $value;
242 }
243
244 /**
245 * Load actions for this extension
246 */
247 public function load_actions() {
248 add_action('frm_notification', array($this, 'action_frm_notification'), 30, 3);
249 add_action('e2pdf_model_template_fill_pre', array($this, 'action_e2pdf_model_template_fill_pre'), 10, 2);
250 add_action('e2pdf_model_template_fill_after', array($this, 'action_e2pdf_model_template_fill_after'), 10, 2);
251 }
252
253 /**
254 * Load filters for this extension
255 */
256 public function load_filters() {
257 add_filter('frm_content', array($this, 'filter_frm_content'), 30, 3);
258 add_filter('frm_notification_attachment', array($this, 'filter_frm_notification_attachment'), 30, 3);
259 add_filter('e2pdf_model_options_get_options_options', array($this, 'filter_e2pdf_model_options_get_options_options'), 10, 1);
260 add_filter('e2pdf_controller_templates_backup_options', array($this, 'filter_e2pdf_controller_templates_backup_options'), 10, 3);
261
262 add_filter('e2pdf_controller_templates_backup_pages', array($this, 'filter_e2pdf_controller_templates_backup_pages'), 10, 4);
263 add_filter('e2pdf_controller_templates_backup_dataset_title', array($this, 'filter_e2pdf_controller_templates_backup_replace_shortcodes'), 10, 4);
264 add_filter('e2pdf_controller_templates_backup_button_title', array($this, 'filter_e2pdf_controller_templates_backup_replace_shortcodes'), 10, 4);
265 add_filter('e2pdf_controller_templates_backup_name', array($this, 'filter_e2pdf_controller_templates_backup_replace_shortcodes'), 10, 4);
266 add_filter('e2pdf_controller_templates_backup_password', array($this, 'filter_e2pdf_controller_templates_backup_replace_shortcodes'), 10, 4);
267
268 add_filter('e2pdf_controller_templates_import_pages', array($this, 'filter_e2pdf_controller_templates_import_pages'), 10, 5);
269 add_filter('e2pdf_controller_templates_import_dataset_title', array($this, 'filter_e2pdf_controller_templates_import_replace_shortcodes'), 10, 5);
270 add_filter('e2pdf_controller_templates_import_button_title', array($this, 'filter_e2pdf_controller_templates_import_replace_shortcodes'), 10, 5);
271 add_filter('e2pdf_controller_templates_import_name', array($this, 'filter_e2pdf_controller_templates_import_replace_shortcodes'), 10, 5);
272 add_filter('e2pdf_controller_templates_import_password', array($this, 'filter_e2pdf_controller_templates_import_replace_shortcodes'), 10, 5);
273 }
274
275 /**
276 * Render shortcodes which available in this extension
277 *
278 * @param string $value - Content
279 * @param string $type - Type of rendering value
280 * @param array $field - Field details
281 *
282 * @return string - Value with rendered shortcodes
283 */
284 public function render_shortcodes($value, $type = false, $field = array()) {
285
286 $dataset = $this->get('dataset');
287 $item = $this->get('item');
288
289 $form = false;
290 $entry = false;
291 $maybe_checkbox_separated = false;
292
293 if (class_exists('FrmForm') && class_exists('FrmEntry')) {
294 $form = FrmForm::getOne($item);
295 $entry = FrmEntry::getOne($dataset);
296 }
297
298 if ($form && $entry) {
299 if (false !== strpos($value, '[')) {
300
301 // Start render Separatable fields shortcode
302 if (class_exists('FrmProEntry')) {
303 $shortcode_tags = array();
304 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
305 $tagnames = array_intersect($shortcode_tags, $matches[1]);
306
307 foreach ($matches[1] as $key => $shortcode) {
308 if (strpos($shortcode, ':') !== false) {
309 $shortcode_tags[] = $shortcode;
310 }
311 }
312
313 $tagnames = array_intersect($shortcode_tags, $matches[1]);
314 if (!empty($tagnames)) {
315 $pattern = get_shortcode_regex($tagnames);
316 preg_match_all("/$pattern/", $value, $shortcodes);
317 foreach ($shortcodes[0] as $key => $shortcode_value) {
318 $shortcode = array();
319 $shortcode[1] = $shortcodes[1][$key];
320 $shortcode[2] = $shortcodes[2][$key];
321 $shortcode[3] = $shortcodes[3][$key];
322 $shortcode[4] = $shortcodes[4][$key];
323 $shortcode[5] = $shortcodes[5][$key];
324 $shortcode[6] = $shortcodes[6][$key];
325
326 $shortcode_details = explode(":", $shortcode[2]);
327
328 $field_id = $shortcode_details['0'];
329 $child_id = $shortcode_details['1'];
330 $child_entry = 0;
331
332 $child_field = false;
333 $child_form = false;
334
335 if (class_exists("FrmField")) {
336 $child_field = FrmField::getOne($field_id);
337 if ($child_field) {
338 $child_form = $child_field->form_id;
339 }
340 }
341
342 $childs = FrmProEntry::get_sub_entries($dataset, true);
343 $child_entries = array();
344
345 if ($childs && $child_form) {
346 foreach ($childs as $child) {
347 if ($child->form_id == $child_form) {
348 $child_entries[] = $child->id;
349 }
350 }
351 }
352
353 $new_shortcode = "";
354
355 if (!empty($child_entries) && count($child_entries) >= $child_id) {
356 $start = 1;
357 foreach ($child_entries as $key => $sub_entry) {
358 if ($start == $child_id) {
359 $child_entry = $sub_entry;
360 break;
361 }
362 $start++;
363 }
364
365 $shortcode[2] = "frm-field-value field_id={$field_id} entry={$child_entry}";
366 $new_shortcode = "[" . $shortcode[2] . $shortcode[3] . "]";
367 }
368
369 $maybe_checkbox_separated = true;
370 $value = str_replace($shortcode_value, $new_shortcode, $value);
371 }
372 }
373 }
374 // End render Separatable fields shortcode
375
376 $shortcode_tags = array(
377 'e2pdf-format-number',
378 'e2pdf-format-date',
379 'e2pdf-format-output',
380 );
381
382 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
383 $tagnames = array_intersect($shortcode_tags, $matches[1]);
384
385 if (!empty($tagnames)) {
386
387 $pattern = get_shortcode_regex($tagnames);
388 preg_match_all("/$pattern/", $value, $shortcodes);
389 foreach ($shortcodes[0] as $key => $shortcode_value) {
390 $shortcode = array();
391 $shortcode[1] = $shortcodes[1][$key];
392 $shortcode[2] = $shortcodes[2][$key];
393 $shortcode[3] = $shortcodes[3][$key];
394 $shortcode[4] = $shortcodes[4][$key];
395 $shortcode[5] = $shortcodes[5][$key];
396 $shortcode[6] = $shortcodes[6][$key];
397
398 if (isset($shortcode['5'])) {
399 $sub_value = $this->strip_shortcodes(apply_filters('frm_content', $shortcode['5'], $form, $entry));
400 $value = str_replace($shortcode_value, "[" . $shortcode['2'] . $shortcode['3'] . "]" . $sub_value . "[/" . $shortcode['2'] . "]", $value);
401 }
402 }
403 }
404 }
405
406
407 /*
408 * Checkboxes separatable fix filter add
409 * @since 0.01.43
410 */
411 if ($maybe_checkbox_separated) {
412 add_filter('frm_display_value_custom', array($this, 'filter_frm_display_value_custom'), 0, 3);
413 }
414
415 $value = do_shortcode($value);
416
417 /*
418 * Checkboxes separatable fix filter remove
419 * @since 0.01.43
420 */
421 if ($maybe_checkbox_separated) {
422 remove_filter('frm_display_value_custom', array($this, 'filter_frm_display_value_custom'), 0);
423 }
424
425 $value = apply_filters('frm_content', $value, $form, $entry);
426
427 if (isset($field['type']) && ($field['type'] === 'e2pdf-image' || $field['type'] === 'e2pdf-signature')) {
428 $esig = isset($field['properties']['esig']) && $field['properties']['esig'] ? true : false;
429 if ($esig) {
430 //process e-signature
431 $value = "";
432 } else {
433 preg_match('/src="([^"]*)"/', $value, $matches);
434
435 if (isset($matches[1])) {
436 $value = $matches[1];
437 }
438
439 $helper_e2pdf_image = new Helper_E2pdf_Image();
440
441 if (!$helper_e2pdf_image->get_image($value)) {
442 $value = $this->strip_shortcodes($value);
443 if (
444 $value &&
445 trim($value) != "" &&
446 extension_loaded('gd') &&
447 function_exists('imagettftext')
448 ) {
449 if (isset($field['properties']['text_color']) && $field['properties']['text_color']) {
450 $penColour = $this->helper->hexColorAllocate($field['properties']['text_color']);
451 } else {
452 $penColour = array(0x14, 0x53, 0x94);
453 }
454
455 $default_options = array(
456 'imageSize' => array(isset($field['width']) ? $field['width'] : '400', isset($field['height']) ? $field['height'] : '150'),
457 'bgColour' => 'transparent',
458 'penColour' => $penColour
459 );
460
461 $options = array();
462 $options = apply_filters('e2pdf_frm_sig_output_options', $options, $field_id);
463 $options = array_merge($default_options, $options);
464
465 $model_e2pdf_font = new Model_E2pdf_Font();
466
467 $font = false;
468 if (isset($field['properties']['text_font']) && $field['properties']['text_font']) {
469 $font = $model_e2pdf_font->get_font_path($field['properties']['text_font']);
470 } elseif (class_exists('FrmSigAppHelper')) {
471 if (file_exists(FrmSigAppHelper::plugin_path() . '/assets/journal.ttf')) {
472 $font = FrmSigAppHelper::plugin_path() . '/assets/journal.ttf';
473 }
474 }
475
476 if (!$font) {
477 $font = $model_e2pdf_font->get_font_path('Noto Sans');
478 }
479
480 $size = 150;
481 if (isset($field['properties']['text_font_size']) && $field['properties']['text_font_size']) {
482 $size = $field['properties']['text_font_size'];
483 }
484
485 $model_e2pdf_signature = new Model_E2pdf_Signature();
486 $value = $model_e2pdf_signature->ttf_signature($value, $size, $font, $options);
487 } else {
488 $value = "";
489 }
490 }
491 }
492 }
493
494 if ((false !== strpos($value, '[referer]') || false !== strpos($value, '[browser]')) && isset($entry->description)) {
495 $description = maybe_unserialize($entry->description);
496 $referer = "";
497 if (isset($description['referrer'])) {
498 if (@preg_match('/Referer +\d+\:[ \t]+([^\n\t]+)/', $description['referrer'], $m)) {
499 $referer = $m[1];
500 } else {
501 $referer = $description['referrer'];
502 }
503 }
504
505 $browser = "";
506 if (isset($description['browser'])) {
507 $browser = $description['browser'];
508 }
509
510 $replace = array(
511 'from' => array(
512 '[referer]',
513 '[browser]'
514 ),
515 'to' => array(
516 $referer,
517 $browser
518 )
519 );
520 $value = str_replace($replace['from'], $replace['to'], $value);
521 }
522
523 if (false !== strpos($value, '[entry_num]')) {
524 $entry_num = '';
525 if (class_exists("FrmDb")) {
526 $entry_num = FrmDb::get_count('frm_items', array("form_id = '{$form->id}' AND id <= '{$entry->id}' AND 1" => "1"));
527 }
528
529 $replace = array(
530 'from' => array(
531 '[entry_num]'
532 ),
533 'to' => array(
534 $entry_num
535 )
536 );
537
538
539 $value = str_replace($replace['from'], $replace['to'], $value);
540 }
541
542
543 if (false !== strpos($value, '[pdf_num]')) {
544 $pdf_num = '0';
545 $uid = $this->get('uid');
546 if ($uid) {
547 $entry = new Model_E2pdf_Entry();
548 if ($entry->load_by_uid($uid)) {
549 $pdf_num = $entry->get('pdf_num') + 1;
550 }
551 }
552
553 $replace = array(
554 'from' => array(
555 '[pdf_num]'
556 ),
557 'to' => array(
558 $pdf_num
559 )
560 );
561
562
563 $value = str_replace($replace['from'], $replace['to'], $value);
564 }
565 }
566
567 return $value;
568 }
569
570 /**
571 * Strip unused shortcodes
572 *
573 * @param string $value - Content
574 *
575 * @return string - Value with removed unused shortcodes
576 */
577 public function strip_shortcodes($value) {
578 $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', "", $value);
579 return $value;
580 }
581
582 /**
583 * Search and update shortcodes for this extension inside content
584 * Auto set of dataset id
585 *
586 * @param string $content - Content
587 * @param int $form - ID of form
588 * @param int $dataset - ID of dataset
589 *
590 * @return string - Content with updates shortcodes
591 */
592 public function filter_frm_content($content, $form, $dataset) {
593
594 if (false === strpos($content, '[')) {
595 return $content;
596 }
597
598 $shortcode_tags = array(
599 'e2pdf-download',
600 'e2pdf-save',
601 'e2pdf-view'
602 );
603
604 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
605
606 $tagnames = array_intersect($shortcode_tags, $matches[1]);
607
608 if (!empty($tagnames)) {
609 $pattern = get_shortcode_regex($tagnames);
610
611 preg_match_all("/$pattern/", $content, $shortcodes);
612 foreach ($shortcodes[0] as $key => $shortcode_value) {
613
614 $extension = false;
615 $item = false;
616
617 $shortcode = array();
618 $shortcode[1] = $shortcodes[1][$key];
619 $shortcode[2] = $shortcodes[2][$key];
620 $shortcode[3] = $shortcodes[3][$key];
621 $shortcode[4] = $shortcodes[4][$key];
622 $shortcode[5] = $shortcodes[5][$key];
623 $shortcode[6] = $shortcodes[6][$key];
624
625 $atts = shortcode_parse_atts($shortcode[3]);
626
627 if (array_key_exists('id', $atts)) {
628 $template = new Model_E2pdf_Template();
629 $template->load($atts['id']);
630 if ($template->get('extension') === 'formidable') {
631 $extension = $template->get('extension');
632 $item = $template->get('item');
633 }
634 }
635
636 if ($extension) {
637 if (!array_key_exists('dataset', $atts)) {
638
639 if (is_object($dataset)) {
640 $entry_id = $dataset->id;
641 } else {
642 $entry_id = $dataset;
643 }
644
645 if ($entry_id) {
646 $atts['dataset'] = $entry_id;
647 $shortcode[3] .= " dataset='{$entry_id}'";
648
649 if ($shortcode[2] === 'e2pdf-save') {
650 $shortcode[3] .= " apply='true'";
651 }
652
653 $content = str_replace($shortcode_value, do_shortcode_tag($shortcode), $content);
654 }
655 } else {
656
657 if ($shortcode[2] === 'e2pdf-save') {
658 $shortcode[3] .= " apply='true'";
659 }
660
661 $content = str_replace($shortcode_value, do_shortcode_tag($shortcode), $content);
662 }
663 } elseif ($shortcode['2'] === 'e2pdf-view') {
664 $content = str_replace($shortcode_value, do_shortcode_tag($shortcode), $content);
665 }
666 }
667 }
668
669 return $content;
670 }
671
672 /**
673 * Filter for checkbox repeatable Label/Value fix
674 *
675 * @param array $value - Value
676 * @param obj $field - Field
677 * @param array $atts - Shortcode Attributes
678 *
679 * @return mixed - Updated value
680 */
681 public function filter_frm_display_value_custom($value, $field, $atts = array()) {
682 if (class_exists("FrmProEntriesController")) {
683 $defaults = array('html' => 0, 'type' => $field->type, 'keepjs' => 0);
684 $atts = array_merge($defaults, $atts);
685
686 if ($atts['type'] === 'checkbox') {
687 $value = FrmProEntriesController::get_option_label_for_saved_value($value, $field, $atts);
688 }
689 }
690 return $value;
691 }
692
693 /**
694 * Generate attachments according shortcodes in Email template
695 *
696 * @param array $attachments - List of attachments
697 * @param int $form - ID of form
698 * @param array $args - Arguments
699 *
700 * @return array - Updated list of attachments
701 */
702 public function filter_frm_notification_attachment($attachments = array(), $form, $args) {
703
704 if (!isset($args['email_key'])) {
705 return $attachments;
706 }
707
708 $form_actions = FrmFormAction::get_action_for_form($form->id);
709
710 $dataset = $args['entry']->id;
711 $email_key = $args['email_key'];
712
713 $shortcode_tags = array(
714 'e2pdf-attachment',
715 );
716
717 foreach ($form_actions as $key => $action) {
718 if ($action->ID === $email_key) {
719
720 $content = $action->post_content['email_message'];
721
722 if (false === strpos($content, '[')) {
723 return $attachments;
724 }
725
726 $content = apply_filters('frm_content', $content, $form, $args['entry']);
727
728 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
729 $tagnames = array_intersect($shortcode_tags, $matches[1]);
730
731 if (!empty($tagnames)) {
732 $pattern = get_shortcode_regex($tagnames);
733
734 preg_match_all("/$pattern/", $content, $shortcodes);
735
736 foreach ($shortcodes[0] as $key => $shortcode_value) {
737
738 $shortcode = array();
739 $shortcode[1] = $shortcodes[1][$key];
740 $shortcode[2] = $shortcodes[2][$key];
741 $shortcode[3] = $shortcodes[3][$key];
742 $shortcode[4] = $shortcodes[4][$key];
743 $shortcode[5] = $shortcodes[5][$key];
744 $shortcode[6] = $shortcodes[6][$key];
745
746 $atts = shortcode_parse_atts($shortcode[3]);
747 if (!array_key_exists('dataset', $atts)) {
748 if (is_object($dataset)) {
749 $entry_id = $dataset->id;
750 } else {
751 $entry_id = $dataset;
752 }
753
754 if ($entry_id) {
755 $atts['dataset'] = $entry_id;
756 $shortcode[3] .= " dataset='{$entry_id}' apply='true'";
757 }
758 } else {
759 $shortcode[3] .= " apply='true'";
760 }
761
762 $file = do_shortcode_tag($shortcode);
763
764 if ($file) {
765 $this->helper->add('formidable_attachments', $file);
766 $attachments[] = $file;
767 }
768 }
769 }
770 }
771 }
772 return $attachments;
773 }
774
775 /**
776 * Remove Page Breaks for Visual Mapper
777 *
778 * @param array $fields - List of fields
779 *
780 * @return array - Updated fields
781 */
782 function filter_remove_pagebreaks($fields) {
783 foreach ((array) $fields as $field_key => $field) {
784 if ($field->type == 'break') {
785 unset($fields[$field_key]);
786 }
787 }
788 return $fields;
789 }
790
791 /**
792 * Add options for Formidable extension
793 *
794 * @param array $options - List of options
795 *
796 * @return array - Updated options list
797 */
798 public function filter_e2pdf_model_options_get_options_options($options = array()) {
799 $options['formidable_group'] = array(
800 'name' => __('Formidable', 'e2pdf'),
801 'action' => 'extension',
802 'group' => 'formidable_group',
803 'options' => array(
804 array(
805 'name' => __('Auto PDF and Visual Mapper', 'e2pdf'),
806 'key' => 'e2pdf_formidable_use_keys',
807 'value' => get_option('e2pdf_formidable_use_keys') === false ? 0 : get_option('e2pdf_formidable_use_keys'),
808 'default_value' => 0,
809 'type' => 'checkbox',
810 'placeholder' => __('Use Field Keys instead Field IDs', 'e2pdf'),
811 ),
812 )
813 );
814 return $options;
815 }
816
817 public function filter_e2pdf_controller_templates_backup_options($options, $template, $extension) {
818
819 if ($extension->loaded('formidable')) {
820
821 $options['formidable'] = array(
822 'name' => __('Formidable', 'e2pdf'),
823 'options' => array(
824 array(
825 'name' => __('Force shortcodes to use', 'e2pdf'),
826 'key' => 'options[formidable_force_shortcodes]',
827 'value' => 0,
828 'default_value' => 0,
829 'type' => 'select',
830 'options' => array(
831 '0' => __('None', 'e2pdf'),
832 '1' => __('Fields IDs', 'e2pdf'),
833 '2' => __('Field Keys', 'e2pdf'),
834 )
835 ),
836 )
837 );
838 }
839
840 return $options;
841 }
842
843 public function filter_e2pdf_controller_templates_backup_pages($options, $template, $extension, $pages) {
844
845 if ($extension->loaded('formidable') && (isset($options['formidable_force_shortcodes']) && $options['formidable_force_shortcodes'])) {
846
847 $where = array('fi.form_id' => (int) $template->get('item'));
848 $fields = FrmField::getAll($where, 'id ASC');
849
850 $search = array();
851 $replace = array();
852
853 if ($options['formidable_force_shortcodes'] == '1') {
854 foreach ($fields as $field_key => $field) {
855 if (isset($field->field_options['form_select']) && $field->field_options['form_select']) {
856 $sub_where = array('fi.form_id' => $field->field_options['form_select']);
857 $sub_fields = FrmField::getAll($sub_where, 'id ASC');
858
859 foreach ($sub_fields as $sub_field_key => $sub_field) {
860 $search[] = $sub_field->field_key;
861 $replace[] = $sub_field->id;
862 }
863 } else {
864 $search[] = $field->field_key;
865 $replace[] = $field->id;
866 }
867 }
868 } elseif ($options['formidable_force_shortcodes'] == '2') {
869 foreach ($fields as $field_key => $field) {
870
871 if (isset($field->field_options['form_select']) && $field->field_options['form_select']) {
872 $sub_where = array('fi.form_id' => $field->field_options['form_select']);
873 $sub_fields = FrmField::getAll($sub_where, 'id ASC');
874
875 foreach ($sub_fields as $sub_field_key => $sub_field) {
876 $search[] = $sub_field->id;
877 $replace[] = $sub_field->field_key;
878 }
879 } else {
880 $search[] = $field->id;
881 $replace[] = $field->field_key;
882 }
883 }
884 }
885
886 $search = array_reverse($search);
887 $replace = array_reverse($replace);
888
889 $list = array_combine($search, $replace);
890
891 $pages = $this->pages_replace_shortcodes($pages, $list);
892 }
893
894 return $pages;
895 }
896
897 public function filter_e2pdf_controller_templates_backup_replace_shortcodes($options, $template, $extension, $value) {
898
899 if ($extension->loaded('formidable') && (isset($options['formidable_force_shortcodes']) && $options['formidable_force_shortcodes'])) {
900 $where = array('fi.form_id' => (int) $template->get('item'));
901 $fields = FrmField::getAll($where, 'id ASC');
902
903 $search = array();
904 $replace = array();
905
906 if ($options['formidable_force_shortcodes'] == '1') {
907 foreach ($fields as $field_key => $field) {
908 if (isset($field->field_options['form_select']) && $field->field_options['form_select']) {
909 $sub_where = array('fi.form_id' => $field->field_options['form_select']);
910 $sub_fields = FrmField::getAll($sub_where, 'id ASC');
911
912 foreach ($sub_fields as $sub_field_key => $sub_field) {
913 $search[] = $sub_field->field_key;
914 $replace[] = $sub_field->id;
915 }
916 } else {
917 $search[] = $field->field_key;
918 $replace[] = $field->id;
919 }
920 }
921 } elseif ($options['formidable_force_shortcodes'] == '2') {
922 foreach ($fields as $field_key => $field) {
923
924 if (isset($field->field_options['form_select']) && $field->field_options['form_select']) {
925 $sub_where = array('fi.form_id' => $field->field_options['form_select']);
926 $sub_fields = FrmField::getAll($sub_where, 'id ASC');
927
928 foreach ($sub_fields as $sub_field_key => $sub_field) {
929 $search[] = $sub_field->id;
930 $replace[] = $sub_field->field_key;
931 }
932 } else {
933 $search[] = $field->id;
934 $replace[] = $field->field_key;
935 }
936 }
937 }
938
939 $search = array_reverse($search);
940 $replace = array_reverse($replace);
941
942 $list = array_combine($search, $replace);
943
944 $value = $this->replace_shortcodes($value, $list);
945 }
946
947 return $value;
948 }
949
950 public function filter_e2pdf_controller_templates_import_pages($options, $xml, $template, $extension, $pages) {
951
952 if ($extension->loaded('formidable') &&
953 $template->get('item') &&
954 $template->get('item') != (String) $xml->template->item &&
955 $xml->item->formidable &&
956 $options['item'] &&
957 class_exists('FrmXMLHelper') &&
958 class_exists('FrmField')
959 ) {
960
961 $tmp = tempnam($this->helper->get('tmp_dir'), 'e2pdf');
962 file_put_contents($tmp, base64_decode((String) $xml->item->formidable));
963
964 $dom = new DOMDocument;
965 $success = $dom->loadXML(file_get_contents($tmp));
966
967 $old_ids = array();
968 $old_keys = array();
969
970 if ($success && function_exists('simplexml_import_dom')) {
971 $item_xml = simplexml_import_dom($dom);
972
973 foreach ($item_xml->form as $form_key => $form) {
974 if ((String) $form->id == (String) $xml->template->item) {
975 foreach ($form->field as $field_key => $field) {
976 $field_options = @json_decode((String) $field->field_options, true);
977 if (isset($field_options['form_select']) && $field_options['form_select']) {
978 foreach ($item_xml->form as $sub_form_key => $sub_form) {
979 if ((String) $sub_form->id == $field_options['form_select']) {
980 foreach ($sub_form->field as $sub_field_key => $sub_field) {
981 $old_ids[] = (String) $sub_field->id;
982 $old_keys[] = (String) $sub_field->field_key;
983 }
984 }
985 }
986 } else {
987 $old_ids[] = (String) $field->id;
988 $old_keys[] = (String) $field->field_key;
989 }
990 }
991 }
992 }
993 }
994
995 $where = array('fi.form_id' => (int) $template->get('item'));
996 $fields = FrmField::getAll($where, 'id ASC');
997
998 $new_ids = array();
999 $new_keys = array();
1000
1001 foreach ($fields as $field_key => $field) {
1002
1003 if (isset($field->field_options['form_select']) && $field->field_options['form_select']) {
1004 $sub_where = array('fi.form_id' => (int) $field->field_options['form_select']);
1005 $sub_fields = FrmField::getAll($sub_where, 'id ASC');
1006
1007 foreach ($sub_fields as $sub_field_key => $sub_field) {
1008 $new_ids[] = $sub_field->id;
1009 $new_keys[] = $sub_field->field_key;
1010 }
1011 } else {
1012 $new_ids[] = $field->id;
1013 $new_keys[] = $field->field_key;
1014 }
1015 }
1016
1017
1018 if (count($old_ids) === count($new_ids)) {
1019 $old_ids = array_reverse($old_ids);
1020 $new_ids = array_reverse($new_ids);
1021
1022 $old_keys = array_reverse($old_keys);
1023 $new_keys = array_reverse($new_keys);
1024
1025 $list_ids = array_combine($old_ids, $new_ids);
1026 $pages = $this->pages_replace_shortcodes($pages, $list_ids);
1027
1028 $list_keys = array_combine($old_keys, $new_keys);
1029 $pages = $this->pages_replace_shortcodes($pages, $list_keys);
1030 }
1031
1032 unset($dom);
1033 unlink($tmp);
1034 }
1035
1036 return $pages;
1037 }
1038
1039 public function filter_e2pdf_controller_templates_import_replace_shortcodes($options, $xml, $template, $extension, $value) {
1040
1041 if ($extension->loaded('formidable') &&
1042 $template->get('item') &&
1043 $template->get('item') != (String) $xml->template->item &&
1044 $xml->item->formidable &&
1045 $options['item'] &&
1046 class_exists('FrmXMLHelper') &&
1047 class_exists('FrmField')
1048 ) {
1049
1050 $tmp = tempnam($this->helper->get('tmp_dir'), 'e2pdf');
1051 file_put_contents($tmp, base64_decode((String) $xml->item->formidable));
1052
1053 $dom = new DOMDocument;
1054 $success = $dom->loadXML(file_get_contents($tmp));
1055
1056 $old_ids = array();
1057 $old_keys = array();
1058
1059 if ($success && function_exists('simplexml_import_dom')) {
1060 $item_xml = simplexml_import_dom($dom);
1061
1062 foreach ($item_xml->form as $form_key => $form) {
1063 if ((String) $form->id == (String) $xml->template->item) {
1064 foreach ($form->field as $field_key => $field) {
1065 $field_options = @json_decode((String) $field->field_options, true);
1066 if (isset($field_options['form_select']) && $field_options['form_select']) {
1067 foreach ($item_xml->form as $sub_form_key => $sub_form) {
1068 if ((String) $sub_form->id == $field_options['form_select']) {
1069 foreach ($sub_form->field as $sub_field_key => $sub_field) {
1070 $old_ids[] = (String) $sub_field->id;
1071 $old_keys[] = (String) $sub_field->field_key;
1072 }
1073 }
1074 }
1075 } else {
1076 $old_ids[] = (String) $field->id;
1077 $old_keys[] = (String) $field->field_key;
1078 }
1079 }
1080 }
1081 }
1082 }
1083
1084 $where = array('fi.form_id' => (int) $template->get('item'));
1085 $fields = FrmField::getAll($where, 'id ASC');
1086
1087 $new_ids = array();
1088 $new_keys = array();
1089
1090 foreach ($fields as $field_key => $field) {
1091
1092 if (isset($field->field_options['form_select']) && $field->field_options['form_select']) {
1093 $sub_where = array('fi.form_id' => (int) $field->field_options['form_select']);
1094 $sub_fields = FrmField::getAll($sub_where, 'id ASC');
1095
1096 foreach ($sub_fields as $sub_field_key => $sub_field) {
1097 $new_ids[] = $sub_field->id;
1098 $new_keys[] = $sub_field->field_key;
1099 }
1100 } else {
1101 $new_ids[] = $field->id;
1102 $new_keys[] = $field->field_key;
1103 }
1104 }
1105
1106 if (count($old_ids) === count($new_ids)) {
1107
1108 $old_ids = array_reverse($old_ids);
1109 $new_ids = array_reverse($new_ids);
1110
1111 $old_keys = array_reverse($old_keys);
1112 $new_keys = array_reverse($new_keys);
1113
1114 $list_ids = array_combine($old_ids, $new_ids);
1115 $value = $this->replace_shortcodes($value, $list_ids);
1116
1117 $list_keys = array_combine($old_keys, $new_keys);
1118 $value = $this->replace_shortcodes($value, $list_keys);
1119 }
1120
1121 unset($dom);
1122 unlink($tmp);
1123 }
1124
1125
1126 return $value;
1127 }
1128
1129 /**
1130 * Delete attachments that were sent by email
1131 */
1132 public function action_frm_notification() {
1133
1134 $files = $this->helper->get('formidable_attachments');
1135
1136 if (is_array($files) && !empty($files)) {
1137 foreach ($files as $key => $file) {
1138 $this->helper->delete_dir(dirname($file) . '/');
1139 }
1140 $this->helper->deset('formidable_attachments');
1141 }
1142 }
1143
1144 /**
1145 * Fix for user_id shortcode
1146 *
1147 * @param object $template - Current Template
1148 * @param object $extension - Loaded extension
1149 *
1150 */
1151 public function action_e2pdf_model_template_fill_pre($template, $extension) {
1152 if ($extension->loaded("formidable")) {
1153 if ($extension->get('dataset') && class_exists("FrmEntry") && is_admin()) {
1154 $extension->set('user_id', get_current_user_id());
1155 $entry = FrmEntry::getOne($extension->get('dataset'));
1156 if ($entry) {
1157 set_current_user($entry->user_id);
1158 }
1159 }
1160 }
1161 }
1162
1163 /**
1164 * Revert Fix for user_id shortcode
1165 *
1166 * @param object $template - Current Template
1167 * @param object $extension - Loaded extension
1168 *
1169 */
1170 public function action_e2pdf_model_template_fill_after($template, $extension) {
1171 if ($extension->loaded("formidable")) {
1172 if ($extension->get('dataset') && class_exists("FrmEntry") && is_admin()) {
1173 set_current_user($extension->get('user_id'));
1174 }
1175 }
1176 }
1177
1178 /**
1179 * Auto Generate of Template for this extension
1180 *
1181 * @return array - List of elements
1182 */
1183 public function auto() {
1184
1185 $response = array();
1186 $elements = array();
1187
1188 $form_id = $this->get('item');
1189
1190 $fields = array();
1191
1192 if (class_exists('FrmField')) {
1193 $fields = FrmField::get_all_for_form($form_id);
1194 }
1195
1196 if ($fields) {
1197 foreach ($fields as $key => $field) {
1198
1199 $field_id = get_option('e2pdf_formidable_use_keys') ? $field->field_key : $field->id;
1200
1201 if ($field->type === 'lookup' || $field->type === 'data') {
1202
1203 $dynamic_field_id = false;
1204 $dynamic_form_id = false;
1205
1206 if ($field->type === 'lookup' && isset($field->field_options['get_values_field']) && isset($field->field_options['get_values_form'])) {
1207
1208 $dynamic_form_id = $field->field_options['get_values_form'];
1209 $dynamic_field_id = $field->field_options['get_values_field'];
1210
1211 if (get_option('e2pdf_formidable_use_keys')) {
1212 $dynamic_field_data = FrmField::getOne($dynamic_field_id);
1213
1214 if ($dynamic_field_data) {
1215 $dynamic_field_id = $dynamic_field_data->field_key;
1216 }
1217 }
1218 } elseif ($field->type === 'data' && isset($field->field_options['form_select']) && $field->field_options['form_select'] !== 'taxonomy' && class_exists('FrmField')) {
1219
1220 $dynamic_field_id = $field->field_options['form_select'];
1221 $dynamic_field_data = FrmField::getOne($dynamic_field_id);
1222
1223 if ($dynamic_field_data) {
1224 $dynamic_form_id = $dynamic_field_data->form_id;
1225
1226 if (get_option('e2pdf_formidable_use_keys')) {
1227 $dynamic_field_id = $dynamic_field_data->field_key;
1228 }
1229 }
1230 }
1231
1232 $field->type = $field->field_options['data_type'];
1233 if ($dynamic_field_id && $dynamic_form_id) {
1234 if ($field->field_options['data_type'] == 'select') {
1235
1236 $field->options = array(
1237 "[e2pdf-frm-entry-values id='$dynamic_form_id' field_id='$dynamic_field_id']"
1238 );
1239 } elseif ($field->field_options['data_type'] == 'radio' || $field->field_options['data_type'] == 'checkbox') {
1240
1241 $options = array();
1242
1243 if (class_exists('FrmEntry') && class_exists('FrmEntryMeta')) {
1244 $where = array(
1245 'it.form_id' => $dynamic_form_id
1246 );
1247
1248 $entries_tmp = FrmEntry::getAll($where, ' ORDER BY id ASC');
1249 foreach ($entries_tmp as $key => $entry) {
1250 $options[] = FrmEntryMeta::get_meta_value($entry, $dynamic_field_id);
1251 }
1252 }
1253 $field->options = $options;
1254 }
1255 }
1256 }
1257
1258 /*
1259 * Repeatable fields Field ID modification
1260 * @since 0.01.42
1261 */
1262 if ($field->type !== 'lookup' && $field->type !== 'data' && isset($field->form_id) && $field->form_id != $form_id) {
1263 $field_id = $field_id . ":1";
1264 }
1265
1266 switch ($field->type) {
1267 case 'html':
1268 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1269 'top' => '20',
1270 'left' => '20',
1271 'right' => '20',
1272 'block' => true,
1273 'properties' => array(
1274 'value' => $field->name,
1275 )
1276 ));
1277
1278 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1279 'properties' => array(
1280 'value' => $field->description,
1281 )
1282 ));
1283 break;
1284
1285 case 'signature':
1286 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1287 'top' => '20',
1288 'left' => '20',
1289 'right' => '20',
1290 'block' => true,
1291 'properties' => array(
1292 'value' => $field->name,
1293 )
1294 ));
1295
1296 $elements[] = $this->auto_field($field, 'e2pdf-signature', array(
1297 'properties' => array(
1298 'width' => '400',
1299 'height' => '150',
1300 'scale' => '1',
1301 'dimension' => '1',
1302 'value' => "[$field_id]"
1303 )
1304 ));
1305 break;
1306
1307 case 'scale':
1308 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1309 'top' => '20',
1310 'left' => '20',
1311 'right' => '20',
1312 'block' => true,
1313 'properties' => array(
1314 'value' => $field->name,
1315 )
1316 ));
1317
1318 $left = 0;
1319
1320 if (isset($field->options) && is_array($field->options)) {
1321 foreach ($field->options as $opt_key => $option) {
1322
1323 if (is_array($option)) {
1324 $elements[] = $this->auto_field($field, 'e2pdf-radio', array(
1325 'top' => 0,
1326 'left' => $left,
1327 'properties' => array(
1328 'width' => '15',
1329 'height' => '15',
1330 'value' => "[$field_id]",
1331 'option' => $option['value'],
1332 'group' => "group_" . $field_id
1333 )
1334 ));
1335 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1336 'top' => '20',
1337 'left' => $left - 5,
1338 'properties' => array(
1339 'value' => $option['label'],
1340 'width' => '25',
1341 'text_align' => 'center',
1342 )
1343 ));
1344 } else {
1345
1346 $elements[] = $this->auto_field($field, 'e2pdf-radio', array(
1347 'top' => 0,
1348 'left' => $left,
1349 'properties' => array(
1350 'width' => '15',
1351 'height' => '15',
1352 'value' => "[$field_id]",
1353 'option' => $option,
1354 'group' => "group_" . $field_id
1355 )
1356 ));
1357 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1358 'top' => '20',
1359 'left' => $left - 5,
1360 'properties' => array(
1361 'value' => $option,
1362 'width' => '25',
1363 'text_align' => 'center',
1364 )
1365 ));
1366 }
1367
1368 $left = $left + 25;
1369 }
1370 }
1371 break;
1372
1373 case 'rte':
1374 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1375 'top' => '20',
1376 'left' => '20',
1377 'right' => '20',
1378 'block' => true,
1379 'properties' => array(
1380 'value' => $field->name,
1381 )
1382 ));
1383
1384 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1385 'properties' => array(
1386 'height' => '150',
1387 'value' => "[$field_id wpautop=0]"
1388 )
1389 ));
1390 break;
1391
1392 case 'file':
1393 case 'text':
1394 case 'email':
1395 case 'url':
1396 case 'number':
1397 case 'phone':
1398 case 'date':
1399 case 'time':
1400 case 'image':
1401 case 'tag':
1402 case 'password':
1403 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1404 'top' => '20',
1405 'left' => '20',
1406 'right' => '20',
1407 'block' => true,
1408 'properties' => array(
1409 'value' => $field->name,
1410 )
1411 ));
1412
1413 if ($field->type == 'file') {
1414 if (strpos($field_id, ':') !== false) {
1415 $field_id .= " size='full' show_image='0' add_link='0'";
1416 } else {
1417 $field_id .= " size='full'";
1418 }
1419 }
1420
1421 $elements[] = $this->auto_field($field, 'e2pdf-input', array(
1422 'properties' => array(
1423 'value' => "[$field_id]",
1424 'pass' => $field->type === 'password' ? '1' : '0',
1425 )
1426 ));
1427 break;
1428
1429 case 'select':
1430 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1431 'top' => '20',
1432 'left' => '20',
1433 'right' => '20',
1434 'block' => true,
1435 'properties' => array(
1436 'value' => $field->name,
1437 )
1438 ));
1439
1440 $options_tmp = array();
1441 if (isset($field->options) && is_array($field->options)) {
1442 foreach ($field->options as $opt_key => $option) {
1443 if (is_array($option)) {
1444 $options_tmp[] = isset($option['label']) ? $option['label'] : $option['value'];
1445 } else {
1446 $options_tmp[] = $option;
1447 }
1448 }
1449 }
1450
1451 $elements[] = $this->auto_field($field, 'e2pdf-select', array(
1452 'properties' => array(
1453 'options' => implode("\n", $options_tmp),
1454 'value' => "[$field_id]",
1455 'height' => isset($field->field_options['multiple']) && $field->field_options['multiple'] ? '80' : '',
1456 'multiline' => isset($field->field_options['multiple']) && $field->field_options['multiple'] ? '1' : '0',
1457 )
1458 ));
1459 break;
1460
1461
1462 case 'credit_card':
1463 case 'address':
1464 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1465 'top' => '20',
1466 'left' => '20',
1467 'right' => '20',
1468 'block' => true,
1469 'properties' => array(
1470 'value' => $field->name,
1471 )
1472 ));
1473
1474 $elements[] = $this->auto_field($field, 'e2pdf-textarea', array(
1475 'properties' => array(
1476 'value' => "[$field_id]",
1477 )
1478 ));
1479 break;
1480
1481 case 'textarea':
1482 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1483 'top' => '20',
1484 'left' => '20',
1485 'right' => '20',
1486 'block' => true,
1487 'properties' => array(
1488 'value' => $field->name,
1489 )
1490 ));
1491
1492 $elements[] = $this->auto_field($field, 'e2pdf-textarea', array(
1493 'properties' => array(
1494 'value' => "[$field_id wpautop=0]",
1495 )
1496 ));
1497 break;
1498
1499
1500 case 'radio':
1501 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1502 'top' => '20',
1503 'left' => '20',
1504 'right' => '20',
1505 'block' => true,
1506 'properties' => array(
1507 'value' => $field->name,
1508 )
1509 ));
1510
1511 $top = 0;
1512 if (isset($field->options) && is_array($field->options)) {
1513 foreach ($field->options as $opt_key => $option) {
1514
1515 if (is_array($option)) {
1516 $elements[] = $this->auto_field($field, 'e2pdf-radio', array(
1517 'top' => $top,
1518 'properties' => array(
1519 'width' => '15',
1520 'height' => '15',
1521 'value' => "[$field_id]",
1522 'option' => $option['label'],
1523 'group' => "group_" . $field_id
1524 )
1525 ));
1526 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1527 'top' => $top,
1528 'left' => '20',
1529 'properties' => array(
1530 'value' => $option['label']
1531 )
1532 ));
1533 } else {
1534
1535 $elements[] = $this->auto_field($field, 'e2pdf-radio', array(
1536 'top' => $top,
1537 'properties' => array(
1538 'width' => '15',
1539 'height' => '15',
1540 'value' => "[$field_id]",
1541 'option' => $option,
1542 'group' => "group_" . $field_id
1543 )
1544 ));
1545 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1546 'top' => $top,
1547 'left' => '20',
1548 'properties' => array(
1549 'value' => $option
1550 )
1551 ));
1552 }
1553 $top = $top + 20;
1554 }
1555 }
1556 break;
1557
1558 case 'checkbox':
1559 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1560 'top' => '20',
1561 'left' => '20',
1562 'right' => '20',
1563 'block' => true,
1564 'properties' => array(
1565 'value' => $field->name,
1566 )
1567 ));
1568
1569 $top = 0;
1570 if (isset($field->options) && is_array($field->options)) {
1571 foreach ($field->options as $opt_key => $option) {
1572 if (is_array($option)) {
1573 $elements[] = $this->auto_field($field, 'e2pdf-checkbox', array(
1574 'top' => $top,
1575 'properties' => array(
1576 'width' => '15',
1577 'height' => '15',
1578 'value' => "[$field_id]",
1579 'option' => $option['label']
1580 )
1581 ));
1582 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1583 'top' => $top,
1584 'left' => '20',
1585 'properties' => array(
1586 'value' => $option['label']
1587 )
1588 ));
1589 } else {
1590 $elements[] = $this->auto_field($field, 'e2pdf-checkbox', array(
1591 'top' => $top,
1592 'properties' => array(
1593 'width' => '15',
1594 'height' => '15',
1595 'value' => "[$field_id]",
1596 'option' => $option
1597 )
1598 ));
1599 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
1600 'top' => $top,
1601 'left' => '20',
1602 'properties' => array(
1603 'value' => $option
1604 )
1605 ));
1606 }
1607 $top = $top + 20;
1608 }
1609 }
1610 break;
1611
1612 default:
1613 break;
1614 }
1615 }
1616 }
1617
1618 $response['page'] = array(
1619 'bottom' => '20',
1620 'top' => '20',
1621 );
1622
1623 $response['elements'] = $elements;
1624 return $response;
1625 }
1626
1627 /**
1628 * Generate field for Auto PDF
1629 *
1630 * @param object $field - Formidable field object
1631 * @param string $type - Field type
1632 * @param array $options - Field additional options
1633 *
1634 * @return array - Prepared auto field
1635 */
1636 public function auto_field($field = false, $type = false, $options = array()) {
1637
1638 if (!$field || !$type) {
1639 return false;
1640 }
1641
1642 $classes = array();
1643 if (isset($field->field_options['classes'])) {
1644 $classes = explode(" ", $field->field_options['classes']);
1645 }
1646
1647 $element = array(
1648 'type' => $type,
1649 'properties' => isset($options['properties']) ? $options['properties'] : array(),
1650 );
1651
1652 $element['top'] = isset($options['top']) ? $options['top'] : '0';
1653 $element['left'] = isset($options['left']) ? $options['left'] : '0';
1654 $element['right'] = isset($options['right']) ? $options['right'] : '0';
1655 $element['block'] = isset($options['block']) ? $options['block'] : false;
1656
1657 $float_classes = array(
1658 'frm_half',
1659 'frm_third',
1660 'frm_two_thirds',
1661 'frm_fourth',
1662 'frm_three_fourths',
1663 'frm_fifth',
1664 'frm_two_fifths',
1665 'frm_sixth',
1666 'frm_seventh',
1667 'frm_eighth'
1668 );
1669
1670 $array_intersect = array_intersect($classes, $float_classes);
1671
1672 if (!empty($array_intersect) && !in_array('frm_first', $classes)) {
1673 $element['float'] = true;
1674 };
1675
1676 $primary_class = false;
1677 if (!empty($array_intersect)) {
1678 $primary_class = end($array_intersect);
1679 }
1680
1681 if ($element['block']) {
1682 switch ($primary_class) {
1683 case 'frm_half':
1684 $element['width'] = '50%';
1685 break;
1686
1687 case 'frm_third':
1688 $element['width'] = '33.3%';
1689 break;
1690
1691 case 'frm_two_thirds':
1692 $element['width'] = '66.67%';
1693 break;
1694
1695 case 'frm_fourth':
1696 $element['width'] = '25%';
1697 break;
1698
1699 case 'frm_three_fourths':
1700 $element['width'] = '75%';
1701 break;
1702
1703 case 'frm_fifth':
1704 $element['width'] = '20%';
1705 break;
1706
1707 case 'frm_two_fifths':
1708 $element['width'] = '80%';
1709 break;
1710
1711 case 'frm_sixth':
1712 $element['width'] = '16.67%';
1713 break;
1714
1715 case 'frm_seventh':
1716 $element['width'] = '14.29%';
1717 break;
1718
1719 case 'frm_eighth':
1720 $element['width'] = '12.5%';
1721 break;
1722
1723 default:
1724 break;
1725 }
1726 }
1727
1728 return $element;
1729 }
1730
1731 /**
1732 * Backup form action
1733 *
1734 * @param object xml - XML object where to add params for saved item
1735 */
1736 public function backup($xml = false) {
1737 if (class_exists('FrmXMLController')) {
1738
1739 $ids = array();
1740 $ids[] = $this->get('item');
1741
1742 $type = array();
1743 $type[] = 'forms';
1744
1745 ob_start();
1746 FrmXMLController::generate_xml($type, compact('ids'));
1747 $backup = ob_get_clean();
1748 $xml->addChildCData('formidable', base64_encode($backup));
1749 }
1750 }
1751
1752 /**
1753 * Import form action
1754 *
1755 * @param object xml - XML object to parse data to import form
1756 */
1757 public function import($xml, $item) {
1758
1759 $updated_item = '';
1760
1761 if (class_exists('FrmXMLHelper') && class_exists('FrmField')) {
1762
1763 if ($xml->formidable) {
1764
1765 $tmp = tempnam($this->helper->get('tmp_dir'), 'e2pdf');
1766 file_put_contents($tmp, base64_decode((String) $xml->formidable));
1767
1768 $result = FrmXMLHelper::import_xml($tmp);
1769
1770 unlink($tmp);
1771
1772 FrmXMLHelper::parse_message($result, $message, $errors);
1773 if ($errors) {
1774 return array(
1775 'errors' => $errors
1776 );
1777 } else {
1778 if (isset($result['forms'][$item])) {
1779 $updated_item = $result['forms'][$item];
1780 }
1781 }
1782 }
1783 }
1784
1785 return $updated_item;
1786 }
1787
1788 /**
1789 * Replace Old Formidable shortcodes to new values on pages
1790 *
1791 * @param array $pages - List of pages to replace shortcodes
1792 * @param array $shortcodes_list - List of new/old shortcodes to replace
1793 *
1794 * @return array - List of updated pages
1795 */
1796 public function pages_replace_shortcodes($pages = array(), $shortcodes_list = array()) {
1797
1798 foreach ($pages as $page_key => $page) {
1799 foreach ($page['elements'] as $element_key => $element_value) {
1800 $pages[$page_key]['elements'][$element_key]['value'] = $this->replace_shortcodes($element_value['value'], $shortcodes_list);
1801 }
1802 }
1803
1804 return $pages;
1805 }
1806
1807 /**
1808 * Replace Old Formidable shortcodes to new values
1809 *
1810 * @param string $content - Value that can contains old shortcodes
1811 * @param array $shortcodes_list - List of new/old shortcodes to replace
1812 *
1813 * @return string - Updated value
1814 */
1815 public function replace_shortcodes($content = "", $shortcodes_list = array()) {
1816
1817 if (false === strpos($content, '[')) {
1818 return $content;
1819 }
1820
1821 foreach ($shortcodes_list as $list_key => $list_value) {
1822 $content = preg_replace("/(\[|\[(?:[^\]]*?)\s|\[(?:[^\]]*?)field_id=(?:\'|\"|)){$list_key}(\:(?:.*?)\]|\s(?:.*?)\]|(?:\'|\")(?:.*?)\]|\])/", '${1}' . $list_value . '${2}', $content);
1823 }
1824
1825 return $content;
1826 }
1827
1828 /**
1829 * Verify if form and dataset exists
1830 *
1831 * @return bool - Exists/Not exists
1832 */
1833 public function verify() {
1834 $item = $this->get('item');
1835 $dataset = $this->get('dataset');
1836
1837 if ($item && $dataset && class_exists('FrmEntry')) {
1838 $entry = FrmEntry::getOne($dataset);
1839 if (is_object($entry) && $entry->form_id === $item) {
1840 return true;
1841 }
1842 }
1843 return false;
1844 }
1845
1846 /**
1847 * Visual Mapper for Mapping field
1848 *
1849 * @return bool|string - Prepared form or false
1850 */
1851 public function visual_mapper() {
1852
1853 $item = $this->get('item');
1854
1855 if ($item && class_exists('FrmformsController')) {
1856
1857 if (class_exists("FrmProFieldsHelper")) {
1858 add_filter('frm_get_paged_fields', array($this, 'filter_remove_pagebreaks'), 9, 1);
1859 }
1860
1861 $source = FrmformsController::show_form($item, '', true, true);
1862
1863 if (class_exists("FrmProFieldsHelper")) {
1864 remove_filter('frm_get_paged_fields', array($this, 'filter_remove_pagebreaks'), 9);
1865 }
1866
1867 libxml_use_internal_errors(true);
1868 $dom = new DOMDocument;
1869 $html = $dom->loadHTML($source);
1870 libxml_clear_errors();
1871
1872 if (!$html) {
1873 return __('Form could not be parsed due incorrect HTML', 'e2pdf');
1874 } else {
1875
1876 $xpath = new DomXPath($dom);
1877
1878 $remove_by_class = array(
1879 'frm_ajax_loading',
1880 'wp-editor-tools',
1881 'quicktags-toolbar',
1882 'frm_button_submit',
1883 'frm_range_value',
1884 'star-rating',
1885 'frm_repeat_buttons'
1886 );
1887 foreach ($remove_by_class as $key => $class) {
1888 $elements = $xpath->query("//*[contains(@class, '{$class}')]");
1889 foreach ($elements as $element) {
1890 $element->parentNode->removeChild($element);
1891 }
1892 }
1893
1894 $remove_by_tag = array(
1895 'link',
1896 'style',
1897 'script'
1898 );
1899 foreach ($remove_by_tag as $key => $tag) {
1900 $elements = $xpath->query("//{$tag}");
1901 foreach ($elements as $element) {
1902 $element->parentNode->removeChild($element);
1903 }
1904 }
1905
1906 $remove_classes = array(
1907 'wp-editor-container',
1908 'frm_logic_form'
1909 );
1910
1911 foreach ($remove_classes as $key => $class) {
1912 $elements = $xpath->query("//*[contains(@class, '{$class}')]");
1913 foreach ($elements as $element) {
1914 $element->attributes->getNamedItem("class")->nodeValue = str_replace($class, '', $element->attributes->getNamedItem("class")->nodeValue);
1915 }
1916 }
1917
1918 $remove_styles = array(
1919 'frm_toggle_container'
1920 );
1921
1922 foreach ($remove_styles as $key => $class) {
1923 $elements = $xpath->query("//*[contains(@class, '{$class}')]");
1924 foreach ($elements as $element) {
1925 $element->attributes->getNamedItem("style")->nodeValue = '';
1926 }
1927 }
1928
1929
1930 /*
1931 * Metas patterns to replace field names
1932 * @since 0.01.42
1933 */
1934 $metas_pattern = array(
1935 '/item_meta\[(?:.*?)\]\[0\]\[(.*?)\](?:\[\])?/i' => '$1:1',
1936 '/item_meta\[(.*?)\](\[\])?/i' => '$1',
1937 );
1938
1939 // Replace names
1940 $metas = $xpath->query("//*[contains(@name, 'item_meta')]");
1941 foreach ($metas as $element) {
1942 $field_id = preg_replace(array_keys($metas_pattern), $metas_pattern, $element->attributes->getNamedItem("name")->nodeValue);
1943 if (get_option('e2pdf_formidable_use_keys') && class_exists("FrmField")) {
1944 if (strpos($field_id, ':') !== false) {
1945 $repeat_data = explode(":", $field_id);
1946 if (isset($repeat_data['0'])) {
1947 $field_data = FrmField::getOne($repeat_data['0']);
1948 if ($field_data) {
1949 $field_id = $field_data->field_key . ":1";
1950 }
1951 }
1952 } else {
1953 $field_data = FrmField::getOne($field_id);
1954 if ($field_data) {
1955 $field_id = $field_data->field_key;
1956 }
1957 }
1958 }
1959
1960 $element->attributes->getNamedItem("name")->nodeValue = $field_id;
1961 }
1962
1963
1964
1965
1966 // Time field replace
1967 $time_fields = $xpath->query("//div/select[contains(@class, 'frm_time_select')]/parent::*");
1968 foreach ($time_fields as $key => $element) {
1969 $span = $xpath->query("./span", $element)->item(0);
1970 $select = $xpath->query("./select", $element)->item(0);
1971
1972 if ($span) {
1973
1974 $span_cloned = $span->cloneNode(true);
1975 $select_cloned = $select->cloneNode(true);
1976
1977 foreach ($xpath->query("./select", $span_cloned) as $key => $sub_select) {
1978 $sub_select->attributes->getNamedItem("class")->nodeValue = $sub_select->attributes->getNamedItem("class")->nodeValue . " e2pdf-no-vm";
1979 }
1980
1981 $select_cloned->attributes->getNamedItem("class")->nodeValue = $select_cloned->attributes->getNamedItem("class")->nodeValue . " e2pdf-no-vm";
1982
1983 $span->parentNode->removeChild($span);
1984 $select->parentNode->removeChild($select);
1985
1986 $wrapper = $dom->createElement('div');
1987 $wrapper_atts = array(
1988 'class' => 'e2pdf-vm-field-wrapper',
1989 );
1990
1991 foreach ($wrapper_atts as $key => $value) {
1992 $attr = $dom->createAttribute($key);
1993 $attr->value = $value;
1994 $wrapper->appendChild($attr);
1995 }
1996
1997 $field = $dom->createElement('input');
1998
1999 $field_id = preg_replace('/\[(.*?)\]/i', '', $select_cloned->attributes->getNamedItem("name")->nodeValue);
2000 if (get_option('e2pdf_formidable_use_keys') && class_exists("FrmField")) {
2001 $field_data = FrmField::getOne($field_id);
2002 if ($field_data) {
2003 $field_id = $field_data->field_key;
2004 }
2005 }
2006
2007 $field_atts = array(
2008 'type' => 'text',
2009 'class' => 'e2pdf-vm-field',
2010 'name' => $field_id
2011 );
2012
2013 foreach ($field_atts as $key => $value) {
2014 $attr = $dom->createAttribute($key);
2015 $attr->value = $value;
2016 $field->appendChild($attr);
2017 }
2018
2019 $wrapper->appendChild($field);
2020 $wrapper->appendChild($span_cloned);
2021 $wrapper->appendChild($select_cloned);
2022
2023 $element->appendChild($wrapper);
2024 }
2025 }
2026
2027 // Combo fields
2028 $frm_combo_inputs_container = $xpath->query("//*[contains(@class, 'frm_combo_inputs_container')]");
2029 foreach ($frm_combo_inputs_container as $element) {
2030
2031 $inputs = $xpath->query(".//input", $element);
2032 $selects = $xpath->query(".//select", $element);
2033
2034 foreach ($selects as $key => $sub_element) {
2035 if ($sub_element->attributes->getNamedItem("class")->nodeValue) {
2036 $sub_element->attributes->getNamedItem("class")->nodeValue = $sub_select->attributes->getNamedItem("class")->nodeValue . " e2pdf-no-vm";
2037 } else {
2038 $class = $dom->createAttribute('class');
2039 $class->value = 'e2pdf-no-vm';
2040 $sub_element->appendChild($class);
2041 }
2042 }
2043
2044 foreach ($inputs as $key => $sub_element) {
2045 if ($sub_element->attributes->getNamedItem("class")->nodeValue) {
2046 $sub_element->attributes->getNamedItem("class")->nodeValue = $sub_element->attributes->getNamedItem("class")->nodeValue . " e2pdf-no-vm";
2047 } else {
2048 $class = $dom->createAttribute('class');
2049 $class->value = 'e2pdf-no-vm';
2050 $sub_element->appendChild($class);
2051 }
2052 }
2053
2054 $name = "";
2055 if ($inputs->item(0)) {
2056 $name = $inputs->item(0)->attributes->getNamedItem("name")->nodeValue;
2057 } else {
2058 $name = $selects->item(0)->attributes->getNamedItem("name")->nodeValue;
2059 }
2060
2061 $field_id = preg_replace('/\[(.*?)\]/i', '', $name);
2062 if (get_option('e2pdf_formidable_use_keys') && class_exists("FrmField")) {
2063 $field_data = FrmField::getOne($field_id);
2064 if ($field_data) {
2065 $field_id = $field_data->field_key;
2066 }
2067 }
2068
2069 $field = $dom->createElement('input');
2070 $field_atts = array(
2071 'type' => 'text',
2072 'class' => 'e2pdf-vm-field',
2073 'name' => $field_id
2074 );
2075
2076 foreach ($field_atts as $key => $value) {
2077 $attr = $dom->createAttribute($key);
2078 $attr->value = $value;
2079 $field->appendChild($attr);
2080 }
2081 $element->appendChild($field);
2082
2083 $element->attributes->getNamedItem("class")->nodeValue = $element->attributes->getNamedItem("class")->nodeValue . " e2pdf-vm-field-wrapper";
2084 }
2085
2086 $frm_dropzone = $xpath->query("//*[contains(@class, 'frm_dropzone')]/parent::*");
2087 foreach ($frm_dropzone as $element) {
2088 $dropzone = $xpath->query(".//*[contains(@class, 'frm_dropzone')]", $element)->item(0);
2089 $input = $xpath->query(".//input", $element)->item(0);
2090 $input_cloned = $input->cloneNode(true);
2091 $input_cloned->attributes->getNamedItem("type")->nodeValue = 'text';
2092 $input_cloned->attributes->getNamedItem("value")->nodeValue = __('File Upload', 'e2pdf');
2093
2094 if (strpos($input_cloned->attributes->getNamedItem("name")->nodeValue, ':') !== false) {
2095 $input_cloned->attributes->getNamedItem("name")->nodeValue = $input_cloned->attributes->getNamedItem("name")->nodeValue . " size='full' show_image='0' add_link='0'";
2096 } else {
2097 $input_cloned->attributes->getNamedItem("name")->nodeValue = $input_cloned->attributes->getNamedItem("name")->nodeValue . " size='full'";
2098 }
2099
2100 $style = $dom->createAttribute('style');
2101 $style->value = 'width: 100%; height: 200px; text-align: center;';
2102 $input_cloned->appendChild($style);
2103
2104 $input->parentNode->replaceChild($input_cloned, $input);
2105 $dropzone->parentNode->removeChild($dropzone);
2106 }
2107
2108 // Replace signature
2109 $sigpad = $xpath->query("//*[contains(@class, 'sigPad')]");
2110 foreach ($sigpad as $element) {
2111 $input = $xpath->query(".//input", $element)->item(0);
2112 $input_cloned = $input->cloneNode(true);
2113
2114 $style = $dom->createAttribute('style');
2115 $style->value = 'width: 300px; height: 100px;';
2116 $input_cloned->appendChild($style);
2117
2118 $field_id = preg_replace('/\[(.*?)\]/i', '', $input_cloned->attributes->getNamedItem("name")->nodeValue);
2119
2120 if (get_option('e2pdf_formidable_use_keys') && class_exists("FrmField")) {
2121 $field_data = FrmField::getOne($field_id);
2122 if ($field_data) {
2123 $field_id = $field_data->field_key;
2124 }
2125 }
2126
2127 $input_cloned->attributes->getNamedItem("name")->nodeValue = $field_id;
2128
2129 $element->parentNode->replaceChild($input_cloned, $element);
2130 }
2131
2132 // Replace names
2133 $fields = $xpath->query("//input");
2134 foreach ($fields as $element) {
2135
2136 if (class_exists('FrmField')) {
2137
2138 if ($element->attributes->getNamedItem("type")->nodeValue == 'checkbox' || $element->attributes->getNamedItem("type")->nodeValue == 'radio') {
2139
2140 $field_id = $element->attributes->getNamedItem("name")->nodeValue;
2141 if (false !== strpos($element->attributes->getNamedItem("name")->nodeValue, ':')) {
2142 $field_id = substr($element->attributes->getNamedItem("name")->nodeValue, 0, strpos($element->attributes->getNamedItem("name")->nodeValue, ":"));
2143 }
2144
2145 $field_data = FrmField::getOne($field_id);
2146
2147 if (isset($field_data->type) && $field_data->type === 'data' && isset($field_data->field_options['form_select'])) {
2148 $dynamic_form_id = false;
2149 $dynamic_field_id = false;
2150
2151 $dynamic_field_id = $field_data->field_options['form_select'];
2152 $dynamic_field_data = FrmField::getOne($dynamic_field_id);
2153 if ($dynamic_field_data) {
2154 $dynamic_form_id = $dynamic_field_data->form_id;
2155 }
2156
2157 $options = array();
2158 if (class_exists('FrmEntry') && class_exists('FrmEntryMeta')) {
2159 $where = array(
2160 'it.form_id' => $dynamic_form_id
2161 );
2162 $entries_tmp = FrmEntry::getAll($where, ' ORDER BY id ASC');
2163 foreach ($entries_tmp as $key => $entry) {
2164 $options[] = array(
2165 'label' => FrmEntryMeta::get_meta_value($entry, $dynamic_field_id),
2166 'value' => $key,
2167 );
2168 }
2169 }
2170 $field_data->options = $options;
2171 }
2172
2173 if (isset($field_data->options) && is_array($field_data->options)) {
2174 $field_options = $field_data->options;
2175 $field_value = $element->attributes->getNamedItem("value")->nodeValue;
2176 $field_key = array_search($field_value, array_column($field_options, 'value'));
2177 if ($field_key !== false) {
2178 $field_label = isset($field_options[$field_key]['label']) ? $field_options[$field_key]['label'] : $field_value;
2179 $element->attributes->getNamedItem("value")->nodeValue = $field_label;
2180 }
2181 }
2182 }
2183 }
2184
2185 $element->attributes->getNamedItem("name")->nodeValue = "[" . $element->attributes->getNamedItem("name")->nodeValue . "]";
2186 }
2187
2188 // Replace names
2189 $textareas = $xpath->query("//textarea");
2190 foreach ($textareas as $element) {
2191 $element->attributes->getNamedItem("name")->nodeValue = "[" . $element->attributes->getNamedItem("name")->nodeValue . " wpautop=0]";
2192 }
2193
2194 // Replace names
2195 $selects = $xpath->query("//select");
2196 foreach ($selects as $element) {
2197
2198 if (class_exists('FrmField')) {
2199
2200 $options = $xpath->query(".//option", $element);
2201
2202 $field_id = $element->attributes->getNamedItem("name")->nodeValue;
2203 if (false !== strpos($element->attributes->getNamedItem("name")->nodeValue, ':')) {
2204 $field_id = substr($element->attributes->getNamedItem("name")->nodeValue, 0, strpos($element->attributes->getNamedItem("name")->nodeValue, ":"));
2205 }
2206
2207 $field_data = FrmField::getOne($field_id);
2208
2209 if (isset($field_data->type) && ($field_data->type === 'lookup' || $field_data->type === 'data')) {
2210 $dynamic_form_id = false;
2211 $dynamic_field_id = false;
2212
2213 if ($field_data->type === 'lookup' && isset($field_data->field_options['get_values_field']) && isset($field_data->field_options['get_values_form'])) {
2214 $dynamic_form_id = $field_data->field_options['get_values_form'];
2215 $dynamic_field_id = $field_data->field_options['get_values_field'];
2216
2217 if (get_option('e2pdf_formidable_use_keys')) {
2218 $dynamic_field_data = FrmField::getOne($dynamic_field_id);
2219
2220 if ($dynamic_field_data) {
2221 $dynamic_field_id = $dynamic_field_data->field_key;
2222 }
2223 }
2224 } elseif ($field_data->type === 'data' && isset($field_data->field_options['form_select'])) {
2225 $dynamic_field_id = $field_data->field_options['form_select'];
2226 $dynamic_field_data = FrmField::getOne($dynamic_field_id);
2227
2228 if ($dynamic_field_data) {
2229 $dynamic_form_id = $dynamic_field_data->form_id;
2230
2231 if (get_option('e2pdf_formidable_use_keys')) {
2232 $dynamic_field_id = $dynamic_field_data->field_key;
2233 }
2234 }
2235 }
2236
2237 if ($dynamic_form_id && $dynamic_field_id) {
2238
2239 foreach ($options as $option) {
2240 $option->parentNode->removeChild($option);
2241 }
2242
2243 $wrapper = $dom->createElement('option');
2244 $wrapper_atts = array(
2245 'value' => "[e2pdf-frm-entry-values id='{$dynamic_form_id}' field_id='{$dynamic_field_id}']",
2246 );
2247 foreach ($wrapper_atts as $key => $value) {
2248 $attr = $dom->createAttribute($key);
2249 $attr->value = $value;
2250 $wrapper->appendChild($attr);
2251 }
2252 $element->appendChild($wrapper);
2253 }
2254 } else if (isset($field_data->options) && is_array($field_data->options)) {
2255 $field_options = $field_data->options;
2256 foreach ($options as $option) {
2257 $field_value = $option->attributes->getNamedItem("value")->nodeValue;
2258 foreach ($field_options as $field_option) {
2259 if (isset($field_option['value']) && $field_option['value'] === $field_value) {
2260 $field_label = isset($field_option['label']) ? $field_option['label'] : $field_value;
2261 $option->attributes->getNamedItem("value")->nodeValue = $field_label;
2262 }
2263 }
2264 }
2265 }
2266 }
2267
2268 $element->attributes->getNamedItem("name")->nodeValue = "[" . $element->attributes->getNamedItem("name")->nodeValue . "]";
2269 }
2270
2271 if ($xpath->query("//form")->item(0) && class_exists('FrmStylesHelper')) {
2272 $autocomplete = $dom->createAttribute('autocomplete');
2273 $autocomplete->value = 'off';
2274 $xpath->query("//form")->item(0)->appendChild($autocomplete);
2275 }
2276
2277 return $dom->saveHTML();
2278 }
2279 }
2280 return false;
2281 }
2282
2283 /**
2284 * Convert Field name to Value
2285 * @since 0.01.34
2286 *
2287 * @param string $name - Field name
2288 *
2289 * @return bool|string - Converted value or false
2290 */
2291 public function auto_map($name = false) {
2292
2293 $item = $this->get('item');
2294
2295 if ($item && class_exists("FrmField")) {
2296 $where = array('fi.form_id' => (int) $item,
2297 "(fi.field_key = '{$name}' OR fi.name = '{$name}') AND 1" => "1"
2298 );
2299 $field = FrmField::getAll($where, 'id ASC', '1');
2300
2301 if ($field && is_object($field)) {
2302 if ($field->type == 'textarea' || $field->type == 'rte') {
2303 return "[" . $field->id . " wpautop=0]";
2304 } else {
2305 return "[" . $field->id . "]";
2306 }
2307 }
2308 }
2309
2310 return false;
2311 }
2312
2313 /**
2314 * Load additional shortcodes for this extension
2315 */
2316 public function load_shortcodes() {
2317 add_shortcode('e2pdf-frm-entry-values', array($this, 'shortcode_e2pdf_frm_entry_values'));
2318 add_shortcode('e2pdf-frm-repeatable', array($this, 'shortcode_e2pdf_frm_repeatable'));
2319 }
2320
2321 /**
2322 * [e2pdf-frm-repeatable id='{entry_id}' field_id='{field_id}'] shortcode
2323 *
2324 * @param array $atts - Atributes for shortcode
2325 *
2326 * @return string - Output of shortcodes
2327 */
2328 public function shortcode_e2pdf_frm_repeatable($atts = array()) {
2329 $id = (int) $atts['id'];
2330 $field_id = $atts['field_id'];
2331
2332 $response = "[frm-field-value field_id='{$field_id}' entry='{$id}']";
2333 return $response;
2334 }
2335
2336 /**
2337 * [e2pdf-frm-entry-values id='{form_id}' field_id='{field_id}' separator=''] shortcode
2338 *
2339 * @param array $atts - Atributes for shortcode
2340 *
2341 * @return string - Output of shortcodes
2342 */
2343 public function shortcode_e2pdf_frm_entry_values($atts = array()) {
2344 $form_id = (int) $atts['id'];
2345 $field_id = $atts['field_id'];
2346 $separator = isset($atts['separator']) ? $atts['separator'] : "\r\n";
2347
2348 $response = '';
2349 $values = array();
2350 if ($form_id && $field_id && class_exists('FrmEntry') && class_exists('FrmEntryMeta')) {
2351 $where = array(
2352 'it.form_id' => $form_id
2353 );
2354 $entries_tmp = FrmEntry::getAll($where, ' ORDER BY id ASC');
2355 foreach ($entries_tmp as $key => $entry) {
2356 $values[] = FrmEntryMeta::get_meta_value($entry, $field_id);
2357 }
2358 }
2359 $response = implode($separator, $values);
2360 return $response;
2361 }
2362
2363 /**
2364 * Get styles for generating Map Field function
2365 *
2366 * @return array - List of css files to load
2367 */
2368 public function get_styles() {
2369
2370 $styles = array();
2371 if (class_exists('FrmStylesHelper')) {
2372 $uploads = FrmStylesHelper::get_upload_base();
2373 $saved_css_path = '/formidable/css/formidablepro.css';
2374 if (is_readable($uploads['basedir'] . $saved_css_path)) {
2375 $url = $uploads['baseurl'] . $saved_css_path;
2376 } else {
2377 $url = admin_url('admin-ajax.php?action=frmpro_css');
2378 }
2379
2380 $styles[] = $url;
2381 }
2382
2383 return $styles;
2384 }
2385
2386 }
2387