You are here

function simplenews_statistics_delete_clicks in Simplenews Statistics 7

Same name and namespace in other branches
  1. 7.2 simplenews_statistics.module \simplenews_statistics_delete_clicks()

Delete all click records for a newsletter.

2 calls to simplenews_statistics_delete_clicks()
simplenews_statistics_cron in ./simplenews_statistics.module
Implements hook_cron().
simplenews_statistics_node_delete in ./simplenews_statistics.module
Implements hook_node_delete().

File

./simplenews_statistics.module, line 477
Main simplenews statistics file.

Code

function simplenews_statistics_delete_clicks($nid) {

  // Get urlids for newsletter.
  $urlids = array();
  $query = db_select('simplenews_statistics_url', 'ssu')
    ->fields('ssu', array(
    'urlid',
  ))
    ->condition('ssu.nid', $nid);
  $result = $query
    ->execute();
  foreach ($result as $record) {

    // Archive click count.
    $click_query = db_select('simplenews_statistics_click', 'ssc')
      ->condition('ssc.urlid', $record->urlid);
    $clicks = $query
      ->countQuery()
      ->execute()
      ->fetchField();
    db_update('simplenews_statistics_url')
      ->fields(array(
      'click_count' => $clicks,
    ))
      ->condition('urlid', $record->urlid)
      ->execute();

    // Store ID in array.
    $urlids[] = $record->urlid;
  }

  // Execute delete.
  if (!empty($urlids)) {
    $deleted = db_delete('simplenews_statistics_click')
      ->condition('urlid', $urlids)
      ->execute();
  }
  else {
    $deleted = 0;
  }

  // Return count.
  return $deleted;
}