You are here

function signup_nodeapi in Signup 5

Same name and namespace in other branches
  1. 5.2 signup.module \signup_nodeapi()
  2. 6.2 signup.module \signup_nodeapi()
  3. 6 signup.module \signup_nodeapi()

hook_nodeapi implementation

Related topics

File

./signup.module, line 446

Code

function signup_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  global $form_values;
  switch ($op) {
    case 'insert':
      if (isset($form_values['signup_enabled'])) {
        if ($form_values['signup_enabled'] == 1) {
          $values = array(
            $node->nid,
            $form_values['signup_forwarding_email'],
            $form_values['signup_send_confirmation'],
            $form_values['signup_confirmation_email'],
            $form_values['signup_send_reminder'],
            $form_values['signup_reminder_days_before'],
            $form_values['signup_reminder_email'],
          );
        }
      }
      elseif (variable_get('signup_form_' . $node->type, 0)) {

        // The form doesn't include any information about signups, but
        // the node type is signup-enabled. This would happen if a
        // user without any signup admin permissions creates a node
        // that has been signup-enabled based on the node type. In
        // this case, we use the site-wide default signup settings.
        $defaults = db_fetch_array(db_query("SELECT * from {signup} WHERE nid = 0"));
        $values = array(
          $node->nid,
          $defaults['forwarding_email'],
          $defaults['send_confirmation'],
          $defaults['confirmation_email'],
          $defaults['send_reminder'],
          $defaults['reminder_days_before'],
          $defaults['reminder_email'],
        );
      }
      if (isset($values)) {
        db_query("INSERT INTO {signup} (nid, forwarding_email, send_confirmation, confirmation_email, send_reminder, reminder_days_before, reminder_email) VALUES (%d, '%s', %d, '%s', %d, %d, '%s')", $values);
      }
      break;
    case 'update':
      if (isset($form_values['signup_enabled'])) {
        $has_signup_record = db_result(db_query('SELECT COUNT(*) FROM {signup} WHERE nid = %d', $node->nid));
        switch ($form_values['signup_enabled']) {
          case 1:

            // Enabled
            if ($has_signup_record) {
              db_query("UPDATE {signup} SET forwarding_email  = '%s', send_confirmation = %d, confirmation_email  = '%s', send_reminder = %d, reminder_days_before = %d, reminder_email = '%s' WHERE nid = %d", $node->signup_forwarding_email, $node->signup_send_confirmation, $node->signup_confirmation_email, $node->signup_send_reminder, $node->signup_reminder_days_before, $node->signup_reminder_email, $node->nid);
            }
            else {
              db_query("INSERT INTO {signup} (nid, forwarding_email, send_confirmation, confirmation_email, send_reminder, reminder_days_before, reminder_email) VALUES (%d, '%s', %d, '%s', %d, %d, '%s')", $node->nid, $node->signup_forwarding_email, $node->signup_send_confirmation, $node->signup_confirmation_email, $node->signup_send_reminder, $node->signup_reminder_days_before, $node->signup_reminder_email);
            }
            break;
          case 2:

            // Disabled, and delete {signup_log}, too
            db_query("DELETE FROM {signup_log} WHERE nid = %d", $node->nid);

          // No break, fall through and remove from {signup} too.
          case 0:

            // Disabled, but leave {signup_log} alone
            if ($has_signup_record) {
              db_query("DELETE FROM {signup} WHERE nid = %d", $node->nid);
            }
            break;
        }
      }
      break;
    case 'delete':

      // Clean up the signup tables for the deleted node.
      db_query("DELETE FROM {signup} WHERE nid = %d", $node->nid);
      db_query("DELETE FROM {signup_log} WHERE nid = %d", $node->nid);
      break;
    case 'load':

      // Check for a signup for this node.
      // If it's a new node, load the defaults.
      $result = db_query("SELECT * FROM {signup} WHERE nid = %d", $node->nid ? $node->nid : 0);

      // Load signup data for both new nodes w/ enabled node types,
      // and any existing nodes that are already signup enabled.
      if (!$node->nid && variable_get('signup_form_' . $node->type, 0) || $node->nid && db_num_rows($result)) {
        $signup = db_fetch_object($result);
        $node->signup = 1;
        $node->signup_forwarding_email = $signup->forwarding_email;
        $node->signup_send_confirmation = $signup->send_confirmation;
        $node->signup_confirmation_email = $signup->confirmation_email;
        $node->signup_send_reminder = $signup->send_reminder;
        $node->signup_reminder_days_before = $signup->reminder_days_before;
        $node->signup_reminder_email = $signup->reminder_email;
        $node->signup_completed = $signup->completed;
      }
      else {
        $node->signup = 0;
      }
      break;
    case 'view':
      $suppress = module_invoke_all('signup_suppress', $node);

      // If this is a signup node, start checks for what's to be printed.
      // Only include any of this if we're trying to view the node as
      // a page, not during the view from comment validation, etc.
      if ($node->signup && $page && !in_array(TRUE, $suppress)) {
        global $user;
        $anon_signup_form = array();

        // The node has been closed for signups, and the user has
        // signup permissions.  Let them know it's closed.
        if ($node->signup_completed) {
          if (user_access('sign up for content')) {
            $output = '<h3>' . t('Signups closed for this event') . '</h3>';
          }
        }
        else {
          if ($user->uid == 0) {

            // This is an anonymous user. If they have signup permissions,
            // then build the anon portion of the sigup form.  If not, then
            // display the login link.
            $login_array = array(
              '!login' => l(t('login'), 'user/login', array(), drupal_get_destination()),
              '!register' => l(t('register'), 'user/register', array(), drupal_get_destination()),
            );
            if (user_access('sign up for content')) {
              $needs_signup_form = TRUE;
              $anon_signup_form['signup_anon_mail'] = array(
                '#type' => 'textfield',
                '#title' => t('Email'),
                '#description' => t('An e-mail address is required for users who are not registered at this site. If you are a registered user at this site, please !login to sign up for this event.', $login_array),
                '#size' => 40,
                '#maxlength' => 255,
                '#required' => TRUE,
              );
            }
            else {
              $needs_signup_form = FALSE;

              // If not, then display the appropriate login/register
              // link if the default authenticated user role can signup.
              $signup_roles = user_roles(FALSE, 'sign up for content');
              if (!empty($signup_roles[DRUPAL_AUTHENTICATED_RID])) {
                if (variable_get('user_register', 1) == 0) {
                  $anon_login_text = t('Please !login to sign up for this event.', $login_array);
                }
                else {
                  $anon_login_text = t('Please !login or !register to sign up for this event.', $login_array);
                }
                $output .= '<div class="signup_anonymous_login">' . $anon_login_text . '</div>';
              }
            }
          }
          else {

            // See if the user is already signed up for this node.
            $result = db_query("SELECT signup_time, form_data FROM {signup_log} WHERE uid = %d AND nid = %d", $user->uid, $node->nid);
            $needs_signup_form = db_num_rows($result) == 0;
          }
          if ($needs_signup_form) {

            // User isn't signed up, so check to make sure they have signup
            // permissions, and if so, print the themed signup form.
            if (user_access('sign up for content')) {
              $output = drupal_get_form('signup_form', $node, $anon_signup_form);
            }
          }
          elseif ($user->uid !== 0 && isset($result)) {

            // The authenticated user is already signed up, so print a table
            // of their signup data, and give them the option to cancel.
            $result = db_fetch_object($result);
            $form_data = unserialize($result->form_data);
            $output .= theme('signup_custom_data_table', $form_data);
            $output .= drupal_get_form('signup_form_cancel', $node);
          }
        }

        // If the user has the view signups perm, display the current signups.
        // Pull all users signed up for this event, and start table creation.
        if (user_access('view all signups')) {
          $registered_signups = db_query("SELECT u.uid, u.name, s.signup_time, s.form_data FROM {signup_log} s INNER JOIN {users} u ON u.uid = s.uid WHERE s.nid = %d AND u.uid <> 0", $node->nid);
          $anon_signups = db_num_rows(db_query("SELECT anon_mail FROM {signup_log} WHERE nid = %d AND uid = 0", $node->nid));
          $header = array(
            array(
              'data' => t('!users signed up', array(
                '!users' => format_plural(db_num_rows($registered_signups) + $anon_signups, '1 individual', '@count individuals'),
              )),
            ),
          );
          $rows = array();
          while ($signed_up_user = db_fetch_object($registered_signups)) {
            $rows[] = array(
              theme('username', $signed_up_user),
            );
          }
          if ($anon_signups) {
            $rows[] = array(
              t('!count anonymous', array(
                '!count' => $anon_signups,
              )),
            );
          }
          $output .= theme('table', $header, $rows);
        }

        // Save output into a node property for retrieval from the theme layer.
        $node->signup_view = $output;

        // Store the data directly into the content array, for default display.
        $node->content['signup'] = array(
          '#value' => $output,
          '#weight' => 10,
        );
      }
      break;
  }
}