@@ -22,87 +22,140 @@
     case 'admin/settings/affiliates':
       return t('<p>$Revision: 1.1.4.3 $ $Date: 2007/04/14 04:58:18 $</p>A module that allows affiliate click-throughs to be tracked.');
   }
 }
 
 function affiliates_perm() {
-  return array('administer affiliates', 'view own affiliate stats', 'affiliate click');
+  return array('administer affiliates', 'view own affiliate stats', 'affiliate click', 'access content');
 }
 
 
 function affiliates_menu($may_cache) {
   $items = array();
 
   if ($may_cache) {
+      $items[] = array('path' => 'admin/settings/affiliates', 
+      'title' => t('Affiliates Settings'),
+      'callback' => 'affiliates_settings_page',
+      'access' => user_access('administer affiliates'),
+      'description' => t('Allows the user to configure the layout of questions and answers on a FAQ page.'),
+    );
+				
     $items[] = array(
-      'path' => 'admin/settings/affiliates',
-      'title' => t('Affiliates'),
-      'description' => t('affiliates settings.'),
+      'path' => 'admin/settings/affiliates/general',
+      'title' => t('General'),
+      'description' => t('Allows admin to configure affiliates settings'),
       'callback' => 'drupal_get_form',
       'callback arguments' => array('affiliates_admin_settings'),
-      'access' => user_access('administer site configuration'),
+      'access' => user_access('administer affiliates'),
+      'type' => MENU_DEFAULT_LOCAL_TASK,
+						'weight' => 1,
     );
 
     $items[] = array(
-      'path' => 'affiliates/admin',
-      'title' => 'Affiliates ads',
+      'path' => 'admin/settings/affiliates/ads',
+      'title' => 'Affiliate Ads',
+      'callback' => 'affiliates_ad_list',
       'access' => user_access('administer affiliates'),
-      'callback' => 'affiliates_ad_list',
+      'type' => MENU_LOCAL_TASK,
+						'weight' => 5,
     );
 
     $items[] = array(
-      'path' => 'affiliates/admin/list',
+      'path' => 'admin/settings/affiliates/ads/list',
       'title' => 'List',
       'access' => user_access('administer affiliates'),
       'callback' => 'affiliates_ad_list',
       'type' => MENU_DEFAULT_LOCAL_TASK,
       'weight' => 1,
     );
 
     $items[] = array(
-      'path' => 'affiliates/admin/add',
+      'path' => 'admin/settings/affiliates/ads/add',
       'title' => t('Add'),
       'access' => user_access('administer affiliates'),
-      'callback' => 'affiliates_affiliate',
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('affiliate_form_add_edit'),
       'type' => MENU_LOCAL_TASK,
       'weight' => 5,
     );
 
     $items[] = array(
-      'path' => 'affiliates/admin/edit',
+      'path' => 'admin/settings/affiliates/ads/edit',
       'title' => 'Edit affiliate ad',
       'access' => user_access('administer affiliates'),
       'callback' => 'affiliates_affiliate',
       'type' => MENU_HIDE,
     );
-
+				
     $items[] = array(
-      'path' => 'affiliates/admin/delete',
+      'path' => 'admin/settings/affiliates/ads/delete',
       'title' => t('Delete affiliate ad'),
       'access' => user_access('administer affiliates'),
       'callback' => 'affiliates_affiliate',
       'type' => MENU_HIDE,
     );
 
     $items[] = array(
-      'path' => "aff",
-      'callback' => 'affiliates_click',
-      'access' => true,
-      'type' => MENU_HIDE,
+      'path' => 'admin/settings/affiliates/how',
+      'title' => 'How To - Instructions',
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('affiliates_content'),
+      'access' => user_access('administer affiliates'),
+      'type' => MENU_LOCAL_TASK,
+      'weight' => 6,
+    );
+				
+      $items[] = array(
+      'path' => 'affiliates',
+      'callback' => 'affiliates_overview',
+      'access' => user_access('access content'),
+      'type' => MENU_CALLBACK,
     );
 
-    $items[] = array(
+      $items[] = array(
       'path' => 'affiliates/top_users',
       'title' => t('Top Affiliates'),
       'callback' => 'affiliates_top_users',
       'access' => user_access('access content'),
+      'type' => MENU_DEFAULT_LOCAL_TASK,
+      'weight' => 1,
+    );
+				
+    $items[] = array(
+      'path' => 'affiliates/options',
+      'title' => t('Affiliate Options'),
+      'callback' => 'affiliates_list',
+      'access' => user_access('access content'),
+      'type' => MENU_LOCAL_TASK,
+      'weight' => 2,
+    );
+
+    $items[] = array(
+      'path' => 'affiliates/instructions',
+      'title' => t('Instructions'),
+      'callback' => 'affiliate_instructions',
+      'access' => user_access('access content'),
+      'type' => variable_get('show_affiliate_instructions', '') ? MENU_LOCAL_TASK : MENU_HIDE,
+      'weight' => 3,
+    );
+				
+    $items[] = array(
+      'path' => "aff",
+      'callback' => 'affiliates_click',
+      'access' => true,
       'type' => MENU_HIDE,
     );
   }
 
   return $items;
+}
+
+function affiliates_settings_page($op = NULL, $aid = NULL) {
+  $output .= drupal_get_form('affiliates_admin_settings');
+  return $output;
 }
 
 function affiliates_admin_settings() {
   $form['revenue'][AFFILIATE_HOMEPAGE_PROFILE] = array(
     '#type'          => 'select',
     '#title'         => t('Home Page Profile Field'),
@@ -159,12 +212,56 @@
     '#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_overview() {
+	if (variable_get('affiliate_overview', '')) {
+		$output = variable_get('affiliate_overview', '');
+	}
+	return $output;
+}
+
+function affiliate_instructions() {
+	if (variable_get('affiliate_instructions', '')) {
+		$output = variable_get('affiliate_instructions', '');
+	}
+	return $output;
+}
+
+function affiliates_content() {
+  $form['how']['affiliate_overview'] = array(
+    '#type'          => 'textarea',
+    '#title'         => t('Affiliate Overview'),
+    '#default_value' => variable_get('affiliate_overview', ''),
+    '#cols'          => 70,
+    '#rows'          => 4,
+    '#description'   => t('Explain what top affiliates are and a bit about your affiliate program. '),
+  );
+
+  $form['how']['show_affiliate_instructions'] = array(
+    '#type'          => 'radios',
+    '#title'         => t('Affilate Instructions'),
+    '#default_value' => variable_get('show_affiliate_instructions', 0),
+    '#options'       => array(t('No'), t('Yes')),
+    '#description'   => t('Do you want to provide instructions on how to use the affiliate program? If yes, a tab option will be provided at /affiliates url.'),
+		);
+		
+  $form['how']['affiliate_instructions'] = array(
+    '#type'          => 'textarea',
+    '#title'         => t('Affiliate Instructions'),
+    '#default_value' => variable_get('affiliate_instructions', ''),
+    '#cols'          => 70,
+    '#rows'          => 4,
+    '#description'   => t('Provide detailed instructions on how to use your affiliate program. This is a good place to give ideas on where to place links, banners etc and, how to accumulate more affiliate points.'),
+  );
+		
   return system_settings_form($form);
 }
 
 function affiliates_cron() {
   $last_run = variable_get('cron_last', time());
   $interval = variable_get(AFFILIATE_CRON_INTERVAL, 86400);
@@ -372,23 +469,23 @@
         $form_values['redirect'],
         $form_values['status'],
         $ad_id);
       drupal_set_message('Ad has been saved.');
       break;
   }
-  drupal_goto('affiliates/admin');
+  drupal_goto('admin/settings/affiliates/ads');
 }
 
 function affiliate_form_delete_submit($form_id, $form_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');
+  drupal_goto('admin/settings/affiliates/ads');
 }
 
 function affiliates_ad_list() {
   $header = array(
     array('data' => t('ID'),       'field' => 'ad_id'),
     array('data' => t('Label'),    'field' => 'label'),
@@ -411,14 +508,14 @@
             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)
+        l('edit',   'admin/settings/affiliates/ads/edit/'   . $data->ad_id) . " " .
+        l('delete', 'admin/settings/affiliates/ads/delete/' . $data->ad_id)
         ),
     );
   }
   if (!$rows) {
     $rows[] = array(array('data' => t('No data.'), 'colspan' => '4'));
   }
@@ -457,13 +554,13 @@
   }
 
   $header = array('Rank', 'User', 'Total', 'Period', 'Profile');
 
   $output = theme('table', $header, $rows);
 
-  cache_set(AFFILIATE_TOP_USERS_PAGE, $output);
+  cache_set(AFFILIATE_TOP_USERS_PAGE, 'cache', $output);
 }
 
 function _get_top_users_block($type = 'total') {
   $field = 'total_points';
   if ($type  == 'period') {
     $field = 'period_points';
@@ -490,13 +587,13 @@
   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);
+  cache_set(AFFILIATE_TOP_USERS_BLOCK . $type, 'cache', $output);
 }
 
 function _create_summary_data($limit = 5) {
 
   $period = time() - variable_get(AFFILIATE_PERIOD_INTERVAL, 86400);
 
@@ -637,13 +734,13 @@
       $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_exist('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);
