<?php
// $Id

// Portions Copyright 2006 http://2bits.com

define('AFFILIATE_HOMEPAGE_PROFILE',      'affiliate_homepage_profile');
define('AFFILIATE_BLOCK_TEXT',            'affiliate_block_text');
define('AFFILIATE_STATS_TEXT',            'affiliate_stats_text');
define('AFFILIATE_TOP_USERS_PAGE',        'affiliate_top_users_page');
define('AFFILIATE_TOP_USERS_BLOCK',       'affiliate_top_users_block');
define('AFFILIATE_TOP_USERS_COUNT_PAGE',  'affiliate_top_users_count_page');
define('AFFILIATE_TOP_USERS_COUNT_BLOCK', 'affiliate_top_users_count_block');
define('AFFILIATE_PERIOD_INTERVAL',       'affiliate_period_interval');
define('AFFILIATE_CLICK_IGNORE_INTERVAL', 'affiliate_click_ignore_interval');
define('AFFILIATE_CRON_INTERVAL',         'affiliate_cron_interval');

function affiliates_help($section) {
  switch ($section) {
    case 'admin/help#affiliates':
    case 'admin/modules#description':
      return t('A module that allows affiliate click-throughs to be tracked.');
    case 'admin/settings/affiliates':
      return t('<p>$Revision: 1.1.4.5 $ $Date: 2008/10/23 08:58:03 $</p>A module that allows affiliate click-throughs to be tracked.');
  }
}

function affiliates_perm() {
  return array('administer affiliates', 'view own affiliate stats', 'affiliate click');
}

function affiliates_menu() {
    $items['admin/settings/affiliates'] = array(
      'title' => t('Affiliates'),
      'description' => t('affiliates settings.'),
      'page callback' => 'drupal_get_form',
      'page arguments' => array('affiliates_admin_settings'),
      'access arguments' => array('administer site configuration'),
    );

    $items['affiliates/admin'] = array(
      'title' => 'Affiliates ads',
      'access arguments' => array('administer affiliates'),
      'page callback' => 'affiliates_ad_list',
    );

    $items['affiliates/admin/list'] = array(
      'title' => 'List',
      'access arguments' => array('administer affiliates'),
      'page callback' => 'affiliates_ad_list',
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => 1,
    );

    $items['affiliates/admin/add'] = array(
      'title' => t('Add'),
      'access arguments' => array('administer affiliates'),
      'page callback' => 'affiliates_affiliate',
      'type' => MENU_LOCAL_TASK,
      'weight' => 5,
    );

    $items['affiliates/admin/edit'] = array(
      'title' => 'Edit affiliate ad',
      'access arguments' => array('administer affiliates'),
      'page callback' => 'affiliates_affiliate',
      'type' => MENU_HIDE,
    );

    $items['affiliates/admin/delete'] = array(
      'title' => t('Delete affiliate ad'),
      'access arguments' => array('administer affiliates'),
      'page callback' => 'affiliates_affiliate',
      'type' => MENU_HIDE,
    );

    $items['aff'] = array(
      'page callback' => 'affiliates_click',
      'access arguments' => array('access content'),
      'type' => MENU_HIDE,
    );

    $items['affiliates/top_users'] = array(
      'title' => t('Top Affiliates'),
      'page callback' => 'affiliates_top_users',
      'access arguments' => array('access content'),
      'type' => MENU_HIDE,
    );
    
    $items['affiliates'] = array(
      'title' => t('Affiliates Home'),
      'page callback' => 'affiliates_list',
      'access arguments' => array('access content'),
    );
  return $items;
}

