From a7e825984a4674d25a1d179c2b049bac316d7673 Mon Sep 17 00:00:00 2001
From: Timo Tiuraniemi <timo.tiuraniemi@iki.fi>
Date: Thu, 26 Jul 2012 15:43:20 +0300
Subject: [PATCH] Added default values to the settings page for fields in rules

---
 login_destination.admin.inc |   80 ++++++++++++++++++++++++++++++++++++++----
 login_destination.module    |   39 +++++++++++++++++++--
 2 files changed, 108 insertions(+), 11 deletions(-)

diff --git a/login_destination.admin.inc b/login_destination.admin.inc
index d53d455..38060ce 100755
--- a/login_destination.admin.inc
+++ b/login_destination.admin.inc
@@ -96,7 +96,7 @@ function theme_login_destination_triggers($variables) {
 
 function theme_login_destination_pages($variables) {
   $type = $variables['pages_type'];
-  
+
   if ($type == LOGIN_DESTINATION_REDIRECT_PHP) {
     return nl2br(check_plain($variables['pages']));
   }
@@ -120,7 +120,7 @@ function theme_login_destination_pages($variables) {
       $output .= "~ ";
     }
     $output .= $page . "<br/>";
-  } 
+  }
 
   return $output;
 }
@@ -140,15 +140,24 @@ function theme_login_destination_roles($variables) {
  */
 function login_destination_edit_form($form, &$form_state, array $rule = array()) {
   // default values
+  $default_triggers = array();
+  if (variable_get('login_destination_default_login', FALSE)){
+    $default_triggers['login'] = 'login';
+  }
+  if (variable_get('login_destination_default_logout', FALSE)){
+    $default_triggers['logout'] = 'logout';
+  }
+  $weight = _login_destination_get_new_weight(
+      variable_get('login_destination_default_weight_type', LOGIN_DESTINATION_WEIGHT_ZERO));
   $rule += array(
-    'triggers' => array(),
+    'triggers' => $default_triggers,
     'roles' => array(),
-    'pages_type' => LOGIN_DESTINATION_REDIRECT_NOTLISTED,
-    'pages' => '',
+    'pages_type' => variable_get('login_destination_default_pages_type', LOGIN_DESTINATION_REDIRECT_NOTLISTED),
+    'pages' => variable_get('login_destination_default_pages', ''),
     'destination_type' => LOGIN_DESTINATION_STATIC,
-    'destination' => '<front>',
+    'destination' => variable_get('login_destination_default_destination', '<front>'),
     'id' => NULL,
-    'weight' => 0,
+    'weight' => $weight,
   );
 
   $access = user_access('use PHP for settings');
@@ -252,8 +261,15 @@ function login_destination_edit_form($form, &$form_state, array $rule = array())
     '#description' => 'Redirect only the selected role(s). If you select no roles, all users will be redirected.',
   );
 
+  // Change default delta to one bigger than the original
+  // so that there is always room for 10 more
+  $weight_delta = 10;
+  if ((abs($rule['weight']) +10) > $weight_delta){
+    $weight_delta = abs($rule['weight'])+10;
+  }
   $form['weight'] = array(
     '#type' => 'weight',
+    '#delta' => $weight_delta,
     '#title' => t('Weight'),
     '#default_value' => $rule['weight'],
     '#description' => t('When evaluating login destination rules, those with lighter (smaller) weights get evaluated before rules with heavier (larger) weights.'),
@@ -296,7 +312,7 @@ function login_destination_edit_form_submit($form, &$form_state) {
   }
 
   drupal_set_message(t('Login destination to %destination has been saved.', array('%destination' => $form_state['values']['destination'])));
-  
+
   $form_state['redirect'] = 'admin/config/people/login-destination';
 }
 
@@ -353,5 +369,53 @@ function login_destination_settings() {
     '#description' => t("User will be redirected before given the possibility to change their password."),
   );
 
+  // Default values for new rule
+  $form['settings']['default'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Default rule values'),
+      '#collapsible' => TRUE,
+      '#collapsed' => FALSE,
+  );
+  $form['settings']['default']['login_destination_default_destination'] = array(
+      '#type' => 'textarea',
+      '#default_value' => variable_get('login_destination_default_destination', '<front>'),
+      '#title' => t('Default value for destination text'),
+      '#description' => t("This will be the default value for the destination text when making new rules."),
+  );
+  $form['settings']['default']['login_destination_default_login'] = array(
+      '#type' => 'checkbox',
+      '#default_value' => variable_get('login_destination_default_login', FALSE),
+      '#title' => t('Set "Login, registration, one-time login link" redirect trigger by default'),
+  );
+  $form['settings']['default']['login_destination_default_logout'] = array(
+      '#type' => 'checkbox',
+      '#default_value' => variable_get('login_destination_default_logout', FALSE),
+      '#title' => t('Set "Logout" redirect trigger by default'),
+  );
+  $form['settings']['default']['login_destination_default_pages_type'] = array(
+      '#type' => 'radios',
+      '#default_value' => variable_get('login_destination_default_pages_type', LOGIN_DESTINATION_REDIRECT_NOTLISTED),
+      '#title' => t('Default value for "Redirect from specific pages"'),
+      '#options' => array(
+          LOGIN_DESTINATION_REDIRECT_NOTLISTED => t('All pages except those listed'),
+          LOGIN_DESTINATION_REDIRECT_LISTED => t('Only the listed pages'),
+      ),
+  );
+  $form['settings']['default']['login_destination_default_pages'] = array(
+      '#type' => 'textarea',
+      '#default_value' => variable_get('login_destination_default_pages', ''),
+      '#title' => t('Default value for pages text'),
+  );
+  $form['settings']['default']['login_destination_default_weight_type'] = array(
+      '#type' => 'radios',
+      '#default_value' => variable_get('login_destination_default_weight_type', LOGIN_DESTINATION_WEIGHT_ZERO),
+      '#title' => t('Default value for weight'),
+      '#options' => array(
+          LOGIN_DESTINATION_WEIGHT_ZERO => t('Weight 0'),
+          LOGIN_DESTINATION_WEIGHT_LOWEST => t('Lowest weight, i.e. new rule goes to the top of the list'),
+          LOGIN_DESTINATION_WEIGHT_HIGHEST => t('Highest weight, i.e. new rule goes to the bottom of list'),
+      ),
+  );
+
   return system_settings_form($form);
 }
