| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* File: /model/e2pdf-revision.php |
| 5 |
* |
| 6 |
* @package E2Pdf |
| 7 |
* @license GPLv3 |
| 8 |
* @link https://e2pdf.com |
| 9 |
*/ |
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
die('Access denied.'); |
| 12 |
} |
| 13 |
|
| 14 |
class Model_E2pdf_Revision extends Model_E2pdf_Model { |
| 15 |
|
| 16 |
private $template = array(); |
| 17 |
private $extension = null; |
| 18 |
private $table; |
| 19 |
|
| 20 |
public function __construct() { |
| 21 |
global $wpdb; |
| 22 |
parent::__construct(); |
| 23 |
$this->table = $wpdb->prefix . 'e2pdf_revisions'; |
| 24 |
} |
| 25 |
|
| 26 |
// load |
| 27 |
public function load($template_id, $full = true, $revision_id = 0) { |
| 28 |
global $wpdb; |
| 29 |
|
| 30 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 31 |
$template = $wpdb->get_row($wpdb->prepare('SELECT * FROM `' . $this->get_table() . '` WHERE template_id = %d AND revision_id = %d', $template_id, $revision_id), ARRAY_A); |
| 32 |
|
| 33 |
if ($template && $revision_id) { |
| 34 |
$this->template = $template; |
| 35 |
$extension = new Model_E2pdf_Extension(); |
| 36 |
if ($this->get('extension')) { |
| 37 |
$extension->load($this->get('extension')); |
| 38 |
} |
| 39 |
if ($this->get('item')) { |
| 40 |
$extension->set('item', $this->get('item')); |
| 41 |
} |
| 42 |
if ($this->get('template_id')) { |
| 43 |
$extension->set('template_id', $this->get('template_id')); |
| 44 |
} |
| 45 |
$this->extension = $extension; |
| 46 |
|
| 47 |
if ($full) { |
| 48 |
$this->set('fonts', $this->helper->load('convert')->unserialize($template['fonts'])); |
| 49 |
$this->set('permissions', $this->helper->load('convert')->unserialize($template['permissions'])); |
| 50 |
$this->set('properties', $this->helper->load('convert')->unserialize($template['properties'])); |
| 51 |
$this->set('actions', $this->helper->load('convert')->unserialize($template['actions'])); |
| 52 |
|
| 53 |
$model_e2pdf_page = new Model_E2pdf_Page(); |
| 54 |
$this->set('pages', $model_e2pdf_page->get_pages($this->get('template_id'), $revision_id)); |
| 55 |
} |
| 56 |
return true; |
| 57 |
} |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
public function revision($template_id = false) { |
| 62 |
global $wpdb; |
| 63 |
if ($template_id) { |
| 64 |
$template = $wpdb->get_row($wpdb->prepare('SELECT * FROM `' . $wpdb->prefix . 'e2pdf_templates` WHERE ID = %d', $template_id), ARRAY_A); |
| 65 |
if ($template) { |
| 66 |
$this->template = $template; |
| 67 |
|
| 68 |
$this->set('fonts', $this->helper->load('convert')->unserialize($template['fonts'])); |
| 69 |
$this->set('permissions', $this->helper->load('convert')->unserialize($template['permissions'])); |
| 70 |
$this->set('properties', $this->helper->load('convert')->unserialize($template['properties'])); |
| 71 |
$this->set('actions', $this->helper->load('convert')->unserialize($template['actions'])); |
| 72 |
|
| 73 |
$this->set('template_id', $template_id); |
| 74 |
|
| 75 |
$condition = array( |
| 76 |
'template_id' => array( |
| 77 |
'condition' => '=', |
| 78 |
'value' => $this->get('template_id'), |
| 79 |
'type' => '%d', |
| 80 |
), |
| 81 |
); |
| 82 |
$where = $this->helper->load('db')->prepare_where($condition); |
| 83 |
|
| 84 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 85 |
$wpdb->query($wpdb->prepare('UPDATE `' . $wpdb->prefix . 'e2pdf_elements` set revision_id = revision_id + 1 ' . $where['sql'] . '', $where['filter'])); |
| 86 |
$wpdb->query($wpdb->prepare('UPDATE `' . $wpdb->prefix . 'e2pdf_pages` set revision_id = revision_id + 1 ' . $where['sql'] . '', $where['filter'])); |
| 87 |
$wpdb->query($wpdb->prepare('UPDATE `' . $wpdb->prefix . 'e2pdf_revisions` set revision_id = revision_id + 1 ' . $where['sql'] . '', $where['filter'])); |
| 88 |
// phpcs:enable |
| 89 |
|
| 90 |
$this->save(); |
| 91 |
} |
| 92 |
} |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
public function pre_save() { |
| 97 |
|
| 98 |
$template = array( |
| 99 |
'template_id' => $this->get('template_id'), |
| 100 |
'revision_id' => '1', |
| 101 |
'title' => $this->get('title'), |
| 102 |
'pdf' => $this->get('pdf'), |
| 103 |
'updated_at' => current_time('mysql', 1), |
| 104 |
'flatten' => $this->get('flatten'), |
| 105 |
'tab_order' => $this->get('tab_order'), |
| 106 |
'format' => $this->get('format'), |
| 107 |
'resample' => $this->get('resample'), |
| 108 |
'compression' => $this->get('compression'), |
| 109 |
'optimization' => $this->get('optimization'), |
| 110 |
'appearance' => $this->get('appearance'), |
| 111 |
'width' => $this->get('width'), |
| 112 |
'height' => $this->get('height'), |
| 113 |
'extension' => $this->get('extension'), |
| 114 |
'item' => $this->get('item'), |
| 115 |
'item1' => $this->get('item1'), |
| 116 |
'item2' => $this->get('item2'), |
| 117 |
'dataset_title' => $this->get('dataset_title'), |
| 118 |
'dataset_title1' => $this->get('dataset_title1'), |
| 119 |
'dataset_title2' => $this->get('dataset_title2'), |
| 120 |
'button_title' => $this->get('button_title'), |
| 121 |
'dpdf' => $this->get('dpdf'), |
| 122 |
'inline' => $this->get('inline'), |
| 123 |
'auto' => $this->get('auto'), |
| 124 |
'rtl' => $this->get('rtl'), |
| 125 |
'font_processor' => $this->get('font_processor'), |
| 126 |
'name' => $this->get('name'), |
| 127 |
'savename' => $this->get('savename'), |
| 128 |
'password' => $this->get('password'), |
| 129 |
'owner_password' => $this->get('owner_password'), |
| 130 |
'permissions' => serialize($this->get('permissions')), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 131 |
'hooks' => $this->get('hooks'), |
| 132 |
'meta_title' => $this->get('meta_title'), |
| 133 |
'meta_subject' => $this->get('meta_subject'), |
| 134 |
'meta_author' => $this->get('meta_author'), |
| 135 |
'meta_keywords' => $this->get('meta_keywords'), |
| 136 |
'lang_code' => $this->get('lang_code'), |
| 137 |
'fonts' => serialize($this->get('fonts')), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 138 |
'font' => $this->get('font'), |
| 139 |
'font_size' => $this->get('font_size'), |
| 140 |
'font_color' => $this->get('font_color'), |
| 141 |
'line_height' => $this->get('line_height'), |
| 142 |
'text_align' => $this->get('line_height'), |
| 143 |
'author' => $this->get('author'), |
| 144 |
'properties' => serialize($this->get('properties')), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 145 |
'actions' => serialize($this->get('actions')), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 146 |
'attachments' => $this->get('attachments'), |
| 147 |
'created_at' => $this->get('created_at'), |
| 148 |
); |
| 149 |
|
| 150 |
return $template; |
| 151 |
} |
| 152 |
|
| 153 |
// template |
| 154 |
public function template() { |
| 155 |
return $this->template; |
| 156 |
} |
| 157 |
|
| 158 |
// extension |
| 159 |
public function extension() { |
| 160 |
return $this->extension; |
| 161 |
} |
| 162 |
|
| 163 |
// set |
| 164 |
public function set($key, $value) { |
| 165 |
switch ($key) { |
| 166 |
case 'format': |
| 167 |
if (in_array($value, array('pdf', 'jpg'), true)) { |
| 168 |
$this->template[$key] = $value; |
| 169 |
return true; |
| 170 |
} else { |
| 171 |
return false; |
| 172 |
} |
| 173 |
break; |
| 174 |
case 'permissions': |
| 175 |
if (!is_array($value)) { |
| 176 |
$this->template[$key] = array('printing'); |
| 177 |
} else { |
| 178 |
$this->template[$key] = $value; |
| 179 |
} |
| 180 |
break; |
| 181 |
case 'fonts': |
| 182 |
case 'properties': |
| 183 |
case 'actions': |
| 184 |
if (!is_array($value)) { |
| 185 |
$this->template[$key] = array(); |
| 186 |
} else { |
| 187 |
$this->template[$key] = $value; |
| 188 |
} |
| 189 |
break; |
| 190 |
default: |
| 191 |
$this->template[$key] = $value; |
| 192 |
break; |
| 193 |
} |
| 194 |
return true; |
| 195 |
} |
| 196 |
|
| 197 |
// get |
| 198 |
public function get($key) { |
| 199 |
if (isset($this->template[$key])) { |
| 200 |
$value = $this->template[$key]; |
| 201 |
return $value; |
| 202 |
} else { |
| 203 |
switch ($key) { |
| 204 |
case 'title': |
| 205 |
$value = __('(no title)', 'e2pdf'); |
| 206 |
break; |
| 207 |
case 'width': |
| 208 |
case 'height': |
| 209 |
case 'inline': |
| 210 |
case 'auto': |
| 211 |
case 'trash': |
| 212 |
case 'locked': |
| 213 |
case 'activated': |
| 214 |
case 'revision_id': |
| 215 |
case 'rtl': |
| 216 |
case 'tab_order': |
| 217 |
$value = '0'; |
| 218 |
break; |
| 219 |
case 'flatten': |
| 220 |
case 'appearance': |
| 221 |
$value = '1'; |
| 222 |
break; |
| 223 |
case 'compression': |
| 224 |
$value = '-1'; |
| 225 |
break; |
| 226 |
case 'optimization': |
| 227 |
$value = '-1'; |
| 228 |
break; |
| 229 |
case 'format': |
| 230 |
$value = 'pdf'; |
| 231 |
break; |
| 232 |
case 'resample': |
| 233 |
$value = '100'; |
| 234 |
break; |
| 235 |
case 'text_align': |
| 236 |
$value = 'left'; |
| 237 |
break; |
| 238 |
case 'permissions': |
| 239 |
$value = array( |
| 240 |
'printing', |
| 241 |
); |
| 242 |
break; |
| 243 |
case 'fonts': |
| 244 |
case 'properties': |
| 245 |
case 'actions': |
| 246 |
$value = array(); |
| 247 |
break; |
| 248 |
case 'pages': |
| 249 |
case 'revisions': |
| 250 |
$value = array(); |
| 251 |
break; |
| 252 |
case 'font_processor': |
| 253 |
$value = get_option('e2pdf_font_processor', '0'); |
| 254 |
break; |
| 255 |
default: |
| 256 |
$value = ''; |
| 257 |
break; |
| 258 |
} |
| 259 |
return $value; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
// get table |
| 264 |
public function get_table() { |
| 265 |
return $this->table; |
| 266 |
} |
| 267 |
|
| 268 |
// delete |
| 269 |
public function delete() { |
| 270 |
global $wpdb; |
| 271 |
if ($this->get('template_id') && $this->get('revision_id')) { |
| 272 |
|
| 273 |
if ($this->get('pdf')) { |
| 274 |
$pdf_dir = $this->helper->get('pdf_dir') . $this->get('pdf') . '/'; |
| 275 |
$this->helper->delete_dir($pdf_dir); |
| 276 |
} |
| 277 |
|
| 278 |
$where = array( |
| 279 |
'template_id' => $this->get('template_id'), |
| 280 |
'revision_id' => $this->get('revision_id'), |
| 281 |
); |
| 282 |
$wpdb->delete($this->get_table(), $where); |
| 283 |
|
| 284 |
foreach ($this->get('pages') as $page) { |
| 285 |
$model_e2pdf_page = new Model_E2pdf_Page(); |
| 286 |
$model_e2pdf_page->load($page['page_id'], $page['template_id'], $page['revision_id']); |
| 287 |
$model_e2pdf_page->delete(); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
// save |
| 293 |
public function save() { |
| 294 |
global $wpdb; |
| 295 |
|
| 296 |
$show_errors = false; |
| 297 |
if ($wpdb->show_errors) { |
| 298 |
$wpdb->show_errors(false); |
| 299 |
$show_errors = true; |
| 300 |
} |
| 301 |
|
| 302 |
if ($this->get('template_id')) { |
| 303 |
$template = $this->pre_save(); |
| 304 |
$success = $wpdb->insert($this->get_table(), $template); |
| 305 |
if ($success === false) { |
| 306 |
$this->helper->load('db')->db_init($wpdb->prefix); |
| 307 |
$wpdb->insert($this->get_table(), $template); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
if ($show_errors) { |
| 312 |
$wpdb->show_errors(); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
// flush |
| 317 |
public function flush() { |
| 318 |
if ($this->get('template_id')) { |
| 319 |
$revisions_limit = max(1, (int) get_option('e2pdf_revisions_limit', '3')); |
| 320 |
foreach ($this->revisions($this->get('template_id')) as $revision) { |
| 321 |
if ($revision['revision_id'] > $revisions_limit) { |
| 322 |
$this->load($this->get('template_id'), true, $revision['revision_id']); |
| 323 |
$this->delete(); |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
// revisions |
| 330 |
public function revisions($template_id = false) { |
| 331 |
global $wpdb; |
| 332 |
|
| 333 |
$revisions = array(); |
| 334 |
if ($template_id) { |
| 335 |
$condition = array( |
| 336 |
'template_id' => array( |
| 337 |
'condition' => '=', |
| 338 |
'value' => $template_id, |
| 339 |
'type' => '%d', |
| 340 |
), |
| 341 |
); |
| 342 |
|
| 343 |
$where = $this->helper->load('db')->prepare_where($condition); |
| 344 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 345 |
$revisions = $wpdb->get_results($wpdb->prepare('SELECT `revision_id`, `updated_at` FROM `' . $this->get_table() . '`' . $where['sql'] . ' ORDER BY revision_id ASC', $where['filter']), ARRAY_A); |
| 346 |
} |
| 347 |
|
| 348 |
return $revisions; |
| 349 |
} |
| 350 |
} |
| 351 |
|