1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

Restrict Calendar popup to specific days.

Discussion in 'Standard Support' started by nycreate, Jan 11, 2011.

  1. nycreate

    nycreate Member

    Level: Community
    Hopefully I can solve some of my issues by breaking everything down into little bits..

    firstly...
    I want to restrict the calendar so a user can only select weekdays and only 2 days in advance from current day.
    Also maybe individual days such as bank holidays?

    Thanks in advance.
     
  2. nycreate

    nycreate Member

    Level: Community
    ^^friendly bump^^
     
  3. nycreate

    nycreate Member

    Level: Community
    Ive made a little progess with this by setting up an 'is not' validation and inputting all weekend dates and bank holidays, but
    how can I stop 'todays' date, 'tommorows' date etc?

    Thanks.
     
  4. nycreate

    nycreate Member

    Level: Community
    ^^friendly bump^^
     
  5. nycreate

    nycreate Member

    Level: Community
    ^^Friendly bump^^
     
  6. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Best way of doing this will be to use the disallowDate() feature. When we build the popup calendar widget, we look for a function call disallowDate() on the page, and if it exists, it gets called for every date being displayed on the calendar. If the function returns 'true', the date is not pickable. If it returns 'false', the date is. If the function returns a string (like 'foo') that string is used as the CSS class to apply to that date.

    So, create an XX.js file (in ./components/com_fabrik/js, where XX is the numeric ID of your form), and in it put:

    Code (Text):
    function disallowDate(cal, date) {
       var today = new Date;
       var dayDiff = date.getDayOfYear() - today.getDayOfYear();
       if (dayDiff > 2) {
          return true;
       }
       return false;
    }
    That should exclude all dates more than two days ahead. I'll leave it to your imagination on how to exclude weekends ... you'll find date.getDay() will help, which returns an int from 0 (Sunday) to 6 (Saturday).

    Of course things get a little more difficult if you are trying to do "two working days", but it's all still doable in JS. Just google around for solutions, there's stacks of examples out there on how to do date processing / comparisons in JS.

    Worth a little work to get this done in JS. Then you can disallow typing in the date element, so they can only use the popup, and you won't have to worry about any validation on the server side (excerpt maybe "not empty"), as they won't be able to enter any disallowed dates.

    -- hugh
     
  7. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    OK, so you've got my coding gland interested. Do you need to do 'working days'? In other words:

    If today is Wednesday, no problem - enable Thu and Fri on the cal.

    If today is Thursday, do you need to enable Fri and Mon on the calendar? Or just Fri?

    -- hugh
     
  8. nycreate

    nycreate Member

    Level: Community
    Hi Hugh,
    Thanks for this.
    Yes to get your coding gland satisfied, Always restrict to 2 'working days' from now (or later) so if today is thursday then next available day is mon or later and if today is friday next available days is tues or later, hope this is clear enough.

    I changed the true and false around and this restricts the next 2 days but obviously this doesnt take the weekends into account.

    Thanks in advance.
     
  9. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    OK ... lessee ... googling around there's loads of "add working days" functions out there, I just chose one at random. So, try this:

    Code (Text):
    function addWorkingDays(myDate,days) {
        //myDate = starting date, days = no. working days to add.
        var temp_date = new Date();
        var i = 0;
        var days_to_add = 0;
        while (i < (days)){
            temp_date = new Date(myDate.getTime() + (days_to_add*24*60*60*1000));
            //0 = Sunday, 6 = Saturday, if the date not equals a weekend day then increase by 1
            if ((temp_date.getDay() != 0) && (temp_date.getDay() != 6)){
                i+=1;
            }
            days_to_add += 1;
        }
        return new Date(myDate.getTime() + days_to_add*24*60*60*1000);
    }

    var my_today = new Date;
    var my_cutoff_day = addWorkingDays(my_today, 2);

    function disallowDate(cal, date) {
       return date > my_cutoff_day;
    }
    It would be fairly easy to add bank holidays to this, by creating an array of the days of the year (i.e. 0 thru 364 format) and then in the loop inside addWorkingDays use the dayofYear() method to skip anything that appears in the bank holidays array.

    Anyway, the above is untested, let me know if it works. If not, can you point me at the page? It'll be a lot easier to use firebug and step thru the code.

    -- hugh
     
    1 person likes this.

Share This Page