You are here

function mass_contact_nodeapi in Mass Contact 5.2

Same name and namespace in other branches
  1. 6 mass_contact.module \mass_contact_nodeapi()

Implementation of hook_nodeapi().

File

./mass_contact.module, line 146
This is the main code file for the Mass Contact module. This module enables users to contact multiple users through selected roles.

Code

function mass_contact_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

  // If this is a delete operation and the node type is mass_contact, then
  // check for an attachment and delete the file if one exists.
  if ($op == 'delete' && $node->type == 'mass_contact') {

    // Look for the key phrase in the node body. If it exists, then there is
    // an attachment that needs to be deleted.
    $offset1 = strpos($node->body, '<hr>Attachment:<br />');
    if ($offset1) {

      // Using the saved attachment path, find the file name.
      $file_location = variable_get('mass_contact_attachment_location', file_directory_path() . '/mass_contact_attachments');
      $file_location_start = strpos($node->body, $file_location, $offset1);
      if ($file_location_start) {

        // Get the attachment file name.
        $file_location_end = strpos($node->body, '">', $file_location_start);
        $file_path_name = substr($node->body, $file_location_start, $file_location_end - $file_location_start);
        if (file_exists($file_path_name)) {
          if (!file_delete($file_path_name)) {

            // Log an error.
            watchdog('mass_contact', t('There was an error deleting the attachment.'), WATCHDOG_ERROR);
          }
        }
        else {

          // Log an error.
          watchdog('mass_contact', t('There was an indication of an attachment within the node body, but the attachment was not found. If the attachment is still there, it was NOT deleted.'), WATCHDOG_WARNING);
        }
      }
      else {

        // Log an error.
        watchdog('mass_contact', t('There was an indication of an attachment within the node body, but the attachment path was not found. If the attachment is still there, it was NOT deleted.'), WATCHDOG_WARNING);
      }
    }
  }
}