From 25e79cb73090d246a4918b4e9861cfd35f5b4c9d Mon Sep 17 00:00:00 2001
From: Guillaume Viguier <guillaume@viguierjust.com>
Date: Fri, 3 Feb 2012 14:19:42 +0100
Subject: [PATCH] Issue #1424534: features integration

---
 custom_pub.admin.inc    |   12 +++--
 custom_pub.features.inc |  128 +++++++++++++++++++++++++++++++++++++++++++++++
 custom_pub.module       |   59 ++++++++++++++++++----
 custom_pub.rules.inc    |   39 ++++++++++++++
 4 files changed, 223 insertions(+), 15 deletions(-)
 create mode 100644 custom_pub.features.inc
 create mode 100644 custom_pub.rules.inc

diff --git a/custom_pub.admin.inc b/custom_pub.admin.inc
index c48b98d..9dc73ff 100644
--- a/custom_pub.admin.inc
+++ b/custom_pub.admin.inc
@@ -184,11 +184,13 @@ function custom_pub_edit($form, &$form_state, $type) {
     '#type' => 'submit',
     '#value' => t('Save'),
   );
-  $form['delete'] = array(
-    '#type' => 'submit',
-    '#value' => t('Delete'),
-    '#attributes' => array('id'=>'edit-delete'),
-  );
+  if (!isset($type['default'])) {
+    $form['delete'] = array(
+      '#type' => 'submit',
+      '#value' => t('Delete'),
+      '#attributes' => array('id'=>'edit-delete'),
+    );
+  }
   
   $form['#theme'][] = 'custom_pub_edit_form';
   $form['#validate'][] = 'custom_pub_edit_validate';
diff --git a/custom_pub.features.inc b/custom_pub.features.inc
new file mode 100644
index 0000000..971de99
--- /dev/null
+++ b/custom_pub.features.inc
@@ -0,0 +1,128 @@
+<?php
+
+/**
+ * Implements hook_features_export_options().
+ */
+function custom_pub_features_export_options() {
+  return custom_pub_types_list();
+}
+
+/**
+ * Implements hook_features_export.
+ */
+function custom_pub_features_export($data, &$export, $module_name = '') {
+  $pipe = array();
+  $map = features_get_default_map('custom_pub');
+  $pub_types = array_keys(custom_pub_types_list());
+
+  foreach ($data as $option) {
+    // Poll node module to determine who provides the option.
+    if (in_array($option, $pub_types)) {
+      // If this option is provided by a different module, add it as a dependency
+      if (isset($map[$option]) && $map[$option] != $module_name) {
+        $export['dependencies'][$map[$option]] = $map[$option];
+      }
+      // Otherwise export the option
+      else {
+        $export['features']['custom_pub'][$option] = $option;
+        $export['dependencies']['custom_pub'] = 'custom_pub';
+      }
+    }
+  }
+
+  return $pipe;
+}
+
+/**
+ * Implements hook_features_export_render().
+ */
+function custom_pub_features_export_render($module, $data, $export = NULL) {
+  $pub_types = variable_get('custom_pub_types', array());
+  $pub_type_names = array_keys($pub_types);
+  $output = array();
+  $output[] = '  $options = array();';
+  foreach ($data as $option_name) {
+    if (in_array($option_name, $pub_type_names)) {
+      $option_node_types = $pub_types[$option_name]['node_types'];
+      $output[] = "  // Exported option: {$option_name}";
+      $output[] = "  \$options['{$option_name}'] = array(";
+      $output[] = "    'type' => '{$option_name}',";
+      $output[] = "    'name' => t('{$pub_types[$option_name]['name']}'),";
+      $output[] = "    'node_types' => array(";
+      foreach ($option_node_types as $node_type => $node_name) {
+        $output[] = "      '{$node_type}' => t('{$node_name}'),";
+      }
+      $output[] = "    ),";
+      $output[] = "  );";
+      $output[] = "";
+    }
+  }
+  $output[] = '  return $options;';
+  $output = implode("\n", $output);
+  return array('custom_pub_defaults' => $output);
+}
+
+/**
+ * Implements hook_features_revert().
+ *
+ * @param $module
+ * name of module to revert content for
+ */
+function custom_pub_features_revert($module = NULL) {
+  if ($default_options = features_get_default('custom_pub', $module)) {
+    $pub_types = variable_get('custom_pub_types', array());
+    foreach ($default_options as $option_name => $option_info) {
+      if (isset($pub_types[$option_name])) {
+        unset($pub_types[$option_name]);
+      }
+    }
+    variable_set('custom_pub_types', $pub_types);
+    custom_pub_types_rebuild();
+  }
+}
+
+/**
+ * Implements hook_features_disable_feature().
+ *
+ * @param $module
+ *   Name of module that has been disabled.
+ */
+function custom_pub_features_disable_feature($module) {
+  if ($default_options = features_get_default('custom_pub', $module)) {
+    $pub_types = variable_get('custom_pub_types', array());
+    foreach ($default_options as $option_name => $option_info) {
+      if (isset($pub_types[$option_name])) {
+        unset($pub_types[$option_name]['default']);
+      }
+    }
+    variable_set('custom_pub_types', $pub_types);
+    // Rebuild schema
+    cache_clear_all('schema', 'cache');
+  }
+}
+
+/**
+ * Implements hook_features_enable_feature().
+ *
+ * @param $module
+ *   Name of module that has been enabled.
+ */
+function custom_pub_features_enable_feature($module) {
+  if ($default_options = features_get_default('custom_pub', $module)) {
+    $spec = array(
+      'type' => 'int',
+      'not null' => TRUE,
+      'default' => 0
+    );
+    // Add fields in database
+    foreach ($default_options as $option_name => $option_info) {
+      if (!db_field_exists('node', $option_name)) {
+        $spec['description'] = "Custom publishing option $option_name";
+        db_add_field('node', $option_name, $spec);
+      }
+    }
+    custom_pub_types_rebuild();
+    // Rebuild schema
+    cache_clear_all('schema', 'cache');
+  }
+}
diff --git a/custom_pub.module b/custom_pub.module
index 28b5318..0a286fe 100644
--- a/custom_pub.module
+++ b/custom_pub.module
@@ -13,13 +13,13 @@ function custom_pub_help($path, $arg) {
 }
 
 /**
- * Implements hook_permissions()
+ * Implements hook_permission()
  * 
  * We use these permissions on the inidvidual elements in the
  * node_form via the #access parameter. So in order to use the elements
  * you must add the permissions to a role.
  */
