| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* E2pdf Template Model |
| 5 |
* |
| 6 |
* @copyright Copyright 2017 https://e2pdf.com |
| 7 |
* @license GPL v2 |
| 8 |
* @version 1 |
| 9 |
* @link https://e2pdf.com |
| 10 |
* @since 0.01.33 |
| 11 |
*/ |
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
die('Access denied.'); |
| 14 |
} |
| 15 |
|
| 16 |
class Model_E2pdf_Entry extends Model_E2pdf_Model { |
| 17 |
|
| 18 |
private $table; |
| 19 |
private $key; |
| 20 |
private $entry = array(); |
| 21 |
|
| 22 |
/* |
| 23 |
* On Template init |
| 24 |
*/ |
| 25 |
|
| 26 |
function __construct() { |
| 27 |
global $wpdb; |
| 28 |
parent::__construct(); |
| 29 |
$this->table = $wpdb->prefix . 'e2pdf_entries'; |
| 30 |
$this->key = hash('sha256', md5(NONCE_KEY)); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Load Template by ID |
| 35 |
* |
| 36 |
* @param int $entry_id - ID of template |
| 37 |
*/ |
| 38 |
public function load($entry_id = false) { |
| 39 |
global $wpdb; |
| 40 |
$entry = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$this->get_table()}` WHERE ID = %d", $entry_id), ARRAY_A); |
| 41 |
if ($entry) { |
| 42 |
$this->entry = $entry; |
| 43 |
$this->set('entry', unserialize($entry['entry'])); |
| 44 |
return true; |
| 45 |
} |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Load Entry by UID |
| 51 |
* |
| 52 |
* @param int $entry_uid - ID of template |
| 53 |
*/ |
| 54 |
public function load_by_uid($entry_uid = false) { |
| 55 |
global $wpdb; |
| 56 |
if ($entry_uid) { |
| 57 |
$entry = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$this->get_table()}` WHERE uid = %s", $entry_uid), ARRAY_A); |
| 58 |
if ($entry) { |
| 59 |
$this->entry = $entry; |
| 60 |
$this->set('entry', unserialize($entry['entry'])); |
| 61 |
return true; |
| 62 |
} |
| 63 |
} |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Get loaded Template |
| 69 |
* |
| 70 |
* @return object |
| 71 |
*/ |
| 72 |
public function get_entry() { |
| 73 |
return $this->entry; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Set Entry attribute |
| 78 |
* |
| 79 |
* @param string $key - Attribute Key |
| 80 |
* @param string $value - Attribute Value |
| 81 |
*/ |
| 82 |
public function set($key, $value) { |
| 83 |
$this->entry[$key] = $value; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get Entry attribute by Key |
| 88 |
* |
| 89 |
* @param string $key - Attribute Key |
| 90 |
* |
| 91 |
* @return mixed |
| 92 |
*/ |
| 93 |
public function get($key) { |
| 94 |
if (isset($this->entry[$key])) { |
| 95 |
$value = $this->entry[$key]; |
| 96 |
return $value; |
| 97 |
} else { |
| 98 |
switch ($key) { |
| 99 |
case 'pdf_num': |
| 100 |
$value = 0; |
| 101 |
break; |
| 102 |
|
| 103 |
case 'entry': |
| 104 |
$value = array(); |
| 105 |
break; |
| 106 |
|
| 107 |
case 'uid': |
| 108 |
$value = md5(md5(serialize($this->get('entry'))) . md5($this->key)); |
| 109 |
break; |
| 110 |
|
| 111 |
default: |
| 112 |
$value = ''; |
| 113 |
break; |
| 114 |
} |
| 115 |
return $value; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Before save template |
| 121 |
*/ |
| 122 |
public function pre_save() { |
| 123 |
|
| 124 |
$entry = array( |
| 125 |
'uid' => $this->get('uid'), |
| 126 |
'entry' => serialize($this->get('entry')), |
| 127 |
'pdf_num' => $this->get('pdf_num') |
| 128 |
); |
| 129 |
return $entry; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Save entry |
| 134 |
*/ |
| 135 |
public function save() { |
| 136 |
global $wpdb; |
| 137 |
|
| 138 |
$entry = $this->pre_save(); |
| 139 |
|
| 140 |
if ($this->get('ID')) { |
| 141 |
$where = array( |
| 142 |
'ID' => $this->get('ID') |
| 143 |
); |
| 144 |
$wpdb->update($this->get_table(), $entry, $where); |
| 145 |
} else { |
| 146 |
$wpdb->insert($this->get_table(), $entry); |
| 147 |
$this->set('ID', $wpdb->insert_id); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Delete loaded entry |
| 153 |
*/ |
| 154 |
public function delete() { |
| 155 |
global $wpdb; |
| 156 |
if ($this->get('ID')) { |
| 157 |
$where = array( |
| 158 |
'ID' => $this->get('ID') |
| 159 |
); |
| 160 |
$wpdb->delete($this->get_table(), $where); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
public function get_table() { |
| 165 |
return $this->table; |
| 166 |
} |
| 167 |
|
| 168 |
} |
| 169 |
|