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

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