You are here

features_tools.module in Features Tools 6.2

Same filename and directory in other branches
  1. 6 features_tools.module

features_tools module

File

features_tools.module
View source
<?php

/**
 * @file
 *   features_tools module
 */

/**
 * Implementation of hook_perm().
 *
 * @return array An array of valid permissions for the features_tools module
 */
function features_tools_perm() {
  return array(
    'execute unlink files',
  );
}

/**
 * Implementation of hook_menu().
 *
 * @return An array of menu items.
 */
function features_tools_menu() {
  $items = array();
  $items['admin/build/features/unlink'] = array(
    'title' => 'Execute unlink files',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'features_tools_unlink_form',
    ),
    'access arguments' => array(
      'execute unlink files',
    ),
  );
  return $items;
}

/**
 * Implementation of hook_form_FORM_ID_alter().
 */
function features_tools_form_features_export_form_alter(&$form, &$form_state) {

  //TODO: add backup options.
  $form['buttons']['create'] = array(
    '#type' => 'submit',
    '#value' => t('Auto create feature'),
    '#weight' => 10,
    '#submit' => array(
      'features_tools_export_build_form_submit',
    ),
  );
  $form['features_tools_dest'] = array(
    '#title' => t('Custom module path.'),
    '#type' => 'textfield',
    '#default_value' => $form['#feature']->name ? drupal_get_path('module', $form['#feature']->name) : 'sites/all/modules',
  );
  if ($form['#feature']) {
    $form['buttons']['safe_recreate'] = array(
      '#type' => 'submit',
      '#value' => t('Safe Auto create feature'),
      '#weight' => 10,
      '#submit' => array(
        'features_tools_form_features_export_form_safe_submit',
      ),
    );
    $form['#validate'][] = 'features_tools_form_features_export_form_validate';
  }
}
function features_tools_form_features_export_form_safe_submit($form, &$form_state) {
  module_load_include('inc', 'features', 'features.export');
  features_include();
  rules_include('rules_admin');
  module_load_include('inc', 'rules_admin', 'rules_admin.export');
  module_load_include('inc', 'rules', 'rules.export');
  $destination = $form_state['values']['features_tools_dest'];
  $arr = explode('/', $destination);
  $arr[sizeof($arr) - 1] .= '_unlink';
  $module_name = $arr[sizeof($arr) - 1];
  $destination = implode('/', $arr) . '/';
  $destination_info = _features_tools_get_unlink_version($destination);
  $hooks = _features_tools_get_hooks();
  $new_feature = $form_state['values']['sources'];
  $old_feature = $form['#feature']->info['features'];
  $stub = array();
  foreach ($hooks as $hook) {
    if (isset($old_feature[$hook])) {
      foreach ($old_feature[$hook] as $value) {
        if (!$new_feature[$hook][$value]) {
          $stub[$hook][] = $value;
        }
      }
    }
  }
  if ($stub) {
    $file_content = array();
    foreach ($stub as $hook => $value) {
      $output = array();
      $code_buffer = '';
      foreach ($value as $component) {
        switch ($hook) {
          case 'views':
            $view = views_get_view($component);
            $output[] = $view
              ->export();
            $output[] = '$view->save();';
            break;
          case 'box':
            $export = array(
              'features' => array(
                'box' => array(
                  $component => $component,
                ),
              ),
            );
            $component_hooks = features_export_render_hooks($export, $module_name, TRUE);
            $output[] = str_replace(' return $export;', '', $component_hooks['box']['default_box']);
            $output[] = 'drupal_write_record(' . "'box'" . ', $box);';
            break;
          case 'rules_categories':
            $export = array();
            $export = rules_categories_features_export_render($form['#feature']->name, array(
              $component => $component,
            ));
            $output[] = str_replace('return', '$rules = ', $export['rules_defaults']);
            $output[] = '  foreach ($rules as $item_type => $elements) {';
            $output[] = '    foreach ($elements as $name => $element) {';
            $output[] = "      rules_item_type_invoke(" . '$item_type' . ", 'import', " . 'array(&$name, &$element));';
            $output[] = '      rules_item_save($item_type, $name, $element);';
            $output[] = "      drupal_set_message(t('Imported %label.', array('%label' => rules_get_element_label(" . '$element))));';
            $output[] = "    }";
            $output[] = "  }";
        }
      }
      $code_buffer .= implode("\n", $output);
      $output = array();
      $output[] = "/**";
      $output[] = " * Unlink {$module_name}_{$destination_info['delta']}_{$hook}().";
      $output[] = " */";
      $output[] = "function {$module_name}_{$destination_info['delta']}_{$hook}() {";
      $output[] = $code_buffer;
      $output[] = "}";
      $file_content[] = implode("\n", $output);
    }
    $file_content = "<?php\n\n" . implode("\n\n", $file_content) . "\n";
    mkdir($destination_info['destination'], 0777, TRUE);
    $error = '';
    if (file_put_contents("{$destination_info['destination']}{$module_name}.{$destination_info['delta']}.unlink.inc", $file_content) === FALSE) {
      $error = TRUE;
    }
    if ($error) {
      drupal_set_message(t('One or more files cannot be writing to the file system this is probably a permission issue. @see http://drupal.org/project/features_tools for more info'));
      return;
    }
    else {
      drupal_set_message(t('The unlink file was successfully created.'));
    }
  }
  features_tools_export_build_form_submit($form, $form_state);
}

