You are here

function _simpleads_campaigns_complete in SimpleAds 7

Helper function. Deactivate ad if the ad riches campaings limits.

Parameters

int $nid:

1 call to _simpleads_campaigns_complete()
_simpleads_campaigns_watch in modules/simpleads_campaigns/campaigns.helper.inc
Helper function. Check campaign criterias and set ads to active/inactive states.

File

modules/simpleads_campaigns/campaigns.helper.inc, line 57
SimpleAds helper functions.

Code

function _simpleads_campaigns_complete($campaign_nid, $ad_node = FALSE) {

  // Check campaign filter for all ads within the given campaign.
  if (!is_object($ad_node)) {
    $campaign = node_load($campaign_nid);
    $query = db_select('node', 'n');
    $query
      ->join('field_data_field_adcamp_list', 'l', 'l.entity_id = n.nid AND l.revision_id = n.vid');
    $query
      ->fields('n', array(
      'nid',
    ));
    $query
      ->condition('n.status', 1)
      ->condition('n.type', 'simpleads')
      ->condition('l.field_adcamp_list_value', $campaign->nid)
      ->orderBy('n.title');
    $ads = $query
      ->execute();
  }
  else {
    $node = $ad_node;
    $ads = array(
      $node,
    );
    if (isset($node->field_adcamp_list[$node->language]) && is_numeric($node->field_adcamp_list[$node->language][0]['safe_value'])) {
      $campaign = node_load($node->field_adcamp_list[$node->language][0]['safe_value']);
    }
  }
  $by_impressions = FALSE;
  $impressions_number = "";
  $by_clicks = FALSE;
  $clicks_number = "";
  $by_days = FALSE;
  $days_number = "";
  $campaign_start_time = "";
  if (isset($campaign->field_adcamp_start[$campaign->language]) && !empty($campaign->field_adcamp_start[$campaign->language][0]['value'])) {
    $campaign_start_time = $campaign->field_adcamp_start[$campaign->language][0]['value'];
  }
  $campaign_start_time = strtotime($campaign_start_time);
  if (isset($campaign->field_adcamp_impression[$campaign->language]) && $campaign->field_adcamp_impression[$campaign->language][0]['value'] == 1) {
    $by_impressions = TRUE;
    if (isset($campaign->field_adcamp_impressions[$campaign->language]) && !empty($campaign->field_adcamp_impressions[$campaign->language][0]['safe_value'])) {
      $impressions_number = $campaign->field_adcamp_impressions[$campaign->language][0]['safe_value'];
    }
  }
  if (isset($campaign->field_adcamp_click[$campaign->language]) && $campaign->field_adcamp_click[$campaign->language][0]['value'] == 1) {
    $by_clicks = TRUE;
    if (isset($campaign->field_adcamp_clicks[$campaign->language]) && !empty($campaign->field_adcamp_clicks[$campaign->language][0]['safe_value'])) {
      $clicks_number = $campaign->field_adcamp_clicks[$campaign->language][0]['safe_value'];
    }
  }
  if (isset($campaign->field_adcamp_day[$campaign->language]) && $campaign->field_adcamp_day[$campaign->language][0]['value'] == 1) {
    $by_days = TRUE;
    if (isset($campaign->field_adcamp_days[$campaign->language]) && !empty($campaign->field_adcamp_days[$campaign->language][0]['safe_value'])) {
      $days_number = $campaign->field_adcamp_days[$campaign->language][0]['safe_value'];
    }
  }
  foreach ($ads as $ad) {
    if (!is_object($ad_node)) {
      $node = node_load($ad->nid);
    }
    $all_clicks = _simpleads_campaigns_count_ad_clicks($campaign_start_time, $ad->nid);
    $all_impressions = _simpleads_campaigns_count_ad_impressions($campaign_start_time, $ad->nid);
    $campaign_end_time = REQUEST_TIME - 24 * 3600 * $days_number;
    $days_left = _simpleads_campains_date_diff($campaign_end_time, $campaign_start_time);
    if ($by_impressions && $by_clicks && $by_days) {
      if ($all_impressions >= $impressions_number || $all_clicks >= $clicks_number || $days_left <= 0) {
        module_invoke_all('simpleads_campaign_complete', $node);
      }
    }
    elseif ($by_impressions && $by_clicks && !$by_days) {
      if ($all_impressions >= $impressions_number || $all_clicks >= $clicks_number) {
        module_invoke_all('simpleads_campaign_complete', $node);
      }
    }
    elseif (!$by_impressions && $by_clicks && !$by_days) {
      if ($all_clicks >= $clicks_number) {
        module_invoke_all('simpleads_campaign_complete', $node);
      }
    }
    elseif (!$by_impressions && !$by_clicks && !$by_days) {
      if ($days_left <= 0) {
        module_invoke_all('simpleads_campaign_complete', $node);
      }
    }
    elseif ($by_impressions && !$by_clicks && !$by_days) {
      if ($all_impressions >= $impressions_number) {
        module_invoke_all('simpleads_campaign_complete', $node);
      }
    }
  }
}