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

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