• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

Adapt js datepicker code to work with Fabrik

thellie

Member
Hey :)
I've been given some code which allows me to limit the date selection to only the first and third Monday of each calendar month.

I think it needs adapting to work inside the 'Allow date function' field, but I don't know what to change or how... could anyone help please?
Code:
// test: first monday of this month
// result: true
//var dates = [new Date(2017,8,4)];

// test: third monday of this month
// result: true
//var dates = [new Date(2017,8,18)];

// test: first and third monday of this month
// result: true
var dates = [new Date(2017,8,4), new Date(2017,8,18)];

// test: first monday, third monday, and random day from this month
// result: false
//var dates = [new Date(2017,8,4), new Date(2017,8,18), new Date(2017,8,22)];

alert(validate(dates));

function validate(dates) {
  var valid = true;
  var mondays = getMondays();
  var firstMonday = mondays[0].setHours(0,0,0,0);
  var thirdMonday = mondays[2].setHours(0,0,0,0);
 
  if (dates && dates.length > 0) {
    for (var i = 0; i < dates.length; i++) {
      // Zero out time so only year, month, and day is compared
      var d = dates[i].setHours(0,0,0,0);
     
      if (d != firstMonday && d != thirdMonday) {
        return false;
      }
    }
  }
  else {
    valid = false;
  }
 
  return valid;
}

function getMondays() {
  var d = new Date(),
    month = d.getMonth(),
    mondays = [];

  d.setDate(1);

  // Get the first Monday in the month
  while (d.getDay() !== 1) {
    d.setDate(d.getDate() + 1);
  }

  // Get all the other Mondays in the month
  while (d.getMonth() === month) {
    mondays.push(new Date(d.getTime()));
    d.setDate(d.getDate() + 7);
  }

  return mondays;
}
 
That would have to go in the "PHP get allowed dates". And the problem there is that when doing it with PHP, you have to build an array of ALL the dates that are allowed. So you'd need to know how far into the future you want to allow selections.

It only when using the JavaScript "allow dates" that it works dynamically, so the JS code you provide is called for each date as it is displayed. But you have PHP, not JS, code.

So ... how far into the future do you need to go?

-- hugh
 
Hey Hugh :)
Huh, I asked for js code on stackoverflow :p

I'd prefer js if possible as it does seem more flexible for this. Initially, I took your example from the datepicker element wiki for allowing every second Thursday, and adapted it to allow only the first Monday of each month. But I need, if possible, to get every 1st and 3rd Monday. I spent a day or two combing the net and trying different suggestions to accomplish this but couldn't nail it.

To answer your question, in theory it's infinite, but practically speaking probably about a year.
 
Well, obviously you can't built an infinitely sized array of dates. :)

The PHP function is useful when you have a finite sized list of allowed dates.

The JS function is useful when you need to check any possible date. Rather than building an array of allowed dates, that feature has to take a date and return true or false. It's called for every date in the month being displayed in the widget.

So the JS you need doesn't need to build an array of dates, it needs to be a function that takes a date, and decides if that date is allowed or not.

-- hugh
 
Okay, I understand the explanation, thanks. Still can't work out how I write the js (or php) correctly to allow the two non-consecutive specified days per month, so I'll leave it for now as I have other work to be getting on with.
 
Based on ...

https://stackoverflow.com/questions/32192982/get-a-given-weekday-in-a-given-month-with-javascript

Code:
var thisdate = new Date(date);
thisdate.setHours(0,0,0,0);

var day = 1; // monday
var nth = 1; // first

var first = new Date(thisdate.getFullYear(), thisdate.getMonth(), 1),
      add = (day - first.getDay() + 7) % 7 + (nth - 1) * 7;
first.setDate(1 + add);

nth = 3; // third

var third = new Date(thisdate.getFullYear(), thisdate.getMonth(), 1),
      add = (day - third.getDay() + 7) % 7 + (nth - 1) * 7;
third.setDate(1 + add);

//console.log(thisdate + ', ' + first + ', ' + third);

var result = (first.getTime() !== thisdate.getTime()) && (third.getTime() !== thisdate.getTime());

One of these days it'd be nice if you would take out a sub ... :)

-- hugh
 
You are a gentleman Mr Grits :)

Can you feel the glow of shame from here? That's why I haven't posted any questions for most of the year :(

I still nudge the board for extra funds to cover a subscription, but it's not the easiest thing in the world to convince them... especially as the subscription is equivalent to 3 days wages for me :eek:
 
Last edited:
I understand, which is why I do answer your questions when I can. But you might point out to them that $20 a month is pretty cheap for support on mission critical software, and that there is a very finite limit to the amount of free support I can provide. This is how I make my living. If they needed someone to fix the plumbing in their office, they'd expect to have to pay.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top