By jodathegrey on
Hi, I was working on validation of CCK fields for the session node (D6 COD) - an alternative way of entering session requests bypassing the room slot selection, extending the _nodeapi, started small with comparison of two date fields, then had to bring in some SQL for verify that dates entered did not already exist, then depending on the node status (updated by admin) send an email out... thought it may be of some use to anyone joining the community as an example, also if someone would like to advise on where I can improve my Drupalese in the code, then I would appreciate that.
<?php
// $Id: glue.module,v 1.0.0.1 2012/05/17 12:12:12 Joda Exp $
// JRS - Misc code to glue the application together
function glue_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
if ($node->type == 'session') {
// This next line will show you the whole node, copy and paste into a text editor and you can see more readily the array and element names
//print_r($node); // can be used to see all fields in the node and their array positions and labels
if ($op == 'insert' || $op == 'update' || $op == 'validate' || $op == 'presave') {
// Date validation - make sure that "to date" is not more than 2 hours after the "from date"
$fromdate = $node->field_session_calendardate[0]['value'];
// returns YYYY-MM-DD HH:MM:SS formatted 'from date'
$todate = $node->field_session_calendardate[0]['value2'];
// returns YYYY-MM-DD HH:MM:SS formatted 'to date'
if ($fromdate != '' && $todate != '') {
$intervalo = date_diff(date_create($fromdate), date_create($todate));
$out = $intervalo->format("Years:%Y,Months:%M,Days:%d,Hours:%H,Minutes:%i");
$intvalY = (int)$intervalo->format("%Y");
$intvalM = (int)$intervalo->format("%M");
$intvald = (int)$intervalo->format("%d");
$intvalH = (int)$intervalo->format("%H");
$intvali = (int)$intervalo->format("%i");
if ($intvalY > 0 || $intvalM > 0 || $intvald > 0 || $intvalH > 2 || ($intvalH == 2 && $intvali > 0)){
$info = 'session nid'.$node->nid.':Length of meeting cannot be greater than 2 hours: ['.$out.']';
watchdog('Information - glue module', $info);
form_set_error('title', t('Length of meeting cannot be greater than 2 hours: ['.$out.']'));
}
}
// Make sure that this date and time have not already requested for this specific room
// discover if any rooms have been booked or already requested to start between the from and to dates
// BUT excluding the node (if one exists) currently being updated
// add 1 minute to fromdate so it doesnt fall into a time span that another booking ends with the exact same time
$sql = "SELECT s.nid FROM content_field_session_room c, content_type_session s ";
$sql .= " WHERE DATE_ADD('".$fromdate."', INTERVAL 1 MINUTE) BETWEEN s.field_session_calendardate_value AND s.field_session_calendardate_value2 ";
if ($node->nid) { // if no node (its new) then nothing to self check against
$sql .= " AND s.nid != ".$node->nid;
} else {
$sql .= " AND s.nid != 0";
}
$sql .= " AND c.nid = s.nid ";
$sql .= " AND c.field_session_room_nid = ".$node->field_session_room[0]['nid'];
$res = db_query($sql);
if (!db_result($res) == FALSE) {
//form_set_error($node->title,$sql);
watchdog('Information - glue module:','session nid'.$node->nid.'Meetings are already booked or requested after the start date selected, in the room selected.['.$sql.']');
form_set_error('title', t('Meetings are already booked or requested after the start date selected, in the room selected'));
} else {
// discover if any rooms have been booked or already requested to end between the from and to dates
// BUT excluding the node currently being updated
$sql = "SELECT s.nid FROM content_field_session_room c, content_type_session s ";
$sql .= " WHERE '".$todate."' BETWEEN s.field_session_calendardate_value AND s.field_session_calendardate_value2 ";
if ($node->nid) { // if no node (its new) then nothing to self check against
$sql .= " AND s.nid != ".$node->nid;
} else {
$sql .= " AND s.nid != 0";
}
$sql .= " AND c.nid = s.nid ";
$sql .= " AND c.field_session_room_nid = ".$node->field_session_room[0]['nid'];
$res = db_query($sql);
if (!db_result($res) == FALSE) {
//form_set_error($node->title,$sql);
watchdog('Information - glue module:','session nid'.$node->nid.'Meetings are already booked or requested before the end date selected, in the room selected.['.$sql.']');
form_set_error($node->title, t('Meetings are already booked or requested before the end date selected, in the room selected'));
}
}
}
// Before saving
$todate = new DateTime($todate);
$fromdate = new DateTime($fromdate);
if ($op == 'presave') {
// Check if not errors, then send an email for this meeting decision, if one has been made to accept or decline it
$errors = form_get_errors();
if (count($errors)) {
// Do nothing as there are errors
} else {
// if all data entered OK and its an approved message then send an email
$to = $node->field_session_bookedby[0]['email'];
$from = "fromme@mydomain.org";
$subject = "Meeting Room Booking Request Notification";
switch ($node->field_accepted[0]['value']) {
case "1":
$body = "Your Meeting Request has been approved.\n\n";
$body .= " Meeting Title:".$node->title."\n\n";
$body .= " Meeting Date/Time: From:".$fromdate->format('l jS \of F Y h:i A')."\n\n";
$body .= " Meeting Date/Time: To:".$todate->format('l jS \of F Y h:i A')."\n\n";
$sent = drupal_mail('glue', 'reply', $to, language_default(),array('body' => $body, 'subject' => $subject), $from, TRUE);
if(!$sent){
drupal_set_message('!!!! Confirmation acceptance email FAILED to send to:'.$to);
} else {
drupal_set_message('Confirmation acceptance email sent to:'.$to);
}
break;
case "2":
$body = "Your Meeting Request has been declined.\n\n";
$body .= " Meeting Title:".$node->title."\n\n";
$body .= " Meeting Date/Time: From:".$fromdate->format('l jS \of F Y h:i A')."\n\n";
$body .= " Meeting Date/Time: To:".$todate->format('l jS \of F Y h:i A')."\n\n";
$sent = drupal_mail('glue', 'reply', $to, language_default(),array('body' => $body, 'subject' => $subject), $from, TRUE);
if(!$sent){
drupal_set_message('!!!! Confirmation declined email FAILED to send to:'.$to);
} else {
drupal_set_message('Confirmation declined email sent to:'.$to);
}
break;
}
}
}
}
}
/**
* Implements hook_mail().
*/
function glue_mail($key, &$message, $params) {
$message['subject'] = $params['subject'];
$message['body'] = $params['body'];
//'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
$headers = array(
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal'
);
foreach ($headers as $key => $value) {
$message['headers'][$key] = $value;
}
}
/**
* This module is Views 2.0 enabled.
* Implementation of hook_views_api().
*/
function glue_views_api() {
return array('api' => 2.0);
}