controllers
2 days ago
controller.php
2 days ago
index.html
2 days ago
model.php
2 days ago
table.php
2 days ago
view.php
2 days ago
model.php
333 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * This class implements helpful methods for models instances. |
| 16 | * JModelBaseUI is a placeholder used to support both JModel and JModelLegacy. |
| 17 | * |
| 18 | * @since 1.7 |
| 19 | */ |
| 20 | class JModelVAP extends JModelBaseUI |
| 21 | { |
| 22 | /** |
| 23 | * Returns a Model object, always creating it. |
| 24 | * |
| 25 | * @param string $type The model type to instantiate. |
| 26 | * @param string $prefix Prefix for the model class name. |
| 27 | * @param array $config Configuration array for model. |
| 28 | * |
| 29 | * @return mixed A model instance or false on failure. |
| 30 | */ |
| 31 | public static function getInstance($type, $prefix = '', $config = array()) |
| 32 | { |
| 33 | if (!$prefix) |
| 34 | { |
| 35 | // use default system prefix |
| 36 | $prefix = 'VikAppointmentsModel'; |
| 37 | } |
| 38 | |
| 39 | // invoke parent to complete instantiation |
| 40 | return parent::getInstance($type, $prefix, $config); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Basic item loading implementation. |
| 45 | * |
| 46 | * @param mixed $pk An optional primary key value to load the row by, or an array of fields to match. |
| 47 | * If not set the instance property value is used. |
| 48 | * @param boolean $new True to return an empty object if missing. |
| 49 | * |
| 50 | * @return mixed The record object on success, null otherwise. |
| 51 | */ |
| 52 | public function getItem($pk, $new = false) |
| 53 | { |
| 54 | $table = $this->getTable(); |
| 55 | |
| 56 | // do not save in the user state the loaded columns |
| 57 | $table->_setUserState = false; |
| 58 | |
| 59 | // reset table to make sure we obtain valid values |
| 60 | $table->reset(); |
| 61 | |
| 62 | /** |
| 63 | * We need to avoid using a default value equals to '' in case |
| 64 | * the default value is set to an empty string via SQL. |
| 65 | * |
| 66 | * @since 1.7.1 |
| 67 | */ |
| 68 | foreach ($table->getProperties() as $k => $v) |
| 69 | { |
| 70 | // check whether the value is equals to an empty string |
| 71 | if (is_string($v) && preg_match("/^(\"\"|'')$/", $v)) |
| 72 | { |
| 73 | // use an empty string |
| 74 | $table->{$k} = ''; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // attempt to load record |
| 79 | $loaded = ($pk && $table->load($pk)); |
| 80 | |
| 81 | // enable user state again |
| 82 | $table->_setUserState = true; |
| 83 | |
| 84 | if ($loaded || $new === true) |
| 85 | { |
| 86 | // loaded successfully or requested an empty object |
| 87 | return (object) $table->getProperties(); |
| 88 | } |
| 89 | |
| 90 | // something went wrong, try to obtain an error |
| 91 | $error = $table->getError($get_last = null, $string = true); |
| 92 | |
| 93 | if ($error) |
| 94 | { |
| 95 | // error found, register it within the model |
| 96 | $this->setError($error); |
| 97 | } |
| 98 | |
| 99 | return null; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Basic save implementation. |
| 104 | * |
| 105 | * @param mixed $data Either an array or an object of data to save. |
| 106 | * |
| 107 | * @return mixed The ID of the record on success, false otherwise. |
| 108 | */ |
| 109 | public function save($data) |
| 110 | { |
| 111 | $table = $this->getTable(); |
| 112 | |
| 113 | // attempt to save data |
| 114 | if ($table->save($data)) |
| 115 | { |
| 116 | // register save data within the internal state |
| 117 | $this->set('data', $table->getProperties()); |
| 118 | |
| 119 | // saved successfully, return PK |
| 120 | return $table->{$table->getKeyName()}; |
| 121 | } |
| 122 | |
| 123 | // something went wrong, try to obtain an error |
| 124 | $error = $table->getError($get_last = null, $string = true); |
| 125 | |
| 126 | if ($error) |
| 127 | { |
| 128 | // error found, register it within the model |
| 129 | $this->setError($error); |
| 130 | } |
| 131 | |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns the table properties, useful to retrieve the information |
| 137 | * that have been registered while saving a record. |
| 138 | * |
| 139 | * @return array |
| 140 | */ |
| 141 | public function getData() |
| 142 | { |
| 143 | // try to get save data |
| 144 | $data = $this->get('data', null); |
| 145 | |
| 146 | if (!$data) |
| 147 | { |
| 148 | // no saved data, return the table properties |
| 149 | $data = $this->getTable()->getProperties(); |
| 150 | } |
| 151 | |
| 152 | return $data; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Basic delete implementation. |
| 157 | * |
| 158 | * @param mixed $ids Either the record ID or a list of records. |
| 159 | * |
| 160 | * @return boolean True on success, false otherwise. |
| 161 | */ |
| 162 | public function delete($ids) |
| 163 | { |
| 164 | $table = $this->getTable(); |
| 165 | |
| 166 | // attempt to delete data |
| 167 | if ($table->delete($ids)) |
| 168 | { |
| 169 | // deleted successfully |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | // something went wrong, try to obtain an error |
| 174 | $error = $table->getError($get_last = null, $string = true); |
| 175 | |
| 176 | if ($error) |
| 177 | { |
| 178 | // error found, register it within the model |
| 179 | $this->setError($error); |
| 180 | } |
| 181 | |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Basic publish/unpublish implementation. |
| 187 | * |
| 188 | * @param mixed $ids Either the record ID or a list of records. |
| 189 | * @param integer $state The publishing status. |
| 190 | * |
| 191 | * @return boolean True on success, false otherwise. |
| 192 | */ |
| 193 | public function publish($ids, $state = 1, $alias = null) |
| 194 | { |
| 195 | $table = $this->getTable(); |
| 196 | |
| 197 | $defaultAlias = null; |
| 198 | |
| 199 | if ($alias) |
| 200 | { |
| 201 | // get default column alias |
| 202 | $defaultAlias = $table->getColumnAlias('published'); |
| 203 | // then change it with the specified one |
| 204 | $table->setColumnAlias('published', $alias); |
| 205 | } |
| 206 | |
| 207 | $status = true; |
| 208 | |
| 209 | // attempt to delete data |
| 210 | if (!$table->publish($ids, $state)) |
| 211 | { |
| 212 | // Something went wrong or the status didn't change. |
| 213 | // Try to obtain an error. |
| 214 | $error = $table->getError($get_last = null, $string = true); |
| 215 | |
| 216 | if ($error) |
| 217 | { |
| 218 | // error found, register it within the model |
| 219 | $this->setError($error); |
| 220 | |
| 221 | $status = false; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // finally restore the default alias |
| 226 | $table->setColumnAlias('published', $defaultAlias); |
| 227 | |
| 228 | return $status; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Basic duplicate implementation. |
| 233 | * |
| 234 | * @param mixed $ids Either the record ID or a list of records. |
| 235 | * @param mixed $src Specifies some values to be used while duplicating. |
| 236 | * @param array $ignore A list of columns to skip. |
| 237 | * |
| 238 | * @return mixed The ID of the records on success, false otherwise. |
| 239 | */ |
| 240 | public function duplicate($ids, $src = array(), $ignore = array()) |
| 241 | { |
| 242 | $table = $this->getTable(); |
| 243 | |
| 244 | // always treat as an array |
| 245 | $ids = (array) $ids; |
| 246 | |
| 247 | $result = array(); |
| 248 | |
| 249 | // duplicate one by one |
| 250 | foreach ($ids as $id) |
| 251 | { |
| 252 | // load existing record |
| 253 | if ($table->load($id)) |
| 254 | { |
| 255 | // get record columns |
| 256 | $data = $table->getProperties(); |
| 257 | |
| 258 | // unset primary key |
| 259 | $data[$table->getKeyName()] = 0; |
| 260 | |
| 261 | // unset all the properties to skip |
| 262 | foreach ($ignore as $col) |
| 263 | { |
| 264 | unset($data[$col]); |
| 265 | } |
| 266 | |
| 267 | // iterate properties to use |
| 268 | foreach ($src as $col => $val) |
| 269 | { |
| 270 | $data[$col] = $val; |
| 271 | } |
| 272 | |
| 273 | // reset table before save |
| 274 | $table->reset(); |
| 275 | |
| 276 | // create new record by using the model |
| 277 | $tmp = $this->save($data); |
| 278 | |
| 279 | if ($tmp) |
| 280 | { |
| 281 | // register saved ID |
| 282 | $result[] = $tmp; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | return $result; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Sets the relations between the given entry and the specified records list. |
| 292 | * |
| 293 | * @param mixed $id The assoc column of the primary table. |
| 294 | * @param array $records The assoc column of the foreign table. |
| 295 | * |
| 296 | * @return void |
| 297 | */ |
| 298 | public function setRelation($id, array $records) |
| 299 | { |
| 300 | // make relation |
| 301 | $this->getTable()->setRelation($id, $records); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Method to get a table object. |
| 306 | * |
| 307 | * @param string $name The table name. |
| 308 | * @param string $prefix The class prefix. |
| 309 | * @param array $options Configuration array for table. |
| 310 | * |
| 311 | * @return JTable A table object. |
| 312 | * |
| 313 | * @throws Exception |
| 314 | */ |
| 315 | public function getTable($name = '', $prefix = '', $options = array()) |
| 316 | { |
| 317 | if (!$name) |
| 318 | { |
| 319 | // use same name of model |
| 320 | $name = $this->getName(); |
| 321 | } |
| 322 | |
| 323 | if (!$prefix) |
| 324 | { |
| 325 | // use default system prefix |
| 326 | $prefix = 'VAPTable'; |
| 327 | } |
| 328 | |
| 329 | // invoke parent |
| 330 | return parent::getTable($name, $prefix, $options); |
| 331 | } |
| 332 | } |
| 333 |