:100644 100644 69bb163... 7cc2fdf... M	sites/all/modules/patched/ckeditor/ckeditor.module
:000000 100644 0000000... a6dce85... A	sites/all/modules/patched/ckeditor/includes/ckeditor.features.inc

diff --git a/sites/all/modules/patched/ckeditor/ckeditor.module b/sites/all/modules/patched/ckeditor/ckeditor.module
index 69bb163..7cc2fdf 100644
--- a/sites/all/modules/patched/ckeditor/ckeditor.module
+++ b/sites/all/modules/patched/ckeditor/ckeditor.module
@@ -1126,3 +1126,19 @@ function ckeditor_get_nodetype($get_q) {
 
   return $nodetype;
 }
+
+/**
+ * Implementation of hook_features_api()
+ * 
+ * Allow exporting of CKEditor profiles by the Features module
+ */
+function ckeditor_features_api() {
+  return array(
+    'ckeditor_profile' => array(
+      'name' => t('CKEditor profiles'),
+      'default_hook' => 'ckeditor_profile_defaults',
+      'default_file' => FEATURES_DEFAULTS_INCLUDED,
+      'file' => drupal_get_path('module', 'ckeditor') .'/includes/ckeditor.features.inc',
+    )
+  );
+}
diff --git a/sites/all/modules/patched/ckeditor/includes/ckeditor.features.inc b/sites/all/modules/patched/ckeditor/includes/ckeditor.features.inc
new file mode 100644
index 0000000..a6dce85
--- /dev/null
+++ b/sites/all/modules/patched/ckeditor/includes/ckeditor.features.inc
@@ -0,0 +1,97 @@
+<?php
+
+/**
+ * Implementation of hook_features_export_options()
+ */
+function ckeditor_profile_features_export_options()  {
+  $options = array();
+  foreach (ckeditor_profile_load() as $name => $profile) {
+    $options[$name] = $profile->name;
+  }
+  return $options;
+}
+
+/**
+ * Implementation of hook_features_export()
+ */
+function ckeditor_profile_features_export($data, &$export, $module_name = '') {
+  $pipe = array();
+  foreach ($data as $name) {
+    $profile = ckeditor_profile_load($name);
+    if ($profile) {
+      $export['features']['ckeditor_profile'][$name] = $name;
+
+      // Write dependencies on all the roles referenced by this profile
+      foreach ($profile->rids as $rid => $role_name) {
+        $pipe['user_role'][] = $role_name;
+      }
+    }
+  }
+  $export['dependencies'][] = 'ckeditor';
+  return $pipe;
+}
+
+/**
+ * Implementation of hook_features_export_render()
+ */
+function ckeditor_profile_features_export_render($module_name, $data) {
+  $profiles = array();
+  $roles = user_roles();
+  foreach ($data as $name) {
+    $profile = (array)ckeditor_profile_load($name);
+
+    // Convert Role IDs into role names only
+    if (!empty($profile['rids'])) {
+      $profile['roles'] = array_combine(array_values($profile['rids']), array_values($profile['rids']));
+    }
+    else {
+      $profile['roles'] = array();
+    }
+    unset($profile['rids']);
+
+    foreach ($profile['settings']['rank'] as &$role) {
+      if (isset($roles[$role])) {
+        $role = $roles[$role];
+      }
+    }
+
+    $profiles[$name] = $profile;
+  }
+  $code = '  $data = '.features_var_export($profiles, '  ').';'.PHP_EOL;
+  $code .= '  return $data;';
+
+  return array('ckeditor_profile_defaults' => $code);
+}
+
+/**
+ * Implementation of hook_features_revert()
+ */
+function ckeditor_profile_features_revert($module) {
+  $data = module_invoke($module, 'ckeditor_profile_defaults');
+  $roles = user_roles();
+  foreach ($data as $name => $profile) {
+    // Convert role names back into rids
+    foreach ($profile['settings']['rank'] as &$role) {
+      if ($rid = array_search($role, $user_roles)) {
+        $role = $rid;
+      }
+    }
+
+    // Restore the profile settings
+    db_query("DELETE FROM {ckeditor_settings} WHERE name = '%s'", $name);
+    db_query("INSERT INTO {ckeditor_settings} (name, settings) VALUES('%s', '%s')", $name, serialize($profile['settings']));
+
+    // Restore the profile roles
+    foreach ($roles as $rid => $role_name) {
+      if (in_array($role_name, $profile['roles'])) {
+        if (!db_result(db_query("SELECT rid FROM {ckeditor_role} WHERE rid = %d AND name = '%s'", $rid, $name))) {
+          db_query("INSERT INTO {ckeditor_role} (rid, name) VALUES(%d, '%s')", $rid, $name);
+        }
+      }
+      else {
+        // Make sure they don't have access
+        db_query("DELETE FROM {ckeditor_role} WHERE rid = %d AND name = '%s'", $rid, $name);
+      }
+    }
+  }
+}
