| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage com_vikbooking |
| 5 |
* @author Alessio Gaggii - e4j - Extensionsforjoomla.com |
| 6 |
* @copyright Copyright (C) 2018 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 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 12 |
|
| 13 |
JHtml::fetch('jquery.framework', true, true); |
| 14 |
|
| 15 |
$vbo_app = VikBooking::getVboApplication(); |
| 16 |
$rules_helper = VikBooking::getConditionalRulesInstance(); |
| 17 |
$rules_names = $rules_helper->getRuleNames(); |
| 18 |
|
| 19 |
$cond_text_rules = count($this->condtext) ? json_decode($this->condtext['rules']) : array(); |
| 20 |
$cond_text_rules = !is_array($cond_text_rules) ? array() : $cond_text_rules; |
| 21 |
|
| 22 |
$editor = JEditor::getInstance(JFactory::getApplication()->get('editor')); |
| 23 |
|
| 24 |
// load language vars for JavaScript |
| 25 |
JText::script('VBO_CONDTEXT_RULE_DISABLED'); |
| 26 |
JText::script('VBO_CONDTEXT_NORULES_SEL'); |
| 27 |
JText::script('VBO_CONDTEXT_RULE_RMCONF'); |
| 28 |
|
| 29 |
?> |
| 30 |
|
| 31 |
<form name="adminForm" id="adminForm" action="index.php" method="post" enctype="multipart/form-data"> |
| 32 |
|
| 33 |
<div class="vbo-admin-container"> |
| 34 |
<div class="vbo-config-maintab-left vbo-config-maintab-left-halfsize"> |
| 35 |
<fieldset class="adminform"> |
| 36 |
<div class="vbo-params-wrap"> |
| 37 |
<legend class="adminlegend"><?php echo JText::translate('VBOADMINLEGENDDETAILS'); ?></legend> |
| 38 |
<div class="vbo-params-container"> |
| 39 |
<div class="vbo-param-container"> |
| 40 |
<div class="vbo-param-label"><?php echo JText::translate('VBO_CONDTEXT_NAME'); ?></div> |
| 41 |
<div class="vbo-param-setting"> |
| 42 |
<input type="text" id="condtextname" name="condtextname" value="<?php echo count($this->condtext) ? JHtml::fetch('esc_attr', $this->condtext['name']) : ''; ?>" size="30" onkeyup="vboComposeToken(this.value);" autocomplete="off" required /> |
| 43 |
</div> |
| 44 |
</div> |
| 45 |
</div> |
| 46 |
<div class="vbo-params-container"> |
| 47 |
<div class="vbo-param-container"> |
| 48 |
<div class="vbo-param-label"><?php echo JText::translate('VBO_CONDTEXT_TKN'); ?></div> |
| 49 |
<div class="vbo-param-setting"> |
| 50 |
<input type="text" id="condtexttkn" name="condtexttkn" value="<?php echo count($this->condtext) ? JHtml::fetch('esc_attr', $this->condtext['token']) : ''; ?>" size="30" readonly /> |
| 51 |
<span class="vbo-param-setting-comment"><?php echo JText::translate('VBO_CONDTEXT_TKN_HELP'); ?></span> |
| 52 |
</div> |
| 53 |
</div> |
| 54 |
</div> |
| 55 |
<div class="vbo-param-container"> |
| 56 |
<div class="vbo-param-label"><?php echo JText::translate('VBO_CONDTEXT_DEBUG_RULES'); ?></div> |
| 57 |
<div class="vbo-param-setting"> |
| 58 |
<?php echo $vbo_app->printYesNoButtons('debug', JText::translate('VBYES'), JText::translate('VBNO'), (count($this->condtext) ? (int)$this->condtext['debug'] : 0), 1, 0); ?> |
| 59 |
<span class="vbo-param-setting-comment"><?php echo JText::translate('VBO_CONDTEXT_DEBUG_RULES_HELP'); ?></span> |
| 60 |
</div> |
| 61 |
</div> |
| 62 |
<div class="vbo-param-container vbo-param-container-full"> |
| 63 |
<div class="vbo-param-label vbo-param-label-above"><?php echo JText::translate('VBO_CONDTEXT_MSG'); ?></div> |
| 64 |
<div class="vbo-param-setting"> |
| 65 |
<?php |
| 66 |
if (interface_exists('Throwable')) { |
| 67 |
/** |
| 68 |
* With PHP >= 7 supporting throwable exceptions for Fatal Errors |
| 69 |
* we try to avoid issues with third party plugins that make use |
| 70 |
* of the WP native function get_current_screen(). |
| 71 |
* |
| 72 |
* @wponly - but we also use it on Joomla @joomlaonly |
| 73 |
*/ |
| 74 |
try { |
| 75 |
echo $editor->display( "msg", (count($this->condtext) ? $this->condtext['msg'] : ""), '100%', 300, 70, 20 ); |
| 76 |
} catch (Throwable $t) { |
| 77 |
echo $t->getMessage() . ' in ' . $t->getFile() . ':' . $t->getLine() . '<br/>'; |
| 78 |
} |
| 79 |
} else { |
| 80 |
// we cannot catch Fatal Errors in PHP 5.x |
| 81 |
echo $editor->display( "msg", (count($this->condtext) ? $this->condtext['msg'] : ""), '100%', 300, 70, 20 ); |
| 82 |
} |
| 83 |
?> |
| 84 |
<span class="vbo-param-setting-comment"><?php echo JText::translate('VBO_CONDTEXT_MSG_HELP'); ?></span> |
| 85 |
</div> |
| 86 |
</div> |
| 87 |
<div class="vbo-params-container"> |
| 88 |
<div class="vbo-param-container"> |
| 89 |
<div class="vbo-param-setting"> |
| 90 |
<p id="vbo-condtext-warn-norules" class="warn notice-noicon"<?php echo count($cond_text_rules) ? ' style="display: none;"' : ''; ?>><?php VikBookingIcons::e('info-circle'); ?> <?php echo JText::translate('VBO_CONDTEXT_WARN_NORULES'); ?></p> |
| 91 |
<button type="button" class="btn vbo-config-btn" onclick="vboAddNewRule();"><?php VikBookingIcons::e('plus-circle'); ?> <?php echo JText::translate('VBO_CONDTEXT_ADDRULE'); ?></button> |
| 92 |
</div> |
| 93 |
</div> |
| 94 |
</div> |
| 95 |
</div> |
| 96 |
</fieldset> |
| 97 |
</div> |
| 98 |
|
| 99 |
<div id="vbo-condtext-rules-wrap" class="vbo-config-maintab-right vbo-config-maintab-right-halfsize"> |
| 100 |
<?php |
| 101 |
// load all current rules with their respective params |
| 102 |
$rules_loaded = array(); |
| 103 |
foreach ($cond_text_rules as $rule_data) { |
| 104 |
if (!is_object($rule_data) || empty($rule_data->id)) { |
| 105 |
continue; |
| 106 |
} |
| 107 |
$rule = $rules_helper->getRule($rule_data->id); |
| 108 |
if ($rule === false) { |
| 109 |
// rule object not found |
| 110 |
continue; |
| 111 |
} |
| 112 |
|
| 113 |
// get identifier |
| 114 |
$identifier = $rule->getIdentifier(); |
| 115 |
|
| 116 |
// inject rule params |
| 117 |
$rule->setParams($rule_data->params); |
| 118 |
|
| 119 |
// check whether the rule object overrides the callbackAction method |
| 120 |
$supports_action = $rules_helper->supportsAction($rule); |
| 121 |
?> |
| 122 |
<fieldset class="adminform vbo-condtext-rule" data-ruleid="<?php echo $identifier; ?>" data-ruleaction="<?php echo (int)$supports_action; ?>"> |
| 123 |
<div class="vbo-params-wrap"> |
| 124 |
<legend class="adminlegend"> |
| 125 |
<span class="vbo-rule-name"><?php echo $rule->getName(); ?></span> |
| 126 |
<span class="vbo-rule-trash" onclick="vboRemoveRule(this);"><?php VikBookingIcons::e('trash'); ?></span> |
| 127 |
</legend> |
| 128 |
<div class="vbo-params-container"> |
| 129 |
<?php |
| 130 |
// display the params of this rule |
| 131 |
$rule->renderParams(); |
| 132 |
|
| 133 |
// push the rule to the pool of the ones already rendered |
| 134 |
array_push($rules_loaded, $identifier); |
| 135 |
?> |
| 136 |
</div> |
| 137 |
</div> |
| 138 |
</fieldset> |
| 139 |
<?php |
| 140 |
} |
| 141 |
|
| 142 |
// keep loading the rules not present, but hide them by default |
| 143 |
foreach ($rules_helper->getRules() as $rule) { |
| 144 |
$identifier = $rule->getIdentifier(); |
| 145 |
if (in_array($identifier, $rules_loaded)) { |
| 146 |
continue; |
| 147 |
} |
| 148 |
|
| 149 |
// unset the params for this rule |
| 150 |
$rule->setParams(null); |
| 151 |
|
| 152 |
// check whether the rule object overrides the callbackAction method |
| 153 |
$supports_action = $rules_helper->supportsAction($rule); |
| 154 |
?> |
| 155 |
<fieldset class="adminform vbo-condtext-rule vbo-condtext-rule-ghost" style="display: none;" data-ruleid="<?php echo $identifier; ?>" data-ruleaction="<?php echo (int)$supports_action; ?>"> |
| 156 |
<div class="vbo-params-wrap"> |
| 157 |
<legend class="adminlegend"> |
| 158 |
<span class="vbo-rule-name"><?php echo $rule->getName(); ?></span> |
| 159 |
<span class="vbo-rule-trash" onclick="vboRemoveRule(this);"><?php VikBookingIcons::e('trash'); ?></span> |
| 160 |
</legend> |
| 161 |
<div class="vbo-params-container"> |
| 162 |
<?php |
| 163 |
// display the params of this rule |
| 164 |
$rule->renderParams(); |
| 165 |
?> |
| 166 |
</div> |
| 167 |
</div> |
| 168 |
</fieldset> |
| 169 |
<?php |
| 170 |
} |
| 171 |
?> |
| 172 |
<div class="vbo-condtext-addrule-helper"> |
| 173 |
<p id="vbo-condtext-warn-norules-scnd" class="warn notice-noicon"<?php echo count($cond_text_rules) ? ' style="display: none;"' : ''; ?>><?php VikBookingIcons::e('info-circle'); ?> <?php echo JText::translate('VBO_CONDTEXT_WARN_NORULES'); ?></p> |
| 174 |
<button id="vbo-condtext-addrule-scnd" type="button" class="btn vbo-config-btn" onclick="vboAddNewRule();"><?php VikBookingIcons::e('plus-circle'); ?> <?php echo JText::translate('VBO_CONDTEXT_ADDRULE'); ?></button> |
| 175 |
</div> |
| 176 |
</div> |
| 177 |
|
| 178 |
</div> |
| 179 |
<input type="hidden" name="task" value=""> |
| 180 |
<input type="hidden" name="option" value="com_vikbooking"> |
| 181 |
<?php |
| 182 |
if (count($this->condtext)) { |
| 183 |
?> |
| 184 |
<input type="hidden" name="where" value="<?php echo $this->condtext['id']; ?>"> |
| 185 |
<?php |
| 186 |
} |
| 187 |
?> |
| 188 |
<?php echo JHtml::fetch('form.token'); ?> |
| 189 |
</form> |
| 190 |
|
| 191 |
<div class="vbo-modal-overlay-block vbo-modal-overlay-block-condtext"> |
| 192 |
<a class="vbo-modal-overlay-close" href="javascript: void(0);"></a> |
| 193 |
<div class="vbo-modal-overlay-content vbo-modal-tall vbo-modal-overlay-content-condtext"> |
| 194 |
<div class="vbo-modal-overlay-content-head vbo-modal-overlay-content-head-condtext"> |
| 195 |
<h3><span><?php echo JText::translate('VBO_CONDTEXT_ADDRULE'); ?></span> <span class="vbo-modal-overlay-close-times" onclick="hideVboModalCondtext();">×</span></h3> |
| 196 |
</div> |
| 197 |
<div class="vbo-modal-overlay-content-body vbo-modal-overlay-content-body-scroll"> |
| 198 |
<div class="vbo-modal-condtext-rules"> |
| 199 |
<?php |
| 200 |
foreach ($rules_names as $rule) { |
| 201 |
$rule_used = in_array($rule->id, $rules_loaded); |
| 202 |
?> |
| 203 |
<div class="vbo-modal-condtext-rule<?php echo $rule_used ? ' vbo-modal-condtext-rule-disabled' : ''; ?>" onclick="vboSelectRule(this);" data-ruleid="<?php echo $rule->id; ?>"> |
| 204 |
<h5><?php echo $rule->name; ?></h5> |
| 205 |
<div class="vbo-condtext-rule-descr"> |
| 206 |
<?php echo $rule->descr; ?> |
| 207 |
</div> |
| 208 |
</div> |
| 209 |
<?php |
| 210 |
} |
| 211 |
?> |
| 212 |
</div> |
| 213 |
</div> |
| 214 |
<div class="vbo-modal-overlay-content-footer"> |
| 215 |
<div class="vbo-modal-overlay-content-footer-right"> |
| 216 |
<button type="button" class="btn btn-success" onclick="vboAddRuleToDoc();"><?php VikBookingIcons::e('plus-circle'); ?> <?php echo JText::translate('VBO_CONDTEXT_ADDRULE'); ?></button> |
| 217 |
</div> |
| 218 |
</div> |
| 219 |
</div> |
| 220 |
</div> |
| 221 |
|
| 222 |
<script type="text/javascript"> |
| 223 |
/** |
| 224 |
* Declare global scope variables. |
| 225 |
*/ |
| 226 |
var vbo_modal_condtext_on = false; |
| 227 |
|
| 228 |
/** |
| 229 |
* Composes the token from the name of the rule. |
| 230 |
*/ |
| 231 |
function vboComposeToken(name) { |
| 232 |
// convert to lower case and replace whitespaces with underscore |
| 233 |
name = (name + '').toLowerCase().replace(/\s/g, '_'); |
| 234 |
// convert anything except [0-9A-Za-z_] to an empty string |
| 235 |
name = name.replace(/\W/g, ''); |
| 236 |
// compose token string |
| 237 |
var tkn = '{condition: ' + name + '}'; |
| 238 |
// set token string |
| 239 |
document.getElementById('condtexttkn').value = tkn; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Shows the modal window |
| 244 |
*/ |
| 245 |
function vboOpenModalCondtext() { |
| 246 |
jQuery('.vbo-modal-overlay-block-condtext').show(); |
| 247 |
vbo_modal_condtext_on = true; |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Hides the modal window |
| 252 |
*/ |
| 253 |
function hideVboModalCondtext() { |
| 254 |
if (vbo_modal_condtext_on === true) { |
| 255 |
jQuery(".vbo-modal-overlay-block-condtext").fadeOut(400, function () { |
| 256 |
jQuery(".vbo-modal-overlay-content-condtext").show(); |
| 257 |
}); |
| 258 |
// turn flag off |
| 259 |
vbo_modal_condtext_on = false; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Displays the modal for adding a new rule. |
| 265 |
*/ |
| 266 |
function vboAddNewRule() { |
| 267 |
vboOpenModalCondtext(); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Adds the selected rule to the document. |
| 272 |
*/ |
| 273 |
function vboAddRuleToDoc() { |
| 274 |
var rule_sel = jQuery('.vbo-modal-condtext-rule-selected').not('.vbo-modal-condtext-rule-disabled'); |
| 275 |
if (!rule_sel.length) { |
| 276 |
// no rules selected |
| 277 |
alert(Joomla.JText._('VBO_CONDTEXT_NORULES_SEL')); |
| 278 |
return false; |
| 279 |
} |
| 280 |
var rule_id = rule_sel.first().attr('data-ruleid'); |
| 281 |
if (!rule_id || !rule_id.length) { |
| 282 |
alert(Joomla.JText._('VBO_CONDTEXT_NORULES_SEL')); |
| 283 |
console.error('rule ID is empty', rule_sel); |
| 284 |
return false; |
| 285 |
} |
| 286 |
// append the "ghost" (not yet used) rule to the document |
| 287 |
var rule_wrap = jQuery('.vbo-condtext-rule-ghost[data-ruleid="' + rule_id + '"]'); |
| 288 |
if (!rule_wrap.length) { |
| 289 |
alert(Joomla.JText._('VBO_CONDTEXT_NORULES_SEL')); |
| 290 |
console.error('could not find ghost rule with ID ' + rule_id); |
| 291 |
return false; |
| 292 |
} |
| 293 |
// find the element to append the new rule |
| 294 |
if (jQuery('.vbo-condtext-rule').not('.vbo-condtext-rule-ghost').length) { |
| 295 |
// append this rule to the last one used |
| 296 |
var append_to = jQuery('.vbo-condtext-rule').not('.vbo-condtext-rule-ghost').last(); |
| 297 |
// move rule by showing it and by removing the ghost class |
| 298 |
rule_wrap.show().removeClass('vbo-condtext-rule-ghost').insertAfter(append_to); |
| 299 |
} else { |
| 300 |
// append it to the rules wrapper, as the first rule |
| 301 |
var append_to = jQuery('#vbo-condtext-rules-wrap').find('.vbo-condtext-addrule-helper'); |
| 302 |
// move rule by showing it and by removing the ghost class |
| 303 |
rule_wrap.show().removeClass('vbo-condtext-rule-ghost').insertBefore(append_to); |
| 304 |
} |
| 305 |
// hide warning messages |
| 306 |
jQuery('#vbo-condtext-warn-norules').hide(); |
| 307 |
jQuery('#vbo-condtext-warn-norules-scnd').hide(); |
| 308 |
// close modal |
| 309 |
hideVboModalCondtext(); |
| 310 |
// disable this rule from the modal window |
| 311 |
rule_sel.first().removeClass('vbo-modal-condtext-rule-selected').addClass('vbo-modal-condtext-rule-disabled'); |
| 312 |
if (jQuery('.vbo-condtext-rule').not('.vbo-condtext-rule-ghost').length > 0) { |
| 313 |
// show second add rule button |
| 314 |
// jQuery('#vbo-condtext-addrule-scnd').show(); |
| 315 |
} |
| 316 |
// animate scroll to the new rule position |
| 317 |
jQuery('html,body').animate({scrollTop: rule_wrap.offset().top - 40}, {duration: 400}); |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Selects a rule or adds the selected rule. |
| 322 |
*/ |
| 323 |
function vboSelectRule(rule) { |
| 324 |
var elem = jQuery(rule); |
| 325 |
if (elem.hasClass('vbo-modal-condtext-rule-disabled')) { |
| 326 |
alert(Joomla.JText._('VBO_CONDTEXT_RULE_DISABLED')); |
| 327 |
return false; |
| 328 |
} |
| 329 |
if (elem.hasClass('vbo-modal-condtext-rule-selected')) { |
| 330 |
// add rule to document |
| 331 |
vboAddRuleToDoc(); |
| 332 |
return true; |
| 333 |
} |
| 334 |
// unset any selected class from other rules |
| 335 |
jQuery('.vbo-modal-condtext-rule').removeClass('vbo-modal-condtext-rule-selected'); |
| 336 |
// add selected class to this rule |
| 337 |
elem.addClass('vbo-modal-condtext-rule-selected'); |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Removes (hides) a rule from the document. |
| 342 |
*/ |
| 343 |
function vboRemoveRule(rule) { |
| 344 |
var elem = jQuery(rule).closest('.vbo-condtext-rule'); |
| 345 |
if (!confirm(Joomla.JText._('VBO_CONDTEXT_RULE_RMCONF'))) { |
| 346 |
return false; |
| 347 |
} |
| 348 |
// find the ID of the rule to remove |
| 349 |
var rule_id = elem.attr('data-ruleid'); |
| 350 |
if (!rule_id || !rule_id.length) { |
| 351 |
alert(Joomla.JText._('VBO_CONDTEXT_NORULES_SEL')); |
| 352 |
console.error('rule ID is empty', rule, elem); |
| 353 |
return false; |
| 354 |
} |
| 355 |
// enable this rule in the modal from next selection |
| 356 |
jQuery('.vbo-modal-condtext-rule[data-ruleid="' + rule_id + '"]').removeClass('vbo-modal-condtext-rule-disabled vbo-modal-condtext-rule-selected'); |
| 357 |
// move this rule to the "ghost" list |
| 358 |
var append_to = null; |
| 359 |
if (jQuery('.vbo-condtext-rule-ghost').length) { |
| 360 |
// move it after the last ghost rule found |
| 361 |
append_to = jQuery('.vbo-condtext-rule-ghost').last(); |
| 362 |
} else if (jQuery('.vbo-condtext-rule').not('.vbo-condtext-rule-ghost').length > 1) { |
| 363 |
// move it as the first ghost rule, after the last rule used |
| 364 |
append_to = jQuery('.vbo-condtext-rule').not('.vbo-condtext-rule-ghost').last(); |
| 365 |
} |
| 366 |
// add ghost class and hide rule |
| 367 |
elem.addClass('vbo-condtext-rule-ghost').hide(); |
| 368 |
// make any input field for this rule empty, or when saving the conditional text this rule will be applied |
| 369 |
elem.find('input, select, textarea').val('').trigger('change'); |
| 370 |
if (append_to !== null) { |
| 371 |
// move rule only if more than one available |
| 372 |
elem.insertAfter(append_to); |
| 373 |
} |
| 374 |
// show warning messages if no more rules |
| 375 |
if (!jQuery('.vbo-condtext-rule').not('.vbo-condtext-rule-ghost').length) { |
| 376 |
jQuery('#vbo-condtext-warn-norules').show(); |
| 377 |
jQuery('#vbo-condtext-warn-norules-scnd').show(); |
| 378 |
// hide second add rule button |
| 379 |
// jQuery('#vbo-condtext-addrule-scnd').hide(); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Declares document ready event processes. |
| 385 |
*/ |
| 386 |
jQuery(function() { |
| 387 |
|
| 388 |
/** |
| 389 |
* Dismiss modal window with Esc. |
| 390 |
*/ |
| 391 |
jQuery(document).keydown(function(e) { |
| 392 |
if (e.keyCode == 27) { |
| 393 |
if (vbo_modal_condtext_on === true) { |
| 394 |
hideVboModalCondtext(); |
| 395 |
} |
| 396 |
} |
| 397 |
}); |
| 398 |
|
| 399 |
/** |
| 400 |
* Dismiss modal window by clicking on an external element. |
| 401 |
*/ |
| 402 |
jQuery(document).mouseup(function(e) { |
| 403 |
if (!vbo_modal_condtext_on) { |
| 404 |
return; |
| 405 |
} |
| 406 |
if (vbo_modal_condtext_on) { |
| 407 |
var vbo_overlay_cont = jQuery(".vbo-modal-overlay-content-condtext"); |
| 408 |
if (!vbo_overlay_cont.is(e.target) && vbo_overlay_cont.has(e.target).length === 0) { |
| 409 |
hideVboModalCondtext(); |
| 410 |
} |
| 411 |
} |
| 412 |
}); |
| 413 |
|
| 414 |
}); |
| 415 |
</script> |
| 416 |
|