You are here

function _webform_report_get_body_content in Webform Report 5

Same name and namespace in other branches
  1. 6.2 webform_report.inc \_webform_report_get_body_content()
  2. 6 webform_report.inc \_webform_report_get_body_content()
  3. 7 webform_report.inc \_webform_report_get_body_content()

Get node body content for the specified webform report

Parameters

data is a database query result set:

node is the current node object:

formatcsv if TRUE format the output as a CSV file:

Return value

a string of text or a themed table

2 calls to _webform_report_get_body_content()
webform_report_csv in ./webform_report.module
Output a webform report in CSV format
webform_report_view in ./webform_report.module
Implementation of hook_view

File

./webform_report.module, line 567

Code

function _webform_report_get_body_content($data, $node, $formatcsv = FALSE) {
  if (db_num_rows($data) > 0) {
    $fields = array();

    // webform field names
    $values = array();

    // webform field values
    $last_value = 0;

    // begin fields
    if ($node->options['show_edit']) {
      $fields['edit'] = array(
        'data' => t('Edit'),
        'field' => 'edit',
        'sort' => $_GET['sort'],
      );
    }
    if ($node->options['show_date'] || $node->options['show_time']) {
      $fields['date'] = array(
        'data' => t('Submitted'),
        'field' => 'date',
        'sort' => $_GET['sort'],
      );
    }
    if ($node->options['show_user']) {
      $fields['user'] = array(
        'data' => t('User'),
        'field' => 'user',
        'sort' => $_GET['sort'],
      );
    }
    if ($node->options['show_ip']) {
      $fields['remote_addr'] = array(
        'data' => t('IP Address'),
        'field' => 'remote_addr',
        'sort' => $_GET['sort'],
      );
    }
    while ($row = db_fetch_object($data)) {
      if (!isset($fields[$row->cid])) {

        // load fields once only
        $fields[$row->cid] = array(
          'data' => check_plain($row->name),
          'field' => $row->cid,
          'sort' => $_GET['sort'],
        );
      }

      // end fields
      if ($node->options['show_edit']) {
        $values[$row->sid]['edit'] = array(
          'data' => l(t('edit'), 'node/' . $row->nid . '/submission/' . $row->sid . '/edit'),
        );
      }
      if ($node->options['show_date'] || $node->options['show_time']) {
        if ($node->options['show_date']) {
          $dateformat = 'Y-m-d';
          if ($node->options['show_time']) {
            $dateformat .= ' H:m';
          }
        }
        else {
          if ($node->options['show_time']) {
            $dateformat = 'H:m';
          }
        }
        $values[$row->sid]['date'] = array(
          'data' => date($dateformat, $row->submitted),
        );
      }
      if ($node->options['show_user']) {
        if ($row->user == '') {
          $row->user = t('anonymous');
        }
        $values[$row->sid]['user'] = array(
          'data' => $row->user,
        );
      }
      if ($node->options['show_ip']) {
        $values[$row->sid]['remote_addr'] = array(
          'data' => $row->remote_addr,
        );
      }

      // The value '0' means that a group of checkboxes has no selection.
      if ($row->data != '0') {

        // This will comma-separate multiple selections from the same group of checkboxes in the same submission.
        if ($row->cid == $last_cid && $row->sid == $last_sid && !empty($last_value)) {
          $row->data .= ', ' . $last_value;
        }

        // special handling for file uploads
        if ($row->type == 'file') {
          $tmp = unserialize($row->data);
          $link = ' ';
          if (!empty($tmp['filename'])) {
            $link = '<a href="' . base_path() . $tmp['filepath'] . '">' . $tmp['filename'] . ' (' . (int) ($tmp['filesize'] / 1024) . ' KB)' . '</a>';
          }
          $values[$row->sid][$row->cid] = array(
            'data' => $link,
          );
        }
        else {
          $values[$row->sid][$row->cid] = array(
            'data' => filter_xss($row->data),
          );
        }
      }
      else {
        if (!$formatcsv) {
          $values[$row->sid][$row->cid] = array(
            'data' => '&nbsp;',
          );

          // prevents the table cell from being omitted
        }
      }

      // override the report's sort column with the table sort column, if applicable
      if (isset($_GET['order'])) {
        if ($_GET['order'] == $row->name && $node->kcid != $row->cid) {
          $node->kcid = $row->cid;
        }
      }
      else {

        // no table sort link has been clicked yet
        switch ($node->kcid) {
          case $row->cid:
            $_GET['order'] = $row->name;

            // make the sort arrow marker appear in the table column header
            break;
          case 'remote_addr':
            $_GET['order'] = t('IP Address');
            break;
          case 'user':
            $_GET['order'] = t('User');
            break;
          case 'date':
            $_GET['order'] = t('Submitted');
            break;
        }
      }
      $last_cid = $row->cid;
      $last_sid = $row->sid;
      $last_value = $row->data;
    }
    reset($fields);

    // top
    // get an array of columns for sorting
    $column = array();
    foreach ($fields as $key1 => $field) {
      foreach ($values as $key2 => $value) {

        // make sort order case-insensitive and remove spaces (and accents if accents module installed)
        if (module_exists('accents')) {
          $column[$key1][$key2] = trim(strtolower(accents_search_preprocess($value[$key1]['data'])));
        }
        else {
          $column[$key1][$key2] = trim(strtolower($value[$key1]['data']));
        }
      }
    }

    // override the report's sort order with the table sort order, if applicable
    if (isset($_GET['sort'])) {
      switch ($_GET['sort']) {
        case 'asc':
          $node->sort = SORT_ASC;
          break;
        case 'desc':
          $node->sort = SORT_DESC;
          break;
      }
    }
    else {

      // override the table's sort order with the report's
      if ($node->sort == SORT_ASC) {
        $_GET['sort'] = 'asc';

        // default table sort will be ascending
      }
      else {
        $_GET['sort'] = 'desc';

        // default table sort will be descending
      }
    }

    // handle the sorting of optional, non-component fields
    switch ($_GET['order']) {
      case t('IP Address'):
        $node->kcid = 'remote_addr';
        $sort = SORT_REGULAR;
        break;
      case t('User'):
        $node->kcid = 'user';
        $sort = SORT_STRING;
        break;
      case t('Submitted'):
        $node->kcid = 'date';
        $sort = SORT_REGULAR;
        break;
      default:
        $sort = SORT_REGULAR;
    }

    // sort the columns and their contents according to specified criteria
    array_multisort($column[$node->kcid], (int) $node->sort, $values);

    // filter the table values
    if ($node->filter_type != 0) {
      $values = _webform_report_filter_values($values, $node);
    }
    if ($formatcsv) {

      // format as csv
      $output = _webform_report_output_csv($fields, $values);
    }
    else {
      $values = _webform_report_add_data_links($fields, $values);

      // display number of rows after description
      $output .= ' (' . count($values) . ' ' . t('results') . ') ' . l(t('Download as CSV'), 'node/' . arg(1) . '/csv') . '</p>';
      $output .= _webform_report_pager($fields, $values, $node);
    }
  }
  else {

    // no submitted data
    $output = t('Note: There are no submissions for the selected webform.</b> Either the form
       has not yet been completed by anyone, or the results have been cleared. This will not
       prevent you from creating this report, but this message will be displayed on the report
       page until someone submits the selected webform.');
  }
  return $output;
}