
@@ -1,257 +1,257 @@
-<?php
-// $Id: affiliate.module,v 1.5 2005/06/05 22:05:41 incidentist Exp $
-
-/**
- * Implementation of hook_help().
- */
-function affiliate_help($section) {
-  switch ($section) {
-    case 'admin/modules#description':
-      return t('Tracks source of visitors.');
-      break;
-    case 'admin/modules/affiliate':
-      return t('Find out which users have registered under which affiliates by clicking on the affiliate name.');
-  	  break;
-  }
-}
-
-function affiliate_perm() {
-  return array('administer affiliates');
-}
-
-/**
- * Implementation of hook_menu().
- */
- // TODO: maycache and some checks on the callbacks
-function affiliate_menu($may_cache) {
-    affiliate_record();
-    $access = user_access('administer affiliates');
-    $items[] = array('path' => 'admin/affiliate', 'title' => t('affiliates'), 'access' => $access,
-      'callback' => 'affiliate_admin', 'weight' => 5, 'type' => MENU_NORMAL_ITEM);
-    $items[] = array('path' => 'admin/affiliate/user', 'title' => t('users by affiliate'), 'access' => $access,
-      'callback' => 'affiliate_admin_user', 'weight' => 5, 'type' => MENU_DEFAULT_LOCAL_TASK);
-    $items[] = array('path' => 'admin/affiliate/form', 'title' => t('create new affiliate'), 'access' => $access,
-      'callback' => 'affiliate_form', 'weight' => 5, 'type' => MENU_LOCAL_TASK);
-    $items[] = array('path' => 'admin/affiliate/delete', 'title' => t('delete affiliate'), 'access' => $access,
-      'callback' => 'affiliate_delete', 'weight' => 5, 'type' => MENU_CALLBACK);
-    $items[] = array('path' => 'affiliate/cron', 'title' => t('affiliate cron'), 'access' => TRUE,
-      'callback' => 'affiliate_cron', 'weight' => 5, 'type' => MENU_CALLBACK);
-
-return $items;
-
-}
-
-/**
- * Records affiliate ID in the user session.
- */
-function affiliate_record() {
-  global $user;
-  $aff = $_REQUEST['aff'];
-  if (is_numeric($aff) && !$user->affiliate) {
-    $sql = 'SELECT * FROM {affiliates} WHERE aid = %d';
-    $result = db_query($sql, $aff);
-    if (db_num_rows($result) ) {
-      $_SESSION['affiliate'] = $aff;
-    }
-  }
-}
-
-/**
- * Implementation of hook_user()
- */
-function affiliate_user($op, $array = NULL, $user) {
-  switch ($op) {
-    case 'insert':
-      user_save($user, array('affiliate' => $_SESSION['affiliate']));
-      break;
-    case 'view':
-      if (user_access('administer affiliates')) {
-        if ($user->affiliate) {
-          $sql = 'SELECT name FROM {affiliates} WHERE aid = %d';
-          $afname = db_result(db_query($sql, $user->affiliate));
-          $output = form_item(t('Affiliate'), l($afname, "admin/affiliate/user/$user->affiliate"));
-          return array('History' => $output);
-        }
-      }
-	break;
-    case 'form':
-      if (user_access('administer affiliates')) {
-        $options = affiliate_get_affiliates();
-        $options = array(0 => t('<none>')) + $options;
-        $output = form_select(t('Affiliate'), 'affiliate', $user->affiliate, $options);
-        return array(array('title' => t('Misc'), 'data' => $output, 'weight' => 0));
-      }
-  }
-}
-
-/**
- * Get affiliate ids and names.
- */
-function affiliate_get_affiliates() {
-  $all = array();
-  $sql = 'SELECT * FROM {affiliates} ORDER BY name';
-  $result = db_query($sql);
-  while ($row = db_fetch_object($result)) {
-    $all[$row->aid] = $row->name;
-  }
-  return $all;
-}
-
-/**
- * Implementation of hook_cron()
- */
-function affiliate_cron() {
-  $last = variable_get('affiliate_cron_last', 0);
-  if (time()-$last > 24*60*60) {
-    $from = variable_get("site_mail", ini_get("sendmail_from"));
-    $headers = "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from";
-
-    $sql = 'SELECT u.name, u.uid, u.affiliate, a.name AS affil_name, a.mail AS affil_mail FROM {users} u INNER JOIN {affiliates} a ON u.affiliate = a.aid WHERE u.created > %d ORDER BY u.created';
-    $result = db_query($sql, $last);
-    // build output and info about each affiliate
-    while ($row = db_fetch_object($result)) {
-      $affil[$row->affiliate]['text'][] = $row->name. ':  '.  url("user/view/$row->uid", NULL, NULL, 1);
-      $affil[$row->affiliate]['name'] = $row->affil_name;
-      $affil[$row->affiliate]['mail'] = $row->affil_mail;
-    }
-
-    // assemble and send each affiliate email
-    if ($affil) {
-      foreach ($affil as $key => $value) { // $i=0; $i< count($affil); $i++
-        $cnt = format_plural(count($affil[$key]['text']), '1 new user', count($affil[$key]['text']). ' new users');
-        $preamble = t('%persons for %afname since %last', array('%afname' => $affil[$key]['name'], '%persons' => $cnt, '%last' => date('F j G:i', $last))). "\n\n";
-        $subj = t('Affiliate report: %persons since %last at %sn', array('%persons' => $cnt, '%sn' => variable_get('site_name', 'Drupal'), '%last' => date('F j G:i', $last)));
-        $txt = implode("\n", $affil[$key]['text']);
-        user_mail($affil[$key]['mail'], $subj, $preamble. $txt, $headers);
-
-        $output_admin .= strtoupper($affil[$key]['name']). " ($cnt)". "\n". $txt. "\n\n\n";
-        $count_admin = $count_admin + count($affil[$key]['text']);
-      }
-      $cnt = format_plural($count_admin, '1 new user', $count_admin. ' new users');
-      $subj = t('Admin Affiliate report: %persons since %last at %sn', array('%persons' => $cnt, '%sn' => variable_get('site_name', 'Drupal'), '%last' => date('F j G:i', $last)));
-      user_mail($from, $subj, $output_admin, $headers);
-
-    }
-    variable_set('affiliate_cron_last', time());
-  }
-}
-
-/**
- * Displays users who have registered from a given affiliate.
- */
-function affiliate_admin_user($aid) {
-  $sql = 'SELECT name FROM {affiliates} WHERE aid = %d';
-  $afname = db_result(db_query($sql, $aid));
-  $header = array(
-      array("data" => t('username'), "field" => 'name'),
-      array("data" => t('status'), "field" => 'status'),
-      array("data" => t('last access'), "field" => 'changed', 'sort' => 'desc'),
-      array("data" => t('operations'))
-    );
-  $sql = 'SELECT uid, u.name, u.status, u.changed FROM {users} u INNER JOIN {affiliates} a ON u.affiliate = a.aid WHERE affiliate = '. check_query($aid);
-  $sql .= tablesort_sql($header);
-  $result = pager_query($sql, 50);
-  $status = array(t("blocked"), t("active"));
-  while ($row = db_fetch_object($result)) {
-    $rows[] = array(
-      array("data" => format_name($row)),
-      array("data" => $status[$row->status]),
-      array("data" => format_date($row->changed, 'small')),
-      array("data" => l(t('edit'), "user/$row->uid/edit")),
-    );
-  }
-  if (!$rows) {
-    $rows[] = array(array("data" => t("No users."), "colspan" => "4"));
-  }
-  $pager = theme("pager", NULL, 50, 0, tablesort_pager());
-  if (!empty($pager)) {
-    $rows[] = array(array("data" => $pager, "colspan" => "4"));
-  }
-  print theme('page', theme("table", $header, $rows), t('Users affiliated with %name', array('%name' => $afname)));
-}
-
-/**
- * Displays affiliates.
- */
-function affiliate_admin() {
-  $header = array(
-    array("data" => t('ID'), "field" => 'aid'),
-    array("data" => t('Name'), "field" => 'name'),
-    array("data" => t('Mail'), "field" => 'mail'),
-    array("data" => t('Created'), "field" => 'created', 'sort' => 'desc'),
-    array("data" => t('Operations'), "colspan" => '2')
-  );
-  $sql = 'SELECT * FROM {affiliates}';
-  $sql .= tablesort_sql($header);
-  $result = pager_query($sql, 50);
-
-  while ($row = db_fetch_object($result)) {
-    $rows[] = array(
-      array("data" => l($row->aid, '', array('title' => t('an affiliate formatted link')), "aff=$row->aid")),
-      array("data" => l($row->name, "admin/affiliate/user/$row->aid", array('title' => t('View users attached to this affiliate')))),
-      array("data" => $row->mail),
-      array("data" => format_date($row->created)),
-      array("data" => l(t('edit'), "admin/affiliate/form/$row->aid")),
-      array("data" => l(t('delete'), "admin/affiliate/delete/$row->aid"))
-    );
-  }
-  if (!$rows) {
-    $rows[] = array(array("data" => t("None available."), "colspan" => "5"));
-  }
-  $pager = theme("pager", NULL, 50, 0, tablesort_pager());
-  if (!empty($pager)) {
-    $rows[] = array(array("data" => $pager, "colspan" => "5"));
-  }
-  $output = theme("table", $header, $rows);
-  print theme('page', $output);
-}
-
-function affiliate_form($aid = NULL) {
-  $edit = $_POST['edit'];
-  if ($aid && $edit) {
-    $sql = 'UPDATE {affiliates} SET name=\''. $edit['name']. '\', mail=\''. $edit['mail']. '\' WHERE aid = %d';
-    db_query($sql, $aid);
-    drupal_set_message(t('Affiliate edited'));
-  }
-  elseif ($edit) {
-    $id = db_next_id('{affiliates}_aid');
-    $sql = "INSERT INTO {affiliates} (aid, name, mail, created) VALUES (%d, '%s', '%s', %d)";
-    db_query($sql, $id, $edit['name'], $edit['mail'], time());
-    drupal_set_message(t('Affiliate added'));
-  }
-
-  if ($aid) {
-    $btn = t('Edit affiliate');
-    $sql = 'SELECT * FROM {affiliates} WHERE aid = %d';
-    $row = db_fetch_object(db_query($sql, $aid));
-    $output .= form_item(t('Affiliate ID'), $aid);
-// Make this link to registration page
-    $output .= form_item(t('Affiliate Link'), l(t('Home'), '', array(), "aff=$aid"));
-  }
-  else {
-    $btn = t('Add affiliate');
-  }
-
-  $output .= form_textfield(t('Name'), 'name', $row->name, NULL, NULL, t(''), NULL, TRUE);
-  $output .= form_textfield(t('Mail'), 'mail', $row->mail, NULL, NULL, t(''), NULL, TRUE);
-  $output .= form_button($btn);
-  print theme('page', form($output), $btn);
-}
-
-function affiliate_delete($aid) {
-  if ($_POST['op'] == t('Delete affiliate')) {
-    $sql = 'DELETE FROM {affiliates} WHERE aid = %d';
-    db_query($sql, $aid);
-    drupal_set_message(t('affiliate deleted'));
-  }
-  else {
-    $sql = 'SELECT name FROM {affiliates} WHERE aid = %d';
-    $name = db_result(db_query($sql, $aid));
-    $output = form_item(t('Confirm'), $name);
-    $output .= form_button(t('Delete affiliate'));
-    $output = form($output);
-  }
-  print theme('page', $output);
-}
-
-?>
+<?php
+// $Id: affiliate.module,v 1.5 2005/06/05 22:05:41 incidentist Exp $
+
+/**
+ * Implementation of hook_help().
+ */
+function affiliate_help($section) {
+  switch ($section) {
+    case 'admin/modules#description':
+      return t('Tracks source of visitors.');
+      break;
+    case 'admin/modules/affiliate':
+      return t('Find out which users have registered under which affiliates by clicking on the affiliate name.');
+  	  break;
+  }
+}
+
+function affiliate_perm() {
+  return array('administer affiliates');
+}
+
+/**
+ * Implementation of hook_menu().
+ */
+ // TODO: maycache and some checks on the callbacks
+function affiliate_menu($may_cache) {
+    affiliate_record();
+    $access = user_access('administer affiliates');
+    $items[] = array('path' => 'admin/affiliate', 'title' => t('affiliates'), 'access' => $access,
+      'callback' => 'affiliate_admin', 'weight' => 5, 'type' => MENU_NORMAL_ITEM);
+    $items[] = array('path' => 'admin/affiliate/user', 'title' => t('users by affiliate'), 'access' => $access,
+      'callback' => 'affiliate_admin_user', 'weight' => 5, 'type' => MENU_DEFAULT_LOCAL_TASK);
+    $items[] = array('path' => 'admin/affiliate/form', 'title' => t('create new affiliate'), 'access' => $access,
+      'callback' => 'affiliate_form', 'weight' => 5, 'type' => MENU_LOCAL_TASK);
+    $items[] = array('path' => 'admin/affiliate/delete', 'title' => t('delete affiliate'), 'access' => $access,
+      'callback' => 'affiliate_delete', 'weight' => 5, 'type' => MENU_CALLBACK);
+    $items[] = array('path' => 'affiliate/cron', 'title' => t('affiliate cron'), 'access' => TRUE,
+      'callback' => 'affiliate_cron', 'weight' => 5, 'type' => MENU_CALLBACK);
+
+return $items;
+
+}
+
+/**
+ * Records affiliate ID in the user session.
+ */
+function affiliate_record() {
+  global $user;
+  $aff = $_REQUEST['aff'];
+  if (is_numeric($aff) && !$user->affiliate) {
+    $sql = 'SELECT * FROM {affiliates} WHERE aid = %d';
+    $result = db_query($sql, $aff);
+    if (db_num_rows($result) ) {
+      $_SESSION['affiliate'] = $aff;
+    }
+  }
+}
+
+/**
+ * Implementation of hook_user()
+ */
+function affiliate_user($op, $array = NULL, $user) {
+  switch ($op) {
+    case 'insert':
+      user_save($user, array('affiliate' => $_SESSION['affiliate']));
+      break;
+    case 'view':
+      if (user_access('administer affiliates')) {
+        if ($user->affiliate) {
+          $sql = 'SELECT name FROM {affiliates} WHERE aid = %d';
+          $afname = db_result(db_query($sql, $user->affiliate));
+          $output = form_item(t('Affiliate'), l($afname, "admin/affiliate/user/$user->affiliate"));
+          return array('History' => $output);
+        }
+      }
+	break;
+    case 'form':
+      if (user_access('administer affiliates')) {
+        $options = affiliate_get_affiliates();
+        $options = array(0 => t('<none>')) + $options;
+        $output = form_select(t('Affiliate'), 'affiliate', $user->affiliate, $options);
+        return array(array('title' => t('Misc'), 'data' => $output, 'weight' => 0));
+      }
+  }
+}
+
+/**
+ * Get affiliate ids and names.
+ */
+function affiliate_get_affiliates() {
+  $all = array();
+  $sql = 'SELECT * FROM {affiliates} ORDER BY name';
+  $result = db_query($sql);
+  while ($row = db_fetch_object($result)) {
+    $all[$row->aid] = $row->name;
+  }
+  return $all;
+}
+
+/**
+ * Implementation of hook_cron()
+ */
+function affiliate_cron() {
+  $last = variable_get('affiliate_cron_last', 0);
+  if (time()-$last > 24*60*60) {
+    $from = variable_get("site_mail", ini_get("sendmail_from"));
+    $headers = "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from";
+
+    $sql = 'SELECT u.name, u.uid, u.affiliate, a.name AS affil_name, a.mail AS affil_mail FROM {users} u INNER JOIN {affiliates} a ON u.affiliate = a.aid WHERE u.created > %d ORDER BY u.created';
+    $result = db_query($sql, $last);
+    // build output and info about each affiliate
+    while ($row = db_fetch_object($result)) {
+      $affil[$row->affiliate]['text'][] = $row->name. ':  '.  url("user/view/$row->uid", NULL, NULL, 1);
+      $affil[$row->affiliate]['name'] = $row->affil_name;
+      $affil[$row->affiliate]['mail'] = $row->affil_mail;
+    }
+
+    // assemble and send each affiliate email
+    if ($affil) {
+      foreach ($affil as $key => $value) { // $i=0; $i< count($affil); $i++
+        $cnt = format_plural(count($affil[$key]['text']), '1 new user', count($affil[$key]['text']). ' new users');
+        $preamble = t('%persons for %afname since %last', array('%afname' => $affil[$key]['name'], '%persons' => $cnt, '%last' => date('F j G:i', $last))). "\n\n";
+        $subj = t('Affiliate report: %persons since %last at %sn', array('%persons' => $cnt, '%sn' => variable_get('site_name', 'Drupal'), '%last' => date('F j G:i', $last)));
+        $txt = implode("\n", $affil[$key]['text']);
+        user_mail($affil[$key]['mail'], $subj, $preamble. $txt, $headers);
+
+        $output_admin .= strtoupper($affil[$key]['name']). " ($cnt)". "\n". $txt. "\n\n\n";
+        $count_admin = $count_admin + count($affil[$key]['text']);
+      }
+      $cnt = format_plural($count_admin, '1 new user', $count_admin. ' new users');
+      $subj = t('Admin Affiliate report: %persons since %last at %sn', array('%persons' => $cnt, '%sn' => variable_get('site_name', 'Drupal'), '%last' => date('F j G:i', $last)));
+      user_mail($from, $subj, $output_admin, $headers);
+
+    }
+    variable_set('affiliate_cron_last', time());
+  }
+}
+
+/**
+ * Displays users who have registered from a given affiliate.
+ */
+function affiliate_admin_user($aid) {
+  $sql = 'SELECT name FROM {affiliates} WHERE aid = %d';
+  $afname = db_result(db_query($sql, $aid));
+  $header = array(
+      array("data" => t('username'), "field" => 'name'),
+      array("data" => t('status'), "field" => 'status'),
+      array("data" => t('last access'), "field" => 'changed', 'sort' => 'desc'),
+      array("data" => t('operations'))
+    );
+  $sql = 'SELECT uid, u.name, u.status, u.changed FROM {users} u INNER JOIN {affiliates} a ON u.affiliate = a.aid WHERE affiliate = aid'; //jo1ene
+  $sql .= tablesort_sql($header);
+  $result = pager_query($sql, 50);
+  $status = array(t("blocked"), t("active"));
+  while ($row = db_fetch_object($result)) {
+    $rows[] = array(
+      array("data" => format_name($row)),
+      array("data" => $status[$row->status]),
+      array("data" => format_date($row->changed, 'small')),
+      array("data" => l(t('edit'), "user/$row->uid/edit")),
+    );
+  }
+  if (!$rows) {
+    $rows[] = array(array("data" => t("No users."), "colspan" => "4"));
+  }
+  $pager = theme("pager", NULL, 50, 0, tablesort_pager());
+  if (!empty($pager)) {
+    $rows[] = array(array("data" => $pager, "colspan" => "4"));
+  }
+  print theme('page', theme("table", $header, $rows), t('Users affiliated with %name', array('%name' => $afname)));
+}
+
+/**
+ * Displays affiliates.
+ */
+function affiliate_admin() {
+  $header = array(
+    array("data" => t('ID'), "field" => 'aid'),
+    array("data" => t('Name'), "field" => 'name'),
+    array("data" => t('Mail'), "field" => 'mail'),
+    array("data" => t('Created'), "field" => 'created', 'sort' => 'desc'),
+    array("data" => t('Operations'), "colspan" => '2')
+  );
+  $sql = 'SELECT * FROM {affiliates}';
+  $sql .= tablesort_sql($header);
+  $result = pager_query($sql, 50);
+
+  while ($row = db_fetch_object($result)) {
+    $rows[] = array(
+      array("data" => l($row->aid, '', array('title' => t('an affiliate formatted link')), "aff=$row->aid")),
+      array("data" => l($row->name, "admin/affiliate/user/$row->aid", array('title' => t('View users attached to this affiliate')))),
+      array("data" => $row->mail),
+      array("data" => format_date($row->created)),
+      array("data" => l(t('edit'), "admin/affiliate/form/$row->aid")),
+      array("data" => l(t('delete'), "admin/affiliate/delete/$row->aid"))
+    );
+  }
+  if (!$rows) {
+    $rows[] = array(array("data" => t("None available."), "colspan" => "5"));
+  }
+  $pager = theme("pager", NULL, 50, 0, tablesort_pager());
+  if (!empty($pager)) {
+    $rows[] = array(array("data" => $pager, "colspan" => "5"));
+  }
+  $output = theme("table", $header, $rows);
+  print theme('page', $output);
+}
+
+function affiliate_form($aid = NULL) {
+  $edit = $_POST['edit'];
+  if ($aid && $edit) {
+    $sql = 'UPDATE {affiliates} SET name=\''. $edit['name']. '\', mail=\''. $edit['mail']. '\' WHERE aid = %d';
+    db_query($sql, $aid);
+    drupal_set_message(t('Affiliate edited'));
+  }
+  elseif ($edit) {
+    $id = db_next_id('{affiliates}_aid');
+    $sql = "INSERT INTO {affiliates} (aid, name, mail, created) VALUES (%d, '%s', '%s', %d)";
+    db_query($sql, $id, $edit['name'], $edit['mail'], time());
+    drupal_set_message(t('Affiliate added'));
+  }
+
+  if ($aid) {
+    $btn = t('Edit affiliate');
+    $sql = 'SELECT * FROM {affiliates} WHERE aid = %d';
+    $row = db_fetch_object(db_query($sql, $aid));
+    $output .= form_item(t('Affiliate ID'), $aid);
+// Make this link to registration page
+    $output .= form_item(t('Affiliate Link'), l(t('Home'), '', array(), "aff=$aid"));
+  }
+  else {
+    $btn = t('Add affiliate');
+  }
+
+  $output .= form_textfield(t('Name'), 'name', $row->name, NULL, NULL, t(''), NULL, TRUE);
+  $output .= form_textfield(t('Mail'), 'mail', $row->mail, NULL, NULL, t(''), NULL, TRUE);
+  $output .= form_button($btn);
+  print theme('page', form($output), $btn);
+}
+
+function affiliate_delete($aid) {
+  if ($_POST['op'] == t('Delete affiliate')) {
+    $sql = 'DELETE FROM {affiliates} WHERE aid = %d';
+    db_query($sql, $aid);
+    drupal_set_message(t('affiliate deleted'));
+  }
+  else {
+    $sql = 'SELECT name FROM {affiliates} WHERE aid = %d';
+    $name = db_result(db_query($sql, $aid));
+    $output = form_item(t('Confirm'), $name);
+    $output .= form_button(t('Delete affiliate'));
+    $output = form($output);
+  }
+  print theme('page', $output);
+}
+
+?>
