cvs diff: Diffing .
Index: signup_status_limit.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/signup_status/modules/signup_status_limit/signup_status_limit.install,v
retrieving revision 1.2
diff -u -p -r1.2 signup_status_limit.install
--- signup_status_limit.install	16 Sep 2009 19:12:53 -0000	1.2
+++ signup_status_limit.install	16 Sep 2009 23:35:21 -0000
@@ -64,11 +64,11 @@ function signup_status_limit_schema() {
         'default' => 0,
       ),
       'limit_type' => array(
-        'description' => "The kind of signup limits in effect for this node (can be either 'status' for per-status limits, or 'global')",
+        'description' => "The kind of signup limits in effect for this node (can be either 'status' for per-status limits, 'global' for a global limit across all status values, or 'default' to use the current site-wide defaults)",
         'type' => 'varchar',
         'length' => 32,
         'not null' => TRUE,
-        'default' => 'global',
+        'default' => 'default',
       ),
     ),
     'primary key' => array('nid'),
Index: signup_status_limit.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/signup_status/modules/signup_status_limit/signup_status_limit.module,v
retrieving revision 1.2
diff -u -p -r1.2 signup_status_limit.module
--- signup_status_limit.module	16 Sep 2009 18:55:12 -0000	1.2
+++ signup_status_limit.module	16 Sep 2009 23:35:22 -0000
@@ -67,22 +67,47 @@ function signup_status_limit_nodeapi_del
 
 /**
  * When a node is loaded, add the signup status limit settings.
+ *
+ * @param $node
+ *   Reference to a node object to load signup status limit settings into.
+ * @param $load_all
+ *   Optional boolean to indicate if all status limit settings should be
+ *   loaded, regardless of the currently active limit type. Defaults to FALSE.
  */
