| 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 |
/** |
| 15 |
* VikBooking Overrides view. |
| 16 |
* @wponly |
| 17 |
* |
| 18 |
* @since 1.6.5 |
| 19 |
*/ |
| 20 |
class VikBookingViewOverrides extends JView |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @override |
| 24 |
* View display method. |
| 25 |
* |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public function display($tpl = null) |
| 29 |
{ |
| 30 |
$input = JFactory::getApplication()->input; |
| 31 |
$user = JFactory::getUser(); |
| 32 |
|
| 33 |
if (!$user->authorise('core.admin', 'com_vikbooking')) |
| 34 |
{ |
| 35 |
wp_die( |
| 36 |
'<h1>' . JText::translate('FATAL_ERROR') . '</h1>' . |
| 37 |
'<p>' . JText::translate('RESOURCE_AUTH_ERROR') . '</p>', |
| 38 |
403 |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
// load filters from request |
| 43 |
$filters = []; |
| 44 |
$filters['client'] = $input->get('client', 'site', 'string'); |
| 45 |
$filters['status'] = $input->get('status', '', 'string'); |
| 46 |
$filters['file'] = $input->get('selectedfile', '', 'base64'); |
| 47 |
$filters['override'] = $input->get('overridefile', '', 'base64'); |
| 48 |
|
| 49 |
$filters['hasoverride'] = false; |
| 50 |
$filters['published'] = false; |
| 51 |
|
| 52 |
// get view model |
| 53 |
$model = $this->getModel(); |
| 54 |
|
| 55 |
// scan overrides tree |
| 56 |
$this->tree = $model->getTree($filters['client']); |
| 57 |
|
| 58 |
// check whether a file was specified |
| 59 |
if ($filters['file']) |
| 60 |
{ |
| 61 |
// decode file path |
| 62 |
$filters['path'] = base64_decode($filters['file']); |
| 63 |
$filters['override'] = base64_decode($filters['override']); |
| 64 |
} |
| 65 |
else if ($filters['override']) |
| 66 |
{ |
| 67 |
// override provided, attempt to fetch the path of the original file |
| 68 |
$filters['override'] = base64_decode($filters['override']); |
| 69 |
|
| 70 |
// search node with matching override file |
| 71 |
$node = $model->isSupported($this->tree, $filters['override']); |
| 72 |
|
| 73 |
if ($node) |
| 74 |
{ |
| 75 |
// node found |
| 76 |
$filters['file'] = base64_encode($node['path']); |
| 77 |
$filters['path'] = $node['path']; |
| 78 |
} |
| 79 |
else |
| 80 |
{ |
| 81 |
// node not found |
| 82 |
$filters['path'] = false; |
| 83 |
$filters['override'] = ''; |
| 84 |
} |
| 85 |
} |
| 86 |
else |
| 87 |
{ |
| 88 |
// no selected file |
| 89 |
$filters['path'] = false; |
| 90 |
} |
| 91 |
|
| 92 |
// check if we should filter the tree according to the specified status |
| 93 |
if ($filters['status'] !== '') |
| 94 |
{ |
| 95 |
// apply filters |
| 96 |
foreach ($this->tree as $i => &$node) |
| 97 |
{ |
| 98 |
// apply filters to the first level nodes |
| 99 |
if (!$this->filterOverrides($this->tree[$i], (int) $filters['status'])) |
| 100 |
{ |
| 101 |
// detach node as there are no matching overrides |
| 102 |
unset($this->tree[$i]); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
// in case of selected file, make sure it supports overrides |
| 108 |
if ($filters['path']) |
| 109 |
{ |
| 110 |
// invoke model check |
| 111 |
$supported = $model->isSupported($this->tree, $filters['path']); |
| 112 |
|
| 113 |
if ($supported) |
| 114 |
{ |
| 115 |
// read default file contents |
| 116 |
$filters['defaultcode'] = file_get_contents($filters['path']); |
| 117 |
|
| 118 |
// override supported, check if already exists |
| 119 |
if (JFile::exists($filters['override'])) |
| 120 |
{ |
| 121 |
// existing override, read contents |
| 122 |
$filters['code'] = file_get_contents($filters['override']); |
| 123 |
|
| 124 |
$filters['hasoverride'] = true; |
| 125 |
|
| 126 |
$filters['published'] = true; |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
// override is missing, look for an unpublished override |
| 131 |
$_path = JPath::clean(dirname($filters['override']) . '/__' . basename($filters['override'])); |
| 132 |
|
| 133 |
if (JFile::exists($_path)) |
| 134 |
{ |
| 135 |
// existing unpublished override, read contents |
| 136 |
$filters['code'] = file_get_contents($_path); |
| 137 |
|
| 138 |
$filters['hasoverride'] = true; |
| 139 |
} |
| 140 |
else |
| 141 |
{ |
| 142 |
// no override, use default code |
| 143 |
$filters['code'] = $filters['defaultcode']; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Adjust the comments describing the layout location |
| 149 |
* to the wordpress structure. |
| 150 |
*/ |
| 151 |
$filters['code'] = preg_replace_callback( |
| 152 |
"/\*\s*(\/administrator)?\/?components\/com_vikbooking(.*?)\R/i", |
| 153 |
function($match) |
| 154 |
{ |
| 155 |
return '* /wp-content/plugins/vikbooking/' |
| 156 |
. ($match[1] ? 'admin' : 'site') |
| 157 |
. $match[2] . "\n"; |
| 158 |
}, |
| 159 |
$filters['code'] |
| 160 |
); |
| 161 |
} |
| 162 |
else |
| 163 |
{ |
| 164 |
// the selected file doesn't support overrides, unset selection |
| 165 |
$filters['file'] = ''; |
| 166 |
$filters['override'] = ''; |
| 167 |
$filters['path'] = false; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
// register filters |
| 172 |
$this->filters = $filters; |
| 173 |
|
| 174 |
// set up toolbar |
| 175 |
$this->addToolbar(); |
| 176 |
|
| 177 |
// display parent |
| 178 |
parent::display($tpl); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Helper method to setup the toolbar. |
| 183 |
* |
| 184 |
* @return void |
| 185 |
*/ |
| 186 |
public function addToolbar() |
| 187 |
{ |
| 188 |
JToolbarHelper::title(__('VikBooking - Page Overrides', 'vikbooking')); |
| 189 |
|
| 190 |
// back to the configuration |
| 191 |
JToolbarHelper::back('JTOOLBAR_BACK', 'index.php?option=com_vikbooking&view=config'); |
| 192 |
|
| 193 |
// check if we have a selected file |
| 194 |
if ($this->filters['path']) |
| 195 |
{ |
| 196 |
// add save button |
| 197 |
JToolbarHelper::apply('override.save'); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Helper function used to build a navigator node. |
| 203 |
* |
| 204 |
* @param array $node The tree node. |
| 205 |
* |
| 206 |
* @return string The HTML to display. |
| 207 |
*/ |
| 208 |
protected function buildNode($node) |
| 209 |
{ |
| 210 |
// in case the selected path contains the current node, mark the node as selected |
| 211 |
if ($this->filters['path'] && strpos($this->filters['path'], $node['path'] . '/') === 0) |
| 212 |
{ |
| 213 |
$node['selected'] = true; |
| 214 |
} |
| 215 |
else |
| 216 |
{ |
| 217 |
$node['selected'] = false; |
| 218 |
} |
| 219 |
|
| 220 |
// set up current node |
| 221 |
$this->currentNode = $node; |
| 222 |
|
| 223 |
if ($node['folder']) |
| 224 |
{ |
| 225 |
// build folder link |
| 226 |
$html = $this->loadTemplate('folder'); |
| 227 |
} |
| 228 |
else |
| 229 |
{ |
| 230 |
// build file link |
| 231 |
$html = $this->loadTemplate('file'); |
| 232 |
} |
| 233 |
|
| 234 |
// check whether the node is a leaf |
| 235 |
if (!empty($node['files'])) |
| 236 |
{ |
| 237 |
// fetch node children visibility |
| 238 |
$style = $node['selected'] ? '' : ' style="display:none;"'; |
| 239 |
|
| 240 |
// create children list |
| 241 |
$html .= '<ul' . $style . '>'; |
| 242 |
|
| 243 |
// iterate children |
| 244 |
foreach ($node['files'] as $file) |
| 245 |
{ |
| 246 |
// create child node |
| 247 |
$html .= '<li>'; |
| 248 |
// build child HTML |
| 249 |
$html .= $this->buildNode($file); |
| 250 |
// close child node |
| 251 |
$html .= '</li>'; |
| 252 |
} |
| 253 |
|
| 254 |
// close children list |
| 255 |
$html .= '</ul>'; |
| 256 |
} |
| 257 |
|
| 258 |
return $html; |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Filter the available pages/widgets/layouts according to the specified status. |
| 263 |
* |
| 264 |
* @param array &$node The node holding the available files. |
| 265 |
* @param int $status The filter status (1: active only, 0: inactive only). |
| 266 |
* |
| 267 |
* @return void |
| 268 |
*/ |
| 269 |
protected function filterOverrides(array &$node, int $status) |
| 270 |
{ |
| 271 |
// scan all the files of the node |
| 272 |
foreach ($node['files'] as $k => &$file) |
| 273 |
{ |
| 274 |
// check whether we have a sub-level node |
| 275 |
if (isset($file['files'])) |
| 276 |
{ |
| 277 |
// recursively filter the overrides under this sub-level |
| 278 |
if (!$this->filterOverrides($file, $status)) |
| 279 |
{ |
| 280 |
// retach the sub-level in case it has no overrides |
| 281 |
unset($node['files'][$k]); |
| 282 |
} |
| 283 |
} |
| 284 |
// or if we have a leaf (file override) |
| 285 |
else |
| 286 |
{ |
| 287 |
// make sure the file has an override and it matches the provided status |
| 288 |
if (!$file['has'] || $file['published'] != $status) |
| 289 |
{ |
| 290 |
// not compliant, detach file override |
| 291 |
unset($node['files'][$k]); |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
if (count($node['files']) === 0) |
| 297 |
{ |
| 298 |
// return false in case the node should be detached from the tree |
| 299 |
return false; |
| 300 |
} |
| 301 |
|
| 302 |
// the node contains at least a matching override |
| 303 |
return true; |
| 304 |
} |
| 305 |
} |
| 306 |
|