Publish node in-between two dates

<< Go back | Posted: Friday, February 6 2009 - 01:04

Tags: drupal, node, publish

I needed a piece of functionality in a rather large Drupal site I am working on at the moment - to publish nodes only between two dates (or after the first date, end date is optional).

I decided to write a module for it, seen as the hooks in hook_nodeapi don't do quite what I want, and the contributed modules I was looking at were somewhat bloated.

So basically, this module sets up a table called node_between_dates (in the module install file) which holds the node ID, from time, and to time (which are both from the date field/widget in CCK). If the date in CCK has no end date, then it marks the 'to' value the same as the 'from' value.

The cron hook in the module then checks to see if there are rows in this table. If it finds rows where the start and end time are the same, then it only enables the node if the current time is more than the start time (and disables if less), and if it finds rows where the start and end times are different, then it will do it between the times. (I will stop there incase I am getting confusing, it has been known to happen...)

So...


/*
 * Implementation of hook_nodeapi().
 */
function node_betweendates_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
	switch ($op) {
		case 'prepare':
			break;
		case 'validate':
			
			####### DO THE VALIDATION STUFF HERE TO MAKE SURE YOU DON'T ENTER THE TO DATE BEFORE THE FROM DATE ETC.
			
			break;
		case 'submit':
			
      if (strlen($node->field_date[0]['value']) > 0) {
        // insert into database, first check:
        $result = db_result(db_query("SELECT nid FROM {node_between_dates} WHERE nid = %d", $node->nid));
        if ($result) {
          db_query("UPDATE {node_between_dates} SET tfrom = %d, tto = %d WHERE nid = %d", strtotime($node->field_date[0]['value']), strtotime($node->field_date[0]['value2']), $node->nid);
        }
        else {
        	db_query("INSERT INTO {node_between_dates} (nid, tfrom, tto) VALUES(%d, %d, %d)", $node->nid, strtotime($node->field_date[0]['value']), strtotime($node->field_date[0]['value2']));
        }
      }
			
			break;
		case 'insert':
			break;
		case 'update':
			break;
		case 'view':
			break;
	}
}

/*
 * Implementation of hook_cron().
 */
function node_betweendates_cron() {

	$result = db_query("SELECT * FROM {node_between_dates}");
	if (db_num_rows($result) > 0){
		while($x = db_fetch_object($result)) {
			$node = node_load($x->nid);
			if ($x->tfrom == $x->tto) { // the node only has a start date
				if (time() < $x->tfrom) { // not enabled yet
					if ($node->status == 1){
						$node->status = 0;
						node_save($node);
					}
				}
				else { // enable now
					if ($node->status = 0) {
						$node->status = 1;
						node_save($node);
					}
				}
			}
			else { // the node has both a start and end date, so we need to only enable between them
				if (time() < $x->tfrom || time() > $x->tto) { // not enabled yet
				  if ($node->status == 1){
            $node->status = 0;
            node_save($node);
          }
				}
				else { // enable now
					if ($node->status = 0) {
						$node->status = 1;
						node_save($node);
					}
				}
			}
		}
	}
	
}

Works a treat and didn't take long at all.


click here to add a comment