You are here

blockexport.module in Blockexport 7.2

Same filename and directory in other branches
  1. 7 blockexport.module

This features component provide developer to export all blocks into a feature.

File

blockexport.module
View source
<?php

/**
 * @file
 * This features component provide developer to export all blocks into a
 * feature.
 */

/**
 * Implements hook_help().
 */
function blockexport_help($path, $arg) {
  switch ($path) {
    case 'admin/help#blockexport':
      $output = '<h3>' . t('Installation') . '</h3>';
      $output .= '<p>' . t('Quite simple, Download the module and simply put into your your_drupal_path/sites/all/modules and install from your admin panel.') . '</p>';
      $output .= '<h3>' . t('Configuration and Features') . '</h3>';
      $output .= '<p>' . t('After successfull installation you able to export your all local box from Admin >> Structure >> Features >> Create Features') . '</p>';
      $output .= '<p>' . t('Enter your features name, description, package(optional), version(optional), URL(optional) and then choose "Block: blockexport_settings" from edit components drop-down box and then should check "Block: Export All Blocks" checkbox and then press download button.') . '</p>';
      $output .= '<p>' . t('Unzip your downloaded file and upload it into your drupal installed remote server or in any vanila drupal(drupal_root/sites/all/modules) where you want to import all the exported blocks and enable this module by drush or from admin.') . '</p>';
      $output .= '<h3>' . t('Dependencies') . '</h3>';
      $output .= '<p>' . t('1. Ctools') . '</br>' . t('2. Features') . '</p>';
      return $output;
      break;
  }
}

/**
 * Implements hook_features_api().
 */
function blockexport_features_api() {
  return array(
    'blockexport_settings' => array(
      'name' => t('Block'),
      'feature_source' => TRUE,
      'default_file' => FEATURES_DEFAULTS_INCLUDED,
      'default_hook' => 'default_blockexport_settings',
    ),
  );
}

/**
 * Implements hook_features_export_options().
 */
function blockexport_settings_features_export_options() {
  $options = array();
  $options['block_settings'] = t('Block: Export All Blocks');
  return $options;
}

/**
 * Implements hook_features_export().
 */
function blockexport_settings_features_export($data, &$export, $module_name = '') {
  $export['dependencies']['blockexport'] = 'blockexport';
  foreach ($data as $object_name) {
    $export['features']['blockexport_settings'][$object_name] = $object_name;
  }
  return array();
}

/**
 * Implements hook_features_export_render().
 */
function blockexport_settings_features_export_render($module_name, $data, $export = NULL) {
  $tables = array(
    'block',
    'block_custom',
    'block_node_type',
    'block_role',
    'block_class',
  );
  return _blockexport_genarateschema($tables);
}

/**
 * Implements hook_features_export_revert().
 */
function blockexport_settings_features_revert($module_name = NULL) {
  $mycomponents = features_get_default('blockexport_settings', $module_name);
  if (!empty($mycomponents)) {
    if (count($mycomponents['block']) > 0) {
      db_truncate('block')
        ->execute();
      foreach ($mycomponents['block'] as $mycomponent) {
        db_insert('block')
          ->fields($mycomponent)
          ->execute();
      }
    }
    if (count($mycomponents['block_custom']) > 0) {
      db_truncate('block_custom')
        ->execute();
      foreach ($mycomponents['block_custom'] as $mycomponent_custom) {
        db_insert('block_custom')
          ->fields($mycomponent_custom)
          ->execute();
      }
    }
    if (count($mycomponents['block_node_type']) > 0) {
      db_truncate('block_node_type')
        ->execute();
      foreach ($mycomponents['block_node_type'] as $mycomponent_block_type) {
        db_insert('block_node_type')
          ->fields($mycomponent_block_type)
          ->execute();
      }
    }
    if (count($mycomponents['block_role']) > 0) {
      db_truncate('block_role')
        ->execute();
      foreach ($mycomponents['block_role'] as $mycomponent_block_role) {
        db_insert('block_role')
          ->fields($mycomponent_block_role)
          ->execute();
      }
    }
    if (isset($mycomponents['block_class']) && count($mycomponents['block_class']) > 0) {
      db_truncate('block_class')
        ->execute();
      foreach ($mycomponents['block_class'] as $mycomponent_block_class) {
        db_insert('block_class')
          ->fields($mycomponent_block_class)
          ->execute();
      }
    }
  }
}

/**
 * Implements private function which will return table schema.
 */
function _blockexport_genarateschema($table_to_export) {
  $code = array();
  $code[] = "  \$export = array();";
  foreach ($table_to_export as $table) {
    if (db_table_exists($table)) {
      $results = db_select($table, 't')
        ->fields('t')
        ->execute();
      foreach ($results as $result) {
        $data = array();
        $schema = drupal_get_schema($table);
        $fields = array_keys($schema['fields']);
        foreach ($fields as $field) {
          $data[$field] = $result->{$field};
        }
        $code[] = "  \$export['{$table}'][] = " . features_var_export($data) . ";";
      }
    }
  }
  $code[] = "return \$export;";
  $code = implode("\n", $code);
  return array(
    'default_blockexport_settings' => $code,
  );
}