| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2023 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 to this file |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
JLoader::import('adapter.mvc.models.form'); |
| 15 |
|
| 16 |
/** |
| 17 |
* VikBooking plugin Override model. |
| 18 |
* @wponly |
| 19 |
* |
| 20 |
* @since 1.6.5 |
| 21 |
* @see JModelForm |
| 22 |
*/ |
| 23 |
class VikBookingModelOverride extends JModelForm |
| 24 |
{ |
| 25 |
/** |
| 26 |
* @override |
| 27 |
* Saves the override. |
| 28 |
* |
| 29 |
* @param array &$data The array containing the override data. |
| 30 |
* |
| 31 |
* @return bool True on success, otherwise false. |
| 32 |
*/ |
| 33 |
public function save(&$data) |
| 34 |
{ |
| 35 |
// make sure we have a destination file |
| 36 |
if (empty($data['file'])) |
| 37 |
{ |
| 38 |
$this->setError('Missing destination file'); |
| 39 |
|
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
// make sure we have a code to write |
| 44 |
if (!isset($data['code'])) |
| 45 |
{ |
| 46 |
$this->setError('Missing override code'); |
| 47 |
|
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Use the unpublished version in case the status is inactive. |
| 53 |
* |
| 54 |
* @since 1.6.10 |
| 55 |
*/ |
| 56 |
if (($data['published'] ?? true) == false) |
| 57 |
{ |
| 58 |
$data['file'] = dirname($data['file']) . '/__' . basename($data['file']); |
| 59 |
} |
| 60 |
|
| 61 |
// generate override |
| 62 |
return JFile::write($data['file'], $data['code']); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Deletes the specified records. |
| 67 |
* |
| 68 |
* @param mixed $ids The PK value (or a list of values) of the record(s) to remove. |
| 69 |
* |
| 70 |
* @return bool True if at least a record has been removed, otherwise false. |
| 71 |
*/ |
| 72 |
public function delete($ids) |
| 73 |
{ |
| 74 |
$deleted = false; |
| 75 |
|
| 76 |
// iterate files |
| 77 |
foreach ((array) $ids as $file) |
| 78 |
{ |
| 79 |
$src = $file; |
| 80 |
|
| 81 |
// in case the override doesn't exist, try to |
| 82 |
// look for an unpublished override |
| 83 |
if (!JFile::exists($file)) |
| 84 |
{ |
| 85 |
// use the unpublished version |
| 86 |
$src = JPath::clean(dirname($file) . '/__' . basename($file)); |
| 87 |
} |
| 88 |
|
| 89 |
// try to delete the file |
| 90 |
$deleted = JFile::delete($src) || $deleted; |
| 91 |
} |
| 92 |
|
| 93 |
return $deleted; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Changes the state of the specified records. |
| 98 |
* |
| 99 |
* @param mixed $ids The PK value (or a list of values) of the record(s) to update. |
| 100 |
* |
| 101 |
* @return mixed True if at least a record has been affected, otherwise false. |
| 102 |
* In case of a single record, the new path will be returned. |
| 103 |
*/ |
| 104 |
public function publish($ids, $state) |
| 105 |
{ |
| 106 |
$changed = $dest = false; |
| 107 |
|
| 108 |
$ids = (array) $ids; |
| 109 |
|
| 110 |
// iterate files |
| 111 |
foreach ($ids as $file) |
| 112 |
{ |
| 113 |
$dest = dirname($file); |
| 114 |
$name = basename($file); |
| 115 |
|
| 116 |
if ($state) |
| 117 |
{ |
| 118 |
// start from unpublished file |
| 119 |
$src = JPath::clean($dest . '/__' . $name); |
| 120 |
// restore default override |
| 121 |
$dest = $file; |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
// start from existing override |
| 126 |
$src = $file; |
| 127 |
// move to unpublished status |
| 128 |
$dest = JPath::clean($dest . '/__' . $name); |
| 129 |
} |
| 130 |
|
| 131 |
// try to rename the file |
| 132 |
$changed = rename($src, $dest) || $changed; |
| 133 |
} |
| 134 |
|
| 135 |
// in case of a single file, return its new path |
| 136 |
if ($changed && count($ids) == 1) |
| 137 |
{ |
| 138 |
return $dest; |
| 139 |
} |
| 140 |
|
| 141 |
return $changed; |
| 142 |
} |
| 143 |
} |
| 144 |
|