You are here

function simplenews_statistics_handler_unsubscribes::render in Simplenews Statistics 7.2

Same name and namespace in other branches
  1. 7 includes/views/handlers/simplenews_statistics_handler_unsubscribes.inc \simplenews_statistics_handler_unsubscribes::render()

Renders the field handler.

Overrides views_handler_field::render

File

includes/views/handlers/simplenews_statistics_handler_unsubscribes.inc, line 93
Definition of simplenews_statistics_handler_unsubscribes.

Class

simplenews_statistics_handler_unsubscribes
Description.

Code

function render($values) {
  if (!($nid = $this
    ->get_value($values, 'nid'))) {
    return;
  }
  $subscribers = simplenews_statistics_count_subscribers($nid);
  $start = !empty($values->send_start_timestamp) ? $values->send_start_timestamp : $this
    ->get_value($values, 'send_start_timestamp');
  $number = $this->options['number'];
  $percentage = $this->options['percentage'];
  $precision = intval($this->options['precision']);
  $source = $this->options['source'];
  if ($start == 0) {
    return 'N/A';

    // Newsletter has not been sent yet.
  }

  // If exists, get next newsletter since sent date.
  $query = db_select('simplenews_statistics', 'ss')
    ->fields('ss', array(
    'send_start_timestamp',
  ))
    ->condition('ss.send_start_timestamp', $start, '>');
  $end = $query
    ->execute()
    ->fetchField();
  if ($end == FALSE) {

    // Count unsubs since newsletter started sending.
    $unsubs = simplenews_statistics_count_unsubscribes($nid, $start, REQUEST_TIME, $source);
  }
  else {

    // Count unsubs between sending of this newsletter and the next.
    $unsubs = simplenews_statistics_count_unsubscribes($nid, $start, $end, $source);
  }

  // Formatting.
  if ($percentage && $subscribers > 0) {

    // Percentage.
    $unsub_percent = round($unsubs / $subscribers * 100, $precision) . '%';
    if ($number) {

      // Percentage and number.
      return $unsub_percent . ' (' . $unsubs . ')';
    }
    return $unsub_percent;
  }
  return $unsubs;
}