<?php
// $Id: node.admin.inc,v 1.4.2.4 2009/09/21 05:23:09 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.
 */
function theme_signup_node_admin_page($node, $signup_node_admin_summary_form, $signup_node_admin_details_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;

  return $output;
}

/**
 * Renders the HTML for the per-node signup summary administrative form.
 */
function theme_signup_node_admin_summary_form($form) {
  $output = '';
  $output .= '<div class="container-inline">' . drupal_render($form['status']);
  if (!empty($form['submit']) && $form['status']['#type'] == 'select') {
    $output .= ' ' . drupal_render($form['submit']);
  }
  $output .= '</div>';
  foreach (element_children($form) as $key) {
    if (!in_array($key, array('status', 'submit', 'nid', 'form_build_id', 'form_token', 'form_id'))) {
      $prefix = '<div class="container-inline">';
      $suffix = '</div>';
      if (empty($form[$key]['#prefix'])) {
        $form[$key]['#prefix'] = $prefix;
      }
      else {
        $form[$key]['#prefix'] .= $prefix;
      }
      if (empty($form[$key]['#suffix'])) {
        $form[$key]['#suffix'] = $suffix;
      }
      else {
        $form[$key]['#suffix'] .= $suffix;
      }
    }
  }
  $output .= drupal_render($form);
  $fieldset = array(
    '#title' => t('Signup summary'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#value' => $output,
  );
  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]),
        'attended' => drupal_render($form['attended'][$key]),
      );
    }
    $fieldset['#value'] = '<div class="container-inline">';
    $fieldset['#value'] .= drupal_render($form['operation']);
    $fieldset['#value'] .= drupal_render($form['submit']);
    $fieldset['#value'] .= '</div>';
    $fieldset['#value'] .= theme('table', $header, $rows);
  }
  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 = '';
  // All of the possible array key values should already be translated as
  // string literals in theme_signup_user_form() via the #title attributes, so
  // passing a variable to t() is actually safe here.  However, to avoid
  // warnings when extracting strings, "hide" the call to t() by using a
  // variable to hold the function name.
  $tr = 't';
  // 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 .= $tr($key) .': '. check_plain($value);
    }
    $output .= "</div>\n";
  }
  return $output;
}

function theme_signup_attended_text($attended = NULL) {
  if ($attended === NULL) {
    return '';
  }
  if ($attended == 0) {
    return t('Did not attend');
  }
  else {
    return t('Attended');
  }
}