-function custom_pub_permissions() {
+function custom_pub_permission() {
   $types = variable_get('custom_pub_types', array());
   foreach ($types as $type) {
     $key = 'edit_custom_pub_' . $type['type'];
@@ -237,10 +237,7 @@ function custom_pub_on_action(&$node, &$context) {
  * Allows the user to pick custom publishing options to turn on.
  */
 function custom_pub_on_action_form($context) {
-  $types = variable_get('custom_pub_types', array());//get the current custom publishing types
-  foreach ($types as $type) {
-    $pub_types[$type['type']] = $type['name'];
-  }
+  $pub_types = custom_pub_types_list();
   $form['types'] = array(
     '#title' => t('Custom Publishing Options'),
     '#description' => t('Select the Custom publishing options to toggle on when this action is triggered.'),
@@ -284,10 +281,7 @@ function custom_pub_off_action(&$node, &$context) {
  * Allows the user to pick custom publishing options to turn off.
  */
 function custom_pub_off_action_form($context) {
-  $types = variable_get('custom_pub_types', array());//get the current custom publishing types
-  foreach ($types as $type) {
-    $pub_types[$type['type']] = $type['name'];
-  }
+  $pub_types = custom_pub_types_list();
   $form['types'] = array(
     '#title' => t('Custom Publishing Options'),
     '#description' => t('Select the Custom publishing options to toggle off when this action is triggered.'),
@@ -299,6 +293,14 @@ function custom_pub_off_action_form($context) {
   return $form;
 }
 
+function custom_pub_types_list() {
+  $types = variable_get('custom_pub_types', array());//get the current custom publishing types
+  foreach ($types as $type) {
+    $pub_types[$type['type']] = $type['name'];
+  }
+  return $pub_types;
+}
+
 function custom_pub_off_action_submit($form, &$form_state) {
   return array('types' => $form_state['values']['types']);
 }
@@ -354,3 +356,40 @@ function custom_pub_by_node_type($n_type) {
   }
   return $return_types;
 }
+
+/**
+ * Implements hook_features_api().
+ */
+function custom_pub_features_api() {
+  return array(
+    'custom_pub' => array(
+      'name' => t('Custom Publishing Options'),
+      'feature_source' => TRUE,
+      'default_hook' => 'custom_pub_defaults',
+      'file' => drupal_get_path('module', 'custom_pub') .'/custom_pub.features.inc',
+    ),
+  );
+}
+
+function custom_pub_types($rebuild = FALSE) {
+  $pub_types = variable_get('custom_pub_types', array());
+
+  if ($rebuild) {
+    foreach (module_implements('custom_pub_defaults') as $module) {
+      $options_array = module_invoke($module, 'custom_pub_defaults');
+      foreach ($options_array as $type => $option) {
+        if (!isset($pub_types[$type])) {
+          $pub_types[$type] = $option;
+        }
+        $pub_types[$type]['default'] = TRUE;
+      }
+    }
+    variable_set('custom_pub_types', $pub_types);
+  }
+
+  return $pub_types;
+}
+
+function custom_pub_types_rebuild() {
+  custom_pub_types(TRUE);
+}
diff --git a/custom_pub.rules.inc b/custom_pub.rules.inc
new file mode 100644
index 0000000..4a85c7c
--- /dev/null
+++ b/custom_pub.rules.inc
@@ -0,0 +1,39 @@
+<?php
+
+
+/**
+ * @file
+ * Rules integration with custom publishing options module.
+ *
+ * @addtogroup rules
+ * @{
+ */
+
+/**
+ * Implements hook_rules_condition_info()
+ */
+function custom_pub_rules_condition_info() {
+  $items = array();
+  $items['custom_pub_node_has_option'] = array(
+    'label' => t('Content has custom publishing option'),
+    'parameter' => array(
+      'node' => array(
+        'type' => 'node',
+        'label' => t('Content'),
+      ),
+      'option' => array(
+        'type' => 'text',
+        'label' => t('Custom publishing option'),
+        'options list' => 'custom_pub_types_list',
+      ),
+    ),
+    'group' => t('Node'),
+    'access callback' => 'rules_node_integration_access',
+    'base' => 'custom_pub_rules_condition_node_has_option',
+  );
+  return $items;
+}
+
+function custom_pub_rules_condition_node_has_option($node, $option) {
+  return (bool)$node->$option;
+}
-- 
1.7.5.4

