| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* File: /model/e2pdf-dataset.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_Dataset extends Model_E2pdf_Model { |
| 15 |
|
| 16 |
private $dataset = array(); |
| 17 |
private $table; |
| 18 |
|
| 19 |
public function __construct() { |
| 20 |
global $wpdb; |
| 21 |
parent::__construct(); |
| 22 |
$this->table = $wpdb->prefix . 'e2pdf_datasets'; |
| 23 |
} |
| 24 |
|
| 25 |
public function get_table() { |
| 26 |
return $this->table; |
| 27 |
} |
| 28 |
|
| 29 |
public function load($id, $item, $extension) { |
| 30 |
global $wpdb; |
| 31 |
|
| 32 |
$condition = array( |
| 33 |
'ID' => array( |
| 34 |
'condition' => '=', |
| 35 |
'value' => $id, |
| 36 |
'type' => '%d', |
| 37 |
), |
| 38 |
'extension' => array( |
| 39 |
'condition' => '=', |
| 40 |
'value' => $extension, |
| 41 |
'type' => '%s', |
| 42 |
), |
| 43 |
'item' => array( |
| 44 |
'condition' => '=', |
| 45 |
'value' => $item, |
| 46 |
'type' => '%s', |
| 47 |
), |
| 48 |
); |
| 49 |
$where = $this->helper->load('db')->prepare_where($condition); |
| 50 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 51 |
$dataset = $wpdb->get_row($wpdb->prepare('SELECT * FROM `' . $this->get_table() . '`' . $where['sql'] . '', $where['filter']), ARRAY_A); |
| 52 |
|
| 53 |
if ($dataset) { |
| 54 |
$this->dataset = $dataset; |
| 55 |
$this->set('entry', $this->helper->load('convert')->unserialize($dataset['entry'])); |
| 56 |
return true; |
| 57 |
} |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
public function set($key, $value) { |
| 62 |
$this->dataset[$key] = $value; |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
public function get($key) { |
| 67 |
if (isset($this->dataset[$key])) { |
| 68 |
$value = $this->dataset[$key]; |
| 69 |
return $value; |
| 70 |
} else { |
| 71 |
switch ($key) { |
| 72 |
case 'entry': |
| 73 |
$value = array(); |
| 74 |
break; |
| 75 |
default: |
| 76 |
$value = ''; |
| 77 |
break; |
| 78 |
} |
| 79 |
return $value; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public function save() { |
| 84 |
global $wpdb; |
| 85 |
|
| 86 |
$dataset = array( |
| 87 |
'extension' => $this->get('extension'), |
| 88 |
'item' => $this->get('item'), |
| 89 |
'entry' => serialize($this->get('entry')), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 90 |
); |
| 91 |
if (!$this->get('ID')) { |
| 92 |
$dataset['created_at'] = current_time('mysql', 1); |
| 93 |
} |
| 94 |
|
| 95 |
$show_errors = false; |
| 96 |
if ($wpdb->show_errors) { |
| 97 |
$wpdb->show_errors(false); |
| 98 |
$show_errors = true; |
| 99 |
} |
| 100 |
|
| 101 |
if ($this->get('ID')) { |
| 102 |
$where = array( |
| 103 |
'ID' => $this->get('ID'), |
| 104 |
); |
| 105 |
$success = $wpdb->update($this->get_table(), $dataset, $where); |
| 106 |
if ($success === false) { |
| 107 |
$this->helper->load('db')->db_init($wpdb->prefix); |
| 108 |
$wpdb->update($this->get_table(), $dataset, $where); |
| 109 |
} |
| 110 |
} else { |
| 111 |
$success = $wpdb->insert($this->get_table(), $dataset); |
| 112 |
if ($success === false) { |
| 113 |
$this->helper->load('db')->db_init($wpdb->prefix); |
| 114 |
$wpdb->insert($this->get_table(), $dataset); |
| 115 |
} |
| 116 |
$this->set('ID', $wpdb->insert_id); |
| 117 |
} |
| 118 |
|
| 119 |
if ($show_errors) { |
| 120 |
$wpdb->show_errors(); |
| 121 |
} |
| 122 |
|
| 123 |
return $this->get('ID'); |
| 124 |
} |
| 125 |
} |
| 126 |
|