| 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.00.01 |
| 11 |
*/ |
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
die('Access denied.'); |
| 14 |
} |
| 15 |
|
| 16 |
class Model_E2pdf_Element extends Model_E2pdf_Model { |
| 17 |
|
| 18 |
private $element = array(); |
| 19 |
private $table; |
| 20 |
|
| 21 |
function __construct() { |
| 22 |
global $wpdb; |
| 23 |
parent::__construct(); |
| 24 |
$this->table = $wpdb->prefix . 'e2pdf_elements'; |
| 25 |
} |
| 26 |
|
| 27 |
public function get_table() { |
| 28 |
return $this->table; |
| 29 |
} |
| 30 |
|
| 31 |
public function set($key, $value) { |
| 32 |
$this->element[$key] = $value; |
| 33 |
} |
| 34 |
|
| 35 |
public function get($key) { |
| 36 |
if (isset($this->element[$key])) { |
| 37 |
$value = $this->element[$key]; |
| 38 |
return $value; |
| 39 |
} else { |
| 40 |
switch ($key) { |
| 41 |
case 'top': |
| 42 |
case 'left': |
| 43 |
case 'width': |
| 44 |
case 'height': |
| 45 |
case 'page_id': |
| 46 |
case 'template_id': |
| 47 |
$value = 0; |
| 48 |
break; |
| 49 |
|
| 50 |
case 'properties': |
| 51 |
case 'actions': |
| 52 |
$value = array(); |
| 53 |
break; |
| 54 |
|
| 55 |
default: |
| 56 |
$value = ''; |
| 57 |
break; |
| 58 |
} |
| 59 |
return $value; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public function get_element() { |
| 64 |
return $this->element; |
| 65 |
} |
| 66 |
|
| 67 |
public function before_save() { |
| 68 |
$element = array( |
| 69 |
'type' => $this->get('type'), |
| 70 |
'page_id' => $this->get('page_id'), |
| 71 |
'template_id' => $this->get('template_id'), |
| 72 |
'element_id' => $this->get('element_id'), |
| 73 |
'name' => $this->get('name'), |
| 74 |
'top' => $this->get('top'), |
| 75 |
'left' => $this->get('left'), |
| 76 |
'width' => $this->get('width'), |
| 77 |
'height' => $this->get('height'), |
| 78 |
'value' => $this->get('value'), |
| 79 |
'properties' => serialize($this->get('properties')), |
| 80 |
'actions' => serialize($this->get('actions')), |
| 81 |
); |
| 82 |
return $element; |
| 83 |
} |
| 84 |
|
| 85 |
public function save() { |
| 86 |
global $wpdb; |
| 87 |
|
| 88 |
$element = $this->before_save(); |
| 89 |
|
| 90 |
if ($this->get('element_id') && $this->get('page_id') && $this->get('template_id')) { |
| 91 |
$wpdb->insert($this->get_table(), $element); |
| 92 |
return $this->get('element_id'); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
public function load($element_id, $template_id) { |
| 97 |
global $wpdb; |
| 98 |
$element = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$this->get_table()}` WHERE element_id = %d AND template_id = %d", $element_id, $template_id), ARRAY_A); |
| 99 |
if ($element) { |
| 100 |
$this->element = $element; |
| 101 |
$this->set('properties', unserialize($element['properties'])); |
| 102 |
$this->set('actions', unserialize($element['actions'])); |
| 103 |
return true; |
| 104 |
} |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
public function get_last_element_id($template_id) { |
| 109 |
global $wpdb; |
| 110 |
|
| 111 |
$element = false; |
| 112 |
if ($template_id) { |
| 113 |
$element = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$this->get_table()}` WHERE template_id = %d ORDER BY element_id DESC", $template_id), ARRAY_A); |
| 114 |
} |
| 115 |
|
| 116 |
if ($element) { |
| 117 |
return $element['element_id']; |
| 118 |
} else { |
| 119 |
return 0; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
public function delete() { |
| 124 |
global $wpdb; |
| 125 |
if ($this->get('element_id') && $this->get('template_id')) { |
| 126 |
$where = array( |
| 127 |
'element_id' => $this->get('element_id'), |
| 128 |
'template_id' => $this->get('template_id') |
| 129 |
); |
| 130 |
$wpdb->delete($this->get_table(), $where); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
public function get_elements($page_id, $template_id) { |
| 135 |
global $wpdb; |
| 136 |
$elements = $wpdb->get_results($wpdb->prepare("SELECT * FROM `{$this->get_table()}` WHERE page_id = %d AND template_id = %d", $page_id, $template_id), ARRAY_A); |
| 137 |
if ($elements) { |
| 138 |
$elements_list = array(); |
| 139 |
foreach ($elements as $element_key => $element) { |
| 140 |
$this->load($element['element_id'], $element['template_id']); |
| 141 |
$elements_list[] = $this->get_element(); |
| 142 |
} |
| 143 |
return $elements_list; |
| 144 |
} |
| 145 |
return array(); |
| 146 |
} |
| 147 |
|
| 148 |
} |
| 149 |
|