Download & Extend

Calling Cron to set Historical Dates to Fully Booked

Project:Availability Calendars
Version:6.x-1.2
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

Thanks for the module.

Is there a way of automatically setting the dates which are in the past to Fully Booked? Or do you have to manually set the dates every day?

Comments

#1

Ok so I'm not very good at this. I've made a first attempt by copying a module that does this (uc_hotels) but the code below doesn't work.

Any ideas? Do you need to update the drupal core database to let if know that it needs to run this function when cron is run?

/**
* Implementation of hook_cron().
*/
function availability_calendars_cron() {
  //Get yesterdays details
  $day   = date('j', mktime(0, 0, 0, date('n'), date('j') - 1, date('y')));
  $month = date('n', mktime(0, 0, 0, date('n'), date('j') - 1, date('y')));
  $year  = date('Y', mktime(0, 0, 0, date('n'), date('j') - 1, date('y')));
 
  //check whether there is a database entry for yesterday - which indicates that there will be entries for all previous dates for that month
  //we don't need to worry about previous months as they are already hidden
  if(db_query("SELECT day FROM availability_calendars_day WHERE year = $year AND month = $month AND day = $day")){
      //update previous dates if they are found
      db_query("UPDATE availability_calendars_day SET status = 1 WHERE year = $year AND month = $month AND day <= $day");
  }
  //if we don't have previous dates then we'll need to add them
  else{
      //check that the day does not exist in the calendar or that the day does not equal 0
     while(db_query("SELECT day FROM availability_calendars_day WHERE year = $year AND month = $month AND day = $day")==FALSE AND $day >0){
         //insert into the database and decrease the day variable
         //I've inserted the node value in as I only have one node. This would need to be made more customisable. Don't know how
         db_query("INSERT INTO availability_calendars_day VALUES (2, $year, $month, $day, 1)");
         $day -= $day;
     }
  }
}

Any help is much appreciated!

#2

Title:Automatically set Historical Dates to Fully Booked» Calling Cron to set Historical Dates to Fully Booked
Category:support request» feature request

#3

Status:active» needs review

For those of you who are interested I have used the following code. It might not be pretty but it works.

/**
* Implementation of hook_cron().
*/
function availability_calendars_cron() {
  //Get yesterdays details
  $day   = date('j', mktime(0, 0, 0, date('n'), date('j') - 1, date('y')));
  $month = date('n', mktime(0, 0, 0, date('n'), date('j') - 1, date('y')));
  $year  = date('Y', mktime(0, 0, 0, date('n'), date('j') - 1, date('y')));

  //check whether there is a database entry for yesterday - which indicates that there will be entries for all previous dates for that month
  //we don't need to worry about previous months as they are already hidden
  if(mysqli_num_rows(db_query("SELECT day FROM availability_calendars_day WHERE year = $year AND month = $month AND day = $day")) != 0) {
//update previous dates if they are found
      db_query("UPDATE availability_calendars_day SET status = 1 WHERE year = $year AND month = $month AND day <= $day");
  }
  else {
//check that the day does not exist in the calendar or that the day does not equal 0
    while(mysqli_num_rows(db_query("SELECT day FROM availability_calendars_day WHERE year = $year AND month = $month AND day = $day"))==0 AND $day >0){
         //insert into the database and decrease the day variable
         //I've inserted the node value in as I only have one node. This would need to be made more customisable. Don't know how
         db_query("INSERT INTO availability_calendars_day VALUES (2, $year, $month, $day, 1)");
         $day = $day-1;
}
  }
}

#4

Actually the above code causes my cron to fail. Anyone have any ideas? I've tested the code separately and it seems to work.

Any help appreciated.

#5

Ok. Trial and error. This works now. But I still have my node id hardcoded into the Insert statement.

/**
* Implementation of hook_cron().
*/
function availability_calendars_cron() {
  //Get yesterdays details
  $day   = date('j', mktime(0, 0, 0, date('n'), date('j')-1, date('y')));
  $month = date('n', mktime(0, 0, 0, date('n'), date('j')-1, date('y')));
  $year  = date('Y', mktime(0, 0, 0, date('n'), date('j')-1, date('y')));

  if(db_result(db_query("SELECT COUNT(*) FROM {availability_calendars_day} WHERE year = $year AND month = $month AND day = $day"))) {
    //update previous date if it is found
    db_query("UPDATE {availability_calendars_day} SET status = 1 WHERE year = $year AND month = $month AND day <= $day");
  }
  else {
    while(!db_result(db_query("SELECT COUNT(*) FROM {availability_calendars_day} WHERE year = $year AND month = $month AND day = $day")) AND $day>0){
      //insert into the database
      //I've inserted the node value in as I only have one node. This would need to be made more customisable. Don't know how
      db_query("INSERT INTO {availability_calendars_day} VALUES (2, $year, $month, $day, 1)");

    //decrease day variable
    $day = $day-1;
    }
  }
}

#6

Status:needs review» fixed

I have implemented a similar feature based on your needs. Instead of altering the previous dates' data I am simply ignoring each dates data from the theme level and outputting them as booked. This means hook_cron is unnecessary and for historical purposes we could potentially get to the data if needed.

http://drupal.org/cvs?commit=471802

There are now options to enable the feature both within the node (per node setting) or from admin page (sitewide setting).

#7

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

nobody click here