From 2cbb4c0324acdff80ea256845e85a31ee9320199 Mon Sep 17 00:00:00 2001
From: netw3rker <netw3rker@drupal.org>
Date: Fri, 31 Jul 2015 07:48:23 -0400
Subject: [PATCH] exportables, with machine name

---
 site_verify.admin.inc |  9 ++++++
 site_verify.install   | 35 ++++++++++++++++++++++-
 site_verify.module    | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 121 insertions(+), 1 deletion(-)
 mode change 100644 => 100755 site_verify.install

diff --git a/site_verify.admin.inc b/site_verify.admin.inc
index 4e843b5..70160af 100644
--- a/site_verify.admin.inc
+++ b/site_verify.admin.inc
@@ -116,6 +116,15 @@ function site_verify_edit_form($form, &$form_state, $record = array(), $engine =
       );
       $form['#engine'] = $record['engine'];
 
+      $form['machinename'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Machine name'),
+        '#default_value' => $record['meta'],
+        '#required' => TRUE,
+        '#description' => t('The exportable machine name of the verification record'),
+        '#maxlength' => NULL,
+      );
+
       $form['meta'] = array(
         '#type' => 'textfield',
         '#title' => t('Verification META tag'),
diff --git a/site_verify.install b/site_verify.install
old mode 100644
new mode 100755
index a1177d5..196a5a9
--- a/site_verify.install
+++ b/site_verify.install
@@ -10,13 +10,32 @@
  */
 function site_verify_schema() {
   $schema['site_verify'] = array(
-    'description' => '',
+    'description' => 'Site Verification',
+    'export' => array(
+      'key' => 'machinename',
+      'key name' => 'Engine',
+      'primary key' => 'svid',
+      'identifier' => 'preset', // Exports will be defined as $preset
+      'default hook' => 'site_verify',  // Function hook name.
+      'api' => array(
+        'owner' => 'site_verify',
+        'api' => 'site_verify',  // Base name for api include files.
+        'minimum_version' => 1,
+        'current_version' => 1,
+      ),
+    ),
     'fields' => array(
       'svid' => array(
         'type' => 'serial',
         'unsigned' => TRUE,
         'not null' => TRUE,
         'description' => 'Primary Key: Unique site verification ID.',
+        'no export' => TRUE,
+      ),
+      'machinename' => array(
+        'type' => 'varchar',
+        'length' => 255,
+        'description' => 'Unique site verification value for exportables.',
       ),
       'engine' => array(
         'type' => 'varchar',
@@ -128,3 +147,17 @@ function site_verify_import_ghs() {
   variable_del('ghs_string_verify');
   module_disable(array('ghs'));
 }
+
+/**
+ * Update to add the machine name field
+ * @param $sandbox
+ */
+function site_verify_update_7001(&$sandbox){
+
+  db_add_field("site_verify", "machinename", array(
+    'type' => 'varchar',
+    'length' => 255,
+    'description' => 'Unique site verification value for exportables.',
+  ));
+  db_query("update {site_verify} set machinename = concat(engine, '_', svid)");
+}
\ No newline at end of file
diff --git a/site_verify.module b/site_verify.module
index 3ae0ead..7403eb8 100755
--- a/site_verify.module
+++ b/site_verify.module
@@ -211,3 +211,81 @@ function site_verify_output($svid) {
     return t('This is a verification page for the @title search engine.', array('!title' => $verification['engine']['name']));
   }
 }
+
+
+/**
+ * Implementation of hook_ctools_plugin_api().
+ *
+ * Tell CTools that we support the default_mymodule_presets API.
+ */
+function site_verify_ctools_plugin_api($owner, $api) {
+  if ($owner == 'site_verify' && $api == 'site_verify') {
+    return array('version' => 1);
+  }
+}
+
+/**
+ * Implementation of hook_ctools_plugin_directory().
+ */
+function site_verify_ctools_plugin_directory($module, $type) {
+  // Load the export_ui plugin.
+  if ($type =='export_ui') {
+    return 'plugins/export_ui';
+  }
+}
+
+/**
+ * This code is shamelessly stolen from Features.
+ *
+ * Modifications were made to have it export data from the database rather than
+ * from the ctools crud system. This allows for features to properly show items
+ * as overridden when there is no corresponding data in the database.
+ *
+ * @param $module
+ * @param $data
+ * @param $export
+ * @return array
+ */
+function site_verify_features_export_render($module, $data, $export){
+  // Reset the export display static to prevent clashes.
+  drupal_static_reset('panels_export_display');
+  $component='site_verify';
+  ctools_include('export');
+  $schema = ctools_export_get_schema($component);
+
+  if (function_exists($schema['export']['to hook code callback'])) {
+    $export = $schema['export']['to hook code callback']($data, $module);
+    $code = explode("{\n", $export);
+    array_shift($code);
+    $code = explode('}', implode($code, "{\n"));
+    array_pop($code);
+    $code = implode('}', $code);
+  }
+  else {
+    $code = '  $export = array();'."\n\n";
+    foreach ($data as $object_name) {
+      if ($object = db_query("select * from {site_verify} where engine=:engine", array(":engine"=>$object_name))->fetchObject()) {
+        $identifier = $schema['export']['identifier'];
+        $code .= _ctools_features_export_crud_export($component, $object, '  ');
+        $code .= "  \$export[" . ctools_var_export($object_name) . "] = \${$identifier};\n\n";
+      }
+    }
+    $code .= '  return $export;';
+  }
+
+  return array($schema['export']['default hook'] => $code);
+}
+
+function site_verify_features_revert($module, $component){
+  if ($objects = features_get_default($component, $module)) {
+    foreach ($objects as $name => $object) {
+      // Some things (like views) do not use the machine name as key
+      // and need to be loaded explicitly in order to be deleted.
+      _ctools_features_export_crud_delete($component, $object);
+      unset($object->svid);
+      $object->export_type = EXPORT_IN_CODE;
+      ctools_export_crud_save('site_verify', $object);
+    }
+  }
+}
+
-- 
2.2.0

