function tripal_core_extensions_form

2.x tripal_core.extensions.inc tripal_core_extensions_form($form, &$form_state = NULL)
1 string reference to 'tripal_core_extensions_form'
tripal_core_menu in tripal_core/tripal_core.module
Implements hook_menu(). Defines all menu items needed by Tripal Core

File

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

Code

function tripal_core_extensions_form($form, &$form_state = NULL) {
  // Get the RSS feed XML from the tripa.info website and save it to
  // a temp file so that we don't have to keep pulling the XML
  // everythime the page is loaded. If the temp file is older than 1 hours
  // then we'll pull it again. The number of seconds in an hour is 3600
  $tmp_file = sys_get_temp_dir() . '/tripal_rss_extensions.xml';
  if (!file_exists($tmp_file) or time() - filemtime($tmp_file) > 3600) {
    $content = file_get_contents("http://tripal.info/rss/extensions.xml");
    file_put_contents($tmp_file, $content);
  }
  else {
    $content = file_get_contents($tmp_file);
  }
  $xml = new SimpleXmlElement($content);
  $namespace = "http://tripal.info/rss/extensions/";


  $tab = array_key_exists('tab', $_GET) ? $_GET['tab'] : '';

  // Get any filters by categories.
  $filters = array();
  if (array_key_exists('values', $form_state)) {
    foreach ($form_state['values'] as $key => $value) {
      // Get the category to be filtered on.
      $matches = array();
      if (preg_match('/^categories-(.*?)$/', $key, $matches)) {
        if ($value == 'any') {
          continue;
        }
        $filters[$matches[1]] = $value;
      }
    }
  }

  // Parse the items into an array indexed by type and compatible versions.
  $extensions = array();
  $types = array();
  $type_ids = array();
  $categories = array();
  $cvs = array();
  $tvs = array();
  foreach ($xml->channel->item as $extension) {
    // Get the type of extension, convert that into a machine-readable name,
    // and build the array of types.
    $type = (string) $extension->category;
    $type_id = preg_replace('/[^\w]/', '_', strtolower($type));
    $type_ids[$type] = $type_id;
    if (!in_array($type_id, $types)) {
      $types[$type] = 1;
    }

    // Get the categories list for this item.
    $cats = preg_split('/, /', (string) $extension->children($namespace)->categories);

    // Get the guid for this extension
    $guid = (string) $extension->guid;

    // In order to get fields in the 'tripal_extension' name space we must
    // pass in the $namespace to the children function.  We first get the
    // Tripal versions, then the chado versions and organize the elements
    // accordintly.
    $tvs_temp = preg_split('/, /', (string) $extension->children($namespace)->tripal_version);
    foreach ($tvs_temp as $tv) {
      $tvs[$tv] = 1;
      $cvs_temp = preg_split('/, /', (string) $extension->children($namespace)->chado_version);
      foreach ($cvs_temp as $cv) {
        $cvs[$cv] = 1;

        // Keep track of the categories this item has been assigned.
        foreach ($cats as $cat) {
          $categories[$tv][$cv][$type][$cat] = 1;
          $categories['any'][$cv][$type][$cat] = 1;
          $categories[$tv]['any'][$type][$cat] = 1;
          $categories['any']['any'][$type][$cat] = 1;
        }

        // If there are filters then only include extensions that match the filters.
        if (array_key_exists($type_id, $filters) and !in_array($filters[$type_id], $cats)) {
          continue;
        }

        // Index the items by type, tripal version and chado version.
        $item = array();
        foreach ($extension->children() as $child) {
          $item[$child->getName()] = (string) $child;
        }
        foreach ($extension->children($namespace) as $child) {
          $item[$namespace][$child->getName()] = (string) $child;
        }
        $extensions[$tv][$cv][$type][$guid] = $item;
        $extensions['any'][$cv][$type][$guid] = $item;
        $extensions[$tv]['any'][$type][$guid] = $item;
        $extensions['any']['any'][$type][$guid] = $item;
      }
    }
  }

  // Convert the arrays from an associative array into a normal array, and sort.
  $types = array_keys($types);
  sort($types);
  $cvs = array_keys($cvs);
  sort($cvs);
  $tvs = array_keys($tvs);
  sort($tvs);

  // Get the Chado version and convert to the expected format
  $chado_version = chado_get_version(TRUE);
  $chado_version = preg_replace('/^(\d\.\d).*$/', "v$1x", $chado_version);
  $my_chado_version = $chado_version;
  // The default value can come from the pager links (thus via $_GET) or
  // via ajax call (thus via $form_state).
  if (array_key_exists('cv', $_GET)) {
    $chado_version = $_GET['cv'];
  }
  if (array_key_exists('values', $form_state) and array_key_exists('cv', $form_state['values'])) {
    $chado_version = $form_state['values']['cv'];
  }

  // Get the Tripal version. This is the version set in the tripal_core.info
  $info = system_get_info('module', 'tripal_core');
  $tripal_version = $info['version'];
  $tripal_version = preg_replace('/^.*?-(\d\.\d+).*$/', "v$1", $tripal_version);
  $my_tripal_version = $tripal_version;
  if (array_key_exists('tv', $_GET)) {
    $tripal_version = $_GET['tv'];
  }
  if (array_key_exists('values', $form_state) and array_key_exists('tv', $form_state['values'])) {
    $tripal_version = $form_state['values']['tv'];
  }

  // Add the instructions.
  $form['instructions'] = array(
    '#type' => 'item',
    '#markup' => t('This page will help you find extensions that are available
      for Tripal.  Select an extension type from the vertical tabs. The content
      of this page is constructed from an RSS feed provided by tripal.info.
      There may be no content if the tripal.info site is unavailable. The RSS
      feed will be cached for one hour.')
  );
  // Add the filters fieldset.
  $form['filters'] = array(
    '#type' => 'fieldset',
    '#title' => 'Filters',
    '#description' => t('You can filter which extensions are visible by
      changing the Tripal ahd Chado versions. By default only those
      extensions that are compatible with the currently installed Tripal
      and Chado verions are shown.'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $summary_message = 'Currently showing extensions compatible with ';
  if ($tripal_version != 'any' and $chado_version != 'any') {
    $summary_message .= "<strong>Tripal $tripal_version</strong> and <strong>Chado $chado_version</strong>.";
  }
  elseif ($tripal_version == 'any' and $chado_version != 'any') {
    $summary_message .= "<strong>any Tripal</strong> version and <strong>Chado $chado_version</strong>.";
  }
  elseif ($tripal_version != 'any' and $chado_version == 'any') {
    $summary_message .= "<strong>Tripal $tripal_version</strong> and <strong>any Chado</strong> version.";
  }
  elseif ($tripal_version == 'any' and $chado_version == 'any') {
    $summary_message .= "<strong>any Tripal</strong> version and <strong>any Chado</strong> version.";
  }
  $form['filter_summary'] = array(
    '#type' => 'item',
    '#markup' => $summary_message,
  );

  // Add the Tripal version select box.
  $options = array();
  $options['any'] = '--Any--';
  foreach ($tvs as $tv) {
    $options[$tv] = $tv;
  }
  $form['filters']['tv'] = array(
    '#type' => 'select',
    '#title' => 'Tripal',
    '#options' => $options,
    '#default_value' => $tripal_version,
    '#ajax' => array(
      'callback' => "tripal_core_extensions_form_ajax_callback",
      'wrapper' => 'tripal_core_extensions',
      'effect' => 'fade',
      'method' => 'replace',
    ),
    '#prefix' => '<div style="float: left;">',
    '#suffix' => '</div>'
  );

  // Add the Chado version select box.
  $options = array();
  $options['any'] = '--Any--';
  foreach ($cvs as $cv) {
    $options[$cv] = $cv;
  }
  $form['filters']['cv'] = array(
    '#type' => 'select',
    '#title' => 'Chado',
    '#options' => $options,
    '#default_value' => $chado_version,
    '#ajax' => array(
      'callback' => "tripal_core_extensions_form_ajax_callback",
      'wrapper' => 'tripal_core_extensions',
      'effect' => 'fade',
      'method' => 'replace',
    ),
    '#prefix' => '<div style="float: left; padding-left: 10px">',
    '#suffix' => '</div>'
  );

  // Add the vertical tabs
  $form['extensions'] = array(
    '#type' => 'vertical_tabs',
    '#default_tab' => $tab,
  );

  // Add the fieldsets for each type
  foreach ($types as $type) {
    $form[$type] = array(
      '#id' => $type_ids[$type],
      '#type' => 'fieldset',
      '#title' => $type . 's',
      '#collapsed' => TRUE,
      '#collapsible' => TRUE,
      '#group' => 'extensions',
    );
  }

  // Iterate through all of the extensions and add them to the form.
  tripal_core_extension_form_add_extensions($form, $form_state, 
  $extensions[$tripal_version][$chado_version], $categories, $tripal_version, 
  $chado_version, $my_tripal_version, $my_chado_version, $type_ids, 
  $namespace, $filters);

  foreach ($types as $type) {
    if (count(element_children($form[$type])) == 0) {
      $form[$type]['empty'] = array(
        '#type' => 'item',
        '#markup' => '<strong>There are no matching ' . strtolower($type) . '(s).</strong>',
      );
    }
  }

  $form['#prefix'] = '<div id="tripal_core_extensions">';
  $form['#suffix'] = '</div>';
  $form['#submit'][] = 'tripal_core_extensions_form_submit';
  return $form;
}