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

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