You are here

function node_registration_block_view in Node registration 7

Implements hook_block_view().

File

./node_registration.module, line 602

Code

function node_registration_block_view($delta = '') {
  global $user;
  switch ($delta) {
    case 'node_registration_cancel':
      if ($node = menu_get_object('node')) {
        $settings = $node->registration;

        // Extensive access check for this node & user.
        $access = node_registration_node_access($node, 'register', $user, $reason);

        // The default block title.
        $title = t('Unregister for %title', array(
          '%title' => $node->title,
        ));

        // Alter title depending on scenario.
        drupal_alter('registration_block_unsubscribe_title', $title, $reason);
        if (!$access && $reason == 'registered') {
          $registration = _node_registration_user_registered($node, $user);

          // Create form.
          module_load_include('inc', 'node_registration', 'includes/node_registration.forms');
          $form = drupal_get_form('node_registration_cancel', $registration);

          // Show form via template.
          $content = theme('node_registration_cancel_block', array(
            'form' => $form,
            'node' => $node,
            'registration' => $registration,
          ));
          return array(
            'subject' => $title,
            'content' => $content,
          );
        }
      }
      break;
    case 'node_registration_form':
    case 'node_registration_link':
      if ($node = menu_get_object('node')) {
        $settings = $node->registration;

        // Extensive access check for this node & user.
        $access = node_registration_node_access($node, 'register', $user, $reason);

        // The default block title.
        $title = t('Register for %title', array(
          '%title' => $node->title,
        ));

        // Alter title depending on scenario.
        drupal_alter('registration_block_title', $title, $reason);
        if ($access) {

          // All access!
          switch ($delta) {
            case 'node_registration_link':

              // Show link via template.
              $content = theme('node_registration_link', array(
                'node' => $node,
              ));
              break;
            case 'node_registration_form':

              // Create empty registration for empty form.
              $registration = entity_get_controller('node_registration')
                ->create(array(
                'nid' => $node->nid,
                'node' => $node,
              ));

              // Create form
              module_load_include('inc', 'node_registration', 'includes/node_registration.forms');
              $form = drupal_get_form('node_registration_form', $registration);

              // Show form via template.
              $content = theme('node_registration_form_block', array(
                'form' => $form,
                'node' => $node,
                'registration' => $registration,
              ));
              break;
          }
          return array(
            'subject' => $title,
            'content' => $content,
          );
        }
        else {

          // No access.
          $message_key = 'show_message_' . $reason;
          $show_message = $settings->{$message_key};
          if ($show_message) {

            // Possibly a Registration exists.
            $registration = NULL;
            if ('registered' == $reason) {
              $registration = _node_registration_user_registered($node, $user);
            }
            $theme_key = 'node_registration_block_message_' . $reason;
            $content = theme($theme_key, array(
              'user' => $user,
              'node' => $node,
              'registration' => $registration,
            ));
            return array(
              'subject' => $title,
              'content' => $content,
            );
          }
        }
      }
      break;
  }
}