<?php

/**
 * @file
 *
 */

/**
 * Form builder to list and manage all relativity presets
 *
 * @ingroup forms
 * @see relativity_overview_presets_submit()
 * @see theme_relativity_overview_presets()
 *
 **/
function relativity_overview_presets() {
  $presets = relativity_get_presets();
  $form = array('#tree' => TRUE);
  foreach ($presets as $preset) {
    $types = array();
    foreach ($preset->nodes as $type) {
      $node_type = node_get_types('name', $type);
      $types[] = $node_type ? check_plain($node_type) : check_plain($type);
    }
    $form[$preset->vid]['#preset'] = (array)$preset;
    $form[$preset->vid]['name'] = array('#value' => check_plain($preset->name));
    $form[$preset->vid]['types'] = array('#value' => implode(', ', $types));
    $form[$preset->vid]['weight'] = array('#type' => 'weight', '#delta' => 10, '#default_value' => $preset->weight);
    $form[$preset->vid]['edit'] = array('#value' => l(t('edit preset'), "admin/content/relativity/edit/preset/$preset->vid"));
    $form[$preset->vid]['list'] = array('#value' => l(t('list relationships'), "admin/content/relativity/$preset->vid"));
    $form[$preset->vid]['add'] = array('#value' => l(t('add relationships'), "admin/content/relativity/$preset->vid/add/relationship"));
  }

  // Only make this form include a submit button and weight if more than one
  // preset exists.
  if (count($presets) > 1) {
    $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  }
  elseif (isset($preset)) {
    unset($form[$preset->vid]['weight']);
  }
  return $form;
}