function affiliates_admin_settings() {
  $form['revenue'][AFFILIATE_HOMEPAGE_PROFILE] = array(
    '#type'          => 'select',
    '#title'         => t('Home Page Profile Field'),
    '#default_value' => variable_get(AFFILIATE_HOMEPAGE_PROFILE, 0),
    '#options'       => _get_profile_fields(),
    '#description'   => t('Select the profile field that users put their home page in. This will be displayed in the top users block and page.'),
  );

  $form['settings'][AFFILIATE_TOP_USERS_COUNT_PAGE] = array(
    '#type'          => 'select',
    '#title'         => t('Top Users Count For Page'),
    '#default_value' => variable_get(AFFILIATE_TOP_USERS_COUNT_PAGE, 5),
    '#options'       => array(5 => 5, 10 => 10, 25 => 25, 50 => 50, 100 => 100), 
    '#description'   => t('Number of users to show in top users page.'),
  );

  $form['settings'][AFFILIATE_TOP_USERS_COUNT_BLOCK] = array(
    '#type'          => 'select',
    '#title'         => t('Top Users Count For Block'),
    '#default_value' => variable_get(AFFILIATE_TOP_USERS_COUNT_BLOCK, 5),
    '#options'       => array(5 => 5, 10 => 10, 25 => 25, 50 => 50, 100 => 100), 
    '#description'   => t('Number of users to show in top users block.'),
  );

  $form['settings'][AFFILIATE_PERIOD_INTERVAL] = array(
    '#type'          => 'select',
    '#title'         => t('Period for top users block and page'),
    '#default_value' => variable_get(AFFILIATE_PERIOD_INTERVAL, 259200),
    '#options'       => drupal_map_assoc(
      array(86400, 172800, 259200, 259200, 432000, 604800, 1209600, 2592000), 'format_interval'),
    '#description'   => t('How long will the "period" be as shown in the top users page.'),
  );

  $form['settings'][AFFILIATE_CLICK_IGNORE_INTERVAL] = array(
    '#type'          => 'select',
    '#title'         => t('IP Click Ignore'),
    '#default_value' => variable_get(AFFILIATE_CLICK_IGNORE_INTERVAL, 86400),
    '#options'       => drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200), 'format_interval'),
    '#description'   => t('Ignore clicks from the same IP address for this time interval.'),
  );

  $form['settings'][AFFILIATE_CRON_INTERVAL] = array(
    '#type'          => 'select',
    '#title'         => t('Top User Refresh Interval'),
    '#default_value' => variable_get(AFFILIATE_CRON_INTERVAL, 86400),
    '#options'       => drupal_map_assoc(
      array(300, 600, 1800, 3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800), 'format_interval'),
    '#description'   => t('How often to refresh the top users data. Requires cron to be running at a more frequent rate than this value to really take effect.'),
  );

  $form['settings'][AFFILIATE_STATS_TEXT] = array(
    '#type'          => 'textarea',
    '#title'         => t("User Text"),
    '#default_value' => variable_get(AFFILIATE_STATS_TEXT, 'Copy and paste the source below to link to us and get affiliate credit.'),
    '#cols'          => 70,
    '#rows'          => 4,
    '#description'   => t("Text to instruct users to copy/paste to earn affiliate credits."),
  );

  return system_settings_form($form);
}

function affiliates_cron() {
  $last_run = variable_get('cron_last', time());
  $interval = variable_get(AFFILIATE_CRON_INTERVAL, 86400);

  if (time() >= $interval) {
    // Refresh the top users data
    _create_summary_data(variable_get(AFFILIATE_TOP_USERS_COUNT_PAGE, 5));

    // Generate the page 
    _get_top_users_page();

    // Generate the blocks
    _get_top_users_block('total');
    _get_top_users_block('period');
  }
}

function affiliates_affiliate() {
  $affiliate = array();
  $op = arg(2);
  $id = (int)arg(3);

  switch($op) {
    case 'add':
      $output = drupal_get_form('affiliate_form_add_edit');
      break;

    case 'edit':
      $affiliate = _get_affiliate($id);
      $output = drupal_get_form('affiliate_form_add_edit', $affiliate);
      break;

    case 'delete':
      $output = drupal_get_form('affiliate_form_delete', $id);
      break;
  }

  return $output;
}

function affiliate_form_delete($id) {
  $form['affiliate']['message'] = array(
    '#type'  => 'markup',
    '#value' => t('Are you sure you?'),
  );
  $form['affiliate']['delete'] = array(
    '#type'  => 'submit',
    '#value' => t('Delete'),
  );
  return $form;
}

