From 3b9739e952b0362890a595c9bbc8fda8618e542b Mon Sep 17 00:00:00 2001
From: quickstart <TravisCarden@236758.no-reply.drupal.org>
Date: Thu, 22 Mar 2012 12:22:34 -0500
Subject: [PATCH] Issue #645152: Added ability to enable/disable post settings and individual node links per content type.

---
 fasttoggle.admin.inc |   19 ------------------
 fasttoggle.js        |   16 +++++++++++++++
 fasttoggle.module    |   52 ++++++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 62 insertions(+), 25 deletions(-)
 create mode 100644 fasttoggle.js

diff --git a/fasttoggle.admin.inc b/fasttoggle.admin.inc
index 97b96df..d05f5f0 100644
--- a/fasttoggle.admin.inc
+++ b/fasttoggle.admin.inc
@@ -27,25 +27,6 @@ function fasttoggle_settings_form() {
     $form['fasttoggle_label_style']['#options'][FASTTOGGLE_LABEL_CUSTOM] = t('Custom (configure in your settings.php)');
   }
 
-  $form['nodes'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Posts'),
-    '#description' => t('Select what options for fast toggling of post settings are available.'),
-    '#access' => user_access('administer fasttoggle'),
-  );
-
-  $form['nodes']['fasttoggle_node_settings'] = array(
-    '#type' => 'checkboxes',
-    '#title' => t('Available settings'),
-    '#options' => array(
-      'status' => t('Status <small>(published/unpublished)</small>'),
-      'sticky' => t('Sticky <small>(stays at the top of listings)</small>'),
-      'promote' => t('Promoted <small>(visible on the front page)</small>'),
-      'comment' => t('Topic opened/closed <small>(users are allowed/disallowed to post comments)</small>'),
-    ),
-    '#default_value' => array_keys(array_filter(variable_get('fasttoggle_node_settings', array('status' => TRUE, 'sticky' => TRUE, 'promote' => TRUE, 'comment' => FALSE)))),
-  );
-
   $form['nodes']['help_text'] = array(
     '#value' => t('Configure access restrictions for these settings on the <a href="@url">access control</a> page.', array('@url' => url('admin/user/permissions', array('fragment' => 'module-fasttoggle')))),
     '#prefix' => '<div>',
diff --git a/fasttoggle.js b/fasttoggle.js
new file mode 100644
index 0000000..7cf8645
--- /dev/null
+++ b/fasttoggle.js
@@ -0,0 +1,16 @@
+(function ($) {
+
+  Drupal.behaviors.fasttoggle = {
+    attach: function (context) {
+      $('#edit-fasttoggle-togglable-options input', context).each(function () {
+        // Disable non-togglable "Add to node links" options on page load
+        document.getElementById(this.id.replace('togglable-options', 'add-to-node-links')).disabled = !this.checked;
+        // Update "Add to node links" options when "Togglable options" change
+        $(this).once().click(function () {
+          document.getElementById(this.id.replace('togglable-options', 'add-to-node-links')).disabled = !this.checked;
+        })
+      });
+    }
+  };
+
+})(jQuery);
diff --git a/fasttoggle.module b/fasttoggle.module
index 3453198..b36c46e 100644
--- a/fasttoggle.module
+++ b/fasttoggle.module
@@ -226,7 +226,7 @@ function fasttoggle_get_options($type) {
 /**
  * Implementation of hook_fasttoggle_options().
  */
-function fasttoggle_fasttoggle_options($type, $obj = NULL) {
+function fasttoggle_fasttoggle_options($type, $obj) {
   $return = array();
 
   switch ($type) {
@@ -234,18 +234,18 @@ function fasttoggle_fasttoggle_options($type, $obj = NULL) {
       $allow = node_access('update', $obj);
 
       // Get an array with all enabled fast toggle links
-      $settings = variable_get('fasttoggle_node_settings', array('status' => TRUE, 'sticky' => TRUE, 'promote' => TRUE, 'comment' => FALSE));
+      $settings = variable_get('fasttoggle_togglable_options_' . $obj->type, array());
 
-      if ($settings['status'] && $allow && user_access('moderate posts')) {
+      if (in_array('status', $settings) && $allow && user_access('moderate posts')) {
         $return['status'] = _fasttoggle_get_label('node_status');
       }
-      if ($settings['sticky'] && $allow && user_access('make posts sticky')) {
+      if (in_array('sticky', $settings) && $allow && user_access('make posts sticky')) {
         $return['sticky'] = _fasttoggle_get_label('node_sticky');
       }
-      if ($settings['promote'] && $allow && user_access('promote posts')) {
+      if (in_array('promote', $settings) && $allow && user_access('promote posts')) {
         $return['promote'] = _fasttoggle_get_label('node_promote');
       }
-      if (module_exists('comment') && $settings['comment'] && $allow && user_access('administer comments')) {
+      if (in_array('comment', $settings) && $allow && module_exists('comment') && user_access('administer comments')) {
         $return['comment'] = _fasttoggle_get_label('comment_admin');
       }
       break;
@@ -306,10 +306,50 @@ function fasttoggle_form_alter(&$form, $form_state, $form_id) {
 }
 
 /**
+ * Implements hook_form_FORM_ID_alter().
+ *
+ * Adds Fasttoggle settings to the node type form.
+ */
+function fasttoggle_form_node_type_form_alter(&$form, $form_state) {
+  $type = $form['#node_type']->type;
+  $options = $form['workflow']['node_options']['#options'];
+  unset($options['revision']);
+  $options['comment'] = t('Comment setting');
+  $form['fasttoggle'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Fasttoggle settings'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+    '#group' => 'additional_settings',
+  );
+  $form['fasttoggle']['fasttoggle_togglable_options'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Togglable options'),
+    '#default_value' => variable_get('fasttoggle_togglable_options_' . $type, array()),
+    '#options' => $options,
+    '#description' => t('The node options that can be toggled with Fasttoggle.'),
+  );
+  $form['fasttoggle']['fasttoggle_add_to_node_links'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Add to node links'),
+    '#default_value' => variable_get('fasttoggle_add_to_node_links_' . $type, array()),
+    '#options' => $options,
+    '#description' => t('Which options to add to node links.'),
+    '#attached' => array('js' => array(
+      drupal_get_path('module', 'fasttoggle') . '/fasttoggle.js' => array(
+        'type' => 'file'
+      ),
+    )),
+  );
+}
+
+/**
  * Implementation of hook_node_view_alter().
  */
 function fasttoggle_node_view($node, $view_mode) {
   $options = fasttoggle_get_options('node', $node);
+  $links = variable_get('fasttoggle_add_to_node_links_' . $node->type, array());
+  $options = array_intersect_key($options, array_flip($links));
  
   foreach (array_keys($options) as $key) {
     $node->content['links']['node']['#links']['fasttoggle_' . $key] = fasttoggle($options[$key][intval($node->$key)], 'node/' . $node->nid . '/toggle/' . $key, FASTTOGGLE_FORMAT_LINK_ARRAY, $key . '_' . $node->nid, 'fasttoggle-status-node-' . $node->nid . '-' . $key . '-' . intval($node->$key));
-- 
1.7.4.1

