function tripal_core_extensions_form_submit

2.x tripal_core.extensions.inc tripal_core_extensions_form_submit($form, &$form_state)

Process the import buttons.

_state

Parameters

$form:

1 string reference to 'tripal_core_extensions_form_submit'

File

tripal_core/includes/tripal_core.extensions.inc, line 519

Code

function tripal_core_extensions_form_submit($form, &$form_state) {
  // get the guid
  $clicked_button = $form_state['clicked_button']['#name'];
  $guid = preg_replace('/^import-(\d+)$/', "$1", $clicked_button);
  if ($guid) {
    $namespace = "http://tripal.info/rss/extensions/";
    $extension = $form_state['values']['extension-' . $guid];
    $type = $extension['category'];
    switch ($type) {
      case 'Bulk Loader Template':
        $options = array(
          'template_name' => $extension['title'],
          'template_array' => $extension[$namespace]['bulkldr_export'],
          'strict' => TRUE,
        );
        $errors = array();
        $warnings = array();
        $success = tripal_insert_bulk_loader_template($options, $errors, $warnings);
        if ($success) {
          drupal_set_message("Bulk loader succesfully added.");
        }
        else {
          drupal_set_message("Error importing this bulk loader.", 'error');
          if (count($errors) > 0) {
            foreach ($errors as $field => $message) {
              drupal_set_message($message, 'error');
            }
          }
        }
        break;
      case 'Materialized View':
        $module_name = 'tripal_core';
        $mview_name = $extension[$namespace]['mview_name'];
        $mview_schema = $extension[$namespace]['mview_schema'];
        $mview_sql = $extension[$namespace]['mview_sql'];

        // Validate the contents of the schema array.
        // TODO: security issue!! Before using 'eval' on this string
        // we should make sure the array is valid and there is no hidden
        // PHP code.
        $schema_array = array();
        $success = eval("\$schema_array = $mview_schema;");
        $error = chado_validate_custom_table_schema($schema_array);
        if ($error) {
          drupal_set_message("Cannot import Materialized View: $error", "error");
        }
        tripal_add_mview($mview_name, $module_name, $schema_array, $mview_sql);
        break;
      case 'Extension Module':
        if (array_key_exists('drupal_project', $extension[$namespace])) {
          module_load_include('module', 'update', 'update');
          module_load_include('inc', 'update', 'update.manager');
          $project = $extension[$namespace]['drupal_project'];
          $tar = tripal_core_extensions_get_latest_module_version($project);
          if (!$tar) {
            drupal_set_message('Cannot find a suitable release of this module
              for download. You may need to manually install this module.', 'error');
          }
          else {

            // Download the file from the Drupal repository
            $local_cache = update_manager_file_get($tar);
            if (!$local_cache) {
              drupal_set_message('Cannot download the file. Check the
                "Recent log messages" for relavent errors.', 'error');
            }
            else {
              // The following code was borrowed from the update_manager_install_form_submit()
              // of drupal in the modules/update/update.manager.inc file.
              $directory = _update_manager_extract_directory();
              try {
                $archive = update_manager_archive_extract($local_cache, $directory);
              }
              catch (Exception $e) {
                drupal_set_message('Cannot extract the file. Please check
                  permissions in the modules directory', 'error');
                return;
              }
              $archive_errors = update_manager_archive_verify($project, $local_cache, $directory);
              if (!empty($archive_errors)) {
                foreach ($archive_errors as $error) {
                  drupal_set_message($error, 'error');
                }
              }
              $project_location = $directory . '/' . $project;
              try {
                $updater = Updater::factory($project_location);
              }
              catch (Exception $e) {
                drupal_set_message($e->getMessage(), 'error');
                return;
              }
              $project_real_location = drupal_realpath($project_location);
              $arguments = array(
                'project' => $project,
                'updater_name' => get_class($updater),
                'local_url' => $project_real_location,
              );
              module_load_include('inc', 'update', 'update.authorize');
              $filetransfer = new FileTransferLocal(DRUPAL_ROOT);
              call_user_func_array('update_authorize_run_install', array_merge(array($filetransfer), $arguments));

              drupal_set_message('It appears the module was downloaded and
                extracted. You can now ' . l('enable this module' . 'admin/modules') . '.');
            }
          }
        }
        else {
          drupal_set_message('Cannot download this module.  The Drpual project
            name is not set. You may have to manually download this module, and
            if possible encourage the developers to set the project name if it
            has been fully published on Drupal.org already.', 'error');
        }
        break;
      default:
        break;
    }
  }
}