You are here

function clients_resources_form in Web Service Clients 6

Same name and namespace in other branches
  1. 7 clients.module \clients_resources_form()

@todo validate uniqueness of name

2 string references to 'clients_resources_form'
clients_resources_add in ./clients.module
clients_resources_edit in ./clients.module

File

./clients.module, line 368
Clients module - handles keys and service connections and provides an API for clients @author Django Beatty - adub

Code

function clients_resources_form(&$form_state, $rid = FALSE) {
  $form = array();
  $resource = FALSE;
  if ($rid) {

    // edit existing
    $resource = clients_resource_load($rid);
    $form['#rid'] = $rid;
  }

  // Register the form with ahah_helper so we can use it. Also updates
  // $form_state['storage'] to ensure it contains the latest values that have
  // been entered, even when the form item has temporarily been removed from
  // the form. So if a form item *once* had a value, you *always* can retrieve
  // it.
  ahah_helper_register($form, $form_state);
  $wrapper = 'clients-resource';

  // can be anything
  $wrapper_values = $form_state['storage'][$wrapper];
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Resource name'),
    '#default_value' => $rid ? $resource->name : '',
    '#size' => 50,
    '#maxlength' => 100,
    '#description' => t('Must be unique'),
    '#required' => TRUE,
  );
  $connections = array();
  foreach (clients_connections_list() as $cid => $connection) {
    $connections[$cid] = $connection['name'];
  }
  $form[$wrapper] = array(
    '#type' => 'fieldset',
    '#title' => t('Resource'),
    '#prefix' => '<div id="' . $wrapper . '">',
    // This is our wrapper div.
    '#suffix' => '</div>',
    '#tree' => TRUE,
  );

  /**
   * @todo fix cid being saved as string
   */
  $connection_id = isset($wrapper_values['connection']) ? (int) $wrapper_values['connection'] : ($rid ? (int) $resource->configuration['connection'] : NULL);
  $form[$wrapper]['connection'] = array(
    '#type' => 'select',
    '#title' => t('Connection'),
    '#default_value' => isset($connection_id) ? $connection_id : key($connections),
    // needs reset?
    '#options' => $connections,
    '#required' => TRUE,
    '#ahah' => array(
      'path' => ahah_helper_path(array(
        $wrapper,
      )),
      'wrapper' => $wrapper,
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  if (isset($connection_id)) {

    // triggers hook_clients_service_options in backend modules to inject configuration form fragment here which can use ahah wrapper defined above
    $form[$wrapper]['options'] = clients_service_options($connection_id, $wrapper, $wrapper_values, $resource);
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}