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