/**
 * submit function for the features_export_form, write the file to disk. 
 */
function features_tools_export_build_form_submit($form, &$form_state) {
  module_load_include('inc', 'features', 'features.export');
  features_include();

  // Assemble the combined component list.
  $stub = array();
  $components = array_keys(features_get_components());
  foreach ($components as $component) {

    // User-selected components take precedence.
    if (!empty($form_state['values']['sources'][$component])) {
      $stub[$component] = features_dom_decode_options(array_filter($form_state['values']['sources'][$component]));
    }
    elseif (!empty($form['#feature']->info['features'][$component])) {
      $stub[$component] = $form['#feature']->info['features'][$component];
    }
  }

  // Generate populated feature.
  $module_name = $form_state['values']['module_name'];
  $export = features_populate($stub, $form_state['values']['sources']['dependencies'], $module_name);

  // Directly copy the following attributes.
  $attr = array(
    'name',
    'description',
  );
  foreach ($attr as $key) {
    $export[$key] = isset($form_state['values'][$key]) ? $form_state['values'][$key] : NULL;
  }

  // If either update status-related keys are provided, add a project key
  // corresponding to the module name.
  if (!empty($form_state['values']['version']) || !empty($form_state['values']['project_status_url'])) {
    $export['project'] = $form_state['values']['module_name'];
  }
  if (!empty($form_state['values']['version'])) {
    $export['version'] = $form_state['values']['version'];
  }
  if (!empty($form_state['values']['project_status_url'])) {
    $export['project status url'] = $form_state['values']['project_status_url'];
  }

  // Generate download.
  $directory = '';
  $destination = $form_state['values']['features_tools_dest'];
  if ($destination) {
    $directory .= $destination . '/';
  }

  //TODO: find a better term then arg(3) to find if we are on create page or recreat.
  if (arg(3) == 'create') {
    $directory .= $module_name;
  }
  if ($files = features_export_render($export, $module_name, TRUE)) {

    //If the directory dont exists create it.
    if (!file_exists($directory)) {
      mkdir($directory, 0777, TRUE);
    }
    else {

      //If the directory exists delete all the files before recreation, to clean the dead files in case some elements removed from the feature.
      foreach (glob($directory . '*.*') as $v) {
        unlink($v);
      }
    }
    $error = false;
    foreach ($files as $extension => $file_contents) {
      if (!in_array($extension, array(
        'module',
        'info',
      ))) {
        $extension .= '.inc';
      }
      if (file_put_contents("{$directory}/{$module_name}.{$extension}", $file_contents) === FALSE) {
        $error = true;
      }
    }
    if ($error) {
      drupal_set_message(t('One or more files cannot be writing to the file system this is probably a permission issue. @see http://drupal.org/project/features_tools for more info'));
    }
    else {
      drupal_set_message(t('The feature was successfully created.'));
    }
  }
  drupal_flush_all_caches();
}
function features_tools_unlink_form($form_state) {
  $files = drupal_system_listing('\\.unlink.inc$', 'modules', 'name', 0);
  $form = array();
  $options = array();
  foreach ($files as $value) {
    $options[$value->name] = str_replace('_unlink.unlink', '', $value->name);
  }
  $form['unlink_options'] = array(
    '#type' => 'checkboxes',
    '#options' => $options,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'submit',
    '#name' => 'submit',
  );
  return $form;
}
function features_tools_unlink_form_submit($form, &$form_state) {
  $hooks = _features_tools_get_hooks();
  $files = drupal_system_listing('\\.unlink.inc$', 'modules', 'name', 0);
  foreach ($form_state['values']['unlink_options'] as $key => $value) {
    if ($value) {
      include_once $files[$key]->filename;
      views_include('view');

      //ft_unlink_unlink_views
      foreach ($hooks as $hook) {
        $function_name = str_replace('.unlink', '', $key) . "_{$hook}";
        $function_name = str_replace('.', '_', $function_name);
        if (function_exists($function_name)) {
          call_user_func_array($function_name, array());
          drupal_set_message("{$hook} {$function_name}");
        }
      }
    }
  }
}
function _features_tools_get_hooks() {
  return array(
    'box',
    'views',
    'rules_categories',
  );
}

/**
 * Get the next version for the unlink directory.
 * @param string $destination , the $destination path.
 * @return array with the directory delta and the new destination.
 */
function _features_tools_get_unlink_version($destination) {
  $destination = substr($destination, 0, strlen($destination) - 1);
  $i = 1;
  while (file_exists("{$destination}_{$i}")) {
    $i++;
  }
  return array(
    'destination' => "{$destination}_{$i}/",
    'delta' => $i,
  );
}