PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.22
E2Pdf – Export Pdf Tool for WordPress v1.32.22
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-ninja.php

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

1,081 lines 50.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * File: /extension/e2pdf-ninja.php
5 *
6 * @package E2Pdf
7 * @license GPLv3
8 * @link https://e2pdf.com
9 */
10 if (!defined('ABSPATH')) {
11 die('Access denied.');
12 }
13
14 class Extension_E2pdf_Ninja extends Model_E2pdf_Model {
15
16 private $options;
17 private $info = array(
18 'key' => 'ninja',
19 'title' => 'Ninja Forms',
20 );
21
22 // info
23 public function info($key = false) {
24 if ($key && isset($this->info[$key])) {
25 return $this->info[$key];
26 } else {
27 return array(
28 $this->info['key'] => $this->info['title'],
29 );
30 }
31 }
32
33 // active
34 public function active() {
35 if (defined('E2PDF_NINJA_EXTENSION') || $this->helper->load('extension')->is_plugin_active('ninja-forms/ninja-forms.php')) {
36 return true;
37 }
38 return false;
39 }
40
41 // set
42 public function set($key, $value) {
43 if (!isset($this->options)) {
44 $this->options = new stdClass();
45 }
46 $this->options->$key = $value;
47
48 switch ($key) {
49 case 'item':
50 $this->set('cached_form', false);
51 if ($this->get('item') && function_exists('Ninja_Forms')) {
52 $form = Ninja_Forms()->form($this->get('item'))->get();
53 if ($form->get_setting('objectType')) {
54 $this->set('cached_form', $form);
55 }
56 }
57 break;
58 case 'dataset':
59 $this->set('cached_entry', false);
60 if ($this->get('cached_form') && $this->get('dataset')) {
61 $entry = Ninja_Forms()->form()->get_sub($this->get('dataset'));
62 if ($entry->get_form_id() == $this->get('cached_form')->get_id()) {
63 $this->set('cached_entry', $entry);
64
65 $fields_merge_tag_object = Ninja_Forms()->merge_tags['fields'];
66 $fields_merge_tag_object->set_form_id($this->get('item'));
67 $fields = Ninja_Forms()->form($this->get('item'))->get_fields();
68
69 if (class_exists('NF_Adapters_SubmissionsSubmission')) {
70 $fields = new NF_Adapters_SubmissionsSubmission($fields, $this->get('item'), $this->get('cached_entry'));
71 foreach ($fields as $field) {
72 // Fix PHP warnings
73 if (isset($field['type']) && isset($field['value'])) {
74 switch ($field['type']) {
75 case 'checkbox':
76 if (!isset($field['settings'])) {
77 $field['settings'] = array();
78 }
79 if (!isset($field['settings']['unchecked_value'])) {
80 $field['settings']['unchecked_value'] = '';
81 }
82 if (!isset($field['unchecked_calc_value'])) {
83 $field['unchecked_calc_value'] = '';
84 }
85 break;
86 case 'file_upload':
87 $_GET['sub_id'] = '';
88 break;
89 default:
90 break;
91 }
92 }
93 $fields_merge_tag_object->add_field($field);
94 if (isset($field['type']) && isset($field['value'])) {
95 switch ($field['type']) {
96 case 'repeater':
97 $form_field = Ninja_Forms()->form($this->get('item'))->get_field($field['id']);
98 $list_fields_types = array('listcheckbox', 'listmultiselect', 'listradio', 'listselect');
99 $field['value_label'] = $field['value'];
100 foreach ($form_field->get_setting('fields') as $sub_field) {
101 if (in_array($sub_field['type'], $list_fields_types, false)) {
102 foreach ($field['value_label'] as $sub_value_key => $sub_value) {
103 if (0 === strpos($sub_value['id'], $sub_field['id'] . '_')) {
104 $sub_field['value'] = $sub_value['value'];
105 $field['value_label'][$sub_value_key]['value'] = $fields_merge_tag_object->get_list_labels($sub_field);
106 }
107 }
108 }
109 }
110
111 $repeater_data_labels = Ninja_Forms()->fieldsetRepeater->extractSubmissions($field['id'], $field['value_label'], $form_field->get_settings());
112 $rows = array();
113 foreach ($repeater_data_labels as $repeater_key => $repeater) {
114 $row = array();
115 foreach ($repeater as $sub_repeater_key => $sub_repeater) {
116 $sub_value = is_array($sub_repeater['value']) ? implode(', ', $sub_repeater['value']) : $sub_repeater['value'];
117 $fields_merge_tag_object->add('field_' . $field['key'] . '_' . $repeater_key . '_' . $sub_repeater_key . '_label', $field['key'], '{field:' . $field['key'] . ':' . $repeater_key . '_' . $sub_repeater_key . ':label}', $sub_value);
118 $row[] = $sub_repeater['label'] . ': ' . $sub_value;
119 }
120 if (!empty($row)) {
121 $rows[] = implode(', ', $row);
122 }
123 }
124 $fields_merge_tag_object->add('field_' . $field['key'] . '_raw_label', $field['key'], '{field:' . $field['key'] . ':raw:label}', str_replace(array('{', '}'), array('&#123;', '&#125;'), serialize($repeater_data_labels))); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
125 $fields_merge_tag_object->add('field_' . $field['key'] . '_label', $field['key'], '{field:' . $field['key'] . ':label}', implode("\r\n", $rows));
126
127 $repeater_data = Ninja_Forms()->fieldsetRepeater->extractSubmissions($field['id'], $field['value'], $form_field->get_settings());
128 $rows = array();
129 foreach ($repeater_data as $repeater_key => $repeater) {
130 $row = array();
131 foreach ($repeater as $sub_repeater_key => $sub_repeater) {
132 $sub_value = is_array($sub_repeater['value']) ? implode(', ', $sub_repeater['value']) : $sub_repeater['value'];
133 $fields_merge_tag_object->add('field_' . $field['key'] . '_' . $repeater_key . '_' . $sub_repeater_key, $field['key'], '{field:' . $field['key'] . ':' . $repeater_key . '_' . $sub_repeater_key . '}', $sub_value);
134 $row[] = $sub_repeater['label'] . ': ' . $sub_value;
135 }
136 if (!empty($row)) {
137 $rows[] = implode(', ', $row);
138 }
139 }
140 $fields_merge_tag_object->add('field_' . $field['key'] . '_raw', $field['key'], '{field:' . $field['key'] . ':raw}', str_replace(array('{', '}'), array('&#123;', '&#125;'), serialize($repeater_data))); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
141 $fields_merge_tag_object->add('field_' . $field['key'], $field['key'], '{field:' . $field['key'] . '}', implode("\r\n", $rows));
142 break;
143 case 'listcountry':
144 case 'liststate':
145 $form_field = Ninja_Forms()->form($this->get('item'))->get_field($field['id']);
146 $options = apply_filters('ninja_forms_render_options', $form_field->get_setting('options'), $form_field->get_settings());
147 $options = apply_filters('ninja_forms_render_options_' . $form_field->get_setting('type'), $options, $form_field->get_settings());
148 if (isset($field['value']) && $field['value']) {
149 $key = array_search(
150 $field['value'], array_map(
151 function ($v) {
152 return $v['value'];
153 }, $options
154 ),
155 false
156 );
157
158 $label = isset($options[$key]['label']) ? $options[$key]['label'] : '';
159 $fields_merge_tag_object->add('field_' . $field['key'] . '_label', $field['key'], '{field:' . $field['key'] . ':label}', $label);
160 }
161 break;
162 case 'checkbox':
163 $form_field = Ninja_Forms()->form($this->get('item'))->get_field($field['id']);
164 $checked = '';
165 if (null == $form_field->get_setting('checked_value') && (1 == $field['value'] || 'on' == $field['value'])) {
166 $checked = esc_html__('Checked', 'ninja-forms');
167 } elseif (null == $form_field->get_setting('unchecked_value') && 0 == $field['value']) {
168 $checked = esc_html__('Unchecked', 'ninja-forms');
169 }
170 if ($checked == '') {
171 if (1 == $field['value'] || 'on' == $field['value']) {
172 $checked = $form_field->get_setting('checked_value');
173 } elseif ($form_field->get_setting('checked_value') != $field['value']) {
174 $checked = $form_field->get_setting('unchecked_value');
175 }
176 }
177 $fields_merge_tag_object->add('field_' . $field['key'] . '_label', $field['key'], '{field:' . $field['key'] . ':label}', $checked);
178 break;
179 case 'file_upload':
180 if (is_array($field['value'])) {
181 $fields_merge_tag_object->add('field_' . $field['key'], $field['key'], '{field:' . $field['key'] . '}', implode(', ', $field['value']));
182 }
183 break;
184 default:
185 break;
186 }
187 }
188 }
189 $fields_merge_tag_object->include_all_fields_merge_tags();
190 foreach ($fields as $field) {
191 if (isset($field['type']) && ($field['type'] === 'repeater' || $field['type'] === 'file_upload')) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
192 } else {
193 // Fix PHP warnings
194 if (isset($field['type']) && isset($field['value'])) {
195 switch ($field['type']) {
196 case 'checkbox':
197 if (!isset($field['settings'])) {
198 $field['settings'] = array();
199 }
200 if (!isset($field['settings']['unchecked_value'])) {
201 $field['settings']['unchecked_value'] = '';
202 }
203 if (!isset($field['unchecked_calc_value'])) {
204 $field['unchecked_calc_value'] = '';
205 }
206 break;
207 default:
208 break;
209 }
210 }
211 $fields_merge_tag_object->add_field($field); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
212 }
213 }
214 }
215 }
216 }
217 break;
218 default:
219 break;
220 }
221 return true;
222 }
223
224 // get
225 public function get($key) {
226 if (isset($this->options->$key)) {
227 $value = $this->options->$key;
228 } else {
229 switch ($key) {
230 case 'args':
231 $value = array();
232 break;
233 default:
234 $value = false;
235 break;
236 }
237 }
238 return $value;
239 }
240
241 // items
242 public function items() {
243 $items = array();
244 if (function_exists('Ninja_Forms')) {
245 $forms = Ninja_Forms()->form()->get_forms();
246 if (!empty($forms)) {
247 foreach ($forms as $form) {
248 $items[] = $this->item($form->get_id());
249 }
250 }
251 }
252 return $items;
253 }
254
255 // item
256 public function item($item_id = false) {
257 $item_id = (int) $item_id;
258 if (!$item_id && $this->get('item')) {
259 $item_id = $this->get('item');
260 }
261 $form = false;
262 if (function_exists('Ninja_Forms')) {
263 $form = Ninja_Forms()->form($item_id)->get();
264 }
265 $item = new stdClass();
266 if ($form) {
267 $item->id = (string) $item_id;
268 $item->url = $this->helper->get_url(
269 array(
270 'page' => 'ninja-forms',
271 'form_id' => $item_id,
272 )
273 );
274 $item->name = $form->get_setting('title');
275 } else {
276 $item->id = '';
277 $item->url = '';
278 $item->name = '';
279 }
280 return $item;
281 }
282
283 // datasets
284 public function datasets($item_id = false, $name = false) {
285 $item_id = (int) $item_id;
286 $datasets = array();
287 if (function_exists('Ninja_Forms')) {
288 $entries = Ninja_Forms()->form($item_id)->get_subs();
289 if ($entries) {
290 $this->set('item', $item_id);
291 foreach ($entries as $key => $entry) {
292 $this->set('dataset', $entry->get_id());
293 $entry_title = $this->render($name);
294 if (!$entry_title) {
295 $entry_title = $entry->get_id();
296 }
297 $datasets[] = array(
298 'key' => $entry->get_id(),
299 'value' => $entry_title,
300 );
301 }
302 }
303 }
304 return $datasets;
305 }
306
307 // get dataset actions
308 public function get_dataset_actions($dataset_id = false) {
309 $dataset_id = (int) $dataset_id;
310 if (!$dataset_id) {
311 return false;
312 }
313 $actions = new stdClass();
314 $actions->view = $this->helper->get_url(
315 array(
316 'post' => $dataset_id,
317 'action' => 'edit',
318 ),
319 'post.php?'
320 );
321 $actions->delete = false;
322 return $actions;
323 }
324
325 // get template actions
326 public function get_template_actions($template = false) {
327 $template = (int) $template;
328 if (!$template) {
329 return;
330 }
331 $actions = new stdClass();
332 $actions->delete = false;
333 return $actions;
334 }
335
336 // render
337 public function render($value, $field = array(), $convert_shortcodes = true, $raw = false) {
338 $value = $this->render_shortcodes($value, $field);
339 if (!$raw) {
340 $value = $this->strip_shortcodes($value);
341 $value = $this->convert_shortcodes($value, $convert_shortcodes, isset($field['type']) && $field['type'] == 'e2pdf-html' ? true : false);
342 $value = $this->helper->load('field')->render_checkbox($value, $this, $field);
343 }
344 return $value;
345 }
346
347 // load actions
348 public function load_actions() { // phpcs:ignore Squiz.WhiteSpace.SuperfluousWhitespace.EndLine
349 }
350
351 // load filters
352 public function load_filters() {
353 add_filter('ninja_forms_run_action_settings', array($this, 'filter_ninja_forms_run_action_settings'), 10, 4);
354 add_filter('ninja_forms_post_run_action_type_save', array($this, 'filter_ninja_forms_post_run_action_type_save'));
355 add_filter('ninja_forms_post_run_action_type_email', array($this, 'filter_ninja_forms_post_run_action_type_email'));
356 add_filter('ninja_forms_action_email_attachments', array($this, 'filter_ninja_forms_action_email_attachments'), 10, 3);
357 add_filter('ninja_forms_action_email_message', array($this, 'filter_ninja_forms_action_email_message'), 10, 3);
358 // 1.25.14 trigger email action fix
359 add_filter('rest_dispatch_request', array($this, 'filter_rest_dispatch_request'), 10, 3);
360 }
361
362 // run action settings filter
363 public function filter_ninja_forms_run_action_settings($settings, $form_id, $action_id, $form_data) {
364 $type = isset($settings['type']) ? $settings['type'] : false;
365 if ($type == 'successmessage' && isset($settings['success_msg'])) {
366 $settings['success_msg'] = $this->filter_content($settings['success_msg'], $this->get('dataset'), false, 'message');
367 }
368 return $settings;
369 }
370
371 // post run action type save filter
372 public function filter_ninja_forms_post_run_action_type_save($data) {
373 $dataset_id = isset($data['actions']['save']['sub_id']) ? $data['actions']['save']['sub_id'] : false;
374 if ($dataset_id) {
375 $this->set('dataset', $dataset_id);
376 }
377 return $data;
378 }
379
380 // post run action type email filter
381 public function filter_ninja_forms_post_run_action_type_email($data) {
382 $files = $this->helper->get('ninja_attachments');
383 if (is_array($files) && !empty($files)) {
384 foreach ($files as $key => $file) {
385 $this->helper->delete_dir(dirname($file) . '/');
386 }
387 $this->helper->deset('ninja_attachments');
388 }
389 return $data;
390 }
391
392 // email attachments filter
393 public function filter_ninja_forms_action_email_attachments($attachments, $data, $action_settings) {
394 $message = isset($action_settings['email_message']) ? $action_settings['email_message'] : '';
395 if (false !== strpos($message, '[')) {
396 $shortcode_tags = array(
397 'e2pdf-attachment',
398 'e2pdf-save',
399 );
400 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $message, $matches);
401 $tagnames = array_intersect($shortcode_tags, $matches[1]);
402 if (!empty($tagnames)) {
403 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $message, $shortcodes);
404 foreach ($shortcodes[0] as $key => $shortcode_value) {
405 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
406 $atts = shortcode_parse_atts($shortcode[3]);
407 $file = false;
408 if (!isset($atts['dataset']) && isset($atts['id'])) {
409 $template = new Model_E2pdf_Template();
410 $template->load($atts['id']);
411 if ($template->get('extension') === 'ninja') {
412 $dataset_id = isset($data['actions']['save']['sub_id']) ? $data['actions']['save']['sub_id'] : false;
413 if (!$dataset_id) {
414 $dataset_id = isset($action_settings['sub_id']) ? $action_settings['sub_id'] : false;
415 }
416 if ($dataset_id) {
417 $atts['dataset'] = $dataset_id;
418 $shortcode[3] .= ' dataset="' . $dataset_id . '"';
419 }
420 }
421 }
422 if (!isset($atts['apply'])) {
423 $shortcode[3] .= ' apply="true"';
424 }
425 if (!isset($atts['filter'])) {
426 $shortcode[3] .= ' filter="true"';
427 }
428 if ($this->helper->load('shortcode')->is_attachment($shortcode, $atts)) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
429 $file = do_shortcode_tag($shortcode);
430 if ($file) {
431 $tmp = false;
432 if (substr($file, 0, 4) === 'tmp:') {
433 $file = substr($file, 4);
434 $tmp = true;
435 }
436 if ($shortcode[2] === 'e2pdf-save' || isset($atts['pdf'])) {
437 if ($tmp) {
438 $this->helper->add('ninja_attachments', $file);
439 }
440 } else {
441 $this->helper->add('ninja_attachments', $file);
442 }
443 $attachments[] = $file;
444 }
445 }
446 }
447 }
448 }
449 return $attachments;
450 }
451
452 // email message filter
453 public function filter_ninja_forms_action_email_message($message, $data, $action_settings) {
454 $dataset_id = isset($data['actions']['save']['sub_id']) ? $data['actions']['save']['sub_id'] : false;
455 if (!$dataset_id) {
456 $dataset_id = isset($action_settings['sub_id']) ? $action_settings['sub_id'] : false;
457 }
458 $message = $this->filter_content($message, $dataset_id, true);
459 return $message;
460 }
461
462 // rest dispatch request filter
463 public function filter_rest_dispatch_request($result, $request, $route) {
464 if ($route && false !== strpos($route, '/ninja-forms-submissions/email-action')) {
465 $data = json_decode($request->get_body());
466 if (!empty($data->submission) && !empty($data->action_settings)) {
467 $data->action_settings->sub_id = $data->submission;
468 $request->set_body(json_encode($data)); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
469 }
470 }
471 return $result;
472 }
473
474 // filter content
475 public function filter_content($message, $dataset_id, $filter, $type = '') {
476 if (false !== strpos($message, '[')) {
477 $shortcode_tags = array(
478 'e2pdf-download',
479 'e2pdf-save',
480 'e2pdf-attachment',
481 'e2pdf-view',
482 'e2pdf-adobesign',
483 'e2pdf-zapier',
484 );
485 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $message, $matches);
486 $tagnames = array_intersect($shortcode_tags, $matches[1]);
487 if (!empty($tagnames)) {
488 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $message, $shortcodes);
489 foreach ($shortcodes[0] as $key => $shortcode_value) {
490 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
491 $atts = shortcode_parse_atts($shortcode[3]);
492 if (!isset($atts['dataset']) && isset($atts['id'])) {
493 $template = new Model_E2pdf_Template();
494 $template->load($atts['id']);
495 if ($template->get('extension') === 'ninja') {
496 $atts['dataset'] = $dataset_id;
497 $shortcode[3] .= ' dataset="' . $dataset_id . '"';
498 }
499 }
500 if (!isset($atts['apply'])) {
501 $shortcode[3] .= ' apply="true"';
502 }
503 if (!isset($atts['filter']) && $filter) {
504 $shortcode[3] .= ' filter="true"';
505 }
506 if (!isset($atts['iframe_download']) && $type == 'message') {
507 $shortcode[3] .= ' iframe_download="true"';
508 }
509 if ($this->helper->load('shortcode')->is_attachment($shortcode, $atts)) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
510 $message = str_replace($shortcode_value, '', $message);
511 } else {
512 $message = str_replace($shortcode_value, do_shortcode_tag($shortcode), $message);
513 }
514 }
515 }
516 }
517 return $message;
518 }
519
520 // render shortcodes
521 public function render_shortcodes($value, $field = array()) {
522 $element_id = isset($field['element_id']) ? $field['element_id'] : false;
523 if ($this->verify()) {
524 if (false !== strpos($value, '[')) {
525 $value = $this->helper->load('field')->pre_shortcodes($value, $this, $field);
526 $value = $this->helper->load('field')->inner_shortcodes($value, $this, $field);
527 $value = $this->helper->load('field')->wrapper_shortcodes($value, $this, $field);
528 }
529 $value = $this->helper->load('field')->do_shortcodes($value, $this, $field);
530 $value = apply_filters('ninja_forms_merge_tags', $value);
531 $value = $this->helper->load('field')->render(
532 apply_filters('e2pdf_extension_render_shortcodes_pre_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), false, false),
533 $this,
534 $field
535 );
536 }
537 return apply_filters(
538 'e2pdf_extension_render_shortcodes_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), false, false
539 );
540 }
541
542 // strip shortcodes
543 public function strip_shortcodes($value) {
544 $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', '', $value);
545 $value = preg_replace('~a\:\d+\:{[^}]*}(*SKIP)(*FAIL)|{[^}]*}~', '', $value);
546 return $value;
547 }
548
549 // strip math
550 public function strip_math($value, &$replacements) {
551 if ($value) {
552 preg_match_all('/\{[^}]+\}/', $value, $matches);
553 foreach ($matches[0] as $match) {
554 $index = count($replacements) + 1;
555 $replacements[] = $match;
556 $value = str_replace($match, '%' . $index . '$s', $value);
557 }
558 }
559 return $value;
560 }
561
562 // convert shortcodes
563 public function convert_shortcodes($value, $to = false, $html = false) {
564 if ($value) {
565 if ($to) {
566 $search = array('&#91;', '&#93;', '&#091;', '&#093;', '&#123;', '&#125;');
567 $replace = array('[', ']', '[', ']', '{', '}');
568 $value = str_replace($search, $replace, $value);
569 if (!$html) {
570 $value = wp_specialchars_decode($value, ENT_QUOTES);
571 }
572 } else {
573 $search = array('[', ']', '&#091;', '&#093;');
574 $replace = array('&#91;', '&#93;', '&#91;', '&#93;');
575 $value = str_replace($search, $replace, $value);
576 }
577 }
578 return $value;
579 }
580
581 // auto
582 public function auto() {
583 $elements = array();
584 if ($this->get('cached_form')) {
585 $form_fields = Ninja_Forms()->form($this->get('item'))->get_fields();
586 $width = '100';
587 foreach ($form_fields as $form_field) {
588 $field = $form_field->get_settings();
589 switch ($field['type']) {
590 case 'repeater':
591 $elements[] = $this->auto_field(
592 $field,
593 array(
594 'type' => 'e2pdf-html',
595 'block' => true,
596 'float' => true,
597 'properties' => array(
598 'top' => '20',
599 'left' => '20',
600 'right' => '20',
601 'width' => $width . '%',
602 'height' => 'auto',
603 'value' => $field['label'],
604 ),
605 )
606 );
607
608 $elements[] = $this->auto_field(
609 $field,
610 array(
611 'type' => 'e2pdf-html',
612 'block' => true,
613 'float' => true,
614 'properties' => array(
615 'top' => '20',
616 'left' => '20',
617 'right' => '20',
618 'width' => $width . '%',
619 'height' => 'auto',
620 'value' => '<hr>',
621 ),
622 )
623 );
624 foreach ($field['fields'] as $sub_field) {
625 list($field_id, $sub_field_id) = explode('.', $sub_field['id']);
626 $sub_field['key'] = $field['key'] . ':0_' . $sub_field_id;
627 $elements = $this->auto_fields($elements, $sub_field);
628 }
629 $elements[] = $this->auto_field(
630 $field,
631 array(
632 'type' => 'e2pdf-html',
633 'block' => true,
634 'float' => true,
635 'properties' => array(
636 'top' => '20',
637 'left' => '20',
638 'right' => '20',
639 'width' => $width . '%',
640 'height' => 'auto',
641 'value' => '<hr>',
642 ),
643 )
644 );
645 break;
646 default:
647 $elements = $this->auto_fields($elements, $field);
648 break;
649 }
650 }
651 }
652
653 $response = array();
654 $response['page'] = array(
655 'bottom' => '20',
656 'top' => '20',
657 'left' => '20',
658 'right' => '20',
659 );
660
661 $response['elements'] = $elements;
662 return $response;
663 }
664
665 // auto fields
666 public function auto_fields($elements, $field) {
667 $width = '100';
668 switch ($field['type']) {
669 case 'textbox':
670 case 'number':
671 case 'date':
672 case 'city':
673 case 'email':
674 case 'firstname':
675 case 'lastname':
676 case 'phone':
677 case 'zip':
678 case 'hidden':
679 case 'starrating':
680 $elements[] = $this->auto_field(
681 $field,
682 array(
683 'type' => 'e2pdf-html',
684 'block' => true,
685 'float' => true,
686 'properties' => array(
687 'top' => '20',
688 'left' => '20',
689 'right' => '20',
690 'width' => $width . '%',
691 'height' => 'auto',
692 'value' => $field['label'],
693 ),
694 )
695 );
696
697 $elements[] = $this->auto_field(
698 $field,
699 array(
700 'type' => 'e2pdf-input',
701 'properties' => array(
702 'top' => '5',
703 'width' => '100%',
704 'height' => 'auto',
705 'value' => '{field:' . $field['key'] . '}',
706 ),
707 )
708 );
709 break;
710 case 'listselect':
711 case 'listmultiselect':
712 case 'listcountry':
713 case 'liststate':
714 if ($field['type'] == 'listcountry' || $field['type'] == 'liststate') {
715 $options = apply_filters('ninja_forms_render_options', $field['options'], $field);
716 $options = apply_filters('ninja_forms_render_options_' . $field['type'], $options, $field);
717 } else {
718 $options = $field['options'];
719 }
720
721 $elements[] = $this->auto_field(
722 $field,
723 array(
724 'type' => 'e2pdf-html',
725 'block' => true,
726 'float' => true,
727 'properties' => array(
728 'top' => '20',
729 'left' => '20',
730 'right' => '20',
731 'width' => $width . '%',
732 'height' => 'auto',
733 'value' => $field['label'],
734 ),
735 )
736 );
737 $options_tmp = array();
738 foreach ($options as $option) {
739 $options_tmp[] = $option['label'];
740 }
741
742 if ($field['type'] == 'listmultiselect') {
743 $elements[] = $this->auto_field(
744 $field,
745 array(
746 'type' => 'e2pdf-select',
747 'properties' => array(
748 'top' => '5',
749 'width' => '100%',
750 'height' => '44',
751 'multiline' => '1',
752 'options' => implode("\n", $options_tmp),
753 'value' => '{field:' . $field['key'] . ':label}',
754 ),
755 )
756 );
757 } else {
758 $elements[] = $this->auto_field(
759 $field,
760 array(
761 'type' => 'e2pdf-select',
762 'properties' => array(
763 'top' => '5',
764 'width' => '100%',
765 'height' => 'auto',
766 'options' => implode("\n", $options_tmp),
767 'value' => '{field:' . $field['key'] . ':label}',
768 ),
769 )
770 );
771 }
772 break;
773 case 'textarea':
774 case 'address':
775 case 'listimage':
776 case 'file_upload':
777 $elements[] = $this->auto_field(
778 $field,
779 array(
780 'type' => 'e2pdf-html',
781 'block' => true,
782 'float' => true,
783 'properties' => array(
784 'top' => '20',
785 'left' => '20',
786 'right' => '20',
787 'width' => $width . '%',
788 'height' => 'auto',
789 'value' => $field['label'],
790 ),
791 )
792 );
793
794 $elements[] = $this->auto_field(
795 $field,
796 array(
797 'type' => 'e2pdf-textarea',
798 'properties' => array(
799 'top' => '5',
800 'width' => '100%',
801 'height' => 'auto',
802 'value' => '{field:' . $field['key'] . '}',
803 ),
804 )
805 );
806 break;
807 case 'html':
808 $elements[] = $this->auto_field(
809 $field,
810 array(
811 'type' => 'e2pdf-html',
812 'block' => true,
813 'float' => true,
814 'properties' => array(
815 'top' => '20',
816 'left' => '20',
817 'right' => '20',
818 'width' => $width . '%',
819 'height' => 'auto',
820 'value' => $field['label'],
821 ),
822 )
823 );
824
825 $elements[] = $this->auto_field(
826 $field,
827 array(
828 'type' => 'e2pdf-html',
829 'properties' => array(
830 'top' => '5',
831 'width' => '100%',
832 'height' => 'auto',
833 'value' => $field['default'],
834 ),
835 )
836 );
837 break;
838 case 'listradio':
839 $elements[] = $this->auto_field(
840 $field,
841 array(
842 'type' => 'e2pdf-html',
843 'block' => true,
844 'float' => true,
845 'properties' => array(
846 'top' => '20',
847 'left' => '20',
848 'right' => '20',
849 'width' => $width . '%',
850 'height' => 'auto',
851 'value' => $field['label'],
852 ),
853 )
854 );
855
856 foreach ($field['options'] as $opt_key => $option) {
857 $elements[] = $this->auto_field(
858 $field,
859 array(
860 'type' => 'e2pdf-radio',
861 'properties' => array(
862 'top' => '5',
863 'width' => 'auto',
864 'height' => 'auto',
865 'value' => '{field:' . $field['key'] . ':label}',
866 'option' => $option['label'],
867 'group' => '{field:' . $field['key'] . '}',
868 ),
869 )
870 );
871 $elements[] = $this->auto_field(
872 $field,
873 array(
874 'type' => 'e2pdf-html',
875 'float' => true,
876 'properties' => array(
877 'left' => '5',
878 'width' => '100%',
879 'height' => 'auto',
880 'value' => $option['label'],
881 ),
882 )
883 );
884 }
885 break;
886 case 'listcheckbox':
887 case 'checkbox':
888 $elements[] = $this->auto_field(
889 $field,
890 array(
891 'type' => 'e2pdf-html',
892 'block' => true,
893 'float' => true,
894 'properties' => array(
895 'top' => '20',
896 'left' => '20',
897 'right' => '20',
898 'width' => $width . '%',
899 'height' => 'auto',
900 'value' => $field['label'],
901 ),
902 )
903 );
904
905 if ($field['type'] == 'checkbox') {
906 $elements[] = $this->auto_field(
907 $field,
908 array(
909 'type' => 'e2pdf-checkbox',
910 'properties' => array(
911 'top' => '5',
912 'width' => 'auto',
913 'height' => 'auto',
914 'value' => '{field:' . $field['key'] . ':label}',
915 'option' => !isset($field['checked_value']) ? esc_html__('Checked', 'ninja-forms') : $field['checked_value'],
916 ),
917 )
918 );
919 $elements[] = $this->auto_field(
920 $field,
921 array(
922 'type' => 'e2pdf-html',
923 'float' => true,
924 'properties' => array(
925 'left' => '5',
926 'width' => '100%',
927 'height' => 'auto',
928 'value' => $field['label'],
929 ),
930 )
931 );
932 } else {
933 foreach ($field['options'] as $opt_key => $option) {
934 $elements[] = $this->auto_field(
935 $field,
936 array(
937 'type' => 'e2pdf-checkbox',
938 'properties' => array(
939 'top' => '5',
940 'width' => 'auto',
941 'height' => 'auto',
942 'value' => '{field:' . $field['key'] . ':label}',
943 'option' => $option['label'],
944 ),
945 )
946 );
947 $elements[] = $this->auto_field(
948 $field,
949 array(
950 'type' => 'e2pdf-html',
951 'float' => true,
952 'properties' => array(
953 'left' => '5',
954 'width' => '100%',
955 'height' => 'auto',
956 'value' => $option['label'],
957 ),
958 )
959 );
960 }
961 }
962 break;
963 case 'asignature':
964 $elements[] = $this->auto_field(
965 $field,
966 array(
967 'type' => 'e2pdf-html',
968 'block' => true,
969 'float' => true,
970 'properties' => array(
971 'top' => '20',
972 'left' => '20',
973 'right' => '20',
974 'width' => $width . '%',
975 'height' => 'auto',
976 'value' => $field['label'],
977 ),
978 )
979 );
980 $elements[] = $this->auto_field(
981 $field,
982 array(
983 'type' => 'e2pdf-signature',
984 'properties' => array(
985 'top' => '5',
986 'width' => '100%',
987 'height' => '150',
988 'dimension' => '1',
989 'block_dimension' => '1',
990 'value' => '{field:' . $field['key'] . '}',
991 ),
992 )
993 );
994 break;
995 default:
996 break;
997 }
998
999 return $elements;
1000 }
1001
1002 // auto field
1003 public function auto_field($field = false, $element = array()) {
1004 if (!$field) {
1005 return false;
1006 }
1007 return $element;
1008 }
1009
1010 // verify
1011 public function verify() {
1012 if ($this->get('cached_form') && $this->get('cached_entry')) {
1013 return true;
1014 }
1015 return false;
1016 }
1017
1018 // visual mapper
1019 public function visual_mapper() {
1020 $html = '';
1021 if ($this->get('item')) {
1022 $form = $this->auto();
1023 $html = '<div class="ninja-form">';
1024 $float = false;
1025 foreach ($form['elements'] as $element) {
1026 switch ($element['type']) {
1027 case 'e2pdf-html':
1028 $html .= '<div>' . $element['properties']['value'] . '</div>';
1029 if ($float) {
1030 $html .= '<div class="clearfix"></div>';
1031 $float = false;
1032 }
1033 break;
1034 case 'e2pdf-input':
1035 $html .= '<div><input type="text" name="' . $element['properties']['value'] . '"></div>';
1036 break;
1037 case 'e2pdf-image':
1038 case 'e2pdf-signature':
1039 case 'e2pdf-textarea':
1040 $html .= '<div><textarea name="' . $element['properties']['value'] . '"></textarea></div>';
1041 break;
1042 case 'e2pdf-select':
1043 $html .= '<div>';
1044 $html .= '<select name="' . $element['properties']['value'] . '">';
1045 $options = explode("\n", $element['properties']['options']);
1046 foreach ($options as $option) {
1047 $html .= '<option>' . $option . '</option>';
1048 }
1049 $html .= '</select>';
1050 $html .= '</div>';
1051 break;
1052 case 'e2pdf-checkbox':
1053 $html .= '<div class="ninja-checkbox"><input type="checkbox" name="' . $element['properties']['value'] . '" value="' . $element['properties']['option'] . '"></div>';
1054 $float = true;
1055 break;
1056 case 'e2pdf-radio':
1057 $html .= '<div class="ninja-radio"><input type="radio" name="' . $element['properties']['value'] . '" value="' . $element['properties']['option'] . '"></div>';
1058 $float = true;
1059 break;
1060 default:
1061 break;
1062 }
1063 }
1064 $html .= '</div>';
1065 return $html;
1066 }
1067 return false;
1068 }
1069
1070 // load shortcodes
1071 public function load_shortcodes() { // phpcs:ignore Squiz.WhiteSpace.SuperfluousWhitespace.EndLine
1072 }
1073
1074 // styles
1075 public function styles($item_id = false) {
1076 $styles = array();
1077 $styles[] = plugins_url('css/extension/ninja.css?v=' . time(), $this->helper->get('plugin_file_path'));
1078 return $styles;
1079 }
1080 }
1081