| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* E2Pdf Convert Model |
| 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 Model_E2pdf_Convert extends Model_E2pdf_Model { |
| 17 |
|
| 18 |
protected $convert = NULL; |
| 19 |
|
| 20 |
/** |
| 21 |
* Initialize functions |
| 22 |
*/ |
| 23 |
function __construct() { |
| 24 |
parent::__construct(); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Convert Template to PHP Output |
| 29 |
* |
| 30 |
* @param array $data - Template |
| 31 |
* |
| 32 |
* @return string - Converted template |
| 33 |
*/ |
| 34 |
public function toPHP($data = array()) { |
| 35 |
$content = ""; |
| 36 |
foreach ($data as $key => $value) { |
| 37 |
if (is_array($value)) { |
| 38 |
$content .= "'" . $key . "' => ["; |
| 39 |
$content .= $this->toPHP($value); |
| 40 |
$content .= "],\n"; |
| 41 |
} else { |
| 42 |
$content .= "'" . $key . "' => '" . str_replace("'", "\'", $value) . "',\n"; |
| 43 |
} |
| 44 |
} |
| 45 |
return $content; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Set options for API Request |
| 50 |
* |
| 51 |
* @param string $key - Key |
| 52 |
* @param mixed $value - Value |
| 53 |
*/ |
| 54 |
public function set($key, $value = false) { |
| 55 |
if (!$this->convert) { |
| 56 |
$this->convert = new stdClass(); |
| 57 |
} |
| 58 |
if (is_array($key)) { |
| 59 |
foreach ($key as $attr => $value) { |
| 60 |
$this->convert->$attr = $value; |
| 61 |
} |
| 62 |
} else { |
| 63 |
$this->convert->$key = $value; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Convert Template |
| 69 |
*/ |
| 70 |
public function convert() { |
| 71 |
|
| 72 |
$data = !empty($this->convert->data) ? $this->convert->data : array(); |
| 73 |
|
| 74 |
$file = '<?php' . PHP_EOL; |
| 75 |
$file .= 'function e2pdf_template() {' . PHP_EOL; |
| 76 |
$file .= '$template = [' . PHP_EOL; |
| 77 |
$file .= $this->toPHP($data); |
| 78 |
$file .= '];' . PHP_EOL; |
| 79 |
$file .= 'return $template;' . PHP_EOL; |
| 80 |
$file .= '}'; |
| 81 |
|
| 82 |
if ($file) { |
| 83 |
$response['file'] = base64_encode($file); |
| 84 |
} else { |
| 85 |
$response['error'] = __("Something went wrong!", "e2pdf"); |
| 86 |
} |
| 87 |
return $response; |
| 88 |
} |
| 89 |
|
| 90 |
} |
| 91 |
|