| 1 |
<?php |
| 2 |
/** |
| 3 |
* Quote Log Model |
| 4 |
* |
| 5 |
* @package EasyInvoice |
| 6 |
* @author Your Name |
| 7 |
* @copyright Copyright (c) 2023, Your Company |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace EasyInvoice\Models; |
| 13 |
|
| 14 |
/** |
| 15 |
* Quote Log Model |
| 16 |
* |
| 17 |
* Represents a log entry for quote activities. |
| 18 |
* |
| 19 |
* @since 1.0.0 |
| 20 |
*/ |
| 21 |
class QuoteLog { |
| 22 |
/** |
| 23 |
* Log ID |
| 24 |
* |
| 25 |
* @var int |
| 26 |
*/ |
| 27 |
private $id; |
| 28 |
|
| 29 |
/** |
| 30 |
* Dynamic data storage for all fields |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
private $data = []; |
| 35 |
|
| 36 |
/** |
| 37 |
* Modified flag |
| 38 |
* |
| 39 |
* @var bool |
| 40 |
*/ |
| 41 |
private $is_modified = false; |
| 42 |
|
| 43 |
/** |
| 44 |
* Constructor |
| 45 |
* |
| 46 |
* @since 1.0.0 |
| 47 |
* @param array $data Log data |
| 48 |
*/ |
| 49 |
public function __construct(array $data = []) { |
| 50 |
$this->id = $data['id'] ?? 0; |
| 51 |
$this->data = $data; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Magic getter for dynamic properties |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
* @param string $name Property name |
| 59 |
* @return mixed Property value or null if not found |
| 60 |
*/ |
| 61 |
public function __get($name) { |
| 62 |
return $this->data[$name] ?? null; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Magic setter for dynamic properties |
| 67 |
* |
| 68 |
* @since 1.0.0 |
| 69 |
* @param string $name Property name |
| 70 |
* @param mixed $value Property value |
| 71 |
*/ |
| 72 |
public function __set($name, $value) { |
| 73 |
$this->data[$name] = $value; |
| 74 |
$this->is_modified = true; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Magic isset for dynamic properties |
| 79 |
* |
| 80 |
* @since 1.0.0 |
| 81 |
* @param string $name Property name |
| 82 |
* @return bool True if property exists, false otherwise |
| 83 |
*/ |
| 84 |
public function __isset($name) { |
| 85 |
return isset($this->data[$name]); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Magic unset for dynamic properties |
| 90 |
* |
| 91 |
* @since 1.0.0 |
| 92 |
* @param string $name Property name |
| 93 |
*/ |
| 94 |
public function __unset($name) { |
| 95 |
unset($this->data[$name]); |
| 96 |
$this->is_modified = true; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Dynamic getter method |
| 101 |
* |
| 102 |
* @since 1.0.0 |
| 103 |
* @param string $name Method name |
| 104 |
* @param array $arguments Method arguments |
| 105 |
* @return mixed |
| 106 |
*/ |
| 107 |
public function __call($name, $arguments) { |
| 108 |
// Handle getter methods (getFieldName) |
| 109 |
if (strpos($name, 'get') === 0) { |
| 110 |
$field_name = $this->camelCaseToSnakeCase(substr($name, 3)); // Remove 'get' prefix |
| 111 |
return $this->__get($field_name); |
| 112 |
} |
| 113 |
|
| 114 |
// Handle setter methods (setFieldName) |
| 115 |
if (strpos($name, 'set') === 0) { |
| 116 |
$field_name = $this->camelCaseToSnakeCase(substr($name, 3)); // Remove 'set' prefix |
| 117 |
$value = $arguments[0] ?? null; |
| 118 |
$this->__set($field_name, $value); |
| 119 |
return null; |
| 120 |
} |
| 121 |
|
| 122 |
// Handle isset methods (isFieldName) |
| 123 |
if (strpos($name, 'is') === 0) { |
| 124 |
$field_name = $this->camelCaseToSnakeCase(substr($name, 2)); // Remove 'is' prefix |
| 125 |
return (bool) $this->__get($field_name); |
| 126 |
} |
| 127 |
|
| 128 |
// Handle has methods (hasFieldName) |
| 129 |
if (strpos($name, 'has') === 0) { |
| 130 |
$field_name = $this->camelCaseToSnakeCase(substr($name, 3)); // Remove 'has' prefix |
| 131 |
return !empty($this->__get($field_name)); |
| 132 |
} |
| 133 |
|
| 134 |
throw new \BadMethodCallException("Method $name does not exist"); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Convert camelCase to snake_case |
| 139 |
* |
| 140 |
* @since 1.0.0 |
| 141 |
* @param string $camelCase |
| 142 |
* @return string |
| 143 |
*/ |
| 144 |
private function camelCaseToSnakeCase($camelCase) { |
| 145 |
return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $camelCase)); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Get log ID |
| 150 |
* |
| 151 |
* @since 1.0.0 |
| 152 |
* @return int |
| 153 |
*/ |
| 154 |
public function getId(): int { |
| 155 |
return $this->id; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Set log ID |
| 160 |
* |
| 161 |
* @since 1.0.0 |
| 162 |
* @param int $id |
| 163 |
*/ |
| 164 |
public function setId(int $id): void { |
| 165 |
$this->id = $id; |
| 166 |
$this->is_modified = true; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Convert to array |
| 171 |
* |
| 172 |
* @since 1.0.0 |
| 173 |
* @return array |
| 174 |
*/ |
| 175 |
public function toArray(): array { |
| 176 |
return $this->data; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Get formatted date |
| 181 |
* |
| 182 |
* @since 1.0.0 |
| 183 |
* @return string |
| 184 |
*/ |
| 185 |
public function getFormattedDate(): string { |
| 186 |
if (empty($this->created_date)) { |
| 187 |
return ''; |
| 188 |
} |
| 189 |
return date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($this->created_date)); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Get action label |
| 194 |
* |
| 195 |
* @since 1.0.0 |
| 196 |
* @return string |
| 197 |
*/ |
| 198 |
public function getActionLabel(): string { |
| 199 |
$labels = [ |
| 200 |
'created' => __('Created', 'easy-invoice'), |
| 201 |
'updated' => __('Updated', 'easy-invoice'), |
| 202 |
'accepted' => __('Accepted', 'easy-invoice'), |
| 203 |
'declined' => __('Declined', 'easy-invoice'), |
| 204 |
'sent' => __('Sent', 'easy-invoice'), |
| 205 |
'viewed' => __('Viewed', 'easy-invoice'), |
| 206 |
'status_changed' => __('Status Changed', 'easy-invoice'), |
| 207 |
'converted_to_invoice' => __('Converted to Invoice', 'easy-invoice'), |
| 208 |
'duplicated_to_invoice' => __('Duplicated to Invoice', 'easy-invoice'), |
| 209 |
'deleted' => __('Deleted', 'easy-invoice'), |
| 210 |
'restored' => __('Restored', 'easy-invoice'), |
| 211 |
]; |
| 212 |
|
| 213 |
return $labels[$this->action] ?? ucfirst($this->action); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Get additional data as array |
| 218 |
* |
| 219 |
* @since 1.0.0 |
| 220 |
* @return array |
| 221 |
*/ |
| 222 |
public function getAdditionalDataArray(): array { |
| 223 |
if (empty($this->additional_data)) { |
| 224 |
return []; |
| 225 |
} |
| 226 |
|
| 227 |
$data = json_decode($this->additional_data, true); |
| 228 |
return is_array($data) ? $data : []; |
| 229 |
} |
| 230 |
} |