\ No newline at end of file
diff --git a/login_destination.module b/login_destination.module
index 30ccc8f..87a5a98 100644
--- a/login_destination.module
+++ b/login_destination.module
@@ -14,6 +14,11 @@ define('LOGIN_DESTINATION_REDIRECT_PHP', 2);
 define('LOGIN_DESTINATION_STATIC', 0);
 define('LOGIN_DESTINATION_SNIPPET', 1);
 
+// Default weight constants
+define('LOGIN_DESTINATION_WEIGHT_ZERO', 0);
+define('LOGIN_DESTINATION_WEIGHT_LOWEST', -1);
+define('LOGIN_DESTINATION_WEIGHT_HIGHEST', 1);
+
 /**
  * Implement hook_help().
  */
@@ -102,7 +107,7 @@ function login_destination_load($id) {
   if (empty($result['roles'])) {
     $result['roles'] = array();
   }
-  
+
   return $result;
 }
 
@@ -187,14 +192,14 @@ function login_destination_validate($form, &$form_state) {
       }
       break;
   }
-  
+
   // Fix the current page in case of 403 page.
   if ($form['#form_id'] == 'user_login') {
     if(drupal_get_http_header('Status') == '403 Forbidden') {
       $_GET['current'] = $_GET['destination'];
     }
   }
-    
+
 }
 
 /**
@@ -355,6 +360,34 @@ function login_destination_get_destination($trigger = '', $current = NULL) {
 }
 
 /**
+ * Gets a value for weight that is either the lowest, the highest or zero
+ * depending on given $weight_type.
+ */
+function _login_destination_get_new_weight($weight_type){
+  $weight = 0;
+  if (isset($weight_type) && $weight_type != LOGIN_DESTINATION_WEIGHT_ZERO){
+    $query = db_select('login_destination', 'l');
+    $query->fields('l',array('weight'));
+    if ($weight_type == LOGIN_DESTINATION_WEIGHT_LOWEST){
+      $query->orderBy('weight', 'ASC');
+    }else if ($weight_type == LOGIN_DESTINATION_WEIGHT_HIGHEST){
+      $query->orderBy('weight', 'DESC');
+    }
+    $query->range(0,1);
+    $result = $query->execute();
+    foreach ($query->execute() as $row) {
+      if ($weight_type == LOGIN_DESTINATION_WEIGHT_LOWEST){
+        $weight = $row->weight - 1;
+      }else if ($weight_type == LOGIN_DESTINATION_WEIGHT_HIGHEST){
+        $weight = $row->weight + 1;
+      }
+      break;
+    }
+  }
+  return $weight;
+}
+
+/**
  * Evaluate the code with forms context.
  *
  * This function hides the calling function's scope from eval().
-- 
1.7.3.4

