Italian (Italy)

A custom Conditional Rule driver is a PHP file that declares a class with a proper name according to the file name, that extends the parent abstract class of VikBooking. This means that specific methods must be implemented to define the filters (settings) of the rule, as well as to verify their compliance against the given reservation and room-reservation data.

It is strongly recommended to look at the source code of one of the existing Conditional Text Rules pre-installed in VikBooking in order to understand the logic of the framework, and how to implement your own rule. There are many simple rules available by default in VikBooking, such as the one to match a specific room or a minimum number of nights of stay. You should start from an existing PHP file to clone it, rename it, and give the PHP class a proper name according to the file name.

The below example shows how to declare the PHP class and how to implement the mandatory methods in a custom Conditional Rule file.

<?php
/**
 * @package     VikBooking
 * @subpackage  custom_one_conditional_rule
 */

defined('ABSPATH') or die('No script kiddies please!');

/**
 * Conditional Rule "Custom One" child Class of VikBookingConditionalRule
 */
class VikBookingConditionalRuleCustomOne extends VikBookingConditionalRule
{
    /**
     * Class constructor will define the rule name, description and identifier.
     */
    public function __construct()
    {
        // call parent constructor
        parent::__construct();

        $this->ruleName  = 'Custom One';
        $this->ruleDescr = 'A dummy example about number of nights';
        $this->ruleId    = basename(__FILE__);
    }

    /**
     * Displays the rule parameters (settings).
     * 
     * @return  void
     */
    public function renderParams()
    {
        ?>
        <div class="vbo-param-container">
            <div class="vbo-param-label">Number of nights</div>
            <div class="vbo-param-setting">
                <input type="number" name="<?php echo $this->inputName('nights'); ?>" value="<?php echo $this->getParam('nights', ''); ?>" min="1" />
            </div>
        </div>
        <?php
    }

    /**
     * Tells whether the rule is compliant.
     * 
     * @return  bool    True on success, false otherwise.
     */
    public function isCompliant()
    {
        $booking_nights = (int)$this->getPropVal('booking', 'days', 0);

        $setting_nights = (int)$this->getParam('nights', 1);

        // return true if number of nights for this booking is equal to the setting value
        return $booking_nights === $setting_nights;
    }
}

This sample code will add a new rule available for selection on any Conditional Text. The settings will let you enter an exact number of nights of stay, and the compliance validation method will return "true" if the booking is for that exact number of nights of stay.

Feel free to debug the objects in the various methods, or to check the original source code of the parent abstract class "VikBookingConditionalRule".

Ultimo aggiornamento: 2024-02-22
Utile?
100% delle persone lo ha trovato utile.