record.php
178 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Base class for models |
| 4 | * |
| 5 | * @author Maksym Tsypliakov <maksym.tsypliakov@gmail.com> |
| 6 | */ |
| 7 | class PMXI_Model_Record extends PMXI_Model { |
| 8 | /** |
| 9 | * Initialize model |
| 10 | * @param array[optional] $data Array of record data to initialize object with |
| 11 | */ |
| 12 | public function __construct($data = array()) { |
| 13 | parent::__construct(); |
| 14 | if (! is_array($data)) { |
| 15 | throw new Exception(esc_html("Array expected as paramenter for " . get_class($this) . "::" . __METHOD__)); |
| 16 | } |
| 17 | $data and $this->set($data); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * @see PMXI_Model::getBy() |
| 22 | * @return PMXI_Model_Record |
| 23 | */ |
| 24 | public function getBy($field = NULL, $value = NULL) { |
| 25 | if (is_null($field)) { |
| 26 | throw new Exception(esc_html("Field parameter is expected at " . get_class($this) . "::" . __METHOD__)); |
| 27 | } |
| 28 | $sql = "SELECT * FROM $this->table WHERE " . $this->buildWhere($field, $value); |
| 29 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 30 | $result = $this->wpdb->get_row($sql, ARRAY_A); |
| 31 | if (is_array($result)) { |
| 32 | foreach ($result as $k => $v) { |
| 33 | if (is_serialized($v)) { |
| 34 | $result[$k] = \pmxi_maybe_unserialize($v); |
| 35 | } |
| 36 | } |
| 37 | $this->exchangeArray($result); |
| 38 | } else { |
| 39 | $this->clear(); |
| 40 | } |
| 41 | return $this; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Ger records related to current one |
| 46 | * @param string $model Class name of model of related records |
| 47 | * @param array[optoinal] $keyAssoc |
| 48 | * @return PMXI_Model_List |
| 49 | */ |
| 50 | public function getRelated($model, $keyAssoc = NULL) { |
| 51 | $related = new $model(); |
| 52 | if ( ! empty($this->id)) { |
| 53 | if (is_null($keyAssoc)) { |
| 54 | $defaultPrefix = strtolower(preg_replace('%^' . strtoupper(PMXI_Plugin::PREFIX) . '|_Record$%', '', get_class($this))); |
| 55 | $keyAssoc = array(); |
| 56 | foreach ($this->primary as $key) { |
| 57 | $keyAssoc = array($defaultPrefix . '_' . $key => $key); |
| 58 | } |
| 59 | } |
| 60 | foreach ($keyAssoc as $foreign => $local) { |
| 61 | $keyAssoc[$foreign] = $this->$local; |
| 62 | } |
| 63 | $related->getBy($keyAssoc); |
| 64 | } |
| 65 | return $related instanceof PMXI_Model_List ? $related->convertRecords() : $related; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Saves currently set object data as database record |
| 70 | * @return PMXI_Model_Record |
| 71 | */ |
| 72 | public function insert() { |
| 73 | if ($this->wpdb->insert($this->table, $this->toArray(TRUE))) { |
| 74 | if (isset($this->auto_increment)) { |
| 75 | $this[$this->primary[0]] = $this->wpdb->insert_id; |
| 76 | } |
| 77 | return $this; |
| 78 | } else { |
| 79 | throw new Exception(esc_html($this->wpdb->last_error)); |
| 80 | } |
| 81 | } |
| 82 | /** |
| 83 | * Update record in database |
| 84 | * @return PMXI_Model_Record |
| 85 | */ |
| 86 | public function update() { |
| 87 | $record = $this->toArray(TRUE); |
| 88 | $this->wpdb->update($this->table, $record, array_intersect_key($record, array_flip($this->primary))); |
| 89 | if ($this->wpdb->last_error) { |
| 90 | throw new Exception(esc_html($this->wpdb->last_error)); |
| 91 | } |
| 92 | return $this; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Delete record form database |
| 97 | * @return PMXI_Model_Record |
| 98 | */ |
| 99 | public function delete() { |
| 100 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 101 | if ($this->wpdb->query("DELETE FROM $this->table WHERE " . $this->buildWhere(array_intersect_key($this->toArray(TRUE), array_flip($this->primary))))) { |
| 102 | return $this; |
| 103 | } elseif ($this->wpdb->last_error) { |
| 104 | throw new Exception(esc_html($this->wpdb->last_error)); |
| 105 | } |
| 106 | } |
| 107 | /** |
| 108 | * Insert or Update the record |
| 109 | * WARNING: function doesn't check actual record presents in database, it simply tries to insert if no primary key specified and update otherwise |
| 110 | * @return PMXI_Model_Record |
| 111 | */ |
| 112 | public function save() { |
| 113 | if (array_intersect_key($this->toArray(TRUE), array_flip($this->primary))) { |
| 114 | $this->update(); |
| 115 | } else { |
| 116 | $this->insert(); |
| 117 | } |
| 118 | return $this; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Set record data |
| 123 | * When 1st parameter is an array, it expected to be an associative array of field => value pairs |
| 124 | * If 2 parameters are set, first one is expected to be a field name and second - it's value |
| 125 | * |
| 126 | * @param string|array $field |
| 127 | * @param mixed[optional] $value |
| 128 | * @return PMXI_Model_Record |
| 129 | */ |
| 130 | public function set($field, $value = NULL) { |
| 131 | if (is_array($field) and ( ! is_null($value) or 0 == count($field))) { |
| 132 | throw new Exception(esc_html(__CLASS__ . "::set method expects either not empty associative array as the only paramter or field name and it's value as two seperate parameters.")); |
| 133 | } |
| 134 | if (is_array($field)) { |
| 135 | $this->exchangeArray(array_merge($this->toArray(), $field)); |
| 136 | } else { |
| 137 | $this[$field] = $value; |
| 138 | } |
| 139 | |
| 140 | return $this; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Magic method to resolved object-like request to record values in format $obj->%FIELD_NAME% |
| 145 | * @param string $field |
| 146 | * @return mixed |
| 147 | */ |
| 148 | public function __get($field) { |
| 149 | if ( ! $this->offsetExists($field)) { |
| 150 | throw new Exception(esc_html("Undefined field $field.")); |
| 151 | } |
| 152 | return $this[$field]; |
| 153 | } |
| 154 | /** |
| 155 | * Magic method to assign values to record fields in format $obj->%FIELD_NAME = value |
| 156 | * @param string $field |
| 157 | * @param mixed $value |
| 158 | */ |
| 159 | public function __set($field, $value) { |
| 160 | $this[$field] = $value; |
| 161 | } |
| 162 | /** |
| 163 | * Magic method to check wether some record fields are set |
| 164 | * @param string $field |
| 165 | * @return bool |
| 166 | */ |
| 167 | public function __isset($field) { |
| 168 | return $this->offsetExists($field); |
| 169 | } |
| 170 | /** |
| 171 | * Magic method to unset record fields |
| 172 | * @param string $field |
| 173 | */ |
| 174 | public function __unset($field) { |
| 175 | $this->offsetUnset($field); |
| 176 | } |
| 177 | |
| 178 | } |