function affiliate_form_add_edit(&$form_state, $affiliate = array()) {

  $form['affiliate'] = array(
    '#type'  => 'fieldset',
    '#title' => t('Affiliate Ad'),
  );

  $form['affiliate']['label'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Label'),
    '#default_value' => $affiliate->label,
    '#size'          => 30,
    '#maxlength'     => 30,
  );

  $form['affiliate']['points'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Points'),
    '#default_value' => $affiliate->points,
    '#size'          => 3,
    '#maxlength'     => 3,
  );

  $form['affiliate']['type'] = array(
    '#type'          => 'select',
    '#title'         => t('Type'),
    '#default_value' => $affiliate->type,
    '#options'       => array(
      'image' => 'Image',
      'text'  => 'Text',
      ),
  );
     
  $form['affiliate']['cat_id'] = array(
    '#type'          => 'select',
    '#title'         => t('Category'),
    '#default_value' => $affiliate->cat_id,
    '#options'       => _get_categories(),
  );

  $form['affiliate']['anchor'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Link text'),
    '#default_value' => $affiliate->anchor,
    '#size'          => 40,
    '#maxlength'     => 120,
  );

  $form['affiliate']['redirect'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Redirect to URL'),
    '#default_value' => $affiliate->redirect,
    '#size'          => 40,
    '#maxlength'     => 80,
  );

  $form['affiliate']['order_by'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Order'),
    '#default_value' => $affiliate->order_by,
    '#size'          => 10,
    '#maxlength'     => 5,
  );

  $form['affiliate']['status'] = array(
    '#type'          => 'select',
    '#title'         => t('Status'),
    '#default_value' => $affiliate->status,
    '#options'       =>  _get_status_desc(),
  );

  $form['affiliate']['submit'] = array(
    '#type'  => 'submit',
    '#value' => t('Save'),
  );

  return $form;

}

function affiliate_form_add_edit_validate($form, &$form_state) {
  $form_values = $form_state['values'];
  $op = arg(2);
  if ($op == 'delete') {
    // no need to validate anything
    return;
  }

  if (!$form_values['label']) {
    form_set_error('', t('You must enter a label'));
  }
  if (!is_numeric($form_values['points'])) {
    form_set_error('', t('You must enter a numeric value for points'));
  }
  if (!$form_values['anchor']) {
    form_set_error('', t('You must enter link text'));
  }
  if (!$form_values['type']) {
    form_set_error('', t('You must select a type'));
  }
  if (!$form_values['cat_id']) {
    form_set_error('', t('You must select a category'));
  }
  if (!is_numeric($form_values['order_by'])) {
    form_set_error('', t('You must enter a numeric order value'));
  }
  if (!$form_values['redirect']) {
    form_set_error('', t('You must enter a redirect url'));
  }
  if (!$form_values['status']) {
    form_set_error('', t('You must select a valid status'));
  }
}

function affiliate_form_add_edit_submit($form, &$form_state) {
  $form_values = $form_state['values'];
  $op = arg(2);
  $id = arg(3);

  switch($op) {
    case 'add':
      db_query("INSERT INTO {affiliates_ads}
        (label, points, anchor, type, cat_id, order_by, redirect, status)
        VALUES
        ('%s', %d, '%s', '%s', %d, %d, '%s', '%s')",
        $form_values['label'],
        $form_values['points'],
        $form_values['anchor'],
        $form_values['type'],
        $form_values['cat_id'],
        $form_values['order_by'],
        $form_values['redirect'],
        'a');
      drupal_set_message('Ad has been added.');
      break;

    case 'edit':
      $ad_id = (int)arg(3);
      db_query("UPDATE {affiliates_ads} SET
        label    = '%s',
        points   = %d,
        anchor   = '%s',
        type     = '%s',
        cat_id   = %d,
        order_by = %d,
        redirect = '%s',
        status   = '%s'
        WHERE ad_id = %d",
        $form_values['label'],
        $form_values['points'],
        $form_values['anchor'],
        $form_values['type'],
        $form_values['cat_id'],
        $form_values['order_by'],
        $form_values['redirect'],
        $form_values['status'],
        $ad_id);
      drupal_set_message('Ad has been saved.');
      break;
  }
  drupal_goto('affiliates/admin');
}

function affiliate_form_delete_submit($form, &$form_state) {
  $form_values = $form_state['values'];
  $op = arg(2);
  $id = arg(3);

  db_query("DELETE FROM {affiliates_ads} WHERE ad_id = %d", $id);
  db_query("DELETE FROM {affiliates}     WHERE ad_id = %d", $id);
  drupal_set_message('Ad has been deleted.');
  drupal_goto('affiliates/admin');
}

