<?php
// $Id: node.admin.inc,v 1.4 2008/11/19 18:35:11 dww Exp $


/**
 * @file
 * Theme functions for the signup node administration page (node/N/signups).
 */

/**
 * Theme function for the signup administrative tab (node/N/signups).
 *
 * This is responsible for rendering the signup summary form (allows
 * admins to open/close signups, set a signup limit, and see the total
 * number of signups), the table of signup details (generated by
 * signup_node_admin_page()), and if the node is signup-enabled, the
 * form to signup other users.
 *
 * @param $node
 *   The node object for the signup-enabled node this is a tab on.
 * @param $signup_node_admin_summary_form
 *   The rendered HTML for the signup node summary form (to set the signup
 *   limit, open/close signups, see the total number of signups, etc).
 * @param $signup_node_admin_details_form
 *   The rendered HTML for the signup node details form (to view all the users
 *   who have signed up, their full signup details, and checkboxes to cancel
 *   multiple signups at once.
 * @param $signup_form
 *   If the signups are open on this node, the HTML to signup another user,
 *   otherwise, and empty string.
 */
function theme_signup_node_admin_page($node, $signup_node_admin_summary_form, $signup_node_admin_details_form, $signup_form) {
  $output = '';

  // Administrative summary table to control signups for this node.
  $output .= $signup_node_admin_summary_form;

  // Details for each user who signed up.
  $output .= $signup_node_admin_details_form;

  // Form for admins to signup other users (if signups are still open).
  $output .= $signup_form;

  return $output;
}

/**
 * Renders the HTML for the per-node signup summary administrative form.
 */
function theme_signup_node_admin_summary_form($form) {
  $row = array(
    drupal_render($form['status']),
    drupal_render($form['total']),
    drupal_render($form['limit']),
    drupal_render($form),
  );
  $header = array(
    t('Status'),
    t('Total'),
    t('Limit'),
    t('Operations'),
  );
  $fieldset = array(
    '#title' => t('Signup summary'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#value' => theme('table', $header, array($row)),
  );
  return theme('fieldset', $fieldset);
}

function theme_signup_node_admin_details_form($form) {
  $fieldset = array(
    '#title' => t('Signup details'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  if (!empty($form['users']['#options'])) {
    $header = $form['#header'];
    $rows = array();
    foreach ($form['users']['#options'] as $key => $value) {
      $rows[] = array(
        'cancel_checkbox' => drupal_render($form['users'][$key]),
        'username' => drupal_render($form['username'][$key]),
        'signup_date' => drupal_render($form['signup_date'][$key]),
        'signup_form_data' => drupal_render($form['signup_form_data'][$key]),
      );
    }
    $fieldset['#value'] = theme('table', $header, $rows) . drupal_render($form['submit_cancel']);
  }
  else {
    $fieldset['#value'] = '<span>'. drupal_render($form['no_users']) .'</span>';
  }
  return theme('fieldset', $fieldset) . drupal_render($form);
}

/**
 * Renders custom signup user data into a human-readable format.
 *
 * WARNING: This theme function is recursive (it calls itself for
 * nested data), so if you override it, be sure not to change the part
 * where it does "call_user_func(__FUNCTION__)".
 *
 * @param $data
 *   Array of custom user signup data.
 *
 * @return
 *   User data directly formatted in divs.
 *
 * @see theme_signup_user_form()
 */
function theme_signup_custom_data($data) {
  $output = '';
  // Loop through each first level element.
  foreach ($data as $key => $value) {
    $output .= '<div id="'. signup_id_safe($key) .'">';
    if (is_array($value)) {
      // Element is nested, render it recursively.
      // Instead of the overhead of theme(), just call ourself directly.
      $output .= call_user_func(__FUNCTION__, $value);
    }
    else {
      $output .= $key .': '. check_plain($value);
    }
    $output .= "</div>\n";
  }
  return $output;
}

