| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage com_vikbooking |
| 5 |
* @author E4J srl |
| 6 |
* @copyright Copyright (C) e4j - Extensionsforjoomla.com. All rights reserved. |
| 7 |
* @license GNU General Public License version 2 or later; see LICENSE |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
JLoader::import('adapter.mvc.models.form'); |
| 15 |
|
| 16 |
/** |
| 17 |
* VikBooking plugin Shortcode model. |
| 18 |
* |
| 19 |
* @since 1.0 |
| 20 |
* @see JModelForm |
| 21 |
*/ |
| 22 |
class VikBookingModelShortcode extends JModelForm |
| 23 |
{ |
| 24 |
/** |
| 25 |
* @override |
| 26 |
* This method should be used to pre-load an item considering |
| 27 |
* the data set in the request. |
| 28 |
* |
| 29 |
* For example, if the request owns an ID, this method may try |
| 30 |
* to retrieve the item from the database. |
| 31 |
* Otherwise it may return an empty object. |
| 32 |
* |
| 33 |
* @return object The object found. |
| 34 |
*/ |
| 35 |
public function loadFormData() |
| 36 |
{ |
| 37 |
$input = JFactory::getApplication()->input; |
| 38 |
|
| 39 |
$id = $input->getUint('cid', array(0)); |
| 40 |
|
| 41 |
return $this->getItem(array_shift($id)); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* This method should be used to retrieve the posted data |
| 46 |
* after the form submission. |
| 47 |
* |
| 48 |
* @return object The data object. |
| 49 |
*/ |
| 50 |
public function getFormData() |
| 51 |
{ |
| 52 |
$input = JFactory::getApplication()->input; |
| 53 |
|
| 54 |
// get data from request |
| 55 |
$data = new stdClass; |
| 56 |
|
| 57 |
$data->id = $input->getInt('id', 0); |
| 58 |
$data->title = ''; |
| 59 |
$data->name = $input->getString('name'); |
| 60 |
$data->type = $input->getString('type'); |
| 61 |
$data->lang = $input->getString('lang'); |
| 62 |
$data->parent_id = $input->getUint('parent_id', 0); |
| 63 |
$data->json = array(); |
| 64 |
$data->shortcode = ''; |
| 65 |
|
| 66 |
// only if we are creating the shortcode, set the creation date and user |
| 67 |
if ($data->id <= 0) |
| 68 |
{ |
| 69 |
$data->createdby = JFactory::getUser()->id; |
| 70 |
$data->createdon = JFactory::getDate()->toSql(); |
| 71 |
} |
| 72 |
|
| 73 |
// get layout path |
| 74 |
$path = implode(DIRECTORY_SEPARATOR, array(VBO_SITE_PATH, 'views', $data->type, 'tmpl', 'default.xml')); |
| 75 |
|
| 76 |
// if the file doesn't exist, raise an exception |
| 77 |
if (!is_file($path)) |
| 78 |
{ |
| 79 |
throw new Exception("Missing XML [{$data->type}] view type.", 404); |
| 80 |
} |
| 81 |
|
| 82 |
// load XML form |
| 83 |
$form = JForm::getInstance($data->type, $path); |
| 84 |
|
| 85 |
// obtain view title |
| 86 |
$data->title = (string) $form->getXml()->layout->attributes()->title; |
| 87 |
|
| 88 |
// iterate the layout fields |
| 89 |
foreach ($form->getFields() as $field) |
| 90 |
{ |
| 91 |
$attrs = $field->attributes(); |
| 92 |
|
| 93 |
$name = (string) $attrs->name; |
| 94 |
$filter = (string) $attrs->filter; |
| 95 |
$req = (string) $attrs->required; |
| 96 |
|
| 97 |
// use string if no filter |
| 98 |
if (empty($filter)) |
| 99 |
{ |
| 100 |
$filter = 'string'; |
| 101 |
} |
| 102 |
|
| 103 |
// get field from request |
| 104 |
$data->json[$name] = $input->get($name, '', $filter); |
| 105 |
|
| 106 |
// raise an exception if a mandatory field is empty |
| 107 |
if (empty($data->json[$name]) && $req == 'true') |
| 108 |
{ |
| 109 |
throw new Exception("Missing required [$name] field.", 400); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
$viewData = array(); |
| 114 |
$viewData['view'] = $data->type; |
| 115 |
$viewData['lang'] = $data->lang; |
| 116 |
|
| 117 |
// merge VIEW name and LANG TAG with JSON params |
| 118 |
$args = array_merge($data->json, $viewData); |
| 119 |
// generate shortcode string |
| 120 |
$data->shortcode = JFilterOutput::shortcode('vikbooking', $args); |
| 121 |
|
| 122 |
// finally encode the params in JSON |
| 123 |
$data->json = json_encode($data->json); |
| 124 |
|
| 125 |
return $data; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @override |
| 130 |
* Retrieves the specified item. |
| 131 |
* |
| 132 |
* @param mixed $pk The primary key value or a list of keys. |
| 133 |
* @param boolean $create True to create an empty object. |
| 134 |
* |
| 135 |
* @return object The item found if exists, otherwise an empty object. |
| 136 |
*/ |
| 137 |
public function getItem($pk, $create = true) |
| 138 |
{ |
| 139 |
$item = parent::getItem($pk, $create); |
| 140 |
|
| 141 |
if ($item && !$item->id) |
| 142 |
{ |
| 143 |
$item->name = ''; |
| 144 |
$item->type = ''; |
| 145 |
$item->json = '{}'; |
| 146 |
} |
| 147 |
|
| 148 |
return $item; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* @override |
| 153 |
* Creates or updates the specified record. |
| 154 |
* |
| 155 |
* @param object &$data The record to insert. |
| 156 |
* |
| 157 |
* @return boolean True if the record has been inserted/updated, otherwise false. |
| 158 |
*/ |
| 159 |
public function save(&$data) |
| 160 |
{ |
| 161 |
// get old item to get previous shortcode |
| 162 |
$old = $this->getItem($data->id); |
| 163 |
|
| 164 |
// save shortcode |
| 165 |
$res = parent::save($data); |
| 166 |
|
| 167 |
if ($res && $old) |
| 168 |
{ |
| 169 |
// get saved item to access post ID property |
| 170 |
$item = $this->getItem($data->id); |
| 171 |
|
| 172 |
/** |
| 173 |
* Get the post object. |
| 174 |
* Obtain post only in case the shortcode is assigned |
| 175 |
* to a real ID, otherwise get_post() could retrieve |
| 176 |
* the last post used. |
| 177 |
* |
| 178 |
* @since 1.2.7 |
| 179 |
*/ |
| 180 |
$post = $item->post_id ? get_post($item->post_id) : null; |
| 181 |
|
| 182 |
/** |
| 183 |
* Proceed only in case the post exists. |
| 184 |
* |
| 185 |
* @since 1.1.7 |
| 186 |
*/ |
| 187 |
if ($post) |
| 188 |
{ |
| 189 |
/** |
| 190 |
* Do not proceed in case the post already contains the shortcode. |
| 191 |
* Otherwise we would fall in a loop as wp_update_post() triggers |
| 192 |
* the action that invoked the current method. |
| 193 |
* |
| 194 |
* @see vikbooking.php @ action:save_post |
| 195 |
* |
| 196 |
* @since 1.0.17 |
| 197 |
*/ |
| 198 |
if (strpos($post->post_content, $item->shortcode) === false) |
| 199 |
{ |
| 200 |
// replace old shortcode with the new one from the post contents |
| 201 |
$post->post_content = str_replace($old->shortcode, $item->shortcode, $post->post_content); |
| 202 |
|
| 203 |
// finalize the update |
| 204 |
wp_update_post($post); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return $res; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Obtains the JForm object related to the model view. |
| 214 |
* |
| 215 |
* @param object $item The data to bind. |
| 216 |
* |
| 217 |
* @return JForm The form object. |
| 218 |
*/ |
| 219 |
public function getTypeForm($item) |
| 220 |
{ |
| 221 |
$item = (object) $item; |
| 222 |
|
| 223 |
if (!$item->type) |
| 224 |
{ |
| 225 |
return null; |
| 226 |
} |
| 227 |
|
| 228 |
// inject custom vars to access the layout |
| 229 |
// file of a site view (change model name and client) |
| 230 |
$this->_name = $item->type; |
| 231 |
$this->_client = 'site'; |
| 232 |
|
| 233 |
// get the form |
| 234 |
$form = parent::getForm(); |
| 235 |
|
| 236 |
// reset the vars (it will be taken later if needed) |
| 237 |
$this->_name = null; |
| 238 |
$this->_client = null; |
| 239 |
|
| 240 |
return $form; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Retrieves the IDs of the ancestors of a shortcode. |
| 245 |
* |
| 246 |
* @param mixed $shortcode Either a shortcode ID or an object. |
| 247 |
* |
| 248 |
* @return array Array of ancestor IDs or empty array if there are none. |
| 249 |
* |
| 250 |
* @since 1.5 |
| 251 |
*/ |
| 252 |
public function getAncestors($shortcode) |
| 253 |
{ |
| 254 |
if (is_array($shortcode)) |
| 255 |
{ |
| 256 |
// treat associative array as object |
| 257 |
$shortcode = (object) $shortcode; |
| 258 |
} |
| 259 |
else if (!is_object($shortcode)) |
| 260 |
{ |
| 261 |
// fetch shortcode details from ID |
| 262 |
$shortcode = $this->getItem($shortcode); |
| 263 |
} |
| 264 |
|
| 265 |
$ancestors = []; |
| 266 |
|
| 267 |
if (!$shortcode || empty($shortcode->parent_id) || $shortcode->parent_id == $shortcode->id) |
| 268 |
{ |
| 269 |
return $ancestors; |
| 270 |
} |
| 271 |
|
| 272 |
$id = $shortcode->parent_id; |
| 273 |
$ancestors[] = $id; |
| 274 |
|
| 275 |
while ($ancestor = $this->getItem($id)) |
| 276 |
{ |
| 277 |
// Loop detection: If the ancestor has been seen before, break |
| 278 |
if (empty($ancestor->parent_id) || ($ancestor->parent_id == $shortcode->id ) || in_array($ancestor->parent_id, $ancestors, true)) |
| 279 |
{ |
| 280 |
break; |
| 281 |
} |
| 282 |
|
| 283 |
$id = $ancestor->parent_id; |
| 284 |
$ancestors[] = $id; |
| 285 |
} |
| 286 |
|
| 287 |
return $ancestors; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
/** |
| 292 |
* Creates a page on WordPress with for each requested shortcode. |
| 293 |
* This is useful to automatically link Shortcodes in pages with no manual actions. |
| 294 |
* |
| 295 |
* @param mixed $ids Either an array or a shortcode ID. |
| 296 |
* |
| 297 |
* @return boolean True on success, false otherwise. |
| 298 |
* |
| 299 |
* @since 1.5 |
| 300 |
*/ |
| 301 |
public function addPage($ids) |
| 302 |
{ |
| 303 |
$ids = (array) $ids; |
| 304 |
|
| 305 |
$success = false; |
| 306 |
|
| 307 |
foreach ($ids as $id) |
| 308 |
{ |
| 309 |
// get shortcode record |
| 310 |
$item = $this->getItem($id); |
| 311 |
|
| 312 |
if (empty($item->id)) |
| 313 |
{ |
| 314 |
// missing shortcode |
| 315 |
$this->setError(sprintf('Shortcode [%d] not found', $id)); |
| 316 |
|
| 317 |
continue; |
| 318 |
} |
| 319 |
|
| 320 |
// make sure the shortcode hasn't been assigned to any pages |
| 321 |
if (empty($item->post_id)) |
| 322 |
{ |
| 323 |
$post_parent = 0; |
| 324 |
|
| 325 |
if ($item->parent_id) |
| 326 |
{ |
| 327 |
// get parent shortcode |
| 328 |
$parent = $this->getItem($item->parent_id); |
| 329 |
|
| 330 |
if ($parent && $parent->post_id) |
| 331 |
{ |
| 332 |
// in case the parent shortcode is assigned to a post, use it as parent |
| 333 |
$post_parent = $parent->post_id; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
// Add a new page (we allow a WP_ERROR to be thrown in case of failure). |
| 338 |
// This should automatically trigger the hook that we use to link the shortcode |
| 339 |
// to the new page/post ID, and so there's no need to update the item. |
| 340 |
$post_id = wp_insert_post(array( |
| 341 |
'post_title' => (!empty($item->name) ? $item->name : JText::translate($item->title)), |
| 342 |
'post_content' => $item->shortcode, |
| 343 |
'post_status' => 'publish', |
| 344 |
'post_type' => 'page', |
| 345 |
'post_parent' => $post_parent, |
| 346 |
), $wp_error = false, $fire_after_hooks = true); |
| 347 |
|
| 348 |
// check if we received an error |
| 349 |
if ($post_id instanceof WP_Error) |
| 350 |
{ |
| 351 |
// propagate error message |
| 352 |
$this->setError($post_id); |
| 353 |
|
| 354 |
continue; |
| 355 |
} |
| 356 |
|
| 357 |
// check whether the page has been created |
| 358 |
$success = $success || (is_int($post_id) && $post_id > 0); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
return $success; |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Method to get a table object. |
| 367 |
* |
| 368 |
* @param string $name The table name. |
| 369 |
* @param string $prefix The class prefix. |
| 370 |
* @param array $options Configuration array for table. |
| 371 |
* |
| 372 |
* @return JTable A table object. |
| 373 |
* |
| 374 |
* @since 1.4.7 |
| 375 |
*/ |
| 376 |
public function getTable($name = '', $prefix = 'JTable', $options = array()) |
| 377 |
{ |
| 378 |
if (!$name) |
| 379 |
{ |
| 380 |
$name = 'shortcode'; |
| 381 |
$prefix = 'VBOTable'; |
| 382 |
} |
| 383 |
|
| 384 |
return parent::getTable($name, $prefix, $options); |
| 385 |
} |
| 386 |
} |
| 387 |
|