You are here

function simplenews_statistics_handler_unsubscribes::render in Simplenews Statistics 7

Same name and namespace in other branches
  1. 7.2 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 97
Definition of simplenews_statistics_handler_unsubscribes.

Class

simplenews_statistics_handler_unsubscribes
Description.

Code

function render($values) {
  $subscribers = $values->simplenews_newsletter_sent_subscriber_count;
  $start = $values->simplenews_statistics_send_start_timestamp;
  $tid = $values->simplenews_newsletter_tid;
  $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($tid, $start, REQUEST_TIME, $source);
  }
  else {

    // Count unsubs between sending of this newsletter and the next.
    $unsubs = simplenews_statistics_count_unsubscribes($tid, $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;
}