| @@ -1,0 +1,130 @@ | ||
| 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 | +/** | |
| 14 | + * Class handler for conditional rule "channels". | |
| 15 | + * | |
| 16 | + * @since 1.4.0 | |
| 17 | + */ | |
| 18 | +class VikBookingConditionalRuleChannels extends VikBookingConditionalRule | |
| 19 | +{ | |
| 20 | + /** | |
| 21 | + * Class constructor will define the rule name, description and identifier. | |
| 22 | + */ | |
| 23 | + public function __construct() | |
| 24 | + { | |
| 25 | + // call parent constructor | |
| 26 | + parent::__construct(); | |
| 27 | + | |
| 28 | + $this->ruleName = JText::translate('VBOCHANNELS'); | |
| 29 | + $this->ruleDescr = JText::translate('VBO_CONDTEXT_RULE_CHANNELS_DESCR'); | |
| 30 | + $this->ruleId = basename(__FILE__); | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Displays the rule parameters. | |
| 35 | + * | |
| 36 | + * @return void | |
| 37 | + */ | |
| 38 | + public function renderParams() | |
| 39 | + { | |
| 40 | + $this->vbo_app->loadSelect2(); | |
| 41 | + $channels = $this->loadChannels(); | |
| 42 | + $current_channels = $this->getParam('channels', array()); | |
| 43 | + ?> | |
| 44 | + <div class="vbo-param-container"> | |
| 45 | + <div class="vbo-param-label"><?php echo JText::translate('VBOCHANNELS'); ?></div> | |
| 46 | + <div class="vbo-param-setting"> | |
| 47 | + <select name="<?php echo $this->inputName('channels', true); ?>" id="<?php echo $this->inputID('channels'); ?>" multiple="multiple"> | |
| 48 | + <?php | |
| 49 | + foreach ($channels as $chkey => $chval) { | |
| 50 | + ?> | |
| 51 | + <option value="<?php echo $chkey; ?>"<?php echo is_array($current_channels) && in_array($chkey, $current_channels) ? ' selected="selected"' : ''; ?>><?php echo $chval; ?></option> | |
| 52 | + <?php | |
| 53 | + } | |
| 54 | + ?> | |
| 55 | + </select> | |
| 56 | + </div> | |
| 57 | + </div> | |
| 58 | + | |
| 59 | + <script type="text/javascript"> | |
| 60 | + jQuery(function() { | |
| 61 | + jQuery('#<?php echo $this->inputID('channels'); ?>').select2(); | |
| 62 | + }); | |
| 63 | + </script> | |
| 64 | + <?php | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Tells whether the rule is compliant. | |
| 69 | + * | |
| 70 | + * @return bool True on success, false otherwise. | |
| 71 | + */ | |
| 72 | + public function isCompliant() | |
| 73 | + { | |
| 74 | + $book_channel = $this->getPropVal('booking', 'channel'); | |
| 75 | + $allowed_channels = $this->getParam('channels', array()); | |
| 76 | + $sales_customer = (strpos($book_channel, 'customer') === 0 && strpos($book_channel, '_') !== false); | |
| 77 | + | |
| 78 | + if (empty($book_channel) || $sales_customer) { | |
| 79 | + if (in_array('website', $allowed_channels)) { | |
| 80 | + // website (or sales customer) booking allowed | |
| 81 | + return true; | |
| 82 | + } | |
| 83 | + // useless to proceed | |
| 84 | + return false; | |
| 85 | + } | |
| 86 | + | |
| 87 | + $full_source = $book_channel; | |
| 88 | + if (strpos($book_channel, '_') !== false) { | |
| 89 | + $parts = explode('_', $book_channel); | |
| 90 | + $full_source = $parts[0]; | |
| 91 | + } | |
| 92 | + | |
| 93 | + return in_array(strtolower($full_source), $allowed_channels); | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * Internal function for this rule only. | |
| 98 | + * | |
| 99 | + * @return array | |
| 100 | + */ | |
| 101 | + protected function loadChannels() | |
| 102 | + { | |
| 103 | + $channels = array( | |
| 104 | + 'website' => JText::translate('VBORDFROMSITE') | |
| 105 | + ); | |
| 106 | + | |
| 107 | + if (!is_file(VCM_SITE_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'lib.vikchannelmanager.php')) { | |
| 108 | + return $channels; | |
| 109 | + } | |
| 110 | + | |
| 111 | + $dbo = JFactory::getDbo(); | |
| 112 | + | |
| 113 | + try { | |
| 114 | + $q = "SELECT `name` FROM `#__vikchannelmanager_channel` ORDER BY `name` ASC;"; | |
| 115 | + $dbo->setQuery($q); | |
| 116 | + $dbo->execute(); | |
| 117 | + if ($dbo->getNumRows()) { | |
| 118 | + $records = $dbo->loadAssocList(); | |
| 119 | + foreach ($records as $ch) { | |
| 120 | + $channels[$ch['name']] = ucwords($ch['name']); | |
| 121 | + } | |
| 122 | + } | |
| 123 | + } catch (Exception $e) { | |
| 124 | + // do nothing | |
| 125 | + } | |
| 126 | + | |
| 127 | + return $channels; | |
| 128 | + } | |
| 129 | + | |
| 130 | +} | |