| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* File: /model/e2pdf-page.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_Page extends Model_E2pdf_Model { |
| 15 |
|
| 16 |
private $page = array(); |
| 17 |
private $table; |
| 18 |
|
| 19 |
public function __construct() { |
| 20 |
global $wpdb; |
| 21 |
parent::__construct(); |
| 22 |
$this->table = $wpdb->prefix . 'e2pdf_pages'; |
| 23 |
} |
| 24 |
|
| 25 |
public function get_table() { |
| 26 |
return $this->table; |
| 27 |
} |
| 28 |
|
| 29 |
public function set($key, $value) { |
| 30 |
switch ($key) { |
| 31 |
case 'properties': |
| 32 |
case 'actions': |
| 33 |
if (!is_array($value)) { |
| 34 |
$this->page[$key] = array(); |
| 35 |
} else { |
| 36 |
$this->page[$key] = $value; |
| 37 |
} |
| 38 |
break; |
| 39 |
|
| 40 |
default: |
| 41 |
$this->page[$key] = $value; |
| 42 |
break; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
public function get($key) { |
| 47 |
if (isset($this->page[$key])) { |
| 48 |
$value = $this->page[$key]; |
| 49 |
return $value; |
| 50 |
} else { |
| 51 |
switch ($key) { |
| 52 |
case 'template_id': |
| 53 |
case 'revision_id': |
| 54 |
$value = 0; |
| 55 |
break; |
| 56 |
case 'properties': |
| 57 |
case 'actions': |
| 58 |
$value = array(); |
| 59 |
break; |
| 60 |
default: |
| 61 |
$value = ''; |
| 62 |
break; |
| 63 |
} |
| 64 |
return $value; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public function get_page() { |
| 69 |
return $this->page; |
| 70 |
} |
| 71 |
|
| 72 |
public function before_save() { |
| 73 |
$page = array( |
| 74 |
'template_id' => $this->get('template_id'), |
| 75 |
'page_id' => $this->get('page_id'), |
| 76 |
'properties' => serialize($this->get('properties')), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 77 |
'actions' => serialize($this->get('actions')), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 78 |
'revision_id' => $this->get('revision_id'), |
| 79 |
); |
| 80 |
return $page; |
| 81 |
} |
| 82 |
|
| 83 |
public function save() { |
| 84 |
global $wpdb; |
| 85 |
|
| 86 |
$page = $this->before_save(); |
| 87 |
if ($this->get('page_id') && $this->get('template_id')) { |
| 88 |
$show_errors = false; |
| 89 |
if ($wpdb->show_errors) { |
| 90 |
$wpdb->show_errors(false); |
| 91 |
$show_errors = true; |
| 92 |
} |
| 93 |
|
| 94 |
$success = $wpdb->insert($this->get_table(), $page); |
| 95 |
if ($success === false) { |
| 96 |
$this->helper->load('db')->db_init($wpdb->prefix); |
| 97 |
$wpdb->insert($this->get_table(), $page); |
| 98 |
} |
| 99 |
|
| 100 |
if ($show_errors) { |
| 101 |
$wpdb->show_errors(); |
| 102 |
} |
| 103 |
return $this->get('page_id'); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public function load($page_id, $template_id, $revision_id = 0) { |
| 108 |
global $wpdb; |
| 109 |
|
| 110 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 111 |
$page = $wpdb->get_row($wpdb->prepare('SELECT * FROM `' . $this->get_table() . '` WHERE page_id = %d AND template_id = %d AND revision_id = %d', $page_id, $template_id, $revision_id), ARRAY_A); |
| 112 |
if ($page) { |
| 113 |
$this->page = $page; |
| 114 |
$this->set('properties', $this->helper->load('convert')->unserialize($page['properties'])); |
| 115 |
$this->set('actions', $this->helper->load('convert')->unserialize($page['actions'])); |
| 116 |
$model_e2pdf_element = new Model_E2pdf_Element(); |
| 117 |
$this->set('elements', $model_e2pdf_element->get_elements($this->get('page_id'), $this->get('template_id'), $this->get('revision_id'))); |
| 118 |
return true; |
| 119 |
} |
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
public function delete() { |
| 124 |
global $wpdb; |
| 125 |
if ($this->get('page_id') && $this->get('template_id')) { |
| 126 |
$where = array( |
| 127 |
'page_id' => $this->get('page_id'), |
| 128 |
'template_id' => $this->get('template_id'), |
| 129 |
'revision_id' => $this->get('revision_id'), |
| 130 |
); |
| 131 |
$wpdb->delete($this->get_table(), $where); |
| 132 |
|
| 133 |
foreach ($this->get('elements') as $element) { |
| 134 |
$model_e2pdf_element = new Model_E2pdf_Element(); |
| 135 |
$model_e2pdf_element->load($element['element_id'], $element['template_id'], $element['revision_id']); |
| 136 |
$model_e2pdf_element->delete(); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
public function get_pages($template_id, $revision_id = 0) { |
| 142 |
global $wpdb; |
| 143 |
|
| 144 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 145 |
$pages = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . $this->get_table() . '` WHERE template_id = %d AND revision_id = %d ORDER BY page_id ASC', $template_id, $revision_id), ARRAY_A); |
| 146 |
if ($pages) { |
| 147 |
$pages_list = array(); |
| 148 |
$pages_list[] = array(); |
| 149 |
foreach ($pages as $page_key => $page) { |
| 150 |
$this->load($page['page_id'], $page['template_id'], $page['revision_id']); |
| 151 |
$pages_list[] = $this->get_page(); |
| 152 |
} |
| 153 |
unset($pages_list[0]); |
| 154 |
return $pages_list; |
| 155 |
} |
| 156 |
return array(); |
| 157 |
} |
| 158 |
} |
| 159 |
|