function affiliates_ad_list() {
  $header = array(
    array('data' => t('ID'),       'field' => 'ad_id'),
    array('data' => t('Label'),    'field' => 'label'),
    array('data' => t('Points'),   'field' => 'points', 'sort' => 'desc'),
    array('data' => t('Type'),     'field' => 'type'),
    array('data' => t('Category'), 'field' => 'cat_name'),
    array('data' => t('Status'),   'field' => 'status'),
    array('data' => t('Operations')),
  );

  $status = _get_status_desc();
  $sql = "SELECT aa.*, ac.cat_name
    FROM {affiliates_ads} aa, {affiliates_cats} ac
    WHERE aa.cat_id = ac.cat_id" . tablesort_sql($header);

  $result = pager_query($sql, 50);
  while ($data = db_fetch_object($result)) {
    $rows[] = array(
            array('data' => $data->ad_id),
            array('data' => $data->label),
            array('data' => $data->points),
            array('data' => $data->type),
            array('data' => $data->cat_name),
            array('data' => $status[$data->status]),
            array('data' => 
        l('edit',   'affiliates/admin/edit/'   . $data->ad_id) . " " .
        l('delete', 'affiliates/admin/delete/' . $data->ad_id)
        ),
    );
  }
  if (!$rows) {
    $rows[] = array(array('data' => t('No data.'), 'colspan' => '4'));
  }
  $pager = theme('pager', NULL, 50, 0);
  if (!empty($pager)) {
    $rows[] = array(array('data' => $pager, 'colspan' => '4'));
  }
  print theme('page', theme('table', $header, $rows), t('Affiliate Ads'));
}

