| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* E2pdf Caldera Extension |
| 5 |
* |
| 6 |
* @copyright Copyright 2017 https://e2pdf.com |
| 7 |
* @license GPL v2 |
| 8 |
* @version 1 |
| 9 |
* @link https://e2pdf.com |
| 10 |
* @since 0.01.47 |
| 11 |
*/ |
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
die('Access denied.'); |
| 14 |
} |
| 15 |
|
| 16 |
class Extension_E2pdf_Caldera extends Model_E2pdf_Model { |
| 17 |
|
| 18 |
private $options; |
| 19 |
private $info = array( |
| 20 |
'key' => 'caldera', |
| 21 |
'title' => 'Caldera 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('caldera-forms/caldera-core.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 |
if (!isset($this->options)) { |
| 72 |
$this->options = new stdClass(); |
| 73 |
} |
| 74 |
|
| 75 |
$this->options->$key = $value; |
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get option by key |
| 81 |
* |
| 82 |
* @param string $key - Key to get assigned option value |
| 83 |
* |
| 84 |
* @return mixed |
| 85 |
*/ |
| 86 |
public function get($key) { |
| 87 |
if (isset($this->options->$key)) { |
| 88 |
$value = $this->options->$key; |
| 89 |
return $value; |
| 90 |
} elseif ($key == 'args') { |
| 91 |
return array(); |
| 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 |
$content = array(); |
| 104 |
if (class_exists("Caldera_Forms_Forms")) { |
| 105 |
$forms = Caldera_Forms_Forms::get_forms(true); |
| 106 |
if ($forms) { |
| 107 |
foreach ($forms as $key => $value) { |
| 108 |
$content[] = $this->item($value['ID']); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
return $content; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get entries for export |
| 117 |
* |
| 118 |
* @param int $item - Form ID |
| 119 |
* @param string $name - Entries names |
| 120 |
* |
| 121 |
* @return array() - Entries list |
| 122 |
*/ |
| 123 |
public function datasets($item = false, $name = false) { |
| 124 |
|
| 125 |
$datasets = array(); |
| 126 |
|
| 127 |
if (class_exists("Caldera_Forms_Admin") && $item) { |
| 128 |
|
| 129 |
$form = Caldera_Forms_Admin::get_entries($item, 1, 9999999999); |
| 130 |
|
| 131 |
if ($form && isset($form['entries']) && is_array($form['entries'])) { |
| 132 |
foreach ($form['entries'] as $key => $dataset) { |
| 133 |
$this->set('item', $item); |
| 134 |
$this->set('dataset', $dataset['_entry_id']); |
| 135 |
|
| 136 |
$dataset_title = $this->render($name); |
| 137 |
if (!$dataset_title) { |
| 138 |
$dataset_title = $dataset['_date']; |
| 139 |
} |
| 140 |
$datasets[] = array( |
| 141 |
'key' => $dataset['_entry_id'], |
| 142 |
'value' => $dataset_title |
| 143 |
); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
return $datasets; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Get dataset |
| 154 |
* |
| 155 |
* @param int $dataset - Dataset ID |
| 156 |
* |
| 157 |
* @return object - Dataset |
| 158 |
*/ |
| 159 |
public function dataset($dataset = false) { |
| 160 |
$dataset = (int) $dataset; |
| 161 |
|
| 162 |
if (!$dataset) { |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
$data = new stdClass(); |
| 167 |
$data->url = false; |
| 168 |
|
| 169 |
return $data; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Get item |
| 174 |
* |
| 175 |
* @param string $item - Item ID |
| 176 |
* |
| 177 |
* @return object - Item |
| 178 |
*/ |
| 179 |
public function item($item = false) { |
| 180 |
|
| 181 |
$form = new stdClass(); |
| 182 |
|
| 183 |
if (!$item && $this->get('item')) { |
| 184 |
$item = $this->get('item'); |
| 185 |
} |
| 186 |
|
| 187 |
$caldera_form = false; |
| 188 |
if (class_exists('Caldera_Forms_Forms')) { |
| 189 |
$caldera_form = Caldera_Forms_Forms::get_form($item); |
| 190 |
} |
| 191 |
|
| 192 |
if ($caldera_form) { |
| 193 |
$form->id = (string) $caldera_form['ID']; |
| 194 |
$form->url = $this->helper->get_url(array('page' => 'caldera-forms', 'edit' => $caldera_form['ID'])); |
| 195 |
$form->name = $caldera_form['name']; |
| 196 |
} else { |
| 197 |
$form->id = ''; |
| 198 |
$form->url = 'javascript:void(0);'; |
| 199 |
$form->name = ''; |
| 200 |
} |
| 201 |
return $form; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Render value according to content |
| 206 |
* |
| 207 |
* @param string $value - Content |
| 208 |
* @param string $type - Type of rendering value |
| 209 |
* @param array $field - Field details |
| 210 |
* |
| 211 |
* @return string - Fully rendered value |
| 212 |
*/ |
| 213 |
public function render($value, $type = false, $field = array()) { |
| 214 |
|
| 215 |
$value = $this->render_shortcodes($value, $type, $field); |
| 216 |
$value = $this->strip_shortcodes($value); |
| 217 |
|
| 218 |
if ($type === 'value' && isset($field['type']) && $field['type'] === 'e2pdf-checkbox' && isset($field['properties']['option'])) { |
| 219 |
$option = $this->render($field['properties']['option']); |
| 220 |
$options = explode(', ', $value); |
| 221 |
$option_options = explode(', ', $option); |
| 222 |
if (is_array($options) && is_array($option_options) && !array_diff($option_options, $options)) { |
| 223 |
return $option; |
| 224 |
} else { |
| 225 |
return ""; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
if ($type === 'value') { |
| 230 |
$value = $this->convert_shortcodes($value, true); |
| 231 |
} |
| 232 |
|
| 233 |
return $value; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Render shortcodes which available in this extension |
| 238 |
* |
| 239 |
* @param string $value - Content |
| 240 |
* @param string $type - Type of rendering value |
| 241 |
* @param array $field - Field details |
| 242 |
* |
| 243 |
* @return string - Value with rendered shortcodes |
| 244 |
*/ |
| 245 |
public function render_shortcodes($value, $type = false, $field = array()) { |
| 246 |
|
| 247 |
$dataset = $this->get('dataset'); |
| 248 |
$item = $this->get('item'); |
| 249 |
$user_id = $this->get('user_id'); |
| 250 |
$args = $this->get('args'); |
| 251 |
$template_id = $this->get('template_id') ? $this->get('template_id') : '0'; |
| 252 |
$element_id = isset($field['element_id']) ? $field['element_id'] : '0'; |
| 253 |
|
| 254 |
if ($this->verify()) { |
| 255 |
|
| 256 |
$args = apply_filters('e2pdf_extension_render_shortcodes_args', $args, $element_id, $template_id, $item, $dataset); |
| 257 |
|
| 258 |
if (false !== strpos($value, '[')) { |
| 259 |
|
| 260 |
$shortcode_tags = array( |
| 261 |
'e2pdf-user', |
| 262 |
'e2pdf-arg' |
| 263 |
); |
| 264 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches); |
| 265 |
$tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 266 |
|
| 267 |
if (!empty($tagnames)) { |
| 268 |
|
| 269 |
$pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames); |
| 270 |
|
| 271 |
preg_match_all("/$pattern/", $value, $shortcodes); |
| 272 |
|
| 273 |
foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 274 |
$shortcode = array(); |
| 275 |
$shortcode[1] = $shortcodes[1][$key]; |
| 276 |
$shortcode[2] = $shortcodes[2][$key]; |
| 277 |
$shortcode[3] = $shortcodes[3][$key]; |
| 278 |
$shortcode[4] = $shortcodes[4][$key]; |
| 279 |
$shortcode[5] = $shortcodes[5][$key]; |
| 280 |
$shortcode[6] = $shortcodes[6][$key]; |
| 281 |
|
| 282 |
$atts = shortcode_parse_atts($shortcode[3]); |
| 283 |
|
| 284 |
if ($shortcode['2'] == 'e2pdf-user') { |
| 285 |
if (!isset($atts['id']) && $user_id) { |
| 286 |
$shortcode[3] .= " id=\"" . $user_id . "\""; |
| 287 |
$value = str_replace($shortcode_value, "[" . $shortcode['2'] . $shortcode['3'] . "]", $value); |
| 288 |
} |
| 289 |
} elseif ($shortcode['2'] == 'e2pdf-arg') { |
| 290 |
if (isset($atts['key']) && isset($args[$atts['key']])) { |
| 291 |
$sub_value = $this->strip_shortcodes($args[$atts['key']]); |
| 292 |
$value = str_replace($shortcode_value, $sub_value, $value); |
| 293 |
} else { |
| 294 |
$value = str_replace($shortcode_value, '', $value); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
$shortcode_tags = array( |
| 301 |
'e2pdf-format-number', |
| 302 |
'e2pdf-format-date', |
| 303 |
'e2pdf-format-output', |
| 304 |
); |
| 305 |
$shortcode_tags = apply_filters('e2pdf_extension_render_shortcodes_tags', $shortcode_tags); |
| 306 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches); |
| 307 |
$tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 308 |
|
| 309 |
if (!empty($tagnames)) { |
| 310 |
|
| 311 |
$pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames); |
| 312 |
|
| 313 |
preg_match_all("/$pattern/", $value, $shortcodes); |
| 314 |
foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 315 |
|
| 316 |
$shortcode = array(); |
| 317 |
$shortcode[1] = $shortcodes[1][$key]; |
| 318 |
$shortcode[2] = $shortcodes[2][$key]; |
| 319 |
$shortcode[3] = $shortcodes[3][$key]; |
| 320 |
$shortcode[4] = $shortcodes[4][$key]; |
| 321 |
$shortcode[5] = $shortcodes[5][$key]; |
| 322 |
$shortcode[6] = $shortcodes[6][$key]; |
| 323 |
|
| 324 |
if (!$shortcode['5']) { |
| 325 |
$sub_value = ''; |
| 326 |
} elseif (isset($field['type']) && ($field['type'] === 'e2pdf-image' || $field['type'] === 'e2pdf-signature')) { |
| 327 |
$sub_value = $this->render($shortcode['5'], $type); |
| 328 |
} else { |
| 329 |
$sub_value = $this->render($shortcode['5'], $type, $field); |
| 330 |
} |
| 331 |
$value = str_replace($shortcode_value, "[" . $shortcode['2'] . $shortcode['3'] . "]" . $sub_value . "[/" . $shortcode['2'] . "]", $value); |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
$value = do_shortcode($value); |
| 337 |
|
| 338 |
add_filter('caldera_forms_magic_form', array($this, 'filter_caldera_forms_magic_form'), 30, 2); |
| 339 |
add_filter('caldera_forms_render_get_field', array($this, 'filter_caldera_forms_render_get_field'), 30, 2); |
| 340 |
add_filter('caldera_forms_do_field_magic_value', array($this, 'filter_caldera_forms_do_field_magic_value'), 30, 4); |
| 341 |
|
| 342 |
global $form; |
| 343 |
$tmp_form = false; |
| 344 |
if ($form && isset($form['ID'])) { |
| 345 |
$tmp_form = $form; |
| 346 |
if ($form['ID'] != $this->get('item')) { |
| 347 |
$form = Caldera_Forms_Forms::get_form($this->get('item')); |
| 348 |
} |
| 349 |
if (!empty($form['is_connected_form'])) { |
| 350 |
$form['processors']['_connected_form'] = array( |
| 351 |
'type' => 'form-connector', |
| 352 |
'runtimes' => array( |
| 353 |
'insert' => true |
| 354 |
), |
| 355 |
'config' => array() |
| 356 |
); |
| 357 |
|
| 358 |
// setup fields |
| 359 |
$form_fields = array(); |
| 360 |
// get meta |
| 361 |
$meta = Caldera_Forms::get_entry_meta((int) $dataset, $form); |
| 362 |
|
| 363 |
if (!empty($meta['form-connector']['data']['_connected_form']['entry']['form']['meta_value'])) { |
| 364 |
foreach ((array) $meta['form-connector']['data']['_connected_form']['entry']['form']['meta_value'] as $form_meta) { |
| 365 |
$form_fields = array_merge($form_fields, $form_meta); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
if (!empty($form['condition_points']['forms'])) { |
| 370 |
$form['fields'] = array(); |
| 371 |
foreach ($form['condition_points']['forms'] as $connected_id => $connected_form) { |
| 372 |
if (!empty($form_fields[$connected_id]) && !empty($connected_form['fields'])) { |
| 373 |
$form['fields'] = array_merge($form['fields'], $connected_form['fields']); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
if (function_exists('cf_form_connector_setup_processors_meta')) { |
| 378 |
add_filter('caldera_forms_get_entry_detail', 'cf_form_connector_setup_processors_meta', 10, 3); |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
$value = Caldera_Forms::do_magic_tags($value, $dataset); |
| 385 |
|
| 386 |
if ($tmp_form) { |
| 387 |
$form = $tmp_form; |
| 388 |
} |
| 389 |
|
| 390 |
if (isset($field['type']) && ($field['type'] === 'e2pdf-image' || $field['type'] === 'e2pdf-signature')) { |
| 391 |
$esig = isset($field['properties']['esig']) && $field['properties']['esig'] ? true : false; |
| 392 |
if ($esig) { |
| 393 |
//process e-signature |
| 394 |
$value = ""; |
| 395 |
} else { |
| 396 |
|
| 397 |
if (isset($field['properties']['preg_pattern']) && $field['properties']['preg_pattern'] && isset($field['properties']['preg_replacement']) && $value) { |
| 398 |
$value = $this->helper->load('convert')->preg_replace($field['properties']['preg_pattern'], $field['properties']['preg_replacement'], $value); |
| 399 |
} |
| 400 |
|
| 401 |
if (!$this->helper->load('image')->get_image($value)) { |
| 402 |
$value = $this->strip_shortcodes($value); |
| 403 |
if ( |
| 404 |
$value && |
| 405 |
trim($value) != "" && |
| 406 |
extension_loaded('gd') && |
| 407 |
function_exists('imagettftext') |
| 408 |
) { |
| 409 |
if (isset($field['properties']['text_color']) && $field['properties']['text_color']) { |
| 410 |
$penColour = $this->helper->load('convert')->to_hex_color($field['properties']['text_color']); |
| 411 |
} else { |
| 412 |
$penColour = array(0x14, 0x53, 0x94); |
| 413 |
} |
| 414 |
|
| 415 |
$default_options = array( |
| 416 |
'imageSize' => array(isset($field['width']) ? $field['width'] : '400', isset($field['height']) ? $field['height'] : '150'), |
| 417 |
'bgColour' => 'transparent', |
| 418 |
'penColour' => $penColour |
| 419 |
); |
| 420 |
|
| 421 |
$options = array(); |
| 422 |
$options = apply_filters('e2pdf_image_sig_output_options', $options, $element_id, $template_id); |
| 423 |
$options = array_merge($default_options, $options); |
| 424 |
|
| 425 |
$model_e2pdf_font = new Model_E2pdf_Font(); |
| 426 |
|
| 427 |
$font = false; |
| 428 |
if (isset($field['properties']['text_font']) && $field['properties']['text_font']) { |
| 429 |
$font = $model_e2pdf_font->get_font_path($field['properties']['text_font']); |
| 430 |
} |
| 431 |
if (!$font) { |
| 432 |
$font = $model_e2pdf_font->get_font_path('Noto Sans'); |
| 433 |
} |
| 434 |
|
| 435 |
$size = 150; |
| 436 |
if (isset($field['properties']['text_font_size']) && $field['properties']['text_font_size']) { |
| 437 |
$size = $field['properties']['text_font_size']; |
| 438 |
} |
| 439 |
|
| 440 |
$model_e2pdf_signature = new Model_E2pdf_Signature(); |
| 441 |
$value = $model_e2pdf_signature->ttf_signature($value, $size, $font, $options); |
| 442 |
} else { |
| 443 |
$value = ""; |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
} else { |
| 448 |
|
| 449 |
$value = $this->convert_shortcodes($value); |
| 450 |
|
| 451 |
if (isset($field['properties']['preg_pattern']) && $field['properties']['preg_pattern'] && isset($field['properties']['preg_replacement']) && $value) { |
| 452 |
$value = $this->helper->load('convert')->preg_replace($field['properties']['preg_pattern'], $field['properties']['preg_replacement'], $value); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
remove_filter('caldera_forms_magic_form', array($this, 'filter_caldera_forms_magic_form'), 30); |
| 457 |
remove_filter('caldera_forms_render_get_field', array($this, 'filter_caldera_forms_render_get_field'), 30); |
| 458 |
remove_filter('caldera_forms_do_field_magic_value', array($this, 'filter_caldera_forms_do_field_magic_value'), 30); |
| 459 |
} |
| 460 |
|
| 461 |
$value = apply_filters('e2pdf_extension_render_shortcodes_value', $value, $element_id, $template_id, $item, $dataset); |
| 462 |
|
| 463 |
return $value; |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* Strip unused shortcodes |
| 468 |
* |
| 469 |
* @param string $value - Content |
| 470 |
* |
| 471 |
* @return string - Value with removed unused shortcodes |
| 472 |
*/ |
| 473 |
public function strip_shortcodes($value) { |
| 474 |
$value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', "", $value); |
| 475 |
$value = preg_replace('~(?:%/?)[^/%]+/?%~s', "", $value); |
| 476 |
return $value; |
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
* Convert "shortcodes" inside value string |
| 481 |
* |
| 482 |
* @param string $value - Value string |
| 483 |
* @param bool $to - Convert From/To |
| 484 |
* |
| 485 |
* @return string - Converted value |
| 486 |
*/ |
| 487 |
public function convert_shortcodes($value, $to = false) { |
| 488 |
if ($value) { |
| 489 |
if ($to) { |
| 490 |
$value = str_replace("[", "[", $value); |
| 491 |
//$value = str_replace("%", "%", $value); |
| 492 |
} else { |
| 493 |
$value = str_replace("[", "[", $value); |
| 494 |
//$value = str_replace("%", "%", $value); |
| 495 |
} |
| 496 |
} |
| 497 |
return $value; |
| 498 |
} |
| 499 |
|
| 500 |
/** |
| 501 |
* Verify if item and dataset exists |
| 502 |
* |
| 503 |
* @return bool - item and dataset exists |
| 504 |
*/ |
| 505 |
public function verify() { |
| 506 |
$item = $this->get('item'); |
| 507 |
$dataset = $this->get('dataset'); |
| 508 |
|
| 509 |
if ($item && $dataset && class_exists('Caldera_Forms_Entry') && class_exists('Caldera_Forms_Forms') && class_exists("Caldera_Forms")) { |
| 510 |
$form = Caldera_Forms_Forms::get_form($item); |
| 511 |
if ($form && isset($form['_last_updated'])) { |
| 512 |
$entry = new Caldera_Forms_Entry($form, $dataset); |
| 513 |
if ($entry->found()) { |
| 514 |
return true; |
| 515 |
} |
| 516 |
} |
| 517 |
} |
| 518 |
return false; |
| 519 |
} |
| 520 |
|
| 521 |
/** |
| 522 |
* Create Form based on uploaded PDF |
| 523 |
* |
| 524 |
* @param object $template - Template Object to work with |
| 525 |
* @param array $data - Settings to create labels/shortcodes |
| 526 |
* |
| 527 |
* @return object - Mapped Template Object |
| 528 |
*/ |
| 529 |
public function auto_form($template, $data = array()) { |
| 530 |
|
| 531 |
if ($template->get('ID')) { |
| 532 |
|
| 533 |
$auto_form_label = isset($data['auto_form_label']) && $data['auto_form_label'] ? $data['auto_form_label'] : false; |
| 534 |
$auto_form_shortcode = isset($data['auto_form_shortcode']) ? true : false; |
| 535 |
|
| 536 |
$form = array( |
| 537 |
'_last_updated' => date('r'), |
| 538 |
'success' => sprintf(__('Success. [e2pdf-download id="%s"]', 'e2pdf'), $template->get('ID')), |
| 539 |
'name' => $template->get('title'), |
| 540 |
'db_support' => '1', |
| 541 |
'hide_form' => '1', |
| 542 |
'form_ajax' => '1', |
| 543 |
'custom_callback' => '', |
| 544 |
'fields' => array(), |
| 545 |
'layout_grid' => array( |
| 546 |
'fields' => array(), |
| 547 |
'strucutre' => '' |
| 548 |
), |
| 549 |
'settings' => array( |
| 550 |
'responsive' => array( |
| 551 |
'break_point' => 'sm', |
| 552 |
), |
| 553 |
), |
| 554 |
); |
| 555 |
|
| 556 |
$pages = $template->get('pages'); |
| 557 |
$checkboxes = array(); |
| 558 |
$radios = array(); |
| 559 |
|
| 560 |
foreach ($pages as $page_key => $page) { |
| 561 |
if (isset($page['elements']) && !empty($page['elements'])) { |
| 562 |
foreach ($page['elements'] as $element_key => $element) { |
| 563 |
$type = false; |
| 564 |
$label = ''; |
| 565 |
if ($element['type'] == 'e2pdf-input' || $element['type'] == 'e2pdf-signature') { |
| 566 |
$type = 'text'; |
| 567 |
$label = __('Text', 'e2pdf'); |
| 568 |
} elseif ($element['type'] == 'e2pdf-textarea') { |
| 569 |
$type = 'paragraph'; |
| 570 |
$label = __('Textarea', 'e2pdf'); |
| 571 |
} elseif ($element['type'] == 'e2pdf-select') { |
| 572 |
$type = 'dropdown'; |
| 573 |
$label = __('Select', 'e2pdf'); |
| 574 |
$options = array(); |
| 575 |
$field_options = array(); |
| 576 |
|
| 577 |
if (isset($element['properties']['options'])) { |
| 578 |
$field_options = explode("\n", $element['properties']['options']); |
| 579 |
foreach ($field_options as $option) { |
| 580 |
$option_id = 'opt' . $this->random(); |
| 581 |
$options[$option_id] = array( |
| 582 |
'calc_value' => '', |
| 583 |
'label' => $option, |
| 584 |
'value' => $option |
| 585 |
); |
| 586 |
} |
| 587 |
} |
| 588 |
} elseif ($element['type'] == 'e2pdf-checkbox') { |
| 589 |
$field_key = array_search($element['name'], array_column($checkboxes, 'name')); |
| 590 |
if ($field_key !== false) { |
| 591 |
$option_id = 'opt' . $this->random(); |
| 592 |
$checkboxes[$field_key]['options'][$option_id] = array( |
| 593 |
'calc_value' => $element['properties']['option'], |
| 594 |
'label' => $element['properties']['option'], |
| 595 |
'value' => $element['properties']['option'], |
| 596 |
); |
| 597 |
$pages[$page_key]['elements'][$element_key]['value'] = '%element_' . $checkboxes[$field_key]['element_id'] . '%'; |
| 598 |
} else { |
| 599 |
$type = 'checkbox'; |
| 600 |
$label = __('Checkbox', 'e2pdf'); |
| 601 |
} |
| 602 |
} elseif ($element['type'] == 'e2pdf-radio') { |
| 603 |
if (isset($element['properties']['group']) && $element['properties']['group']) { |
| 604 |
$element['name'] = $element['properties']['group']; |
| 605 |
} else { |
| 606 |
$element['name'] = $element['element_id']; |
| 607 |
} |
| 608 |
|
| 609 |
$field_key = array_search($element['name'], array_column($radios, 'name')); |
| 610 |
if ($field_key !== false) { |
| 611 |
$option_id = 'opt' . $this->random(); |
| 612 |
$radios[$field_key]['options'][$option_id] = array( |
| 613 |
'calc_value' => $element['properties']['option'], |
| 614 |
'label' => $element['properties']['option'], |
| 615 |
'value' => $element['properties']['option'], |
| 616 |
); |
| 617 |
$pages[$page_key]['elements'][$element_key]['value'] = '%element_' . $radios[$field_key]['element_id'] . '%'; |
| 618 |
} else { |
| 619 |
$type = 'radio'; |
| 620 |
$label = __('Radio', 'e2pdf'); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
if ($type) { |
| 625 |
$labels = array(); |
| 626 |
if ($auto_form_shortcode) { |
| 627 |
$labels[] = '%element_' . $element['element_id'] . '%'; |
| 628 |
} |
| 629 |
|
| 630 |
if ($auto_form_label && $auto_form_label == 'value' && isset($element['value']) && $element['value']) { |
| 631 |
$labels[] = $element['value']; |
| 632 |
} elseif ($auto_form_label && $auto_form_label == 'name' && isset($element['name']) && $element['name']) { |
| 633 |
$labels[] = $element['name']; |
| 634 |
} |
| 635 |
|
| 636 |
if ($type == 'checkbox' || $type == 'radio') { |
| 637 |
$field_id = 'opt' . $this->random(); |
| 638 |
$field_data = array( |
| 639 |
'name' => $element['name'], |
| 640 |
'element_id' => $element['element_id'], |
| 641 |
'label' => !empty($labels) ? implode(' ', $labels) : $label, |
| 642 |
'options' => array( |
| 643 |
$field_id => array( |
| 644 |
'calc_value' => $element['properties']['option'], |
| 645 |
'label' => $element['properties']['option'], |
| 646 |
'value' => $element['properties']['option'], |
| 647 |
), |
| 648 |
) |
| 649 |
); |
| 650 |
if ($type == 'checkbox') { |
| 651 |
$checkboxes[] = $field_data; |
| 652 |
} else { |
| 653 |
$radios[] = $field_data; |
| 654 |
} |
| 655 |
} else { |
| 656 |
$field_id = 'fld_' . $this->random(); |
| 657 |
$form['fields'][$field_id] = array( |
| 658 |
'ID' => $field_id, |
| 659 |
'slug' => 'element_' . $element['element_id'], |
| 660 |
'type' => $type, |
| 661 |
'label' => !empty($labels) ? implode(' ', $labels) : $label |
| 662 |
); |
| 663 |
|
| 664 |
if ($type == 'select') { |
| 665 |
$form['fields'][$field_id]['config'] = array( |
| 666 |
'option' => $options |
| 667 |
); |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
$pages[$page_key]['elements'][$element_key]['value'] = '%element_' . $element['element_id'] . '%'; |
| 672 |
if (isset($element['properties']['esig'])) { |
| 673 |
unset($pages[$page_key]['elements'][$element_key]['properties']['esig']); |
| 674 |
} |
| 675 |
} |
| 676 |
} |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
foreach ($checkboxes as $element) { |
| 681 |
|
| 682 |
$field_id = 'fld_' . $this->random(); |
| 683 |
$form['fields'][$field_id] = array( |
| 684 |
'ID' => $field_id, |
| 685 |
'slug' => 'element_' . $element['element_id'], |
| 686 |
'type' => 'checkbox', |
| 687 |
'label' => $element['label'], |
| 688 |
'config' => array( |
| 689 |
'option' => $element['options'] |
| 690 |
) |
| 691 |
); |
| 692 |
} |
| 693 |
|
| 694 |
foreach ($radios as $element) { |
| 695 |
$field_id = 'fld_' . $this->random(); |
| 696 |
$form['fields'][$field_id] = array( |
| 697 |
'ID' => $field_id, |
| 698 |
'slug' => 'element_' . $element['element_id'], |
| 699 |
'type' => 'radio', |
| 700 |
'label' => $element['label'], |
| 701 |
'config' => array( |
| 702 |
'option' => $element['options'] |
| 703 |
) |
| 704 |
); |
| 705 |
} |
| 706 |
|
| 707 |
$field_id = 'fld_' . $this->random(); |
| 708 |
$form['fields'][$field_id] = array( |
| 709 |
'ID' => $field_id, |
| 710 |
'slug' => 'submit', |
| 711 |
'type' => 'button', |
| 712 |
'label' => __('Submit', 'e2pdf'), |
| 713 |
'class' => 'btn btn-default' |
| 714 |
); |
| 715 |
|
| 716 |
$fields = array_keys($form['fields']); |
| 717 |
$x = 1; |
| 718 |
foreach ($fields as $field) { |
| 719 |
$form['layout_grid']['fields'][$field] = $x . ':1'; |
| 720 |
if ($x == 1) { |
| 721 |
$form['layout_grid']['structure'] .= '12'; |
| 722 |
} else { |
| 723 |
$form['layout_grid']['structure'] .= '|12'; |
| 724 |
} |
| 725 |
$x++; |
| 726 |
} |
| 727 |
|
| 728 |
if ($item = Caldera_Forms_Forms::create_form($form)) { |
| 729 |
$template->set('item', $item['ID']); |
| 730 |
$template->set('pages', $pages); |
| 731 |
} |
| 732 |
} |
| 733 |
return $template; |
| 734 |
} |
| 735 |
|
| 736 |
/** |
| 737 |
* Generate Random number for Field ID |
| 738 |
* |
| 739 |
* @return int - Generated number |
| 740 |
*/ |
| 741 |
private function random() { |
| 742 |
return round((float) rand() / (float) getrandmax() * 10000000); |
| 743 |
} |
| 744 |
|
| 745 |
/** |
| 746 |
* Init Visual Mapper data |
| 747 |
* |
| 748 |
* @return bool|string - HTML data source for Visual Mapper |
| 749 |
*/ |
| 750 |
public function visual_mapper() { |
| 751 |
|
| 752 |
$item = $this->get('item'); |
| 753 |
$forms = array(); |
| 754 |
$source = ''; |
| 755 |
$html = ''; |
| 756 |
|
| 757 |
if ($item && class_exists("Caldera_Forms") && class_exists("Caldera_Forms_Forms") && class_exists("Caldera_Forms_Field_Util")) { |
| 758 |
|
| 759 |
$form = Caldera_Forms_Forms::get_form($item); |
| 760 |
if (!empty($form['is_connected_form']) && isset($form['node']) && !empty($form['node'])) { |
| 761 |
foreach ($form['node'] as $node) { |
| 762 |
if (isset($node['form']) && $node['form']) { |
| 763 |
$source .= Caldera_Forms::render_form($node['form']); |
| 764 |
$forms[] = Caldera_Forms_Forms::get_form($node['form']); |
| 765 |
} |
| 766 |
} |
| 767 |
} else { |
| 768 |
$source = Caldera_Forms::render_form($item); |
| 769 |
} |
| 770 |
|
| 771 |
if ($source) { |
| 772 |
libxml_use_internal_errors(true); |
| 773 |
$dom = new DOMDocument(); |
| 774 |
if (function_exists('mb_convert_encoding')) { |
| 775 |
$html = $dom->loadHTML(mb_convert_encoding($source, 'HTML-ENTITIES', 'UTF-8')); |
| 776 |
} else { |
| 777 |
$html = $dom->loadHTML('<?xml encoding="UTF-8">' . $source); |
| 778 |
} |
| 779 |
libxml_clear_errors(); |
| 780 |
} |
| 781 |
|
| 782 |
if (!$source) { |
| 783 |
return __('Form source is empty', 'e2pdf'); |
| 784 |
} elseif (!$html) { |
| 785 |
return __('Form could not be parsed due incorrect HTML', 'e2pdf'); |
| 786 |
} else { |
| 787 |
|
| 788 |
$xml = $this->helper->load('xml'); |
| 789 |
$xml->set('dom', $dom); |
| 790 |
$xpath = new DomXPath($dom); |
| 791 |
|
| 792 |
$remove_by_class = array( |
| 793 |
'live-gravatar', |
| 794 |
); |
| 795 |
foreach ($remove_by_class as $key => $class) { |
| 796 |
$elements = $xpath->query("//*[contains(@class, '{$class}')]"); |
| 797 |
foreach ($elements as $element) { |
| 798 |
$element->parentNode->removeChild($element); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
$remove_by_tag = array( |
| 803 |
'script' |
| 804 |
); |
| 805 |
foreach ($remove_by_tag as $key => $tag) { |
| 806 |
$elements = $xpath->query("//{$tag}"); |
| 807 |
foreach ($elements as $element) { |
| 808 |
$element->parentNode->removeChild($element); |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
// Replace names |
| 813 |
$fields = $xpath->query("//*[contains(@name, 'fld_')]"); |
| 814 |
foreach ($fields as $element) { |
| 815 |
// Retrieve field id |
| 816 |
if ($xml->get_node_value($element, 'type') == 'checkbox') { |
| 817 |
preg_match("/(.*?)\[(.*?)\]/i", $xml->get_node_value($element, 'name'), $matches); |
| 818 |
if (isset($matches[1])) { |
| 819 |
$field_id = $matches[1]; |
| 820 |
} |
| 821 |
} else { |
| 822 |
$field_id = $xml->get_node_value($element, 'name'); |
| 823 |
} |
| 824 |
|
| 825 |
// Modify Star field |
| 826 |
if ($xml->get_node_value($element, 'data-type') == 'star') { |
| 827 |
$element = $xml->set_node_value($element, 'style', ''); |
| 828 |
} |
| 829 |
|
| 830 |
// Modify Range field |
| 831 |
if ($xml->get_node_value($element, 'type') == 'range') { |
| 832 |
|
| 833 |
$element = $xml->set_node_value($element, 'style', '', true); |
| 834 |
$element = $xml->set_node_value($element, 'class', 'col-xs-12', true); |
| 835 |
|
| 836 |
$next_element = $xpath->query(".//following-sibling::div[1]", $element->parentNode)->item(0); |
| 837 |
$next_element->parentNode->removeChild($next_element); |
| 838 |
} |
| 839 |
|
| 840 |
if (!empty($form['is_connected_form'])) { |
| 841 |
foreach ($forms as $sub_form) { |
| 842 |
$field = Caldera_Forms_Field_Util::get_field($field_id, $sub_form); |
| 843 |
if ($field && isset($field['slug'])) { |
| 844 |
$element = $xml->set_node_value($element, 'name', '%' . $field['slug'] . '%'); |
| 845 |
break; |
| 846 |
} |
| 847 |
} |
| 848 |
} else { |
| 849 |
$field = Caldera_Forms_Field_Util::get_field($field_id, $form); |
| 850 |
if ($field && isset($field['slug'])) { |
| 851 |
$element = $xml->set_node_value($element, 'name', '%' . $field['slug'] . '%'); |
| 852 |
} |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
// Convert Calculation Fields |
| 857 |
$calculation_fields = $xpath->query("//input[@data-type='calculation']"); |
| 858 |
foreach ($calculation_fields as $element) { |
| 859 |
|
| 860 |
$element = $xml->set_node_value($element, 'type', 'text'); |
| 861 |
$element = $xml->set_node_value($element, 'class', $xml->get_node_value($element, 'class') . ' form-control'); |
| 862 |
|
| 863 |
$calculation_text = $xpath->query(".//h3", $element->parentNode)->item(0); |
| 864 |
$element->parentNode->removeChild($calculation_text); |
| 865 |
} |
| 866 |
|
| 867 |
// Modify File Upload |
| 868 |
$file_uploads = $xpath->query("//input[@type='file']"); |
| 869 |
foreach ($file_uploads as $element) { |
| 870 |
$element = $xml->set_node_value($element, 'class', $xml->get_node_value($element, 'type') . ' form-control'); |
| 871 |
} |
| 872 |
|
| 873 |
// Modify Toggle Fields |
| 874 |
$toggle_gorups = $xpath->query("//*[contains(@class, 'cf-toggle-switch')]"); |
| 875 |
foreach ($toggle_gorups as $element) { |
| 876 |
$toggle_elements = $xpath->query(".//*[contains(@class, 'cf-toggle-group-buttons')]//a", $element); |
| 877 |
foreach ($toggle_elements as $sub_element) { |
| 878 |
if ($sub_element->attributes->getNamedItem("id")) { |
| 879 |
$toggle_id = $xml->get_node_value($sub_element, "id"); |
| 880 |
$radio = $xpath->query(".//input[@id='{$toggle_id}']", $element)->item(0); |
| 881 |
if ($radio) { |
| 882 |
$radio_cloned = $radio->cloneNode(true); |
| 883 |
$radio_cloned = $xml->set_node_value($radio_cloned, 'style', 'position: absolute;width: 100%;top: 0;left: 0;height: 100%; opacity: 0;'); |
| 884 |
$sub_element->appendChild($radio_cloned); |
| 885 |
} |
| 886 |
} |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
// Modify Advanced File Upload |
| 891 |
$advanced_uploaders = $xpath->query("//*[contains(@class, 'cf-uploader-trigger')]"); |
| 892 |
foreach ($advanced_uploaders as $element) { |
| 893 |
$input = $xpath->query(".//input[@type='hidden']", $element->parentNode)->item(0); |
| 894 |
|
| 895 |
$attr = $dom->createAttribute('style'); |
| 896 |
$attr->value = 'position: relative;'; |
| 897 |
$element->appendChild($attr); |
| 898 |
|
| 899 |
if ($input) { |
| 900 |
$input_cloned = $input->cloneNode(true); |
| 901 |
$input_cloned = $xml->set_node_value($input_cloned, 'style', 'position: absolute;width: 100%;top: 0;left: 0;height: 100%; opacity: 0;'); |
| 902 |
$input_cloned = $xml->set_node_value($input_cloned, 'type', 'text'); |
| 903 |
$element->appendChild($input_cloned); |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
// Modify Text Area same size |
| 908 |
$textareas = $xpath->query("//textarea"); |
| 909 |
foreach ($textareas as $element) { |
| 910 |
$element = $xml->set_node_value($element, 'rows', '4'); |
| 911 |
} |
| 912 |
|
| 913 |
// Remove unecessary elements |
| 914 |
$submit_buttons = $xpath->query("//input[@type='submit']"); |
| 915 |
foreach ($submit_buttons as $element) { |
| 916 |
$element->parentNode->removeChild($element); |
| 917 |
} |
| 918 |
return $dom->saveHTML(); |
| 919 |
} |
| 920 |
} |
| 921 |
return false; |
| 922 |
} |
| 923 |
|
| 924 |
/** |
| 925 |
* Auto Generate of Template for this extension |
| 926 |
* |
| 927 |
* @return array - List of elements |
| 928 |
*/ |
| 929 |
public function auto() { |
| 930 |
|
| 931 |
$response = array(); |
| 932 |
$elements = array(); |
| 933 |
$fields = array(); |
| 934 |
$forms = array(); |
| 935 |
|
| 936 |
$item = $this->get('item'); |
| 937 |
|
| 938 |
if (class_exists("Caldera_Forms_Forms")) { |
| 939 |
$form = Caldera_Forms_Forms::get_form($item); |
| 940 |
if (!empty($form['is_connected_form']) && isset($form['node']) && !empty($form['node'])) { |
| 941 |
foreach ($form['node'] as $node) { |
| 942 |
if (isset($node['form']) && $node['form']) { |
| 943 |
$sub_form = Caldera_Forms_Forms::get_form($node['form']); |
| 944 |
$forms[] = $sub_form; |
| 945 |
if ($sub_form && isset($sub_form['fields']) && is_array($sub_form['fields'])) { |
| 946 |
$fields = array_merge($fields, $sub_form['fields']); |
| 947 |
} |
| 948 |
} |
| 949 |
} |
| 950 |
} else { |
| 951 |
$forms[] = $form; |
| 952 |
$fields = $form['fields']; |
| 953 |
} |
| 954 |
|
| 955 |
foreach ($forms as $sub_form) { |
| 956 |
if ($sub_form && isset($sub_form['layout_grid']['structure']) && isset($sub_form['layout_grid']['fields'])) { |
| 957 |
$struct = explode('|', $sub_form['layout_grid']['structure']); |
| 958 |
foreach ($struct as $row_num => $row) { |
| 959 |
|
| 960 |
$float = false; |
| 961 |
$columns = explode(':', $row); |
| 962 |
foreach ($columns as $column_num => $column) { |
| 963 |
$column_fields = array_keys($sub_form['layout_grid']['fields'], ( $row_num + 1) . ':' . ( $column_num + 1)); |
| 964 |
|
| 965 |
$width = 100 / (12 / $column) . "%"; |
| 966 |
|
| 967 |
foreach ($column_fields as $column_field_key) { |
| 968 |
if (isset($fields[$column_field_key])) { |
| 969 |
$field = $fields[$column_field_key]; |
| 970 |
|
| 971 |
switch ($field['type']) { |
| 972 |
case 'text': |
| 973 |
case 'email': |
| 974 |
case 'phone_better': |
| 975 |
case 'phone_better': |
| 976 |
case 'number': |
| 977 |
case 'phone': |
| 978 |
case 'url': |
| 979 |
case 'date_picker': |
| 980 |
case 'color_picker': |
| 981 |
case 'credit_card_number': |
| 982 |
case 'credit_card_exp': |
| 983 |
case 'credit_card_cvc': |
| 984 |
case 'calculation': |
| 985 |
case 'range_slider': |
| 986 |
case 'star_rating': |
| 987 |
case 'utm': |
| 988 |
case 'file': |
| 989 |
case 'advanced_file': |
| 990 |
$elements[] = $this->auto_field($field, array( |
| 991 |
'type' => 'e2pdf-html', |
| 992 |
'block' => true, |
| 993 |
'float' => $float, |
| 994 |
'properties' => array( |
| 995 |
'top' => '20', |
| 996 |
'left' => '20', |
| 997 |
'right' => '20', |
| 998 |
'width' => $width, |
| 999 |
'height' => 'auto', |
| 1000 |
'value' => $field['label'], |
| 1001 |
) |
| 1002 |
)); |
| 1003 |
|
| 1004 |
$elements[] = $this->auto_field($field, array( |
| 1005 |
'type' => 'e2pdf-input', |
| 1006 |
'properties' => array( |
| 1007 |
'top' => '5', |
| 1008 |
'width' => '100%', |
| 1009 |
'height' => 'auto', |
| 1010 |
'value' => "%{$field['slug']}%", |
| 1011 |
) |
| 1012 |
)); |
| 1013 |
break; |
| 1014 |
case 'paragraph': |
| 1015 |
$elements[] = $this->auto_field($field, array( |
| 1016 |
'type' => 'e2pdf-html', |
| 1017 |
'block' => true, |
| 1018 |
'float' => $float, |
| 1019 |
'properties' => array( |
| 1020 |
'top' => '20', |
| 1021 |
'left' => '20', |
| 1022 |
'right' => '20', |
| 1023 |
'width' => $width, |
| 1024 |
'height' => 'auto', |
| 1025 |
'value' => $field['label'], |
| 1026 |
) |
| 1027 |
)); |
| 1028 |
|
| 1029 |
$elements[] = $this->auto_field($field, array( |
| 1030 |
'type' => 'e2pdf-textarea', |
| 1031 |
'properties' => array( |
| 1032 |
'top' => '5', |
| 1033 |
'width' => '100%', |
| 1034 |
'height' => '150', |
| 1035 |
'value' => "%{$field['slug']}%", |
| 1036 |
) |
| 1037 |
)); |
| 1038 |
break; |
| 1039 |
case 'wysiwyg': |
| 1040 |
case 'html': |
| 1041 |
case 'summary': |
| 1042 |
$elements[] = $this->auto_field($field, array( |
| 1043 |
'type' => 'e2pdf-html', |
| 1044 |
'block' => true, |
| 1045 |
'float' => $float, |
| 1046 |
'properties' => array( |
| 1047 |
'top' => '20', |
| 1048 |
'left' => '20', |
| 1049 |
'right' => '20', |
| 1050 |
'width' => $width, |
| 1051 |
'height' => 'auto', |
| 1052 |
'value' => $field['label'], |
| 1053 |
) |
| 1054 |
)); |
| 1055 |
|
| 1056 |
$elements[] = $this->auto_field($field, array( |
| 1057 |
'type' => 'e2pdf-html', |
| 1058 |
'properties' => array( |
| 1059 |
'top' => '5', |
| 1060 |
'width' => '100%', |
| 1061 |
'height' => '150', |
| 1062 |
'value' => "%{$field['slug']}%", |
| 1063 |
) |
| 1064 |
)); |
| 1065 |
break; |
| 1066 |
case 'dropdown': |
| 1067 |
case 'filtered_select2': |
| 1068 |
case 'states': |
| 1069 |
$elements[] = $this->auto_field($field, array( |
| 1070 |
'type' => 'e2pdf-html', |
| 1071 |
'block' => true, |
| 1072 |
'float' => $float, |
| 1073 |
'properties' => array( |
| 1074 |
'top' => '20', |
| 1075 |
'left' => '20', |
| 1076 |
'right' => '20', |
| 1077 |
'width' => $width, |
| 1078 |
'height' => 'auto', |
| 1079 |
'value' => $field['label'], |
| 1080 |
) |
| 1081 |
)); |
| 1082 |
|
| 1083 |
$options_tmp = array(); |
| 1084 |
|
| 1085 |
if (isset($field['config']['option']) && is_array($field['config']['option'])) { |
| 1086 |
foreach ($field['config']['option'] as $opt_key => $option) { |
| 1087 |
if (is_array($option)) { |
| 1088 |
$options_tmp[] = $option['value']; |
| 1089 |
} |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
if ($field['type'] == 'states' && defined("CFCORE_PATH")) { |
| 1094 |
if (file_exists(CFCORE_PATH . "fields/states/field.php")) { |
| 1095 |
$contents = file_get_contents(CFCORE_PATH . "fields/states/field.php"); |
| 1096 |
preg_match_all('/<option value="(.*?)">(.*?)<\/option>/', $contents, $matches); |
| 1097 |
if (isset($matches[1])) { |
| 1098 |
foreach ($matches[1] as $state) { |
| 1099 |
$options_tmp[] = $state; |
| 1100 |
} |
| 1101 |
} |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
$elements[] = $this->auto_field($field, array( |
| 1106 |
'type' => 'e2pdf-select', |
| 1107 |
'properties' => array( |
| 1108 |
'top' => '5', |
| 1109 |
'width' => '100%', |
| 1110 |
'height' => 'auto', |
| 1111 |
'options' => implode("\n", $options_tmp), |
| 1112 |
'value' => "%{$field['slug']}%", |
| 1113 |
) |
| 1114 |
)); |
| 1115 |
break; |
| 1116 |
case 'checkbox': |
| 1117 |
$elements[] = $this->auto_field($field, array( |
| 1118 |
'type' => 'e2pdf-html', |
| 1119 |
'block' => true, |
| 1120 |
'float' => $float, |
| 1121 |
'properties' => array( |
| 1122 |
'top' => '20', |
| 1123 |
'left' => '20', |
| 1124 |
'right' => '20', |
| 1125 |
'width' => $width, |
| 1126 |
'height' => 'auto', |
| 1127 |
'value' => $field['label'], |
| 1128 |
) |
| 1129 |
)); |
| 1130 |
|
| 1131 |
if (isset($field['config']['option']) && is_array($field['config']['option'])) { |
| 1132 |
foreach ($field['config']['option'] as $opt_key => $option) { |
| 1133 |
if (is_array($option)) { |
| 1134 |
$elements[] = $this->auto_field($field, array( |
| 1135 |
'type' => 'e2pdf-checkbox', |
| 1136 |
'properties' => array( |
| 1137 |
'top' => '5', |
| 1138 |
'width' => 'auto', |
| 1139 |
'height' => 'auto', |
| 1140 |
'value' => "%{$field['slug']}%", |
| 1141 |
'option' => $option['label'] |
| 1142 |
) |
| 1143 |
)); |
| 1144 |
$elements[] = $this->auto_field($field, array( |
| 1145 |
'type' => 'e2pdf-html', |
| 1146 |
'float' => true, |
| 1147 |
'properties' => array( |
| 1148 |
'left' => '5', |
| 1149 |
'width' => '100%', |
| 1150 |
'height' => 'auto', |
| 1151 |
'value' => $option['label'] |
| 1152 |
) |
| 1153 |
)); |
| 1154 |
} |
| 1155 |
} |
| 1156 |
} |
| 1157 |
break; |
| 1158 |
case 'radio': |
| 1159 |
case 'toggle_switch': |
| 1160 |
$elements[] = $this->auto_field($field, array( |
| 1161 |
'type' => 'e2pdf-html', |
| 1162 |
'block' => true, |
| 1163 |
'float' => $float, |
| 1164 |
'properties' => array( |
| 1165 |
'top' => '20', |
| 1166 |
'left' => '20', |
| 1167 |
'right' => '20', |
| 1168 |
'width' => $width, |
| 1169 |
'height' => 'auto', |
| 1170 |
'value' => $field['label'], |
| 1171 |
) |
| 1172 |
)); |
| 1173 |
|
| 1174 |
if (isset($field['config']['option']) && is_array($field['config']['option'])) { |
| 1175 |
foreach ($field['config']['option'] as $opt_key => $option) { |
| 1176 |
if (is_array($option)) { |
| 1177 |
$elements[] = $this->auto_field($field, array( |
| 1178 |
'type' => 'e2pdf-radio', |
| 1179 |
'properties' => array( |
| 1180 |
'top' => '5', |
| 1181 |
'width' => 'auto', |
| 1182 |
'height' => 'auto', |
| 1183 |
'value' => "%{$field['slug']}%", |
| 1184 |
'option' => $option['label'], |
| 1185 |
'group' => "%{$field['slug']}%", |
| 1186 |
) |
| 1187 |
)); |
| 1188 |
$elements[] = $this->auto_field($field, array( |
| 1189 |
'type' => 'e2pdf-html', |
| 1190 |
'float' => true, |
| 1191 |
'properties' => array( |
| 1192 |
'left' => '5', |
| 1193 |
'width' => '100%', |
| 1194 |
'height' => 'auto', |
| 1195 |
'value' => $option['label'] |
| 1196 |
) |
| 1197 |
)); |
| 1198 |
} |
| 1199 |
} |
| 1200 |
} |
| 1201 |
break; |
| 1202 |
default: |
| 1203 |
/* |
| 1204 |
* Unsupported fields: section_break, live_gravatar, button |
| 1205 |
*/ |
| 1206 |
break; |
| 1207 |
} |
| 1208 |
} |
| 1209 |
} |
| 1210 |
|
| 1211 |
$float = true; |
| 1212 |
} |
| 1213 |
} |
| 1214 |
} |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
$response['page'] = array( |
| 1219 |
'bottom' => '20', |
| 1220 |
'top' => '20', |
| 1221 |
); |
| 1222 |
|
| 1223 |
$response['elements'] = $elements; |
| 1224 |
return $response; |
| 1225 |
} |
| 1226 |
|
| 1227 |
/** |
| 1228 |
* Element generation for Auto PDF |
| 1229 |
* |
| 1230 |
* @param array $field - Field options |
| 1231 |
* @param array $element - Element options |
| 1232 |
* |
| 1233 |
* @return array - Element with modified options |
| 1234 |
*/ |
| 1235 |
public function auto_field($field = false, $element = array()) { |
| 1236 |
|
| 1237 |
if (!$field) { |
| 1238 |
return false; |
| 1239 |
} |
| 1240 |
|
| 1241 |
if (!isset($element['block'])) { |
| 1242 |
$element['block'] = false; |
| 1243 |
} |
| 1244 |
|
| 1245 |
if (!isset($element['float'])) { |
| 1246 |
$element['float'] = false; |
| 1247 |
} |
| 1248 |
|
| 1249 |
return $element; |
| 1250 |
} |
| 1251 |
|
| 1252 |
/** |
| 1253 |
* Load actions for this extension |
| 1254 |
*/ |
| 1255 |
public function load_actions() { |
| 1256 |
add_action('caldera_forms_submit_complete', array($this, 'action_caldera_forms_submit_complete'), 99, 3); |
| 1257 |
add_action('caldera_forms_mailer_complete', array($this, 'action_caldera_forms_mailer_complete'), 99, 3); |
| 1258 |
} |
| 1259 |
|
| 1260 |
/** |
| 1261 |
* Delete attachments that were sent by email |
| 1262 |
*/ |
| 1263 |
public function action_caldera_forms_mailer_complete() { |
| 1264 |
|
| 1265 |
$files = $this->helper->get('caldera_attachments'); |
| 1266 |
|
| 1267 |
if (is_array($files) && !empty($files)) { |
| 1268 |
foreach ($files as $key => $file) { |
| 1269 |
$this->helper->delete_dir(dirname($file) . '/'); |
| 1270 |
} |
| 1271 |
$this->helper->deset('caldera_attachments'); |
| 1272 |
} |
| 1273 |
} |
| 1274 |
|
| 1275 |
/** |
| 1276 |
* Convert file and advanced_file field type to not rendered |
| 1277 |
* |
| 1278 |
* @param array $field - Field |
| 1279 |
* @param array $form - Current form |
| 1280 |
* |
| 1281 |
@return array - Updated field |
| 1282 |
*/ |
| 1283 |
public function filter_caldera_forms_render_get_field($field, $form) { |
| 1284 |
if (isset($field['type']) && ($field['type'] == 'file' || $field['type'] == 'advanced_file')) { |
| 1285 |
$field['config']['media_lib'] = false; |
| 1286 |
} |
| 1287 |
return $field; |
| 1288 |
} |
| 1289 |
|
| 1290 |
/** |
| 1291 |
* Fix for {entry_id} |
| 1292 |
* |
| 1293 |
* @param array $form - Current form |
| 1294 |
* @param int $entry_id - Current entry_id |
| 1295 |
* |
| 1296 |
* @return array - Updated form |
| 1297 |
*/ |
| 1298 |
public function filter_caldera_forms_magic_form($form, $entry_id) { |
| 1299 |
if (class_exists("Caldera_Forms") && $entry_id) { |
| 1300 |
Caldera_Forms::set_field_data('_entry_id', $entry_id, $form); |
| 1301 |
} |
| 1302 |
return $form; |
| 1303 |
} |
| 1304 |
|
| 1305 |
/** |
| 1306 |
* Render file and advanced_file field types to url instead image and link |
| 1307 |
* |
| 1308 |
* @param string $value - Text value |
| 1309 |
* @param array $matches - List of matches of magic tags |
| 1310 |
* @param int $entry_id - Current entry |
| 1311 |
* @param array $form - Current form |
| 1312 |
* |
| 1313 |
* @return string - Updated value |
| 1314 |
*/ |
| 1315 |
public function filter_caldera_forms_do_field_magic_value($value, $matches, $entry_id, $form) { |
| 1316 |
|
| 1317 |
if (class_exists('Caldera_Forms') && class_exists('Caldera_Forms_Field_Util')) { |
| 1318 |
if (isset($matches[1])) { |
| 1319 |
foreach ($matches[1] as $key => $tag) { |
| 1320 |
|
| 1321 |
$part_tags = explode(':', $tag); |
| 1322 |
if (!empty($part_tags[1])) { |
| 1323 |
$tag = $part_tags[0]; |
| 1324 |
} |
| 1325 |
$entry = Caldera_Forms::get_slug_data($tag, $form, $entry_id); |
| 1326 |
|
| 1327 |
$field = false; |
| 1328 |
if ($entry !== null) { |
| 1329 |
$field = Caldera_Forms_Field_Util::get_field_by_slug($tag, $form); |
| 1330 |
} |
| 1331 |
|
| 1332 |
if (Caldera_Forms_Field_Util::is_file_field($field, $form)) { |
| 1333 |
if (filter_var($entry, FILTER_VALIDATE_URL)) { |
| 1334 |
$value = $entry; |
| 1335 |
} |
| 1336 |
continue; |
| 1337 |
} |
| 1338 |
$value = str_replace($matches[0][$key], $entry, $value); |
| 1339 |
} |
| 1340 |
} |
| 1341 |
} |
| 1342 |
|
| 1343 |
return $value; |
| 1344 |
} |
| 1345 |
|
| 1346 |
/** |
| 1347 |
* [e2pdf-view] shortcode fix |
| 1348 |
* |
| 1349 |
* @param array $out - Array with response |
| 1350 |
* @param array $form - Current form |
| 1351 |
* |
| 1352 |
* @return array - Updated response |
| 1353 |
*/ |
| 1354 |
public function filter_caldera_forms_ajax_return($out, $form) { |
| 1355 |
|
| 1356 |
if (isset($out['html'])) { |
| 1357 |
|
| 1358 |
if (false === strpos($out['html'], '[')) { |
| 1359 |
return $out; |
| 1360 |
} |
| 1361 |
|
| 1362 |
$shortcode_tags = array( |
| 1363 |
'e2pdf-view' |
| 1364 |
); |
| 1365 |
|
| 1366 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $form['success'], $matches); |
| 1367 |
|
| 1368 |
$tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 1369 |
|
| 1370 |
if (!empty($tagnames)) { |
| 1371 |
|
| 1372 |
$pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames); |
| 1373 |
|
| 1374 |
preg_match_all("/$pattern/", $out['html'], $shortcodes); |
| 1375 |
foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 1376 |
|
| 1377 |
$shortcode = array(); |
| 1378 |
$shortcode[1] = $shortcodes[1][$key]; |
| 1379 |
$shortcode[2] = $shortcodes[2][$key]; |
| 1380 |
$shortcode[3] = $shortcodes[3][$key]; |
| 1381 |
$shortcode[4] = $shortcodes[4][$key]; |
| 1382 |
$shortcode[5] = $shortcodes[5][$key]; |
| 1383 |
$shortcode[6] = $shortcodes[6][$key]; |
| 1384 |
|
| 1385 |
$out['html'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $out['html']); |
| 1386 |
} |
| 1387 |
} |
| 1388 |
} |
| 1389 |
|
| 1390 |
return $out; |
| 1391 |
} |
| 1392 |
|
| 1393 |
/** |
| 1394 |
* Render success message after Caldera Form submission action |
| 1395 |
* |
| 1396 |
* @param array $form - Form data |
| 1397 |
* @param array $referrer - Referrer for submitted data |
| 1398 |
* @param int $process_id - ID of process |
| 1399 |
* |
| 1400 |
* @return array - Modified form data |
| 1401 |
*/ |
| 1402 |
public function action_caldera_forms_submit_complete($form, $referrer, $process_id) { |
| 1403 |
global $form, $transdata; |
| 1404 |
|
| 1405 |
if (isset($form['success'])) { |
| 1406 |
if (false === strpos($form['success'], '[')) { |
| 1407 |
return; |
| 1408 |
} |
| 1409 |
|
| 1410 |
$shortcode_tags = array( |
| 1411 |
'e2pdf-download', |
| 1412 |
'e2pdf-save', |
| 1413 |
'e2pdf-view', |
| 1414 |
'e2pdf-adobesign' |
| 1415 |
); |
| 1416 |
|
| 1417 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $form['success'], $matches); |
| 1418 |
|
| 1419 |
$tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 1420 |
|
| 1421 |
if (!empty($tagnames)) { |
| 1422 |
|
| 1423 |
$pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames); |
| 1424 |
|
| 1425 |
preg_match_all("/$pattern/", $form['success'], $shortcodes); |
| 1426 |
foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 1427 |
|
| 1428 |
$shortcode = array(); |
| 1429 |
$shortcode[1] = $shortcodes[1][$key]; |
| 1430 |
$shortcode[2] = $shortcodes[2][$key]; |
| 1431 |
$shortcode[3] = $shortcodes[3][$key]; |
| 1432 |
$shortcode[4] = $shortcodes[4][$key]; |
| 1433 |
$shortcode[5] = $shortcodes[5][$key]; |
| 1434 |
$shortcode[6] = $shortcodes[6][$key]; |
| 1435 |
|
| 1436 |
$atts = shortcode_parse_atts($shortcode[3]); |
| 1437 |
|
| 1438 |
if (($shortcode[2] === 'e2pdf-save' && isset($atts['attachment']) && $atts['attachment'] == 'true') || $shortcode[2] === 'e2pdf-attachment') { |
| 1439 |
|
| 1440 |
} else { |
| 1441 |
|
| 1442 |
if (!isset($atts['dataset']) && isset($atts['id'])) { |
| 1443 |
$template = new Model_E2pdf_Template(); |
| 1444 |
$template->load($atts['id']); |
| 1445 |
if ($template->get('extension') === 'caldera') { |
| 1446 |
$dataset_id = isset($transdata['entry_id']) ? $transdata['entry_id'] : false; |
| 1447 |
if ($dataset_id) { |
| 1448 |
$atts['dataset'] = $dataset_id; |
| 1449 |
$shortcode[3] .= " dataset=\"{$dataset_id}\""; |
| 1450 |
} |
| 1451 |
} |
| 1452 |
} |
| 1453 |
|
| 1454 |
if (!isset($atts['apply'])) { |
| 1455 |
$shortcode[3] .= " apply=\"true\""; |
| 1456 |
} |
| 1457 |
|
| 1458 |
if ($shortcode[2] === 'e2pdf-view') { |
| 1459 |
$new_shortcode = $shortcode[2] . $shortcode[3]; |
| 1460 |
$form['success'] = str_replace($shortcode_value, "[{$new_shortcode}]", $form['success']); |
| 1461 |
} else { |
| 1462 |
$form['success'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $form['success']); |
| 1463 |
} |
| 1464 |
} |
| 1465 |
} |
| 1466 |
} |
| 1467 |
} |
| 1468 |
} |
| 1469 |
|
| 1470 |
/** |
| 1471 |
* Load filters for this extension |
| 1472 |
*/ |
| 1473 |
public function load_filters() { |
| 1474 |
add_filter('caldera_forms_mailer', array($this, 'filter_caldera_forms_mailer'), 30, 3); |
| 1475 |
add_filter('caldera_forms_ajax_return', array($this, 'filter_caldera_forms_ajax_return'), 30, 2); |
| 1476 |
} |
| 1477 |
|
| 1478 |
/** |
| 1479 |
* Filter to replace mail message E2Pdf shortcodes |
| 1480 |
*/ |
| 1481 |
public function filter_caldera_forms_mailer($mail, $data, $form) { |
| 1482 |
|
| 1483 |
if ($mail && isset($mail['message'])) { |
| 1484 |
if (false !== strpos($mail['message'], '[')) { |
| 1485 |
$shortcode_tags = array( |
| 1486 |
'e2pdf-download', |
| 1487 |
'e2pdf-save', |
| 1488 |
'e2pdf-attachment', |
| 1489 |
'e2pdf-adobesign' |
| 1490 |
); |
| 1491 |
|
| 1492 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $mail['message'], $matches); |
| 1493 |
$tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 1494 |
|
| 1495 |
if (!empty($tagnames)) { |
| 1496 |
|
| 1497 |
$pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames); |
| 1498 |
|
| 1499 |
preg_match_all("/$pattern/", $mail['message'], $shortcodes); |
| 1500 |
|
| 1501 |
foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 1502 |
$shortcode = array(); |
| 1503 |
$shortcode[1] = $shortcodes[1][$key]; |
| 1504 |
$shortcode[2] = $shortcodes[2][$key]; |
| 1505 |
$shortcode[3] = $shortcodes[3][$key]; |
| 1506 |
$shortcode[4] = $shortcodes[4][$key]; |
| 1507 |
$shortcode[5] = $shortcodes[5][$key]; |
| 1508 |
$shortcode[6] = $shortcodes[6][$key]; |
| 1509 |
|
| 1510 |
$atts = shortcode_parse_atts($shortcode[3]); |
| 1511 |
|
| 1512 |
$file = false; |
| 1513 |
|
| 1514 |
if (!isset($atts['dataset']) && isset($atts['id'])) { |
| 1515 |
$template = new Model_E2pdf_Template(); |
| 1516 |
$template->load($atts['id']); |
| 1517 |
if ($template->get('extension') === 'caldera') { |
| 1518 |
$dataset_id = isset($data['_entry_id']) ? $data['_entry_id'] : false; |
| 1519 |
if ($dataset_id) { |
| 1520 |
$atts['dataset'] = $dataset_id; |
| 1521 |
$shortcode[3] .= " dataset=\"{$dataset_id}\""; |
| 1522 |
} |
| 1523 |
} |
| 1524 |
} |
| 1525 |
|
| 1526 |
if (!isset($atts['apply'])) { |
| 1527 |
$shortcode[3] .= " apply=\"true\""; |
| 1528 |
} |
| 1529 |
|
| 1530 |
if (!isset($atts['filter'])) { |
| 1531 |
$shortcode[3] .= " filter=\"true\""; |
| 1532 |
} |
| 1533 |
|
| 1534 |
if (($shortcode[2] === 'e2pdf-save' && isset($atts['attachment']) && $atts['attachment'] == 'true') || $shortcode[2] === 'e2pdf-attachment') { |
| 1535 |
$file = do_shortcode_tag($shortcode); |
| 1536 |
if ($file) { |
| 1537 |
if ($shortcode[2] != 'e2pdf-save' && !isset($atts['pdf'])) { |
| 1538 |
$this->helper->add('caldera_attachments', $file); |
| 1539 |
} |
| 1540 |
$mail['attachments'][] = $file; |
| 1541 |
} |
| 1542 |
$mail['message'] = str_replace($shortcode_value, '', $mail['message']); |
| 1543 |
} else { |
| 1544 |
$mail['message'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $mail['message']); |
| 1545 |
} |
| 1546 |
} |
| 1547 |
} |
| 1548 |
} |
| 1549 |
} |
| 1550 |
|
| 1551 |
return $mail; |
| 1552 |
} |
| 1553 |
|
| 1554 |
/** |
| 1555 |
* Get styles for generating Map Field function |
| 1556 |
* |
| 1557 |
* @return array - List of css files to load |
| 1558 |
*/ |
| 1559 |
public function styles() { |
| 1560 |
|
| 1561 |
$styles = array(); |
| 1562 |
if (class_exists('Caldera_Forms_Render_Assets')) { |
| 1563 |
$url = Caldera_Forms_Render_Assets::make_url('caldera-forms-front', false); |
| 1564 |
$styles[] = $url; |
| 1565 |
} |
| 1566 |
|
| 1567 |
return $styles; |
| 1568 |
} |
| 1569 |
|
| 1570 |
} |
| 1571 |
|