function tripal_core_extension_form_add_extensions

2.x tripal_core.extensions.inc tripal_core_extension_form_add_extensions(&$form, $form_state, $extensions, $categories, $tripal_version, $chado_version, $my_tripal_version, $my_chado_version, $type_ids, $namespace, $filters)

Adds each extension to the form.

This function exits to simplify the the tripal_core_extension_form() function.

1 call to tripal_core_extension_form_add_extensions()

File

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

Code

function tripal_core_extension_form_add_extensions(&$form, $form_state, $extensions, 
$categories, $tripal_version, $chado_version, $my_tripal_version, 
$my_chado_version, $type_ids, $namespace, $filters) {

  // Iterate through the extensions. We will add a pager for
  // each type of extension, and display only those that should appear
  // on the page.
  $type_index = 0;
  foreach ($extensions as $type => $extensions) {
    $total_items = count($extensions);

    // Indicate how many matching extensions were found
    $form[$type]['total_found'] = array(
      '#type' => 'item',
      '#markup' => '<strong>Found ' . $total_items . ' matching ' . strtolower($type) . '(s)</strong>',
    );

    // Add the category select box;
    $cats = array_keys($categories[$tripal_version][$chado_version][$type]);
    sort($cats);
    $options = array();
    $options['any'] = '--Any--';
    foreach ($cats as $cat) {
      $options[$cat] = $cat;
    }
    // The default value can come from the pager links (thus via $_GET) or
    // via ajax call (thus via $form_state).
    $default_filter = '';
    if (array_key_exists('categories-' . $type_ids[$type], $_GET)) {
      $default_filter = $_GET['categories-' . $type_ids[$type]];
    }
    if (array_key_exists('values', $form_state) and array_key_exists('categories-' . $type_ids[$type], $form_state['values'])) {
      $default_filter = $form_state['values']['categories-' . $type_ids[$type]];
    }
    $form[$type]['filters']['categories-' . $type_ids[$type]] = array(
      '#type' => 'select',
      '#title' => 'Filter by Category',
      '#options' => $options,
      '#default_value' => $default_filter,
      '#ajax' => array(
        'callback' => "tripal_core_extensions_form_ajax_callback",
        'wrapper' => 'tripal_core_extensions',
        'effect' => 'fade',
        'method' => 'replace',
      ),
    );

    // Initialize pager and gets current page
    $num_per_page = 5;
    $page = pager_default_initialize($total_items, $num_per_page, $type_index);

    // Gets first record and last record to show
    $start = ($page) * $num_per_page;
    $end = ($start + $num_per_page < $total_items) ? $start + $num_per_page : $total_items;
    // Iterate through each of the elements and add them to the form if
    // they are within the page
    $extension_index = 0;
    foreach ($extensions as $guid => $extension) {
      // Skip items that aren't on our current page.
      if ($extension_index < $start or $extension_index >= $end) {
        $extension_index++;
        continue;
      }

      $extension['title'] = trim($extension['title']);

      // If this is an extension module then there will be a home page for it
      $home_page = '';
      if (array_key_exists('home_page', $extension[$namespace])) {
        $home_page = "<strong>Project Home: </strong>" . $extension[$namespace]['home_page'] . "</br>";
      }

      // Determine if this extension is compatible with this site.
      $incompatible = '';
      $tvs_temp = preg_split('/, /', $extension[$namespace]['tripal_version']);
      $cvs_temp = preg_split('/, /', $extension[$namespace]['chado_version']);
      if (!in_array($my_tripal_version, $tvs_temp)) {
        $incompatible .= "<li>This extension is not compatible with this version of Tripal.</li>";
      }
      if (!in_array($my_chado_version, $cvs_temp)) {
        $incompatible .= "<li>This extension is not compatible with the installed Chado version.</li>";
      }
      $incompatible = t($incompatible);

      // Determine if this extension is already installed.
      $is_installed = '';
      switch ($type) {
        case 'Bulk Loader Template':
          $blk_id = db_select('tripal_bulk_loader_template', 'tblt')
            ->fields('tblt', array('template_id'))
            ->condition('name', $extension['title'])
            ->execute()
            ->fetchField();
          if ($blk_id) {
            $is_installed = '<li>A bulk loader template with this name is already installed.</li>';
          }
          break;
        case 'Materialized View':
          $mview_id = tripal_get_mview_id($extension[$namespace]['mview_name']);
          if ($mview_id) {
            $is_installed = '<li>A materialized view with this name is already installed.</li>';
          }
          break;
        case 'Extension Module':
          if (array_key_exists('drupal_project', $extension[$namespace]) and 
            module_exists($extension[$namespace]['drupal_project'])) {
            $is_installed = '<li>A module with this name is already installed.</li>';
          }
          break;
        default:
          break;
      }
      $is_installed = t($is_installed);

      // Does this module appear to be available on Drupal.org?
      $project = '';
      if ($type == 'Extension Module') {
        // Does it have a drupal project name?
        if (!array_key_exists('drupal_project', $extension[$namespace])) {
          // Since it doesn't have a drupal project name is it in a sandbox?
          if (!preg_match('/www.drupal.org\/sandbox/', $extension[$namespace]['home_page'])) {
            $project = t("<li>This module does not appear to be available as a " .
              "full Drupal project and thus cannot " .
              "be downloaded here. You may have to manually download it.</li>");
          }
          else {
            $project = ("<li>This module is in a sandbox on Drupal.org, and
              cannot be downloaded from here. You will have to manually download
              this module.</li>");
          }
        }
      }

      // Make sure the bulk loader module is installed, or we cannot provide
      // the bulk loader import button.
      $other = '';
      if ($type == 'Bulk Loader Template' and !module_exists('tripal_bulk_loader')) {
        $other = t('<li>The bulk loader
          module is not enabled. If you would like to import a loading template
          please enable it.</li>');
      }

      // If the user click's the button to import the extension then we
      // need the item in the submit function so we can process the import.
      $form[$type]['extension-' . $guid] = array(
        '#type' => 'value',
        '#value' => $extension,
      );

      $notices = '';
      if ($is_installed) {
        $notices = '<div class="messages status"><ul>' . $is_installed . '</ul></div>';
      }
      $warnings = '';
      if ($project or $other) {
        $warnings = '<div class="messages warning"><ul>' .
          $project . ' ' .
          $other . '</ul></div>';
      }
      $errors = '';
      if ($incompatible) {
        $errors = '<div class="messages error"><ul>' . $incompatible . '</ul></div>';
      }

      $state = '';
      if (array_key_exists('dev_stage', $extension[$namespace])) {
        $state = '<strong>Development State: </strong>' . $extension[$namespace]['dev_stage'] . "</br>";
      }

      // Create the form elements that we'll later theme into tables.
      $form[$type][$guid]['header'] = array(
        '#markup' => l($extension['title'], $extension['link']),
      );
      $form[$type][$guid]['details'] = array(
        '#markup' => "" .
          "<strong>Type:</strong> " . $type . "</br>" .
          "<strong>Categories: </strong>" . $extension[$namespace]['categories'] . "</br>" .
          "<strong>Authors: </strong>" . $extension[$namespace]['authors'] . "</br>" .
          $state .
          "<strong>Chado compatible versions: </strong>" . $extension[$namespace]['chado_version'] . "</br>" .
          "<strong>Tripal compatible versions: </strong>" . $extension[$namespace]['tripal_version'] . "</br>" .
          $home_page .
          "<strong>tripal.info Page: </strong>" . l($extension['link'], $extension['link']) . "</br>" .
          $notices .
          $warnings .
          $errors .
          "<p>" . $extension['description'] . "</p>",
      );
      // Add an import button to each of types that can support import.
      switch ($type) {
        case 'Bulk Loader Template':
          if (!$incompatible and !$is_installed and !$project) {
            $form[$type][$guid]['import'] = array(
              '#type' => 'submit',
              '#value' => "Import Loader",
              '#name' => "import-" . $guid,
            );
          }
          break;
        case 'Materialized View':
          if (!$incompatible and !$is_installed and !$project) {
            $form[$type][$guid]['import'] = array(
              '#type' => 'submit',
              '#value' => "Import MView",
              '#name' => "import-" . $guid,
            );
          }
          break;
        case 'Extension Module':
          if (!$incompatible and !$is_installed and !$project) {
            $form[$type][$guid]['import'] = array(
              '#type' => 'submit',
              '#value' => "Download Module",
              '#name' => "import-" . $guid,
            );
          }
          break;
        default:
          break;
      }
      $form[$type][$guid]['#theme'] = 'tripal_core_extensions_form_tables';
      $extension_index++;
    }

    // Now create and theme the pager.
    $pager = array(
      'tags' => array(),
      'element' => $type_index,
      'parameters' => array(
        'tab' => $type_ids[$type],
        'cv' => $chado_version,
        'tv' => $tripal_version,
      ),
      'quantity' => $num_per_page,
    );

    // now add the category filters to the params array
    foreach ($filters as $filter_id => $value) {
      $pager['parameters']['categories-' . $filter_id] = $value;
    }

    // because this may be an ajax callback, the theme_pager will set the URL to be
    // "system/ajax", so we need to reset that
    $pager = theme('pager', $pager);
    global $base_path;
    $pager = str_replace($base_path . "system/ajax", "", $pager);

    $form[$type]['pager'] = array(
      '#type' => 'item',
      '#markup' => $pager,
    );
    $type_index++;
  }
}