function _get_top_users_page() {

  $limit = variable_get(AFFILIATE_TOP_USERS_COUNT_PAGE, 5); 
  $homepage_profile = variable_get(AFFILIATE_HOMEPAGE_PROFILE, ''); 

  $result = db_query("SELECT s.user_id, u.name, s.total_points, s.period_points, pv.value AS homepage
    FROM {affiliates_summary} s INNER JOIN {users} u ON u.uid = s.user_id
    LEFT JOIN {profile_values} pv ON (u.uid = pv.uid AND pv.fid = %d)
    GROUP BY s.user_id
    ORDER BY total_points DESC LIMIT %d", $homepage_profile, $limit);

  $rank = 1;
  while ($data = db_fetch_object($result)) {
    $name = ($data->homepage) ? l($data->name, $data->homepage) : $data->name;
    $rows[] = array(
      array('data' => $rank++),
      array('data' => $name),
      array('data' => $data->total_points),
      array('data' => $data->period_points),
      array('data' => l('view profile', 'user/' . $data->user_id)),
    );
  }

  if (!$rows) {
    $rows[] = array(array('data' => t('No clicks yet'), 'colspan' => '2'));
  }

  $header = array('Rank', 'User', 'Total', 'Period', 'Profile');

  $output = theme('table', $header, $rows);

  cache_set(AFFILIATE_TOP_USERS_PAGE, $output);
}

function _get_top_users_block($type = 'total') {
  $field = 'total_points';
  if ($type  == 'period') {
    $field = 'period_points';
  }

  $limit = variable_get(AFFILIATE_TOP_USERS_COUNT_BLOCK, 5);
  $homepage_profile = variable_get(AFFILIATE_HOMEPAGE_PROFILE, '');

  $result = db_query("SELECT s.user_id, u.name, s." . $field . " AS points, pv.value AS homepage
    FROM {affiliates_summary} s INNER JOIN {users} u ON u.uid = s.user_id
    LEFT JOIN {profile_values} pv ON (u.uid = pv.uid AND pv.fid = %d)
    GROUP BY s.user_id
    ORDER BY points DESC LIMIT %d", $homepage_profile, $limit);

  while ($data = db_fetch_object($result)) {
    $name = ($data->homepage) ? l($data->name, $data->homepage) : $data->name;
    $rows[] = array(
      array('data' => $name),
      array('data' => $data->points),
      array('data' => l('view', 'user/' . $data->user_id)),
    );
  }

  if (!$rows) {
    $rows[] = array(array('data' => t('No clicks yet'), 'colspan' => '3'));
  }

  $output = theme('table', array(), $rows); 
  cache_set(AFFILIATE_TOP_USERS_BLOCK . $type, $output);
}

function _create_summary_data($limit = 5) {

  $period = time() - variable_get(AFFILIATE_PERIOD_INTERVAL, 86400);

  // Drop the table
  db_query("DROP TABLE IF EXISTS {affiliates_summary}");

  // Create it again
  db_query("CREATE TABLE {affiliates_summary} (
    user_id       INT(10),
    total_points  INT(10),
    period_points INT(10),
    PRIMARY KEY (user_id));");

  // Populate it with the users and total clicks
  db_query("INSERT INTO {affiliates_summary}
    SELECT a.user_id, SUM(aa.points) as lp, 0
    FROM {affiliates} a, {affiliates_ads} aa
    WHERE aa.ad_id = a.ad_id AND a.user_id != 0
    GROUP BY a.user_id
    ORDER BY lp DESC
    LIMIT %d", $limit);

  // Some SQL wizardry to update the period
  db_query("UPDATE {affiliates_summary} s,
    (
      SELECT a.user_id, SUM(aa.points) AS lp
      FROM {affiliates} a, {affiliates_ads} aa
      WHERE a.click_time >= %d
      AND a.ad_id = aa.ad_id
      GROUP BY a.user_id
      LIMIT %d
      ) q
    SET s.period_points = lp
    WHERE q.user_id = s.user_id", $period, $limit);
}

function affiliates_user($op, &$edit, &$account, $category = false) {
  global $user;

  switch ($op) {
    case 'view':
      $items = array();

      if ($user->uid != $account->uid) {
        // someone else viewing their points
        $points = db_result(db_query("SELECT SUM(aa.points) AS points
          FROM {affiliates} a, {affiliates_ads} aa
          WHERE aa.ad_id = a.ad_id
          AND a.user_id = %d", $account->uid));
           
        $items[] = array(
          'title' => t('Affiliate points'),
          'value' => $account->name . " has earned " . number_format($points) . " points.",
        );
      }
    return array(t('Affiliate') => $items);
  }
}

function affiliates_list() {
  global $user, $base_url;

  if ($user->uid) {
    $output .= '<br/>' . variable_get(AFFILIATE_STATS_TEXT, 'Copy and paste the source below to link to us and get affiliate credit.');
    $output .= '<br/>' . "You have " . _get_user_points($user->uid) . " points.";
  }

  $cat_name = '';

  $result = db_query("SELECT aa.*, ac.cat_name
    FROM {affiliates_ads} aa INNER JOIN {affiliates_cats} ac
    ON ac.cat_id = aa.cat_id 
    WHERE status = 'A'
    ORDER BY order_by ASC");

  while ($row = db_fetch_object($result)) {
    if ($row->cat_name != $cat_name) {
      // If the cat_name is different, we've entered a new category and should print it.
      $output .= "<h2>" . $row->cat_name . "</h2>";
      $cat_name = $row->cat_name;
    }

    if ($row->type == "image") {
      // If the type specified is an image (rather than a text link), print image so users can preview
      // what they're going to be adding to their site.
      $output .= "\n<img src=\"" . $row->anchor . "\"/><br/>";
    }
    else {
      // Print text code.
      $output .= "anchor: " . $row->anchor . "<br/>";
    }

    // Now add the textarea for copying the code.
    $output .= "<textarea rows=\"7\" cols=\"50\">";
    $output .= "<a href=\"" . $base_url . "/aff/" . $user->uid . "/" . (int)$row->ad_id . "\">";

    if ($row->type == "image") {
      $output .= "\n<img border=\"0\"";
      $output .= " alt=\"" . variable_get('site_name', $base_url);
      $output .= "\" title=\"" . variable_get('site_name', $base_url) . "\" src=\"" . $row->anchor . "\"/>";
    }
    else {
      $output .= $row->anchor;
    }

    $output .= "</a></textarea><br/>\n";
    //Now add the label assigned through the admin interface.
    $output .= $row->label . " : " . $row->points . " points<br/><br/>";   
  }

  print theme('page', $output);
}

function affiliates_top_users() {
  // This is generated in affiliates_cron() because it stands to be a pretty beefy query and won't scale otherwise.
  $obj = cache_get(AFFILIATE_TOP_USERS_PAGE);
  print theme('page', $obj->data);
}

function affiliates_click() {
  $cookie_value = md5(microtime());
  $timeout_flag = true;

  $uid = (int)arg(1);
  $ad_id = (int)arg(2);

  $timeout_flag = _check_timeout_query();
  //If there's no cookie, proceed.
  if (!$_COOKIE['aff']) {
    setcookie('aff', $cookie_value, time() + 86400);
  }

  if (!$timeout_flag) {
    //Only log the hit if it looks like this isn't a cheater.
    db_query("INSERT INTO {affiliates} (
      user_id, cookie_id, ip, referer, ad_id, click_time)
      VALUES
      (%d, '%s', '%s', '%s', %d, %d)",
      $uid, $cookie_value, $_SERVER['REMOTE_ADDR'], filter_xss($_SERVER['HTTP_REFERER']), $ad_id, time());
  }

  $redirect = db_result(db_query("SELECT redirect FROM {affiliates_ads} WHERE ad_id = %d", $ad_id));

  // Record it in the userpoints
  if (module_exists('userpoints')) {
    $points = (int)db_result(db_query("SELECT points FROM {affiliates_ads} WHERE ad_id = %d", $ad_id));
    userpoints_userpointsapi('points', $points, $uid, "affiliates");
  }

  if ($redirect) {
    header("Location: " . $redirect);
  }
  else {
    // Redirect to the home page if there is no redirect
    drupal_goto();
  }
}

function affiliates_userpoints($op, $points = 0, $uid = 0, $event = 0) {
  switch($op) {
    case 'setting':
      $group = 'affiliates';
      $form[$group] = array(
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#title' => t('%Points for nodevote', userpoints_translation()),
      );
    
      $form[$group][AFFILIATES_USERPOINTS] = array(
        '#type' => 'textfield',
        '#title' => t('%Points for affiliate click', userpoints_translation()),
        '#default_value' => variable_get(AFFILIATES_USERPOINTS, 1),
        '#size' => 5,
        '#maxlength' => 5,
      );
      return $form;
  }
}

function affiliates_block($op = 'list', $delta = 0) {
  global $user;

  switch($op) {
    case 'list':
      $block[0]['info'] = t('Top Affiliates');
      $block[1]['info'] = t('Top Affiliates For Period');
      break;

    case 'view':
      switch($delta) {
        case 0:
          $block['subject'] = t('Top Affiliates');

          $output .= '<br/>' . t('Check out these leading affiliates!');
          if ($obj = cache_get(AFFILIATE_TOP_USERS_BLOCK . 'total')) {
            $output .= '<br/>' . $obj->data;
          }
          else {
            $output .= '<br/>' . t('No clicks yet.');
          }
          $output .= "<br/><a href=\"/affiliates/top_users\">more &raquo;</a>";

          if ($user->uid) {
            $output .= "<br/><br/>You have " . _get_user_points($user->uid) . " points.";
          }   

          $block['content'] = $output;
        break;

        case 1:
          $block['subject'] = t('Top Affiliates For Period');

          $output .= '<br/>' . t('Leading affiliates for the last @period', array('@period' => format_interval(variable_get(AFFILIATE_PERIOD_INTERVAL, 259200))));
          if ($obj = cache_get(AFFILIATE_TOP_USERS_BLOCK . 'period')) {
            $output .= '<br/>' . $obj->data;
          }
          else {
            $output .= '<br/>' . t('No clicks yet.');
          }

          $block['content'] = $output;
        break;

      }
      break;
  }

  return $block;
}

function _get_user_points($uid = 0) {
  $points = 0;

  if ($uid) {
    $points = (int)db_result(db_query('SELECT SUM(aa.points) AS points FROM {affiliates} a, {affiliates_ads} aa
    WHERE a.user_id = %d
    AND aa.ad_id = a.ad_id LIMIT 1', $uid));
  }

  return $points;
}

function _check_timeout_query() {
  // Timeout allows one click per IP per day. Hope there aren't too many people going over a proxy.
  return (int)db_result(db_query("SELECT count(*) FROM {affiliates} WHERE ip = '%s' AND %d < (click_time + %d)",
    $_SERVER['REMOTE_ADDR'], time(), variable_get(AFFILIATE_CLICK_IGNORE_INTERVAL, 86400)));
}

function _get_profile_fields() {
  $profile = array(0 => 'None');

  $result = db_query("SELECT fid, title FROM {profile_fields} ORDER BY fid");
  while ($row = db_fetch_object($result)) {
    $profile[$row->fid] = $row->title;
  }

  return $profile;
}

function _get_status_desc() {
  return array(
    'a' => 'Active',
    'i' => 'Inactive',
    'h' => 'Hidden',
    );
}

function _get_categories() {
  $cats = array();

  $result = db_query("SELECT cat_id, cat_name FROM {affiliates_cats} WHERE active = 'Y' ORDER BY cat_name ASC");
  while ($row = db_fetch_object($result)) {
    $cats[$row->cat_id] = $row->cat_name;
  }
  return $cats;
}

function _get_affiliate($ad_id = 0) {
  return db_fetch_object(db_query('SELECT * FROM {affiliates_ads} WHERE ad_id = %d', $ad_id));
}
