| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* E2pdf Templates Controller |
| 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 Controller_E2pdf_Templates extends Helper_E2pdf_View { |
| 17 |
|
| 18 |
/** |
| 19 |
* @url admin.php?page=e2pdf-templates |
| 20 |
*/ |
| 21 |
public function index_action() { |
| 22 |
|
| 23 |
if ($this->post->get()) { |
| 24 |
if ($this->post->get('screenoptionnonce')) { |
| 25 |
$this->screen_action(); |
| 26 |
} else { |
| 27 |
$this->check_nonce($this->post->get('_nonce'), 'e2pdf_post'); |
| 28 |
if (($this->post->get('action') == '-1' && $this->post->get('action2') == '-1')) { |
| 29 |
if ($this->post->get('s')) { |
| 30 |
$location = $this->helper->get_url( |
| 31 |
array( |
| 32 |
'page' => 'e2pdf-templates', |
| 33 |
'status' => $this->get->get('status'), |
| 34 |
'orderby' => $this->get->get('orderby'), |
| 35 |
'order' => $this->get->get('order'), |
| 36 |
's' => $this->post->get('s') |
| 37 |
) |
| 38 |
); |
| 39 |
} else { |
| 40 |
$location = $this->helper->get_url( |
| 41 |
array( |
| 42 |
'page' => 'e2pdf-templates', |
| 43 |
'status' => $this->get->get('status'), |
| 44 |
'orderby' => $this->get->get('orderby'), |
| 45 |
'order' => $this->get->get('order'), |
| 46 |
) |
| 47 |
); |
| 48 |
} |
| 49 |
$this->redirect($location); |
| 50 |
} else { |
| 51 |
$action = $this->post->get('action') != '-1' ? $this->post->get('action') : $this->post->get('action2'); |
| 52 |
if ($this->post->get('post')) { |
| 53 |
if ($action == 'trash') { |
| 54 |
foreach ($this->post->get('post') as $key => $value) { |
| 55 |
$this->trash_template($value); |
| 56 |
} |
| 57 |
$this->add_notification('update', __('Templates moved to trash', 'e2pdf')); |
| 58 |
} elseif ($action == 'restore') { |
| 59 |
foreach ($this->post->get('post') as $key => $value) { |
| 60 |
$this->restore_template($value); |
| 61 |
} |
| 62 |
$this->add_notification('update', __('Templates restored', 'e2pdf')); |
| 63 |
} elseif ($action == 'delete') { |
| 64 |
foreach ($this->post->get('post') as $key => $value) { |
| 65 |
$this->delete_template($value); |
| 66 |
} |
| 67 |
$this->add_notification('update', __('Templates deleted', 'e2pdf')); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @url admin.php?page=e2pdf-templates&action=view&id=$id |
| 77 |
* |
| 78 |
* @return file |
| 79 |
*/ |
| 80 |
public function view_action() { |
| 81 |
|
| 82 |
if ($this->get->get('id')) { |
| 83 |
$template = new Model_E2pdf_Template(); |
| 84 |
|
| 85 |
if ($template->load($this->get->get('id'))) { |
| 86 |
$pages = $template->get('pages'); |
| 87 |
foreach ($pages as $page_key => $page_value) { |
| 88 |
if (isset($page_value['elements'])) { |
| 89 |
foreach ($page_value['elements'] as $element_key => $element_value) { |
| 90 |
if (isset($element_value['type']) && $element_value['type'] == 'e2pdf-image') { |
| 91 |
if (!$this->helper->load('image')->get_image($element_value['value'])) { |
| 92 |
$pages[$page_key]['elements'][$element_key]['value'] = plugins_url('', $this->helper->get('plugin_file_path')) . "/img/upload.png"; |
| 93 |
|
| 94 |
if (isset($pages[$page_key]['elements'][$element_key]['properties']['dimension'])) { |
| 95 |
unset($pages[$page_key]['elements'][$element_key]['properties']['dimension']); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
$template->set('pages', $pages); |
| 104 |
$request = $template->render(); |
| 105 |
|
| 106 |
if (isset($request['error'])) { |
| 107 |
$this->add_notification('error', $request['error']); |
| 108 |
$this->render('blocks', 'notifications'); |
| 109 |
} else { |
| 110 |
$filename = $template->get_filename(); |
| 111 |
$file = $request['file']; |
| 112 |
$this->download_response('pdf', $file, $filename); |
| 113 |
} |
| 114 |
} else { |
| 115 |
$this->add_notification('error', __("Template can't be loaded", 'e2pdf')); |
| 116 |
$this->render('blocks', 'notifications'); |
| 117 |
} |
| 118 |
} else { |
| 119 |
$location = $this->helper->get_url( |
| 120 |
array( |
| 121 |
'page' => 'e2pdf', |
| 122 |
) |
| 123 |
); |
| 124 |
$this->redirect($location); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @url admin.php?page=e2pdf-templates&action=preview |
| 130 |
*/ |
| 131 |
public function preview_action() { |
| 132 |
|
| 133 |
$preview = array(); |
| 134 |
|
| 135 |
if ($this->post->get('preview')) { |
| 136 |
$preview = json_decode(stripslashes($this->post->get('preview')), true); |
| 137 |
} |
| 138 |
|
| 139 |
if (empty($preview)) { |
| 140 |
$this->close_tab_response(); |
| 141 |
} else { |
| 142 |
$data = $preview; |
| 143 |
$pages = $preview['pages']; |
| 144 |
unset($preview['pages']); |
| 145 |
|
| 146 |
$this->check_nonce($preview['_nonce'], 'e2pdf_post'); |
| 147 |
|
| 148 |
$template = new Model_E2pdf_Template(); |
| 149 |
foreach ($data as $key => $value) { |
| 150 |
$template->set($key, $value); |
| 151 |
} |
| 152 |
|
| 153 |
$preview_pages = array(); |
| 154 |
foreach ($pages as $page_key => $page_value) { |
| 155 |
$page = new Model_E2pdf_Page(); |
| 156 |
foreach ($page_value as $page_value_key => $page_value_value) { |
| 157 |
$page->set($page_value_key, $page_value_value); |
| 158 |
} |
| 159 |
$page->set('page_id', $page_key); |
| 160 |
|
| 161 |
$elements = array(); |
| 162 |
|
| 163 |
$this->helper->load('sort')->uasort($page_value['elements'], "sort_by_elementid"); |
| 164 |
$this->helper->load('sort')->stable_uasort($page_value['elements'], "sort_by_zindex"); |
| 165 |
|
| 166 |
foreach ($page_value['elements'] as $element_key => $element_value) { |
| 167 |
$element = new Model_E2pdf_Element(); |
| 168 |
$esig = isset($element_value['properties']['esig']) && $element_value['properties']['esig'] ? true : false; |
| 169 |
if (isset($element_value['type']) && ($element_value['type'] == 'e2pdf-image' || ($element_value['type'] == 'e2pdf-signature' && !$esig))) { |
| 170 |
if (!$this->helper->load('image')->get_image($element_value['value'])) { |
| 171 |
$element_value['value'] = plugins_url('', $this->helper->get('plugin_file_path')) . "/img/upload.png"; |
| 172 |
if (isset($element_value['properties']['dimension'])) { |
| 173 |
unset($element_value['properties']['dimension']); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
foreach ($element_value as $element_value_key => $element_value_value) { |
| 179 |
$element->set($element_value_key, $element_value_value); |
| 180 |
} |
| 181 |
|
| 182 |
if (!$element_value['properties']) { |
| 183 |
$element_properties = array(); |
| 184 |
} else { |
| 185 |
$element_properties = $element_value['properties']; |
| 186 |
} |
| 187 |
|
| 188 |
$element->set('properties', $element_properties); |
| 189 |
$elements[] = $element->get_element(); |
| 190 |
} |
| 191 |
|
| 192 |
$page->set('elements', $elements); |
| 193 |
$preview_pages[] = $page->get_page(); |
| 194 |
} |
| 195 |
|
| 196 |
$template->set('pages', $preview_pages); |
| 197 |
$request = $template->render(true); |
| 198 |
|
| 199 |
if (isset($request['error'])) { |
| 200 |
$this->add_notification('error', $request['error']); |
| 201 |
$this->render('blocks', 'notifications'); |
| 202 |
} else { |
| 203 |
$filename = $template->get_filename(); |
| 204 |
$file = $request['file']; |
| 205 |
$this->download_response('pdf', $file, $filename); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* @url admin.php?page=e2pdf-templates&action=convert&type=php |
| 212 |
*/ |
| 213 |
public function convert_action() { |
| 214 |
$preview = array(); |
| 215 |
|
| 216 |
if ($this->post->get('preview')) { |
| 217 |
$preview = json_decode(stripslashes($this->post->get('preview')), true); |
| 218 |
} |
| 219 |
|
| 220 |
if (empty($preview)) { |
| 221 |
$this->close_tab_response(); |
| 222 |
} else { |
| 223 |
$data = $preview; |
| 224 |
$pages = $preview['pages']; |
| 225 |
unset($preview['pages']); |
| 226 |
|
| 227 |
$this->check_nonce($preview['_nonce'], 'e2pdf_post'); |
| 228 |
|
| 229 |
$template = new Model_E2pdf_Template(); |
| 230 |
foreach ($data as $key => $value) { |
| 231 |
$template->set($key, $value); |
| 232 |
} |
| 233 |
|
| 234 |
$preview_pages = array(); |
| 235 |
foreach ($pages as $page_key => $page_value) { |
| 236 |
$page = new Model_E2pdf_Page(); |
| 237 |
foreach ($page_value as $page_value_key => $page_value_value) { |
| 238 |
$page->set($page_value_key, $page_value_value); |
| 239 |
} |
| 240 |
$page->set('page_id', $page_key); |
| 241 |
|
| 242 |
$elements = array(); |
| 243 |
|
| 244 |
$this->helper->load('sort')->uasort($page_value['elements'], "sort_by_elementid"); |
| 245 |
$this->helper->load('sort')->stable_uasort($page_value['elements'], "sort_by_zindex"); |
| 246 |
|
| 247 |
foreach ($page_value['elements'] as $element_key => $element_value) { |
| 248 |
$element = new Model_E2pdf_Element(); |
| 249 |
$esig = isset($element_value['properties']['esig']) && $element_value['properties']['esig'] ? true : false; |
| 250 |
if (isset($element_value['type']) && ($element_value['type'] == 'e2pdf-image' || ($element_value['type'] == 'e2pdf-signature' && !$esig))) { |
| 251 |
if (!$this->helper->load('image')->get_image($element_value['value'])) { |
| 252 |
$element_value['value'] = plugins_url('', $this->helper->get('plugin_file_path')) . "/img/upload.png"; |
| 253 |
if (isset($element_value['properties']['dimension'])) { |
| 254 |
unset($element_value['properties']['dimension']); |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
foreach ($element_value as $element_value_key => $element_value_value) { |
| 260 |
$element->set($element_value_key, $element_value_value); |
| 261 |
} |
| 262 |
|
| 263 |
if (!$element_value['properties']) { |
| 264 |
$element_properties = array(); |
| 265 |
} else { |
| 266 |
$element_properties = $element_value['properties']; |
| 267 |
} |
| 268 |
|
| 269 |
$element->set('properties', $element_properties); |
| 270 |
$elements[] = $element->get_element(); |
| 271 |
} |
| 272 |
|
| 273 |
$page->set('elements', $elements); |
| 274 |
$preview_pages[] = $page->get_page(); |
| 275 |
} |
| 276 |
|
| 277 |
$template->set('pages', $preview_pages); |
| 278 |
|
| 279 |
$allowed_types = array( |
| 280 |
'php' |
| 281 |
); |
| 282 |
|
| 283 |
if (!in_array($this->get->get('type'), $allowed_types)) { |
| 284 |
$type = 'php'; |
| 285 |
} else { |
| 286 |
$type = $this->get->get('type'); |
| 287 |
} |
| 288 |
|
| 289 |
$request = $template->convert(true, $type); |
| 290 |
|
| 291 |
if (isset($request['error'])) { |
| 292 |
$this->add_notification('error', $request['error']); |
| 293 |
$this->render('blocks', 'notifications'); |
| 294 |
} else { |
| 295 |
$filename = 'template'; |
| 296 |
$file = $request['file']; |
| 297 |
$this->download_response($type, $file, $filename); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* @url admin.php?page=e2pdf-templates&action=create |
| 304 |
*/ |
| 305 |
public function create_action() { |
| 306 |
|
| 307 |
$template = new Model_E2pdf_Template(); |
| 308 |
$this->load_metaboxes(); |
| 309 |
$this->load_scripts(); |
| 310 |
$this->load_styles(); |
| 311 |
|
| 312 |
$this->view('license', $this->helper->get('license')); |
| 313 |
$this->view('template', $template); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* @url admin.php?page=e2pdf-templates&action=edit&id=$id |
| 318 |
*/ |
| 319 |
public function edit_action() { |
| 320 |
if ($this->get->get('id')) { |
| 321 |
$template = new Model_E2pdf_Template(); |
| 322 |
if ($template->load($this->get->get('id'))) { |
| 323 |
$this->view('license', $this->helper->get('license')); |
| 324 |
$this->view('template', $template); |
| 325 |
$this->load_metaboxes(); |
| 326 |
$this->load_scripts(); |
| 327 |
$this->load_styles($template->get('extension')); |
| 328 |
} else { |
| 329 |
$this->add_notification('error', __("Template can't be loaded", 'e2pdf')); |
| 330 |
$this->render('blocks', 'notifications'); |
| 331 |
} |
| 332 |
} else { |
| 333 |
$location = $this->helper->get_url( |
| 334 |
array( |
| 335 |
'page' => 'e2pdf-templates', |
| 336 |
) |
| 337 |
); |
| 338 |
$this->redirect($location); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* @url admin.php?page=e2pdf-templates&action=trash&id=$id |
| 344 |
*/ |
| 345 |
public function trash_action() { |
| 346 |
|
| 347 |
if ($this->get->get('id')) { |
| 348 |
$this->trash_template($this->get->get('id')); |
| 349 |
$this->add_notification('update', __('Template moved to trash', 'e2pdf')); |
| 350 |
} |
| 351 |
$location = $this->helper->get_url( |
| 352 |
array( |
| 353 |
'page' => 'e2pdf-templates', |
| 354 |
'status' => $this->get->get('status'), |
| 355 |
'orderby' => $this->get->get('orderby'), |
| 356 |
'order' => $this->get->get('order'), |
| 357 |
's' => $this->get->get('s'), |
| 358 |
) |
| 359 |
); |
| 360 |
$this->redirect($location); |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* @url admin.php?page=e2pdf-templates&action=restore&id=$id&status=$status |
| 365 |
*/ |
| 366 |
public function restore_action() { |
| 367 |
|
| 368 |
if ($this->get->get('id')) { |
| 369 |
$this->restore_template($this->get->get('id')); |
| 370 |
$this->add_notification('update', __('Template restored', 'e2pdf')); |
| 371 |
} |
| 372 |
|
| 373 |
$location = $this->helper->get_url( |
| 374 |
array( |
| 375 |
'page' => 'e2pdf-templates', |
| 376 |
'status' => $this->get->get('status'), |
| 377 |
'orderby' => $this->get->get('orderby'), |
| 378 |
'order' => $this->get->get('order'), |
| 379 |
's' => $this->get->get('s'), |
| 380 |
) |
| 381 |
); |
| 382 |
$this->redirect($location); |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* @url admin.php?page=e2pdf-templates&action=delete&id=$id&status=$status |
| 387 |
*/ |
| 388 |
public function delete_action() { |
| 389 |
|
| 390 |
if ($this->get->get('id')) { |
| 391 |
$this->delete_template($this->get->get('id')); |
| 392 |
$this->add_notification('update', __('Template deleted', 'e2pdf')); |
| 393 |
} |
| 394 |
$location = $this->helper->get_url( |
| 395 |
array( |
| 396 |
'page' => 'e2pdf-templates', |
| 397 |
'status' => $this->get->get('status'), |
| 398 |
'orderby' => $this->get->get('orderby'), |
| 399 |
'order' => $this->get->get('order'), |
| 400 |
's' => $this->get->get('s'), |
| 401 |
) |
| 402 |
); |
| 403 |
$this->redirect($location); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* @url admin.php?page=e2pdf-templates&action=duplicate&id=$id |
| 408 |
*/ |
| 409 |
public function duplicate_action() { |
| 410 |
|
| 411 |
if ($this->get->get('id')) { |
| 412 |
$this->duplicate_template($this->get->get('id')); |
| 413 |
$this->add_notification('update', __('Template copy created', 'e2pdf')); |
| 414 |
} |
| 415 |
$location = $this->helper->get_url( |
| 416 |
array( |
| 417 |
'page' => 'e2pdf-templates', |
| 418 |
'status' => $this->get->get('status'), |
| 419 |
'orderby' => $this->get->get('orderby'), |
| 420 |
'order' => $this->get->get('order'), |
| 421 |
's' => $this->get->get('s'), |
| 422 |
) |
| 423 |
); |
| 424 |
$this->redirect($location); |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* @url admin.php?page=e2pdf-templates&action=import |
| 429 |
*/ |
| 430 |
public function import_action() { |
| 431 |
|
| 432 |
if ($this->post->get()) { |
| 433 |
|
| 434 |
$this->check_nonce($this->post->get('_nonce'), 'e2pdf_post'); |
| 435 |
$errors = array(); |
| 436 |
$allowed_ext = array( |
| 437 |
'xml' |
| 438 |
); |
| 439 |
|
| 440 |
$import = $this->files->get('template'); |
| 441 |
$name = $import['name']; |
| 442 |
$tmp = $import['tmp_name']; |
| 443 |
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION)); |
| 444 |
|
| 445 |
if (!$tmp) { |
| 446 |
$this->add_notification('error', __('Please choose template to upload', 'e2pdf')); |
| 447 |
} else if ($import['error']) { |
| 448 |
$this->add_notification('error', $import['error']); |
| 449 |
} elseif (!in_array($ext, $allowed_ext)) { |
| 450 |
$this->add_notification('error', __('Incorrect file extension', 'e2pdf')); |
| 451 |
} elseif ($import['type'] != 'text/xml') { |
| 452 |
$this->add_notification('error', __('Incompatible type', 'e2pdf')); |
| 453 |
} else { |
| 454 |
|
| 455 |
$options = $this->post->get('options'); |
| 456 |
|
| 457 |
$xml = simplexml_load_file($import['tmp_name'], "SimpleXMLElement", LIBXML_PARSEHUGE); |
| 458 |
|
| 459 |
$pages = stripslashes_deep(unserialize(base64_decode((String) $xml->template->pages))); |
| 460 |
|
| 461 |
foreach ($pages as $page_key => $page) { |
| 462 |
if (isset($page['elements']) && !empty($page['elements'])) { |
| 463 |
foreach ($page['elements'] as $element_key => $element) { |
| 464 |
if ($element['type'] === 'e2pdf-image' && isset($element['base64']) && $element['base64']) { |
| 465 |
if ($options['images']) { |
| 466 |
$image = base64_decode($element['base64']); |
| 467 |
$file_name = basename($element['value']); |
| 468 |
$upload_file = wp_upload_bits($file_name, null, $image); |
| 469 |
if (!$upload_file['error']) { |
| 470 |
$wp_filetype = wp_check_filetype($file_name, null); |
| 471 |
$attachment = array( |
| 472 |
'post_mime_type' => $wp_filetype['type'], |
| 473 |
'post_parent' => 0, |
| 474 |
'post_title' => preg_replace('/\.[^.]+$/', '', $file_name), |
| 475 |
'post_content' => '', |
| 476 |
'post_status' => 'inherit' |
| 477 |
); |
| 478 |
$attachment_id = wp_insert_attachment($attachment, $upload_file['file'], 0); |
| 479 |
if (!is_wp_error($attachment_id)) { |
| 480 |
require_once(ABSPATH . "wp-admin" . '/includes/image.php'); |
| 481 |
$attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']); |
| 482 |
wp_update_attachment_metadata($attachment_id, $attachment_data); |
| 483 |
$pages[$page_key]['elements'][$element_key]['value'] = $upload_file['url']; |
| 484 |
} |
| 485 |
} |
| 486 |
} |
| 487 |
unset($pages[$page_key]['elements'][$element_key]['base64']); |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
$template = new Model_E2pdf_Template(); |
| 494 |
if ($options['overwrite']) { |
| 495 |
$template->load((String) $xml->template->ID); |
| 496 |
} |
| 497 |
|
| 498 |
$template->set('title', (String) $xml->template->title); |
| 499 |
$template->set('flatten', (String) $xml->template->flatten); |
| 500 |
$template->set('format', (String) $xml->template->format); |
| 501 |
$template->set('compression', (String) $xml->template->compression); |
| 502 |
$template->set('appearance', (String) $xml->template->appearance); |
| 503 |
$template->set('width', (String) $xml->template->width); |
| 504 |
$template->set('height', (String) $xml->template->height); |
| 505 |
$template->set('extension', (String) $xml->template->extension); |
| 506 |
|
| 507 |
if (!$options['overwrite']) { |
| 508 |
$template->set('item', ""); |
| 509 |
} |
| 510 |
|
| 511 |
$fpro2pdf_backup = false; |
| 512 |
|
| 513 |
if ($xml->source) { |
| 514 |
if ((String) $xml->source == 'fpro2pdf') { |
| 515 |
$template->set('item', (String) $xml->template->item); |
| 516 |
$fpro2pdf_backup = true; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
$template->set('inline', (String) $xml->template->inline); |
| 521 |
$template->set('auto', (String) $xml->template->auto); |
| 522 |
$template->set('font', (String) $xml->template->font); |
| 523 |
$template->set('font_size', (String) $xml->template->font_size); |
| 524 |
$template->set('font_color', (String) $xml->template->font_color); |
| 525 |
$template->set('line_height', (String) $xml->template->line_height); |
| 526 |
$template->set('fonts', stripslashes_deep(unserialize(base64_decode((String) $xml->template->fonts)))); |
| 527 |
|
| 528 |
$extension = new Model_E2pdf_Extension(); |
| 529 |
$extension->load($template->get('extension')); |
| 530 |
|
| 531 |
if ($options['item'] && $xml->item && !$fpro2pdf_backup) { |
| 532 |
$template->set('item', (String) $xml->template->item); |
| 533 |
$extension->set('item', $template->get('item')); |
| 534 |
if ($updated_item = $extension->import($xml->item, (String) $xml->template->item, $options)) { |
| 535 |
if (is_array($updated_item) && array_key_exists('errors', $updated_item)) { |
| 536 |
$errors = array_merge($errors, $updated_item['errors']); |
| 537 |
} else { |
| 538 |
$template->set('item', $updated_item); |
| 539 |
$extension->set('item', $updated_item); |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
$pages = apply_filters('e2pdf_controller_templates_import_pages', $pages, $options, $xml, $template, $extension); |
| 545 |
$dataset_title = apply_filters('e2pdf_controller_templates_import_dataset_title', (String) $xml->template->dataset_title, $options, $xml, $template, $extension); |
| 546 |
$button_title = apply_filters('e2pdf_controller_templates_import_button_title', (String) $xml->template->button_title, $options, $xml, $template, $extension); |
| 547 |
$name = apply_filters('e2pdf_controller_templates_import_name', (String) $xml->template->name, $options, $xml, $template, $extension); |
| 548 |
$password = apply_filters('e2pdf_controller_templates_import_password', (String) $xml->template->password, $options, $xml, $template, $extension); |
| 549 |
$meta_title = apply_filters('e2pdf_controller_templates_import_meta_title', (String) $xml->template->meta_title, $options, $xml, $template, $extension); |
| 550 |
$meta_subject = apply_filters('e2pdf_controller_templates_import_meta_subject', (String) $xml->template->meta_subject, $options, $xml, $template, $extension); |
| 551 |
$meta_author = apply_filters('e2pdf_controller_templates_import_meta_author', (String) $xml->template->meta_author, $options, $xml, $template, $extension); |
| 552 |
$meta_keywords = apply_filters('e2pdf_controller_templates_import_meta_keywords', (String) $xml->template->meta_keywords, $options, $xml, $template, $extension); |
| 553 |
|
| 554 |
$template->set('pages', $pages); |
| 555 |
$template->set('dataset_title', $dataset_title); |
| 556 |
$template->set('button_title', $button_title); |
| 557 |
$template->set('name', $name); |
| 558 |
$template->set('password', $password); |
| 559 |
$template->set('meta_title', $meta_title); |
| 560 |
$template->set('meta_subject', $meta_subject); |
| 561 |
$template->set('meta_author', $meta_author); |
| 562 |
$template->set('meta_keywords', $meta_keywords); |
| 563 |
|
| 564 |
if ($options['fonts'] && $xml->fonts) { |
| 565 |
$model_e2pdf_font = new Model_E2pdf_Font(); |
| 566 |
$fonts = $model_e2pdf_font->get_fonts(); |
| 567 |
|
| 568 |
if ($xml->fonts) { |
| 569 |
foreach ($xml->fonts->children() as $key => $font) { |
| 570 |
$title = (String) $font->title; |
| 571 |
$name = (String) $font->name; |
| 572 |
$value = (String) $font->value; |
| 573 |
|
| 574 |
$exist = array_search($title, $fonts); |
| 575 |
if (!$exist) { |
| 576 |
if (!file_exists($this->helper->get('fonts_dir') . $name)) { |
| 577 |
$f_name = $name; |
| 578 |
} else { |
| 579 |
$i = 0; |
| 580 |
do { |
| 581 |
$f_name = $i . '_' . $name; |
| 582 |
$i++; |
| 583 |
} while (file_exists($this->helper->get('fonts_dir') . $f_name)); |
| 584 |
} |
| 585 |
file_put_contents($this->helper->get('fonts_dir') . $f_name, base64_decode($value)); |
| 586 |
} |
| 587 |
} |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
if ($xml->pdf && $xml->pdf->source) { |
| 592 |
|
| 593 |
$pdf_name = md5(time()); |
| 594 |
$pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . "/"; |
| 595 |
$pdf_images_dir = $pdf_dir . "images/"; |
| 596 |
|
| 597 |
$this->helper->create_dir($pdf_dir); |
| 598 |
$this->helper->create_dir($pdf_images_dir); |
| 599 |
|
| 600 |
$pdf_source = (String) $xml->pdf->source; |
| 601 |
file_put_contents($pdf_dir . $pdf_name . ".pdf", base64_decode($pdf_source)); |
| 602 |
|
| 603 |
if ($xml->pdf->images) { |
| 604 |
foreach ($xml->pdf->images->children() as $key => $image) { |
| 605 |
$page_id = (String) $image->page_id; |
| 606 |
$image_source = (String) $image->source; |
| 607 |
file_put_contents($pdf_dir . "images/" . $page_id . ".png", base64_decode($image_source)); |
| 608 |
} |
| 609 |
} |
| 610 |
$template->set('pdf', $pdf_name); |
| 611 |
} |
| 612 |
|
| 613 |
if (empty($errors)) { |
| 614 |
$template->save(true); |
| 615 |
|
| 616 |
if ($options['item'] && (String) $xml->template->item != $template->get('item') && $template->get('ID') != (String) $xml->template->ID) { |
| 617 |
$extension->after_import((String) $xml->template->ID, $template->get('ID')); |
| 618 |
} |
| 619 |
|
| 620 |
$this->add_notification('update', __('Template imported successfully', 'e2pdf')); |
| 621 |
} else { |
| 622 |
foreach ($errors as $key => $error) { |
| 623 |
$this->add_notification('error', __($error, 'e2pdf')); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
unlink($import['tmp_name']); |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
$this->view('import_disabled', false); |
| 632 |
|
| 633 |
if (!$this->helper->load('xml')->check()) { |
| 634 |
$this->add_notification('error', sprintf(__("PHP Extension <strong>%s</strong> not found", 'e2pdf'), 'SimpleXml')); |
| 635 |
$this->view('import_disabled', true); |
| 636 |
} |
| 637 |
|
| 638 |
$options = array( |
| 639 |
'common' => array( |
| 640 |
'name' => __('Template Options', 'e2pdf'), |
| 641 |
'options' => array( |
| 642 |
array( |
| 643 |
'name' => __('Overwrite template by ID', 'e2pdf'), |
| 644 |
'key' => 'options[overwrite]', |
| 645 |
'value' => '0', |
| 646 |
'default_value' => '0', |
| 647 |
'type' => 'checkbox', |
| 648 |
'placeholder' => '', |
| 649 |
), |
| 650 |
array( |
| 651 |
'name' => __('Import Images', 'e2pdf'), |
| 652 |
'key' => 'options[images]', |
| 653 |
'value' => '1', |
| 654 |
'default_value' => '0', |
| 655 |
'type' => 'checkbox', |
| 656 |
'placeholder' => '', |
| 657 |
), |
| 658 |
array( |
| 659 |
'name' => __('Import Fonts', 'e2pdf'), |
| 660 |
'key' => 'options[fonts]', |
| 661 |
'value' => '1', |
| 662 |
'default_value' => '0', |
| 663 |
'type' => 'checkbox', |
| 664 |
'placeholder' => '', |
| 665 |
) |
| 666 |
) |
| 667 |
), |
| 668 |
'item' => array( |
| 669 |
'name' => __('Item Options', 'e2pdf'), |
| 670 |
'options' => array( |
| 671 |
array( |
| 672 |
'name' => __('Import Item', 'e2pdf'), |
| 673 |
'key' => 'options[item]', |
| 674 |
'value' => '0', |
| 675 |
'default_value' => '0', |
| 676 |
'type' => 'checkbox', |
| 677 |
'placeholder' => '', |
| 678 |
'class' => 'e2pdf-collapse', |
| 679 |
'data-collapse' => 'e2pdf-import-extension-option' |
| 680 |
), |
| 681 |
) |
| 682 |
) |
| 683 |
); |
| 684 |
|
| 685 |
$options = apply_filters('e2pdf_controller_templates_import_options', $options); |
| 686 |
$this->view('options', $options); |
| 687 |
} |
| 688 |
|
| 689 |
/** |
| 690 |
* @url admin.php?page=e2pdf-templates&action=backup&id=$id |
| 691 |
*/ |
| 692 |
public function backup_action() { |
| 693 |
|
| 694 |
if ($this->post->get()) { |
| 695 |
|
| 696 |
$this->check_nonce($this->post->get('_nonce'), 'e2pdf_post'); |
| 697 |
|
| 698 |
if ($this->helper->load('xml')->check()) { |
| 699 |
|
| 700 |
$options = $this->post->get('options'); |
| 701 |
$xml = $this->helper->load('xml')->create('backup'); |
| 702 |
|
| 703 |
$template = new Model_E2pdf_Template(); |
| 704 |
if ($template->load($this->post->get('id'))) { |
| 705 |
|
| 706 |
if ($template->get('title') != '') { |
| 707 |
$filename = $template->get('title') . "." . date('Y-m-d.H-i-s') . ".e2pdf"; |
| 708 |
} else { |
| 709 |
$filename = date('Y-m-d.H-i-s') . ".e2pdf"; |
| 710 |
} |
| 711 |
|
| 712 |
$pages = $template->get('pages'); |
| 713 |
if ($options['images']) { |
| 714 |
foreach ($pages as $page_key => $page) { |
| 715 |
if (isset($page['elements']) && !empty($page['elements'])) { |
| 716 |
foreach ($page['elements'] as $element_key => $element) { |
| 717 |
if ($element['type'] === 'e2pdf-image') { |
| 718 |
$pages[$page_key]['elements'][$element_key]['base64'] = $this->helper->load('image')->get_image($element['value']); |
| 719 |
} |
| 720 |
} |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
$pages = apply_filters('e2pdf_controller_templates_backup_pages', $pages, $options, $template, $template->extension()); |
| 726 |
$dataset_title = apply_filters('e2pdf_controller_templates_backup_dataset_title', $template->get('dataset_title'), $options, $template, $template->extension()); |
| 727 |
$button_title = apply_filters('e2pdf_controller_templates_backup_button_title', $template->get('button_title'), $options, $template, $template->extension()); |
| 728 |
$name = apply_filters('e2pdf_controller_templates_backup_name', $template->get('name'), $options, $template, $template->extension()); |
| 729 |
$password = apply_filters('e2pdf_controller_templates_backup_password', $template->get('password'), $options, $template, $template->extension()); |
| 730 |
$meta_title = apply_filters('e2pdf_controller_templates_backup_meta_title', $template->get('meta_title'), $options, $template, $template->extension()); |
| 731 |
$meta_subject = apply_filters('e2pdf_controller_templates_backup_meta_subject', $template->get('meta_subject'), $options, $template, $template->extension()); |
| 732 |
$meta_author = apply_filters('e2pdf_controller_templates_backup_meta_author', $template->get('meta_author'), $options, $template, $template->extension()); |
| 733 |
$meta_keywords = apply_filters('e2pdf_controller_templates_backup_meta_keywords', $template->get('meta_keywords'), $options, $template, $template->extension()); |
| 734 |
|
| 735 |
// build xml |
| 736 |
$xml->addChildCData('version', $this->helper->get('version')); |
| 737 |
$xml->addChildCData('date', date('Y-m-d H:i:s')); |
| 738 |
|
| 739 |
$options_xml = $xml->addChild('options'); |
| 740 |
foreach ($options as $option_key => $option) { |
| 741 |
$options_xml->addChildCData($option_key, $option); |
| 742 |
} |
| 743 |
|
| 744 |
$template_xml = $xml->addChild('template'); |
| 745 |
$template_xml->addChildCData('ID', $template->get('ID')); |
| 746 |
$template_xml->addChildCData('title', $template->get('title')); |
| 747 |
$template_xml->addChildCData('created_at', $template->get('created_at')); |
| 748 |
$template_xml->addChildCData('updated_at', $template->get('updated_at')); |
| 749 |
$template_xml->addChildCData('flatten', $template->get('flatten')); |
| 750 |
$template_xml->addChildCData('format', $template->get('format')); |
| 751 |
$template_xml->addChildCData('compression', $template->get('compression')); |
| 752 |
$template_xml->addChildCData('appearance', $template->get('appearance')); |
| 753 |
$template_xml->addChildCData('width', $template->get('width')); |
| 754 |
$template_xml->addChildCData('height', $template->get('height')); |
| 755 |
$template_xml->addChildCData('extension', $template->get('extension')); |
| 756 |
$template_xml->addChildCData('item', $template->get('item')); |
| 757 |
$template_xml->addChildCData('dataset_title', $dataset_title); |
| 758 |
$template_xml->addChildCData('button_title', $button_title); |
| 759 |
$template_xml->addChildCData('inline', $template->get('inline')); |
| 760 |
$template_xml->addChildCData('auto', $template->get('auto')); |
| 761 |
$template_xml->addChildCData('name', $name); |
| 762 |
$template_xml->addChildCData('password', $password); |
| 763 |
$template_xml->addChildCData('meta_title', $meta_title); |
| 764 |
$template_xml->addChildCData('meta_subject', $meta_subject); |
| 765 |
$template_xml->addChildCData('meta_author', $meta_author); |
| 766 |
$template_xml->addChildCData('meta_keywords', $meta_keywords); |
| 767 |
$template_xml->addChildCData('font', $template->get('font')); |
| 768 |
$template_xml->addChildCData('font_size', $template->get('font_size')); |
| 769 |
$template_xml->addChildCData('font_color', $template->get('font_color')); |
| 770 |
$template_xml->addChildCData('line_height', $template->get('line_height')); |
| 771 |
$template_xml->addChildCData('fonts', base64_encode(serialize($template->get('fonts')))); |
| 772 |
$template_xml->addChildCData('pages', base64_encode(serialize($pages))); |
| 773 |
|
| 774 |
//include pdf |
| 775 |
if ($template->get('pdf')) { |
| 776 |
$pdf_xml = $xml->addChild('pdf'); |
| 777 |
$pdf_xml->addChildCData('source', base64_encode(file_get_contents($this->helper->get('pdf_dir') . $template->get('pdf') . "/" . $template->get('pdf') . ".pdf"))); |
| 778 |
$pdf_xml_images = $pdf_xml->addChild('images'); |
| 779 |
foreach ($template->get('pages') as $key => $page) { |
| 780 |
$pdf_xml_image = $pdf_xml_images->addChild('image'); |
| 781 |
$pdf_xml_image->addChildCData('page_id', $page['page_id']); |
| 782 |
$pdf_xml_image->addChildCData('source', base64_encode(file_get_contents($this->helper->get('pdf_dir') . $template->get('pdf') . "/images/" . $page['page_id'] . ".png"))); |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
//include fonts |
| 787 |
if ($options['fonts']) { |
| 788 |
$fonts_xml = $xml->addChild('fonts'); |
| 789 |
if ($template->get('fonts')) { |
| 790 |
$model_e2pdf_font = new Model_E2pdf_Font(); |
| 791 |
foreach ($template->get('fonts') as $key => $value) { |
| 792 |
$font = $fonts_xml->addChild('font'); |
| 793 |
$font->addChildCData('title', $value); |
| 794 |
$font->addChildCData('name', $key); |
| 795 |
$font->addChildCData('value', $model_e2pdf_font->get_font($key)); |
| 796 |
} |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
//include item |
| 801 |
if ($options['item']) { |
| 802 |
if ($template->extension()->method('backup')) { |
| 803 |
$item = $xml->addChild('item'); |
| 804 |
$template->extension()->backup($item); |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
$file = $this->helper->load('xml')->get_xml(); |
| 809 |
$this->download_response('xml', $file, $filename); |
| 810 |
} else { |
| 811 |
$this->add_notification('error', __("Template can't be loaded", 'e2pdf')); |
| 812 |
$this->render('blocks', 'notifications'); |
| 813 |
} |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
$this->view('export_disabled', false); |
| 818 |
|
| 819 |
if (!$this->helper->load('xml')->check()) { |
| 820 |
$this->add_notification('error', sprintf(__("PHP Extension <strong>%s</strong> not found", 'e2pdf'), 'SimpleXml')); |
| 821 |
$this->view('export_disabled', true); |
| 822 |
} |
| 823 |
|
| 824 |
$template = new Model_E2pdf_Template(); |
| 825 |
if ($template->load($this->get->get('id'))) { |
| 826 |
|
| 827 |
$this->view('template', $template); |
| 828 |
|
| 829 |
$options = array( |
| 830 |
'common' => array( |
| 831 |
'name' => __('Options', 'e2pdf'), |
| 832 |
'options' => array( |
| 833 |
array( |
| 834 |
'name' => __('Include Item', 'e2pdf'), |
| 835 |
'key' => 'options[item]', |
| 836 |
'value' => $template->extension()->method('backup') ? 1 : 0, |
| 837 |
'default_value' => 0, |
| 838 |
'type' => 'checkbox', |
| 839 |
'placeholder' => '', |
| 840 |
'disabled' => $template->extension()->method('backup') ? false : 'disabled' |
| 841 |
), |
| 842 |
array( |
| 843 |
'name' => __('Include Images', 'e2pdf'), |
| 844 |
'key' => 'options[images]', |
| 845 |
'value' => 1, |
| 846 |
'default_value' => 0, |
| 847 |
'type' => 'checkbox', |
| 848 |
'placeholder' => '', |
| 849 |
), |
| 850 |
array( |
| 851 |
'name' => __('Include Fonts', 'e2pdf'), |
| 852 |
'key' => 'options[fonts]', |
| 853 |
'value' => 1, |
| 854 |
'default_value' => 0, |
| 855 |
'type' => 'checkbox', |
| 856 |
'placeholder' => '', |
| 857 |
) |
| 858 |
) |
| 859 |
) |
| 860 |
); |
| 861 |
|
| 862 |
$options = apply_filters('e2pdf_controller_templates_backup_options', $options, $template, $template->extension()); |
| 863 |
$this->view('options', $options); |
| 864 |
} else { |
| 865 |
$this->add_notification('error', __("Template can't be loaded", 'e2pdf')); |
| 866 |
$this->render('blocks', 'notifications'); |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
public function screen_action() { |
| 871 |
if ($this->post->get()) { |
| 872 |
$this->check_nonce($this->post->get('screenoptionnonce'), 'screen-options-nonce'); |
| 873 |
$option = $this->post->get('wp_screen_options'); |
| 874 |
if (is_array($option) && isset($option['option']) && isset($option['value']) && $option['value']) { |
| 875 |
update_option($option['option'], $option['value']); |
| 876 |
} |
| 877 |
} |
| 878 |
$location = $this->helper->get_url( |
| 879 |
array( |
| 880 |
'page' => 'e2pdf-templates', |
| 881 |
) |
| 882 |
); |
| 883 |
$this->redirect($location); |
| 884 |
} |
| 885 |
|
| 886 |
/** |
| 887 |
* Save template via ajax |
| 888 |
* action: wp_ajax_e2pdf_save_form |
| 889 |
* function: e2pdf_save_form |
| 890 |
* |
| 891 |
* @return json |
| 892 |
*/ |
| 893 |
public function ajax_save_form() { |
| 894 |
global $wpdb; |
| 895 |
$this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax'); |
| 896 |
|
| 897 |
$data = json_decode($this->post->get('data'), true); |
| 898 |
|
| 899 |
$template = new Model_E2pdf_Template(); |
| 900 |
if (isset($data['ID'])) { |
| 901 |
$template->load($data['ID']); |
| 902 |
if ($template->get('pdf') && $template->get('pdf') !== $data['pdf']) { |
| 903 |
$old_pdf_path = $this->helper->get('pdf_dir') . $template->get('pdf') . "/"; |
| 904 |
$this->helper->delete_dir($old_pdf_path); |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
foreach ($data as $key => $value) { |
| 909 |
$template->set($key, $value); |
| 910 |
} |
| 911 |
$template_id = $template->save(true); |
| 912 |
|
| 913 |
if (!$template_id) { |
| 914 |
if ($wpdb->last_error) { |
| 915 |
$response = array( |
| 916 |
'error' => $wpdb->last_error |
| 917 |
); |
| 918 |
} else { |
| 919 |
$response = array( |
| 920 |
'error' => __("Something went wrong!", "e2pdf") |
| 921 |
); |
| 922 |
} |
| 923 |
} else { |
| 924 |
if (isset($data['item']) && $data['item'] == '-1' && $template_id) { |
| 925 |
$template->load($template_id); |
| 926 |
if ($template->extension()->method('auto_form')) { |
| 927 |
$template = $template->extension()->auto_form($template, $data); |
| 928 |
} else { |
| 929 |
$template->set('item', ''); |
| 930 |
} |
| 931 |
$template->save(true); |
| 932 |
} |
| 933 |
|
| 934 |
if ($template->get('activated')) { |
| 935 |
$this->activate_template($template->get('ID')); |
| 936 |
} elseif (!$template->get('activated')) { |
| 937 |
$this->deactivate_template($template->get('ID')); |
| 938 |
} |
| 939 |
|
| 940 |
$response = array( |
| 941 |
'redirect' => $this->helper->get_url(array( |
| 942 |
'page' => 'e2pdf-templates', |
| 943 |
'action' => 'edit', |
| 944 |
'id' => $template->get('ID')) |
| 945 |
) |
| 946 |
); |
| 947 |
|
| 948 |
$this->add_notification('update', __('Template saved successfully', 'e2pdf')); |
| 949 |
} |
| 950 |
|
| 951 |
$this->json_response($response); |
| 952 |
} |
| 953 |
|
| 954 |
public function ajax_get_styles() { |
| 955 |
|
| 956 |
$this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax'); |
| 957 |
$data = $this->post->get('data'); |
| 958 |
|
| 959 |
$extension = new Model_E2pdf_Extension(); |
| 960 |
if ($extension->load($data)) { |
| 961 |
$content = $extension->styles(); |
| 962 |
} else { |
| 963 |
$content = array(); |
| 964 |
} |
| 965 |
$response = array( |
| 966 |
'content' => $content, |
| 967 |
); |
| 968 |
$this->json_response($response); |
| 969 |
} |
| 970 |
|
| 971 |
/** |
| 972 |
* Get extensions via ajax |
| 973 |
* action: wp_ajax_e2pdf_extension |
| 974 |
* function: e2pdf_extension |
| 975 |
* |
| 976 |
* @return json |
| 977 |
*/ |
| 978 |
public function ajax_extension() { |
| 979 |
|
| 980 |
$this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax'); |
| 981 |
$data = $this->post->get('data'); |
| 982 |
|
| 983 |
$extension = new Model_E2pdf_Extension(); |
| 984 |
|
| 985 |
$content = array(); |
| 986 |
if ($extension->load($data)) { |
| 987 |
$content = $extension->items(); |
| 988 |
|
| 989 |
if ($extension->method('auto_form')) { |
| 990 |
$select = new stdClass(); |
| 991 |
$select->id = '-1'; |
| 992 |
$select->url = 'javascript:void(0);'; |
| 993 |
$select->name = __("Auto Form from PDF", "e2pdf"); |
| 994 |
|
| 995 |
array_unshift($content, $select); |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
$select = new stdClass(); |
| 1000 |
$select->id = ''; |
| 1001 |
$select->url = 'javascript:void(0);'; |
| 1002 |
$select->name = __("--- Select ---", "e2pdf"); |
| 1003 |
|
| 1004 |
array_unshift($content, $select); |
| 1005 |
|
| 1006 |
$response = array( |
| 1007 |
'content' => $content, |
| 1008 |
); |
| 1009 |
$this->json_response($response); |
| 1010 |
} |
| 1011 |
|
| 1012 |
/** |
| 1013 |
* Get extensions via ajax |
| 1014 |
* action: wp_ajax_e2pdf_extension |
| 1015 |
* function: e2pdf_extension |
| 1016 |
* |
| 1017 |
* @return json |
| 1018 |
*/ |
| 1019 |
public function ajax_visual_mapper() { |
| 1020 |
|
| 1021 |
$this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax'); |
| 1022 |
|
| 1023 |
$data = $this->post->get('data'); |
| 1024 |
|
| 1025 |
$item = isset($data['item']) ? $data['item'] : ''; |
| 1026 |
|
| 1027 |
$extension = new Model_E2pdf_Extension(); |
| 1028 |
$extension->load($data['extension']); |
| 1029 |
$extension->set('item', $item); |
| 1030 |
|
| 1031 |
if ($visual_mapper = $extension->visual_mapper()) { |
| 1032 |
$response = array( |
| 1033 |
'content' => $visual_mapper |
| 1034 |
); |
| 1035 |
} elseif (!$item) { |
| 1036 |
$response = array( |
| 1037 |
'content' => __("You must set Item to use <strong>Visual Mapper</strong>", "e2pdf"), |
| 1038 |
); |
| 1039 |
} else { |
| 1040 |
$response = array( |
| 1041 |
'content' => __("Sorry, this extension doesn't support <strong>Visual Mapper</strong>", "e2pdf"), |
| 1042 |
); |
| 1043 |
} |
| 1044 |
|
| 1045 |
$this->json_response($response); |
| 1046 |
} |
| 1047 |
|
| 1048 |
/** |
| 1049 |
* Upload Template via Ajax |
| 1050 |
* action: wp_ajax_e2pdf_upload |
| 1051 |
* function: e2pdf_upload |
| 1052 |
* |
| 1053 |
* @return json |
| 1054 |
*/ |
| 1055 |
public function ajax_upload() { |
| 1056 |
|
| 1057 |
$this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax'); |
| 1058 |
|
| 1059 |
$data = $this->post->get(); |
| 1060 |
|
| 1061 |
$post_extension = $data['extension']; |
| 1062 |
$item = isset($data['item']) ? $data['item'] : ''; |
| 1063 |
$template_id = isset($data['template_id']) && $data['template_id'] ? $data['template_id'] : false; |
| 1064 |
|
| 1065 |
$font = isset($data['font']) ? $data['font'] : false; |
| 1066 |
$font_size = isset($data['font_size']) ? $data['font_size'] : false; |
| 1067 |
$line_height = isset($data['line_height']) ? $data['line_height'] : false; |
| 1068 |
$activated = isset($data['activated']) ? $data['activated'] : 0; |
| 1069 |
$title = isset($data['title']) ? $data['title'] : __('(no title)', 'e2pdf'); |
| 1070 |
|
| 1071 |
$allowed_ext = array( |
| 1072 |
'pdf' |
| 1073 |
); |
| 1074 |
|
| 1075 |
$pdf = $this->files->get('pdf'); |
| 1076 |
|
| 1077 |
$name = strtolower($pdf['name']); |
| 1078 |
$tmp = $pdf['tmp_name']; |
| 1079 |
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION)); |
| 1080 |
|
| 1081 |
if ($pdf['error']) { |
| 1082 |
$error = $pdf['error']; |
| 1083 |
} elseif (!in_array($ext, $allowed_ext)) { |
| 1084 |
$error = __('Incorrect file extension', 'e2pdf'); |
| 1085 |
} elseif ($pdf['type'] != 'application/pdf') { |
| 1086 |
$error = __('Incompatible type', 'e2pdf'); |
| 1087 |
} |
| 1088 |
|
| 1089 |
if ($error) { |
| 1090 |
$response = array( |
| 1091 |
'error' => $error, |
| 1092 |
); |
| 1093 |
} else { |
| 1094 |
|
| 1095 |
$model_e2pdf_api = new Model_E2pdf_Api(); |
| 1096 |
$model_e2pdf_api->set(array( |
| 1097 |
'action' => 'template/upload', |
| 1098 |
'data' => array( |
| 1099 |
'title' => $name, |
| 1100 |
'pdf' => base64_encode(file_get_contents($pdf['tmp_name'])) |
| 1101 |
) |
| 1102 |
)); |
| 1103 |
$request = $model_e2pdf_api->request(); |
| 1104 |
|
| 1105 |
if (!isset($request['error'])) { |
| 1106 |
|
| 1107 |
$extension = new Model_E2pdf_Extension(); |
| 1108 |
if ($post_extension) { |
| 1109 |
$extension->load($post_extension); |
| 1110 |
if ($item && $item != '-1') { |
| 1111 |
$extension->set('item', $item); |
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
$pdf_name = md5(time()); |
| 1116 |
$pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . "/"; |
| 1117 |
$pdf_images_dir = $pdf_dir . "images/"; |
| 1118 |
|
| 1119 |
$this->helper->create_dir($pdf_dir); |
| 1120 |
$this->helper->create_dir($pdf_images_dir); |
| 1121 |
|
| 1122 |
move_uploaded_file($pdf['tmp_name'], $pdf_dir . $pdf_name . ".pdf"); |
| 1123 |
|
| 1124 |
$xml = simplexml_load_string(base64_decode($request['file']), "SimpleXMLElement", LIBXML_PARSEHUGE); |
| 1125 |
$pages = stripslashes_deep(unserialize(base64_decode((String) $xml->template->pages))); |
| 1126 |
|
| 1127 |
if (is_array($pages) && !empty($pages)) { |
| 1128 |
foreach ($pages as $page_key => $page) { |
| 1129 |
|
| 1130 |
$pages[$page_key]['page_id'] = $page_key; |
| 1131 |
|
| 1132 |
if (isset($page['properties']['background'])) { |
| 1133 |
file_put_contents($pdf_images_dir . $page_key . ".png", base64_decode($page['properties']['background'])); |
| 1134 |
unset($pages[$page_key]['properties']['background']); |
| 1135 |
} |
| 1136 |
|
| 1137 |
if (isset($page['elements']) && !empty($page['elements'])) { |
| 1138 |
foreach ($page['elements'] as $element_key => $element) { |
| 1139 |
if ($element['type'] === 'e2pdf-image' && isset($element['base64']) && $element['base64']) { |
| 1140 |
|
| 1141 |
$image = base64_decode($element['base64']); |
| 1142 |
$file_name = basename($element['value']); |
| 1143 |
|
| 1144 |
//exif_imagetype |
| 1145 |
if (!$file_name) { |
| 1146 |
$ext = $this->helper->load('image')->get_image_extension($image); |
| 1147 |
if ($ext) { |
| 1148 |
$file_name = md5(mktime()) . "." . $ext; |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
if ($file_name) { |
| 1153 |
$upload_file = wp_upload_bits($file_name, null, $image); |
| 1154 |
if (!$upload_file['error']) { |
| 1155 |
$wp_filetype = wp_check_filetype($file_name, null); |
| 1156 |
$attachment = array( |
| 1157 |
'post_mime_type' => $wp_filetype['type'], |
| 1158 |
'post_parent' => 0, |
| 1159 |
'post_title' => preg_replace('/\.[^.]+$/', '', $file_name), |
| 1160 |
'post_content' => '', |
| 1161 |
'post_status' => 'inherit' |
| 1162 |
); |
| 1163 |
$attachment_id = wp_insert_attachment($attachment, $upload_file['file'], 0); |
| 1164 |
if (!is_wp_error($attachment_id)) { |
| 1165 |
require_once(ABSPATH . "wp-admin" . '/includes/image.php'); |
| 1166 |
$attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']); |
| 1167 |
wp_update_attachment_metadata($attachment_id, $attachment_data); |
| 1168 |
$pages[$page_key]['elements'][$element_key]['value'] = $upload_file['url']; |
| 1169 |
} |
| 1170 |
} |
| 1171 |
|
| 1172 |
unset($pages[$page_key]['elements'][$element_key]['base64']); |
| 1173 |
} else { |
| 1174 |
unset($pages[$page_key]['elements'][$element_key]); |
| 1175 |
} |
| 1176 |
} elseif (isset($element['name']) && $element['name']) { |
| 1177 |
$el_value = $extension->auto_map($element['name']); |
| 1178 |
if ($el_value !== false) { |
| 1179 |
$pages[$page_key]['elements'][$element_key]['value'] = $el_value; |
| 1180 |
} |
| 1181 |
} |
| 1182 |
} |
| 1183 |
} |
| 1184 |
} |
| 1185 |
|
| 1186 |
$this->helper->load('sort')->stable_uasort($pages, "sort_by_pageid"); |
| 1187 |
|
| 1188 |
$template = new Model_E2pdf_Template(); |
| 1189 |
|
| 1190 |
if ($template_id) { |
| 1191 |
$template->load($template_id); |
| 1192 |
if ($template->get('pdf')) { |
| 1193 |
$old_pdf_path = $this->helper->get('pdf_dir') . $template->get('pdf') . "/"; |
| 1194 |
$this->helper->delete_dir($old_pdf_path); |
| 1195 |
} |
| 1196 |
$template->set('title', $title); |
| 1197 |
$template->set('pages', $pages); |
| 1198 |
$template->set('pdf', $pdf_name); |
| 1199 |
$template->set('width', (String) $xml->template->width); |
| 1200 |
$template->set('height', (String) $xml->template->height); |
| 1201 |
$template->set('extension', $post_extension); |
| 1202 |
$template->set('item', $item); |
| 1203 |
$template->set('font', $font ? $font : (String) $xml->template->font); |
| 1204 |
$template->set('font_size', $font_size ? $font_size : (String) $xml->template->font_size); |
| 1205 |
$template->set('line_height', $line_height ? $line_height : (String) $xml->template->font_size); |
| 1206 |
} else { |
| 1207 |
$template->set('title', $title); |
| 1208 |
$template->set('flatten', (String) $xml->template->flatten); |
| 1209 |
$template->set('format', (String) $xml->template->format); |
| 1210 |
$template->set('compression', (String) $xml->template->compression); |
| 1211 |
$template->set('appearance', (String) $xml->template->appearance); |
| 1212 |
$template->set('width', (String) $xml->template->width); |
| 1213 |
$template->set('height', (String) $xml->template->height); |
| 1214 |
$template->set('extension', $post_extension); |
| 1215 |
$template->set('item', $item); |
| 1216 |
$template->set('dataset_title', ""); |
| 1217 |
$template->set('button_title', ""); |
| 1218 |
$template->set('inline', ""); |
| 1219 |
$template->set('auto', ""); |
| 1220 |
$template->set('name', ""); |
| 1221 |
$template->set('password', ""); |
| 1222 |
$template->set('meta_title', (String) $xml->template->meta_title); |
| 1223 |
$template->set('meta_subject', (String) $xml->template->meta_subject); |
| 1224 |
$template->set('meta_author', (String) $xml->template->meta_author); |
| 1225 |
$template->set('meta_keywords', (String) $xml->template->meta_keywords); |
| 1226 |
$template->set('font', $font ? $font : (String) $xml->template->font); |
| 1227 |
$template->set('font_size', $font_size ? $font_size : (String) $xml->template->font_size); |
| 1228 |
$template->set('line_height', $line_height ? $line_height : (String) $xml->template->font_size); |
| 1229 |
$template->set('font_color', (String) $xml->template->font_color); |
| 1230 |
$template->set('pages', $pages); |
| 1231 |
$template->set('pdf', $pdf_name); |
| 1232 |
} |
| 1233 |
|
| 1234 |
if ($xml->fonts) { |
| 1235 |
|
| 1236 |
$model_e2pdf_font = new Model_E2pdf_Font(); |
| 1237 |
$fonts = $model_e2pdf_font->get_fonts(); |
| 1238 |
|
| 1239 |
foreach ($xml->fonts->children() as $key => $font) { |
| 1240 |
$font_title = (String) $font->title; |
| 1241 |
$name = (String) $font->name; |
| 1242 |
$value = (String) $font->value; |
| 1243 |
|
| 1244 |
$exist = array_search($font_title, $fonts); |
| 1245 |
if (!$exist) { |
| 1246 |
if (!file_exists($this->helper->get('fonts_dir') . $name)) { |
| 1247 |
$f_name = $name; |
| 1248 |
} else { |
| 1249 |
$i = 0; |
| 1250 |
do { |
| 1251 |
$f_name = $i . '_' . $name; |
| 1252 |
$i++; |
| 1253 |
} while (file_exists($this->helper->get('fonts_dir') . $f_name)); |
| 1254 |
} |
| 1255 |
file_put_contents($this->helper->get('fonts_dir') . $f_name, base64_decode($value)); |
| 1256 |
} |
| 1257 |
} |
| 1258 |
} |
| 1259 |
$template->save(true); |
| 1260 |
|
| 1261 |
if ($item == '-1' && $extension->method('auto_form')) { |
| 1262 |
$template = $extension->auto_form($template, $data); |
| 1263 |
$template->save(true); |
| 1264 |
} |
| 1265 |
|
| 1266 |
if ($activated && !$template->get('activated')) { |
| 1267 |
$this->activate_template($template->get('ID')); |
| 1268 |
} elseif (!$activated && $template->get('activated')) { |
| 1269 |
$this->deactivate_template($template->get('ID')); |
| 1270 |
} |
| 1271 |
|
| 1272 |
$response = array( |
| 1273 |
'redirect' => $this->helper->get_url(array( |
| 1274 |
'page' => 'e2pdf-templates', |
| 1275 |
'action' => 'edit', |
| 1276 |
'id' => $template->get('ID')) |
| 1277 |
) |
| 1278 |
); |
| 1279 |
} else { |
| 1280 |
$this->helper->delete_dir($pdf_dir); |
| 1281 |
$response = array( |
| 1282 |
'error' => __("PDF can't be parsed", 'e2pdf'), |
| 1283 |
); |
| 1284 |
} |
| 1285 |
} else { |
| 1286 |
$response = array( |
| 1287 |
'error' => $request['error'], |
| 1288 |
); |
| 1289 |
} |
| 1290 |
} |
| 1291 |
|
| 1292 |
$this->json_response($response); |
| 1293 |
} |
| 1294 |
|
| 1295 |
/** |
| 1296 |
* Upload Template via Ajax |
| 1297 |
* action: wp_ajax_e2pdf_upload |
| 1298 |
* function: e2pdf_upload |
| 1299 |
* |
| 1300 |
* @return json |
| 1301 |
*/ |
| 1302 |
public function ajax_reupload() { |
| 1303 |
|
| 1304 |
$error = false; |
| 1305 |
$this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax'); |
| 1306 |
$data = $this->post->get(); |
| 1307 |
|
| 1308 |
$new = isset($data['new']) ? $data['new'] : array(); |
| 1309 |
$flush = isset($data['flush']) ? $data['flush'] : array(); |
| 1310 |
$positions = isset($data['positions']) ? $data['positions'] : array(); |
| 1311 |
$template_id = isset($data['template_id']) && $data['template_id'] ? $data['template_id'] : false; |
| 1312 |
|
| 1313 |
if (!$template_id) { |
| 1314 |
return false; |
| 1315 |
} |
| 1316 |
|
| 1317 |
$allowed_ext = array( |
| 1318 |
'pdf' |
| 1319 |
); |
| 1320 |
|
| 1321 |
$pdf = $this->files->get('pdf'); |
| 1322 |
|
| 1323 |
$name = strtolower($pdf['name']); |
| 1324 |
$tmp = $pdf['tmp_name']; |
| 1325 |
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION)); |
| 1326 |
|
| 1327 |
if ($pdf['error']) { |
| 1328 |
$error = $pdf['error']; |
| 1329 |
} elseif (!in_array($ext, $allowed_ext)) { |
| 1330 |
$error = __('Incorrect file extension', 'e2pdf'); |
| 1331 |
} elseif ($pdf['type'] != 'application/pdf') { |
| 1332 |
$error = __('Incompatible type', 'e2pdf'); |
| 1333 |
} |
| 1334 |
|
| 1335 |
if ($error) { |
| 1336 |
$response = array( |
| 1337 |
'error' => $error, |
| 1338 |
); |
| 1339 |
} else { |
| 1340 |
|
| 1341 |
$model_e2pdf_api = new Model_E2pdf_Api(); |
| 1342 |
$model_e2pdf_api->set(array( |
| 1343 |
'action' => 'template/upload', |
| 1344 |
'data' => array( |
| 1345 |
'title' => $name, |
| 1346 |
'pdf' => base64_encode(file_get_contents($pdf['tmp_name'])) |
| 1347 |
) |
| 1348 |
)); |
| 1349 |
$request = $model_e2pdf_api->request(); |
| 1350 |
|
| 1351 |
if (!isset($request['error'])) { |
| 1352 |
$template = new Model_E2pdf_Template(); |
| 1353 |
if ($template->load($template_id)) { |
| 1354 |
if ($template->get('pdf')) { |
| 1355 |
$extension = new Model_E2pdf_Extension(); |
| 1356 |
if ($template->get('extension') && $template->get('item')) { |
| 1357 |
$extension->load($template->get('extension')); |
| 1358 |
$extension->set('item', $template->get('item')); |
| 1359 |
} |
| 1360 |
|
| 1361 |
$xml = simplexml_load_string(base64_decode($request['file']), "SimpleXMLElement", LIBXML_PARSEHUGE); |
| 1362 |
$pages = stripslashes_deep(unserialize(base64_decode((String) $xml->template->pages))); |
| 1363 |
|
| 1364 |
foreach ($pages as $page_key => $page) { |
| 1365 |
$pages[$page_key]['page_id'] = $page_key; |
| 1366 |
} |
| 1367 |
|
| 1368 |
$this->helper->load('sort')->stable_uasort($pages, "sort_by_pageid"); |
| 1369 |
|
| 1370 |
$model_e2pdf_element = new Model_E2pdf_Element(); |
| 1371 |
$last_element_id = $model_e2pdf_element->get_last_element_id($template_id); |
| 1372 |
|
| 1373 |
$original_pages = $template->get('pages'); |
| 1374 |
|
| 1375 |
foreach ($positions as $pos_key => $pos_page) { |
| 1376 |
if (!in_array($pos_key, $flush)) { |
| 1377 |
if (!isset($pages[$pos_page])) { |
| 1378 |
$error = sprintf(__("Position of %s page incorrect", 'e2pdf'), $pos_key); |
| 1379 |
} elseif ($pages[$pos_page]['properties']['width'] < $original_pages[$pos_key]['properties']['width']) { |
| 1380 |
$error = sprintf(__("Width of PDF Page %s can't be less width of Template Page %s", 'e2pdf'), $pos_page, $pos_key); |
| 1381 |
} elseif ($pages[$pos_page]['properties']['height'] < $original_pages[$pos_key]['properties']['height']) { |
| 1382 |
$error = sprintf(__("Height of PDF Page %s can't be less height of Template Page %s", 'e2pdf'), $pos_page, $pos_key); |
| 1383 |
} |
| 1384 |
} |
| 1385 |
} |
| 1386 |
|
| 1387 |
if (!$error) { |
| 1388 |
|
| 1389 |
$pdf_name = md5(time()); |
| 1390 |
$pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . "/"; |
| 1391 |
$pdf_images_dir = $pdf_dir . "images/"; |
| 1392 |
|
| 1393 |
$this->helper->create_dir($pdf_dir); |
| 1394 |
$this->helper->create_dir($pdf_images_dir); |
| 1395 |
|
| 1396 |
move_uploaded_file($pdf['tmp_name'], $pdf_dir . $pdf_name . ".pdf"); |
| 1397 |
|
| 1398 |
if (is_array($pages) && !empty($pages)) { |
| 1399 |
foreach ($pages as $page_key => $page) { |
| 1400 |
|
| 1401 |
if (isset($page['properties']['background'])) { |
| 1402 |
file_put_contents($pdf_images_dir . $page_key . ".png", base64_decode($page['properties']['background'])); |
| 1403 |
unset($pages[$page_key]['properties']['background']); |
| 1404 |
} |
| 1405 |
|
| 1406 |
if (isset($page['elements']) && !empty($page['elements'])) { |
| 1407 |
foreach ($page['elements'] as $element_key => $element) { |
| 1408 |
|
| 1409 |
$last_element_id++; |
| 1410 |
$pages[$page_key]['elements'][$element_key]['element_id'] = $last_element_id; |
| 1411 |
|
| 1412 |
if ($element['type'] === 'e2pdf-image' && isset($element['base64']) && $element['base64']) { |
| 1413 |
|
| 1414 |
$image = base64_decode($element['base64']); |
| 1415 |
$file_name = basename($element['value']); |
| 1416 |
|
| 1417 |
//exif_imagetype |
| 1418 |
if (!$file_name) { |
| 1419 |
$ext = $this->helper->load('image')->get_image_extension($image); |
| 1420 |
if ($ext) { |
| 1421 |
$file_name = md5(mktime()) . "." . $ext; |
| 1422 |
} |
| 1423 |
} |
| 1424 |
|
| 1425 |
if ($file_name) { |
| 1426 |
$upload_file = wp_upload_bits($file_name, null, $image); |
| 1427 |
if (!$upload_file['error']) { |
| 1428 |
$wp_filetype = wp_check_filetype($file_name, null); |
| 1429 |
$attachment = array( |
| 1430 |
'post_mime_type' => $wp_filetype['type'], |
| 1431 |
'post_parent' => 0, |
| 1432 |
'post_title' => preg_replace('/\.[^.]+$/', '', $file_name), |
| 1433 |
'post_content' => '', |
| 1434 |
'post_status' => 'inherit' |
| 1435 |
); |
| 1436 |
$attachment_id = wp_insert_attachment($attachment, $upload_file['file'], 0); |
| 1437 |
if (!is_wp_error($attachment_id)) { |
| 1438 |
require_once(ABSPATH . "wp-admin" . '/includes/image.php'); |
| 1439 |
$attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']); |
| 1440 |
wp_update_attachment_metadata($attachment_id, $attachment_data); |
| 1441 |
$pages[$page_key]['elements'][$element_key]['value'] = $upload_file['url']; |
| 1442 |
} |
| 1443 |
} |
| 1444 |
unset($pages[$page_key]['elements'][$element_key]['base64']); |
| 1445 |
} else { |
| 1446 |
unset($pages[$page_key]['elements'][$element_key]); |
| 1447 |
} |
| 1448 |
} elseif (isset($element['name']) && $element['name']) { |
| 1449 |
$el_value = $extension->auto_map($element['name']); |
| 1450 |
if ($el_value !== false) { |
| 1451 |
$pages[$page_key]['elements'][$element_key]['value'] = $el_value; |
| 1452 |
} |
| 1453 |
} |
| 1454 |
} |
| 1455 |
} |
| 1456 |
|
| 1457 |
foreach ($positions as $pos_key => $pos_page) { |
| 1458 |
if ($pos_page == $page_key) { |
| 1459 |
if (in_array($pos_key, $new)) { |
| 1460 |
if (isset($original_pages[$pos_key]['elements']) && !in_array($pos_key, $flush)) { |
| 1461 |
$elements = $original_pages[$pos_key]['elements']; |
| 1462 |
$pages[$page_key]['elements'] = array_merge($pages[$page_key]['elements'], $elements); |
| 1463 |
} |
| 1464 |
} else { |
| 1465 |
if (isset($original_pages[$pos_key]['elements']) && !in_array($pos_key, $flush)) { |
| 1466 |
$elements = $original_pages[$pos_key]['elements']; |
| 1467 |
$pages[$page_key]['elements'] = $elements; |
| 1468 |
} else { |
| 1469 |
$pages[$page_key]['elements'] = array(); |
| 1470 |
} |
| 1471 |
} |
| 1472 |
} |
| 1473 |
} |
| 1474 |
} |
| 1475 |
|
| 1476 |
$pdf_path = $this->helper->get('pdf_dir') . $template->get('pdf') . "/"; |
| 1477 |
$this->helper->delete_dir($pdf_path); |
| 1478 |
|
| 1479 |
$template->set('pdf', $pdf_name); |
| 1480 |
$template->set('pages', $pages); |
| 1481 |
$template->save(true); |
| 1482 |
|
| 1483 |
$response = array( |
| 1484 |
'redirect' => $this->helper->get_url(array( |
| 1485 |
'page' => 'e2pdf-templates', |
| 1486 |
'action' => 'edit', |
| 1487 |
'id' => $template->get('ID')) |
| 1488 |
) |
| 1489 |
); |
| 1490 |
} else { |
| 1491 |
$this->helper->delete_dir($pdf_dir); |
| 1492 |
$response = array( |
| 1493 |
'error' => __("PDF can't be parsed", 'e2pdf'), |
| 1494 |
); |
| 1495 |
} |
| 1496 |
} else { |
| 1497 |
$response = array( |
| 1498 |
'error' => $error, |
| 1499 |
); |
| 1500 |
} |
| 1501 |
} |
| 1502 |
} |
| 1503 |
} else { |
| 1504 |
$response = array( |
| 1505 |
'error' => $request['error'], |
| 1506 |
); |
| 1507 |
} |
| 1508 |
} |
| 1509 |
|
| 1510 |
$this->json_response($response); |
| 1511 |
} |
| 1512 |
|
| 1513 |
/** |
| 1514 |
* Auto Generation of Template |
| 1515 |
* action: wp_ajax_e2pdf_auto |
| 1516 |
* function: e2pdf_auto |
| 1517 |
* |
| 1518 |
* @return json |
| 1519 |
*/ |
| 1520 |
public function ajax_auto() { |
| 1521 |
$this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax'); |
| 1522 |
$data = $this->post->get('data'); |
| 1523 |
|
| 1524 |
$extension = $data['extension']; |
| 1525 |
$item = $data['item']; |
| 1526 |
|
| 1527 |
if (!$extension || !$item) { |
| 1528 |
return; |
| 1529 |
} |
| 1530 |
|
| 1531 |
$model_e2pdf_extension = new Model_E2pdf_Extension(); |
| 1532 |
$model_e2pdf_extension->load($extension); |
| 1533 |
$model_e2pdf_extension->set('item', $item); |
| 1534 |
|
| 1535 |
if (method_exists($model_e2pdf_extension->extension(), 'auto')) { |
| 1536 |
$content = $model_e2pdf_extension->auto(); |
| 1537 |
} else { |
| 1538 |
$content = array(); |
| 1539 |
} |
| 1540 |
|
| 1541 |
$response = array( |
| 1542 |
'content' => $content, |
| 1543 |
); |
| 1544 |
$this->json_response($response); |
| 1545 |
} |
| 1546 |
|
| 1547 |
public function ajax_activate_template() { |
| 1548 |
|
| 1549 |
$this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax'); |
| 1550 |
$data = $this->post->get('data'); |
| 1551 |
|
| 1552 |
$id = $data['id']; |
| 1553 |
|
| 1554 |
if (!$id) { |
| 1555 |
return; |
| 1556 |
} |
| 1557 |
|
| 1558 |
$request = $this->activate_template($id); |
| 1559 |
if (isset($request['error'])) { |
| 1560 |
$response = array( |
| 1561 |
'error' => $request['error'], |
| 1562 |
); |
| 1563 |
} else { |
| 1564 |
$response = array( |
| 1565 |
'content' => true, |
| 1566 |
); |
| 1567 |
} |
| 1568 |
|
| 1569 |
|
| 1570 |
$this->json_response($response); |
| 1571 |
} |
| 1572 |
|
| 1573 |
public function ajax_deactivate_template() { |
| 1574 |
$this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax'); |
| 1575 |
$data = $this->post->get('data'); |
| 1576 |
|
| 1577 |
$id = $data['id']; |
| 1578 |
|
| 1579 |
if (!$id) { |
| 1580 |
return; |
| 1581 |
} |
| 1582 |
|
| 1583 |
$request = $this->deactivate_template($id); |
| 1584 |
if (isset($request['error'])) { |
| 1585 |
$response = array( |
| 1586 |
'error' => $request['error'], |
| 1587 |
); |
| 1588 |
} else { |
| 1589 |
$response = array( |
| 1590 |
'content' => true, |
| 1591 |
); |
| 1592 |
} |
| 1593 |
$this->json_response($response); |
| 1594 |
} |
| 1595 |
|
| 1596 |
/** |
| 1597 |
* Move template to trash |
| 1598 |
* |
| 1599 |
* @param int $id - ID of template |
| 1600 |
*/ |
| 1601 |
public function trash_template($id = false) { |
| 1602 |
|
| 1603 |
$id = (int) $id; |
| 1604 |
|
| 1605 |
$template = new Model_E2pdf_Template(); |
| 1606 |
if ($id && $template->load($id)) { |
| 1607 |
$template->set('trash', '1'); |
| 1608 |
$template->save(); |
| 1609 |
$this->deactivate_template($template->get('ID')); |
| 1610 |
return true; |
| 1611 |
} |
| 1612 |
return false; |
| 1613 |
} |
| 1614 |
|
| 1615 |
/** |
| 1616 |
* Delete template |
| 1617 |
* |
| 1618 |
* @param int $id - ID of template |
| 1619 |
*/ |
| 1620 |
public function delete_template($id = false) { |
| 1621 |
|
| 1622 |
$id = (int) $id; |
| 1623 |
|
| 1624 |
$template = new Model_E2pdf_Template(); |
| 1625 |
if ($id && $template->load($id)) { |
| 1626 |
$this->deactivate_template($template->get('ID')); |
| 1627 |
$template->delete(); |
| 1628 |
return true; |
| 1629 |
} |
| 1630 |
return false; |
| 1631 |
} |
| 1632 |
|
| 1633 |
/** |
| 1634 |
* Restore template |
| 1635 |
* |
| 1636 |
* @param int $id - ID of template |
| 1637 |
*/ |
| 1638 |
public function restore_template($id = false) { |
| 1639 |
|
| 1640 |
$id = (int) $id; |
| 1641 |
|
| 1642 |
$template = new Model_E2pdf_Template(); |
| 1643 |
if ($id && $template->load($id)) { |
| 1644 |
$template->set('trash', '0'); |
| 1645 |
$template->save(); |
| 1646 |
return true; |
| 1647 |
} |
| 1648 |
return false; |
| 1649 |
} |
| 1650 |
|
| 1651 |
/** |
| 1652 |
* Duplicate template |
| 1653 |
* |
| 1654 |
* @param int $id - ID of template |
| 1655 |
*/ |
| 1656 |
public function duplicate_template($id = false) { |
| 1657 |
|
| 1658 |
$id = (int) $id; |
| 1659 |
|
| 1660 |
$template = new Model_E2pdf_Template(); |
| 1661 |
if ($id && $template->load($id)) { |
| 1662 |
$title = $template->get('title') . " " . __('(Copy)', 'e2pdf'); |
| 1663 |
$template->set('ID', false); |
| 1664 |
$template->set('uid', ''); |
| 1665 |
$template->set('title', $title); |
| 1666 |
$template->set('activated', '0'); |
| 1667 |
|
| 1668 |
if ($template->get('pdf')) { |
| 1669 |
$pdf_name = md5(time()); |
| 1670 |
$pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . "/"; |
| 1671 |
$pdf_images_dir = $pdf_dir . "images/"; |
| 1672 |
|
| 1673 |
$this->helper->create_dir($pdf_dir); |
| 1674 |
$this->helper->create_dir($pdf_images_dir); |
| 1675 |
|
| 1676 |
if (file_exists($this->helper->get('pdf_dir') . $template->get('pdf') . "/" . $template->get('pdf') . ".pdf")) { |
| 1677 |
$images = glob($this->helper->get('pdf_dir') . $template->get('pdf') . "/images/*"); |
| 1678 |
foreach ($images as $image) { |
| 1679 |
copy($image, $pdf_images_dir . pathinfo($image, PATHINFO_BASENAME)); |
| 1680 |
} |
| 1681 |
copy($this->helper->get('pdf_dir') . $template->get('pdf') . "/" . $template->get('pdf') . ".pdf", $this->helper->get('pdf_dir') . $pdf_name . "/" . $pdf_name . ".pdf"); |
| 1682 |
$template->set('pdf', $pdf_name); |
| 1683 |
} |
| 1684 |
} |
| 1685 |
$template->save(true); |
| 1686 |
return true; |
| 1687 |
} |
| 1688 |
return false; |
| 1689 |
} |
| 1690 |
|
| 1691 |
/** |
| 1692 |
* Activate template |
| 1693 |
* |
| 1694 |
* @param int $id - ID of template |
| 1695 |
*/ |
| 1696 |
public function activate_template($id = false) { |
| 1697 |
|
| 1698 |
$id = (int) $id; |
| 1699 |
|
| 1700 |
$template = new Model_E2pdf_Template(); |
| 1701 |
if ($id && $template->load($id)) { |
| 1702 |
$model_e2pdf_api = new Model_E2pdf_Api(); |
| 1703 |
$model_e2pdf_api->set(array( |
| 1704 |
'action' => 'template/activate', |
| 1705 |
'data' => array( |
| 1706 |
'template_id' => $template->get('ID'), |
| 1707 |
'template_uid' => $template->get('uid'), |
| 1708 |
'template_title' => $template->get('title') |
| 1709 |
) |
| 1710 |
)); |
| 1711 |
$request = $model_e2pdf_api->request(); |
| 1712 |
|
| 1713 |
if (!isset($request['error'])) { |
| 1714 |
$template->set('activated', 1); |
| 1715 |
$template->set('uid', $request['template_uid']); |
| 1716 |
$template->save(); |
| 1717 |
} |
| 1718 |
|
| 1719 |
return $request; |
| 1720 |
} |
| 1721 |
return false; |
| 1722 |
} |
| 1723 |
|
| 1724 |
/** |
| 1725 |
* Deactivate template |
| 1726 |
* |
| 1727 |
* @param int $id - ID of template |
| 1728 |
*/ |
| 1729 |
public function deactivate_template($id = false) { |
| 1730 |
|
| 1731 |
$id = (int) $id; |
| 1732 |
|
| 1733 |
$template = new Model_E2pdf_Template(); |
| 1734 |
if ($id && $template->load($id)) { |
| 1735 |
$model_e2pdf_api = new Model_E2pdf_Api(); |
| 1736 |
$model_e2pdf_api->set(array( |
| 1737 |
'action' => 'template/deactivate', |
| 1738 |
'data' => array( |
| 1739 |
'template_id' => $template->get('ID'), |
| 1740 |
'template_uid' => $template->get('uid'), |
| 1741 |
) |
| 1742 |
)); |
| 1743 |
$request = $model_e2pdf_api->request(); |
| 1744 |
|
| 1745 |
if (!isset($request['error'])) { |
| 1746 |
$template->set('activated', 0); |
| 1747 |
$template->set('uid', ''); |
| 1748 |
$template->save(); |
| 1749 |
} |
| 1750 |
return $request; |
| 1751 |
} |
| 1752 |
return false; |
| 1753 |
} |
| 1754 |
|
| 1755 |
/** |
| 1756 |
* Get templates list |
| 1757 |
* |
| 1758 |
* @param array() $filters - Array of filter/order conditions |
| 1759 |
* @param bool $count - Return number of templates |
| 1760 |
* |
| 1761 |
* @return mixed - IF $count - int ELSE array() |
| 1762 |
*/ |
| 1763 |
public function get_templates_list($filters = array(), $count = false) { |
| 1764 |
global $wpdb; |
| 1765 |
$model_e2pdf_template = new Model_E2pdf_Template(); |
| 1766 |
|
| 1767 |
$condition = array(); |
| 1768 |
if (isset($filters['status']) && $filters['status'] == 'trash') { |
| 1769 |
$condition = array( |
| 1770 |
'trash' => array( |
| 1771 |
'condition' => '=', |
| 1772 |
'value' => '1', |
| 1773 |
'type' => '%d' |
| 1774 |
) |
| 1775 |
); |
| 1776 |
} else { |
| 1777 |
$condition = array( |
| 1778 |
'trash' => array( |
| 1779 |
'condition' => '<>', |
| 1780 |
'value' => '1', |
| 1781 |
'type' => '%d' |
| 1782 |
) |
| 1783 |
); |
| 1784 |
} |
| 1785 |
|
| 1786 |
if (isset($filters['s']) && $filters['s']) { |
| 1787 |
$condition['title'] = array( |
| 1788 |
'condition' => 'LIKE', |
| 1789 |
'value' => '%%' . $filters['s'] . '%%', |
| 1790 |
'type' => '%s' |
| 1791 |
); |
| 1792 |
} |
| 1793 |
|
| 1794 |
$order_condition = array(); |
| 1795 |
if (isset($filters['orderby']) && isset($filters['order'])) { |
| 1796 |
$order_condition = array( |
| 1797 |
'orderby' => $filters['orderby'], |
| 1798 |
'order' => $filters['order'], |
| 1799 |
); |
| 1800 |
} else { |
| 1801 |
$order_condition = array( |
| 1802 |
'orderby' => 'id', |
| 1803 |
'order' => 'desc', |
| 1804 |
); |
| 1805 |
} |
| 1806 |
|
| 1807 |
$limit_condition = array(); |
| 1808 |
if (!$count) { |
| 1809 |
$paged = isset($filters['paged']) && $filters['paged'] ? $filters['paged'] : '0'; |
| 1810 |
$paged = (int) $paged <= 0 ? 1 : (int) $paged; |
| 1811 |
$per_page = get_option('e2pdf_templates_screen_per_page') ? get_option('e2pdf_templates_screen_per_page') : '20'; |
| 1812 |
|
| 1813 |
$limit_condition = array( |
| 1814 |
'limit' => $per_page, |
| 1815 |
'offset' => (int) ($paged - 1) * $per_page |
| 1816 |
); |
| 1817 |
} |
| 1818 |
|
| 1819 |
$where = $this->helper->load('db')->prepare_where($condition); |
| 1820 |
$orderby = $this->helper->load('db')->prepare_orderby($order_condition); |
| 1821 |
$limit = $this->helper->load('db')->prepare_limit($limit_condition); |
| 1822 |
|
| 1823 |
$tpls = $wpdb->get_results($wpdb->prepare("SELECT `ID` FROM " . $model_e2pdf_template->get_table() . $where['sql'] . $orderby . $limit . "", $where['filter'])); |
| 1824 |
|
| 1825 |
if ($count) { |
| 1826 |
return count($tpls); |
| 1827 |
} |
| 1828 |
|
| 1829 |
$templates = array(); |
| 1830 |
foreach ($tpls as $key => $tpl) { |
| 1831 |
$template = new Model_E2pdf_Template(); |
| 1832 |
$template->load($tpl->ID, false); |
| 1833 |
if (is_array($this->helper->get('license')->get('activated_templates'))) { |
| 1834 |
if ($template->get('activated') && !in_array($template->get('uid'), $this->helper->get('license')->get('activated_templates'))) { |
| 1835 |
$this->deactivate_template($template->get('ID')); |
| 1836 |
} elseif (!$template->get('activated') && in_array($template->get('uid'), $this->helper->get('license')->get('activated_templates'))) { |
| 1837 |
$this->activate_template($template->get('ID')); |
| 1838 |
} |
| 1839 |
} |
| 1840 |
|
| 1841 |
$templates[] = $template; |
| 1842 |
} |
| 1843 |
|
| 1844 |
return $templates; |
| 1845 |
} |
| 1846 |
|
| 1847 |
/** |
| 1848 |
* Get paper sizes list |
| 1849 |
* |
| 1850 |
* @return array() - Sizes list |
| 1851 |
*/ |
| 1852 |
public function get_sizes_list($size = false, $attr = false) { |
| 1853 |
|
| 1854 |
$sizes = array( |
| 1855 |
'A4' => array( |
| 1856 |
'width' => '595', |
| 1857 |
'height' => '842', |
| 1858 |
), |
| 1859 |
'LETTER' => array( |
| 1860 |
'width' => '612', |
| 1861 |
'height' => '792', |
| 1862 |
), |
| 1863 |
'NOTE' => array( |
| 1864 |
'width' => '540', |
| 1865 |
'height' => '720', |
| 1866 |
), |
| 1867 |
'LEGAL' => array( |
| 1868 |
'width' => '612', |
| 1869 |
'height' => '1008', |
| 1870 |
), |
| 1871 |
'TABLOID' => array( |
| 1872 |
'width' => '792', |
| 1873 |
'height' => '1224', |
| 1874 |
), |
| 1875 |
'EXECUTIVE' => array( |
| 1876 |
'width' => '522', |
| 1877 |
'height' => '756', |
| 1878 |
), |
| 1879 |
'POSTCARD' => array( |
| 1880 |
'width' => '283', |
| 1881 |
'height' => '416', |
| 1882 |
), |
| 1883 |
); |
| 1884 |
|
| 1885 |
if (!$size) { |
| 1886 |
return $sizes; |
| 1887 |
} else { |
| 1888 |
if (isset($sizes[$size][$attr])) { |
| 1889 |
return $sizes[$size][$attr]; |
| 1890 |
} |
| 1891 |
return false; |
| 1892 |
} |
| 1893 |
} |
| 1894 |
|
| 1895 |
/** |
| 1896 |
* Get fonts list |
| 1897 |
* |
| 1898 |
* @return array() - Fonts list |
| 1899 |
*/ |
| 1900 |
public function get_fonts($with_path = false) { |
| 1901 |
|
| 1902 |
$model_e2pdf_font = new Model_E2pdf_Font(); |
| 1903 |
$fonts = $model_e2pdf_font->get_fonts(); |
| 1904 |
|
| 1905 |
if ($with_path) { |
| 1906 |
foreach ($fonts as $key => $value) { |
| 1907 |
$fonts[$key] = array( |
| 1908 |
'key' => $value, |
| 1909 |
'subfield' => array( |
| 1910 |
'path' => $key, |
| 1911 |
), |
| 1912 |
'value' => $value |
| 1913 |
); |
| 1914 |
} |
| 1915 |
} |
| 1916 |
|
| 1917 |
return $fonts; |
| 1918 |
} |
| 1919 |
|
| 1920 |
/** |
| 1921 |
* Get font sizes list |
| 1922 |
* |
| 1923 |
* @return array() - Font sizes list |
| 1924 |
*/ |
| 1925 |
public function get_font_sizes() { |
| 1926 |
|
| 1927 |
$sizes = array(); |
| 1928 |
for ($i = 1; $i <= 72; $i++) { |
| 1929 |
$sizes[$i] = $i . 'px'; |
| 1930 |
} |
| 1931 |
return $sizes; |
| 1932 |
} |
| 1933 |
|
| 1934 |
/** |
| 1935 |
* Get line heights list |
| 1936 |
* |
| 1937 |
* @return array() - Line heights list |
| 1938 |
*/ |
| 1939 |
public function get_line_heights() { |
| 1940 |
|
| 1941 |
$line_heights = array(); |
| 1942 |
for ($i = 1; $i <= 512; $i++) { |
| 1943 |
$line_heights[$i] = $i . 'px'; |
| 1944 |
} |
| 1945 |
return $line_heights; |
| 1946 |
} |
| 1947 |
|
| 1948 |
/** |
| 1949 |
* Load metaboxes on template edit/create action |
| 1950 |
*/ |
| 1951 |
public function load_metaboxes() { |
| 1952 |
|
| 1953 |
add_meta_box( |
| 1954 |
'e2pdf_templates_save', __('Save', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_save') |
| 1955 |
); |
| 1956 |
add_meta_box( |
| 1957 |
'e2pdf_templates_builder', __('PDF Builder', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_builder') |
| 1958 |
); |
| 1959 |
add_meta_box( |
| 1960 |
'e2pdf_templates_settings', __('Settings', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_settings') |
| 1961 |
); |
| 1962 |
} |
| 1963 |
|
| 1964 |
/** |
| 1965 |
* Load javascript on template edit/create action |
| 1966 |
*/ |
| 1967 |
public function load_scripts() { |
| 1968 |
|
| 1969 |
wp_enqueue_script('postbox'); |
| 1970 |
wp_enqueue_script('jquery-ui-droppable'); |
| 1971 |
wp_enqueue_script('jquery-ui-resizable'); |
| 1972 |
wp_enqueue_script('jquery-ui-selectable'); |
| 1973 |
wp_enqueue_script('jquery-ui-dialog'); |
| 1974 |
wp_enqueue_script('jquery-ui-autocomplete'); |
| 1975 |
wp_enqueue_script('wp-color-picker'); |
| 1976 |
wp_enqueue_media(); |
| 1977 |
} |
| 1978 |
|
| 1979 |
/** |
| 1980 |
* Load styles on template edit/create action |
| 1981 |
*/ |
| 1982 |
public function load_styles($ext = false) { |
| 1983 |
wp_enqueue_style('plugin_name-admin-ui-css', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css', false, false, false); |
| 1984 |
wp_enqueue_style('wp-color-picker'); |
| 1985 |
|
| 1986 |
if ($ext) { |
| 1987 |
$extension = new Model_E2pdf_Extension(); |
| 1988 |
if ($extension->load($ext)) { |
| 1989 |
$styles = $extension->styles(); |
| 1990 |
if ($styles && is_array($styles)) { |
| 1991 |
foreach ($styles as $key => $value) { |
| 1992 |
wp_enqueue_style('e2pdf-dynamic-style-' . $key, $value, false, false, false); |
| 1993 |
} |
| 1994 |
} |
| 1995 |
} |
| 1996 |
} |
| 1997 |
} |
| 1998 |
|
| 1999 |
} |
| 2000 |
|