-function signup_status_limit_nodeapi_load($node) {
-  $query = db_query("SELECT cid, status_limit FROM {signup_status_limit_node_limit} WHERE nid = %d", $node->nid);
-  while ($limit = db_fetch_object($query)) {
-    $limits[$limit->cid] = $limit->status_limit;
+function signup_status_limit_nodeapi_load(&$node, $load_all = FALSE) {
+  $limit_type = db_result(db_query("SELECT limit_type FROM {signup_status_limit_node_setting} WHERE nid = %d", $node->nid));
+  if (empty($limit_type)) {
+    $limit_type = empty($node->nid) ? 'global' : 'default';
+  }
+  $node->signup_status_limit_type = $limit_type;
+
+  if ($limit_type == 'default') {
+    $default_limit_type = db_result(db_query("SELECT limit_type FROM {signup_status_limit_node_setting} WHERE nid = %d", 0));
+    $target_nid = 0;
   }
-  $query = db_query("SELECT COUNT(*) as total, status FROM signup_log WHERE nid = %d GROUP BY status", $node->nid);
-  while ($total = db_fetch_object($query)) {
-    $totals[$total->status] = $total->total;
+  else {
+    $target_nid = $node->nid;
   }
 
-  foreach (signup_status_codes() as $cid => $code) {
-    $node->signup_status_limit[$cid]['limit'] = isset($limits[$cid]) ? $limits[$cid] : 0;
-    $node->signup_status_limit[$cid]['total'] = isset($totals[$cid]) ? $totals[$cid] : 0;
+  // Only load the per-status limits if: we're asked to load them regardless,
+  // or the currently active limit type is per-status.
+  if ($load_all || $limit_type == 'status' || ($limit_type == 'default' && $default_limit_type == 'status')) {
+    $query = db_query("SELECT cid, status_limit FROM {signup_status_limit_node_limit} WHERE nid = %d", $target_nid);
+    while ($limit = db_fetch_object($query)) {
+      $limits[$limit->cid] = $limit->status_limit;
+    }
+    if (!empty($node->nid)) {
+      $query = db_query("SELECT COUNT(*) as total, status FROM signup_log WHERE nid = %d GROUP BY status", $node->nid);
+      while ($total = db_fetch_object($query)) {
+        $totals[$total->status] = $total->total;
+      }
+    }
+    foreach (signup_status_codes() as $cid => $code) {
+      $node->signup_status_limit[$cid]['limit'] = isset($limits[$cid]) ? $limits[$cid] : 0;
+      if (!empty($node->nid)) {
+        $node->signup_status_limit[$cid]['total'] = isset($totals[$cid]) ? $totals[$cid] : 0;
+      }
+    }
   }
-  $limit_type = db_result(db_query("SELECT limit_type FROM {signup_status_limit_node_setting} WHERE nid = %d", $node->nid));
-  $node->signup_status_limit_type = !empty($limit_type) ? $limit_type : 'global';
 }
 
Index: signup_status_limit.settings.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/signup_status/modules/signup_status_limit/signup_status_limit.settings.inc,v
retrieving revision 1.2
diff -u -p -r1.2 signup_status_limit.settings.inc
--- signup_status_limit.settings.inc	16 Sep 2009 18:55:12 -0000	1.2
+++ signup_status_limit.settings.inc	16 Sep 2009 23:35:22 -0000
@@ -18,6 +18,10 @@ function signup_status_limit_alter_signu
   $form['#validate'][] = 'signup_status_limit_node_settings_validate';
   $form['#submit'][] = 'signup_status_limit_node_settings_submit';
 
+  // Make sure $form['#node'] contains all the status limit settings, even if
+  // the node isn't currently configured to use them.
+  signup_status_limit_nodeapi_load($form['#node'], TRUE);
+
   // Call our shared helper to actually alter the form elements. In this case,
   // everything lives in the root of the $form array, so we can pass the whole
   // thing.
@@ -36,8 +40,8 @@ function signup_status_limit_alter_signu
   $query = db_query("SELECT nid, close_signup_limit as signup_close_signup_limit FROM {signup} WHERE nid = %d", 0);
   $node = db_fetch_object($query);
   // Add the signup_status_limit properties to this pseudo-node object using
-  // our nodeapi 'load' function since it's exactly what we want.
-  signup_status_limit_nodeapi_load($node);
+  // our nodeapi 'load' function.
+  signup_status_limit_nodeapi_load($node, TRUE);
   $form['#node'] = $node;
   
   // Call our shared helper to actually alter the form elements. In this case,
@@ -74,19 +78,27 @@ function signup_status_limit_alter_signu
   // Figure out the current defaults.
   $limit_type = db_result(db_query("SELECT limit_type FROM {signup_status_limit_node_setting} WHERE nid = %d", $node->nid));
   if (empty($limit_type)) {
-    $limit_type = 'global';
+    $limit_type = 'default';
   }
   $global_limit = $node->signup_close_signup_limit;
 
+  $options = array(
+    'global' => t('Limit on total signups'),
+    'status' => t('Separate limits for each signup status'),
+  );
+  if (!empty($node->nid)) {
+    $options['default'] = t('Use current site-wide default limit settings');
+    $description = t('Signups can either be limited by the total number of signups, there can be separate limits for each signup status, or this node can be configured to use whatever site-wide limit settings are currently active. Depending on which of these options is selected, one of the following sets of text boxes will be used to define the actual limits.');
+  }
+  else {
+    $description = t('Signups can either be limited by the total number of signups, or there can be separate limits for each signup status. Depending on which of these options is selected, one of the following sets of text boxes will be used to define the actual limits.');
+  }
   $form['signup_status_limit_type'] = array(
     '#title' => t('Signup limit type'),
     '#type' => 'radios',
     '#default_value' => $limit_type,
-    '#options' => array(
-      'global' => t('Limit on total signups'),
-      'status' => t('Separate limits for each signup status'),
-    ),
-    '#description' => '<div class="js-hide">' . t('Signups can either be limited by the total number of signups, or there can be separate limits for each signup status. Depending on which of these options is selected, one of the following sets of text boxes will be used to define the actual limits.') . '</div>',
+    '#options' => $options,
+    '#description' => '<div class="js-hide">' . $description . '</div>',
     '#prefix' => '<div class="signup-limit-radios">',
     '#suffix' => '</div>',
     '#weight' => 5,
@@ -115,7 +127,8 @@ function signup_status_limit_alter_signu
     '#prefix' => '<div class="' . $classes . '">',
     '#suffix' => '<div class="description">' . t('For each !signup_status defined on your site, you can control how many signups are allowed at that status. If set to 0, there is no limit for that status.', array('!signup_status' => l(t('Signup status'), 'admin/settings/signup_status'))) . '</div></div>',
   );
-  foreach (signup_status_codes() as $cid => $code) {
+  $signup_status_codes = signup_status_codes();
+  foreach ($signup_status_codes as $cid => $code) {
     $form['signup_status_limit_node_limits'][$cid]['name'] = array(
       '#type' => 'markup',
       '#value' => check_plain($code['name']),
@@ -133,6 +146,54 @@ function signup_status_limit_alter_signu
       );
     }
   }
+
+  if (!empty($node->nid)) {
+    // On the per-node settings page, display the current site-wide default
+    // with help text that links to the site-wide settings page (for users
+    // with sufficient permissions to visit it).
+    $classes = 'signup_status_limit_default_limits';
+    if ($limit_type != 'default') {
+      $classes .= ' js-hide';
+    }
+    $defaults = db_fetch_object(db_query("SELECT nid, close_signup_limit as signup_close_signup_limit FROM {signup} WHERE nid = %d", 0));
+    signup_status_limit_nodeapi_load($defaults);
+    if (user_access('administer all signups')) {
+      $description = t('This is the currently active site-wide default setting for signup limits. To modify the site-wide default, visit the <a href="@signup_settings_url">Signup settings page</a>. To override this configuration only on this %node_type, choose a different value for the "Signup limit type" above.', array('@signup_settings_url' => url('admin/settings/signup'), '%node_type' => node_get_types('name', $node->type)));
+    }
+    else {
+      $description = t('This is the currently active site-wide default setting for signup limits. To override this configuration only on this %node_type, choose a different value for the "Signup limit type" above.', array('%node_type' => node_get_types('name', $node->type)));
+    }
+    if ($defaults->signup_status_limit_type == 'status') {
+      $header = array(
+        t('Signup status'),
+        t('Limit'),
+      );
+      foreach ($signup_status_codes as $cid => $code) {
+        $rows[] = array(
+          check_plain($code['name']),
+          check_plain($defaults->signup_status_limit[$cid]['limit']),
+        );
+      }
+      $form['signup_status_limit_default_limits'] = array(
+        '#type' => 'item',
+        '#value' => theme('table', $header, $rows),
+      );
+    }
+    else {
+      $form['signup_status_limit_default_limits'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Site-wide default: Limit on total signups'),
+        '#size' => 4,
+        '#disabled' => TRUE,
+        '#default_value' => $defaults->signup_close_signup_limit,
+      );
+    }
+    $form['signup_status_limit_default_limits']['#weight'] = 8;
+    $form['signup_status_limit_default_limits']['#description'] = $description;
+    $form['signup_status_limit_default_limits']['#prefix'] = '<div class="' . $classes . '">';
+    $form['signup_status_limit_default_limits']['#suffix'] = '</div>';
+  }
+
 }
 
 /**
@@ -158,19 +219,14 @@ function signup_status_limit_node_settin
   db_query("DELETE FROM {signup_status_limit_node_limit} WHERE nid = %d", $form['#node']->nid);
 
   if ($op == t('Reset to defaults')) {
-    // Ignore what's in the form, and save 'global' as the default.
     if (empty($form['#node']->nid)) {
-      // Reset the global settings.
+      // Site-wide settings page
       $limit_type = 'global';
-      // There are no default per-status limits, so leave $limits empty.
+      $global_limit_reset = 0;
     }
     else {
-      // Reset a node's settings to the current site-wide defaults.
-      $limit_type = db_result(db_query("SELECT limit_type FROM {signup_status_limit_node_setting} WHERE nid = %d", 0));
-      $query = db_query("SELECT cid, status_limit FROM {signup_status_limit_node_limit} WHERE nid = %d", 0);
-      while ($limit = db_fetch_object($query)) {
-        $limits[$limit->cid] = $limit->status_limit;
-      }
+      // Per-node settings page
+      $limit_type = 'default';
     }
   }
   else {
@@ -179,22 +235,36 @@ function signup_status_limit_node_settin
 
     // If the limit type is anything other than 'global', clear out the
     // current global limit.
-    if ($limit_type != 'global') {
-      db_query("UPDATE {signup} SET close_signup_limit = %d WHERE nid = %d", 0, $form['#node']->nid);
+    if ($limit_type == 'status') {
+      $global_limit = 0;
+    }
+    elseif ($limit_type == 'default') {
+      $global_limit = db_result(db_query("SELECT close_signup_limit FROM {signup} WHERE nid = %d", 0));
+    }
+    if (isset($global_limit)) {
+      db_query("UPDATE {signup} SET close_signup_limit = %d WHERE nid = %d", $global_limit, $form['#node']->nid);
     }
 
-    // Save all the new per-status limits defined in the form.
-    foreach ($form_state['values']['signup_status_limit_node_limits'] as $cid => $value) {
-      $limits[$cid] = $value['limit'];
+    if (empty($form['#node']->nid)) {
+      if (!isset($global_limit)) {
+        $global_limit = db_result(db_query("SELECT close_signup_limit FROM {signup} WHERE nid = %d", $form['#node']->nid));
+      }
+      if ($form['#node']->signup_close_signup_limit != $global_limit) {
+        $global_limit_reset = $global_limit;
+      }
     }
-  }
 
-  // Either way, we need to record the appropriate new settings.
-  if (!empty($limits)) {
-    foreach ($limits as $cid => $limit) {
-      db_query("INSERT INTO {signup_status_limit_node_limit} (nid, cid, status_limit) VALUES (%d, %d, %d)", $form['#node']->nid, $cid, $limit);
+    // Save all the new per-status limits defined in the form if the node is
+    // configured to use per-status limits, or we're saving the site-wide
+    // settings form.
+    if ($limit_type == 'status' || empty($form['#node']->nid)) {
+      foreach ($form_state['values']['signup_status_limit_node_limits'] as $cid => $value) {
+        db_query("INSERT INTO {signup_status_limit_node_limit} (nid, cid, status_limit) VALUES (%d, %d, %d)", $form['#node']->nid, $cid, $value['limit']);
+      }
     }
   }
+
+  // Either way, we need to record the appropriate new limit type setting.
   db_query("UPDATE {signup_status_limit_node_setting} SET limit_type = '%s' WHERE nid = %d", $limit_type, $form['#node']->nid);
   if (!db_affected_rows()) {
     // There might not be a record in {signup_status_limit_node_setting},
@@ -203,6 +273,12 @@ function signup_status_limit_node_settin
     db_query("INSERT INTO {signup_status_limit_node_setting} (nid, limit_type) VALUES (%d, '%s')", $form['#node']->nid, $limit_type);
   }
 
+  // If we just changed the site-wide global limit, update all the nodes that
+  // are configured to use the default.
+  if (isset($global_limit_reset)) {
+    db_query("UPDATE {signup} SET close_signup_limit = %d WHERE nid IN (SELECT nid FROM {signup_status_limit_node_setting} WHERE limit_type = '%s')", $global_limit_reset, 'default');
+    // TODO: check all the limits -- this probably needs batch API.
+  }
 }
 
 /**
Index: signup_status_limit.settings.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/signup_status/modules/signup_status_limit/signup_status_limit.settings.js,v
retrieving revision 1.1
diff -u -p -r1.1 signup_status_limit.settings.js
--- signup_status_limit.settings.js	16 Sep 2009 18:04:32 -0000	1.1
+++ signup_status_limit.settings.js	16 Sep 2009 23:35:22 -0000
@@ -8,10 +8,17 @@ Drupal.behaviors.signupStatusLimitShowLi
     if (this.value == 'global') {
       $('div.signup_global_limit').show();
       $('div.signup_status_limit_node_limits').hide();
+      $('div.signup_status_limit_default_limits').hide();
     }
     else if (this.value == 'status') {
       $('div.signup_global_limit').hide();
       $('div.signup_status_limit_node_limits').show();
+      $('div.signup_status_limit_default_limits').hide();
+    }
+    else if (this.value == 'default') {
+      $('div.signup_global_limit').hide();
+      $('div.signup_status_limit_node_limits').hide();
+      $('div.signup_status_limit_default_limits').show();
     }
   });
 };
