View source
<?php
interface ClientsConnectionAdminUIInterface {
function connectionSettingsForm($form, &$form_state);
}
function clients_connections_list() {
$output = '';
$connection_types = clients_get_connection_types();
$connections = clients_connection_load_all();
$rows = array();
foreach ($connections as $name => $connection) {
$delete_label = $connection->export_type & EXPORT_IN_CODE ? t('revert') : t('delete');
$delete_link = $connection->export_type & EXPORT_IN_DATABASE ? l($delete_label, "admin/structure/clients/connections/{$name}/delete") : '';
$type = $connection->type;
if (isset($connection->broken_message)) {
$type .= ' ' . t('(broken)');
}
$rows[] = array(
'name' => l($connection->name, "admin/structure/clients/connections/{$name}/view"),
'type' => $type,
'endpoint' => $connection
->formatEndpoint($connection->endpoint),
'storage' => $connection->export_type_label,
'edit' => l(t('edit'), "admin/structure/clients/connections/{$name}/edit"),
'test' => l(t('test'), "admin/structure/clients/connections/{$name}/test"),
'delete' => $delete_link,
);
}
if (!count($rows)) {
$rows[] = array(
array(
'data' => t('No connections defined yet.'),
'colspan' => '5',
),
);
}
$headers = array(
t('Name'),
t('Type'),
t('Endpoint'),
t('Storage'),
array(
'data' => t('Operations'),
'colspan' => 3,
),
);
$output .= theme('table', array(
'header' => $headers,
'rows' => $rows,
));
$items = array();
if ($connection_types) {
foreach ($connection_types as $type => $type_data) {
$items[] = l(t('Add @type connection', array(
'@type' => $type_data['label'],
)), 'admin/structure/clients/connections/add/' . $type);
}
}
else {
$items[] = t('No connection types are available: you need to enable one or more modules that provide them.');
}
$output .= theme('item_list', array(
'items' => $items,
));
return $output;
}
function clients_connection_add($type) {
$connection_types = clients_get_connection_types();
if (!isset($connection_types[$type])) {
return drupal_not_found();
}
drupal_set_title(t('Add @name connection', array(
'@name' => $type,
)));
$data = new stdClass();
$data->type = $type;
$data->name = '';
$data->new = TRUE;
$class = 'clients_connection_' . $type;
$connection = new $class($data);
return drupal_get_form('clients_connection_form', $connection);
}
function clients_connection_form($form, &$form_state, $connection) {
$form = array();
if (!isset($connection->new)) {
$connection->new = FALSE;
}
$type = $connection->type;
$class = 'clients_connection_' . $type;
$form['old_connection'] = array(
'#type' => 'value',
'#value' => $connection,
);
$form['type'] = array(
'#type' => 'textfield',
'#title' => t('Connection type'),
'#description' => t('The type of this connection. May not be changed.'),
'#value' => $connection->type,
'#size' => 50,
'#disabled' => TRUE,
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Connection machine name'),
'#default_value' => $connection->name,
'#size' => 50,
'#maxlength' => 100,
'#description' => t('The connection name must contain only lowercase letters, numbers, and underscores. It must be unique.'),
'#required' => TRUE,
);
if ($connection->name) {
$form['name']['#description'] .= '<br /><strong>' . t('Warning: Changing the name of an existing connection may affect any data you have stored based on that connection.') . '</strong>';
}
$form['endpoint'] = array(
'#type' => 'textfield',
'#title' => t('Connection endpoint'),
'#default_value' => $connection->new ? '' : $connection->endpoint,
'#size' => 100,
'#maxlength' => 100,
'#description' => t('Remote service URL e.g. http://mysite.com/services/endpoint'),
'#required' => TRUE,
);
$form['configuration'] = array(
'#tree' => TRUE,
);
$form['#connection_type'] = $type;
$form['#connection_class'] = $class;
$form = $connection
->connectionSettingsForm($form, $form_state);
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save connection'),
);
return $form;
}
function clients_connection_form_validate($form, &$form_state) {
if (!preg_match('!^[a-z0-9_]+$!', $form_state['values']['name'])) {
form_set_error('name', t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
}
$old_connection = $form_state['values']['old_connection'];
$connections = clients_connection_load_all();
if (isset($connections[$form_state['values']['name']]) && $form_state['values']['name'] != $old_connection->name) {
form_set_error('name', t('This connection name is already taken.'));
}
}
function clients_connection_form_submit($form, &$form_state) {
$class = $form['#connection_class'];
$old_connection = $form_state['values']['old_connection'];
call_user_func_array(array(
$class,
'connectionSettingsForm_submit',
), array(
$form,
&$form_state,
));
$connection = $form_state['values'];
if (!isset($connection['configuration'])) {
$connection['configuration'] = array();
}
if (isset($old_connection->cid)) {
$connection['cid'] = $old_connection->cid;
drupal_write_record('clients_connections', $connection, 'cid');
}
else {
drupal_write_record('clients_connections', $connection);
}
drupal_set_message(t('Connection saved.'));
$form_state['redirect'] = 'admin/structure/clients/connections/' . $connection['name'];
}
function clients_connection_view($connection) {
$rows = array();
$rows[] = array(
t('Name'),
check_plain($connection->name),
);
$rows[] = array(
t('Type'),
$connection->type,
);
$rows[] = array(
t('Endpoint'),
$connection
->formatEndpoint($connection->endpoint),
);
$rows[] = array(
t('Storage'),
$connection->export_type_label,
);
foreach ($connection->configuration as $label => $val) {
if (is_array($val)) {
$val = implode(', ', $val);
}
$rows[] = array(
ucfirst(t($label)),
nl2br(check_plain($val)),
);
}
return theme('table', array(
'rows' => $rows,
));
}
function client_delete_confirm($form, &$form_state, $type, $service) {
$form['type'] = array(
'#type' => 'value',
'#value' => $type,
);
$form['cid'] = array(
'#type' => 'value',
'#value' => $service->cid,
);
$form['name'] = array(
'#type' => 'value',
'#value' => $service->name,
);
return confirm_form($form, t('Are you sure you want to delete !type %title?', array(
'!type' => $type,
'%title' => $service->name,
)), isset($_GET['destination']) ? $_GET['destination'] : "admin/structure/clients/{$type}s/{$service->cid}", t('This action cannot be undone.'), t('Delete'), t('Cancel'));
}
function client_delete_confirm_submit($form, &$form_state) {
$cid = (int) $form_state['values']['cid'];
if ($form_state['values']['type'] == 'connection') {
$num_deleted = db_delete('clients_connections')
->condition('cid', $cid)
->execute();
if ($num_deleted) {
drupal_set_message(t('Connection @name deleted.', array(
'@name' => $form_state['values']['name'],
)));
$form_state['redirect'] = 'admin/structure/clients/connections';
}
else {
drupal_set_message(t('Problem deleting connection @name.', array(
'@name' => $form_state['values']['name'],
)), 'error');
}
}
}
function clients_connection_test_form($form, &$form_state, $connection) {
$form['#connection'] = $connection;
$form['connection'] = array(
'#type' => 'fieldset',
'#title' => t('Connection details'),
);
$items[] = t('Type') . ': ' . $connection->type;
$items[] = t('Name') . ': ' . check_plain($connection->name);
$items[] = t('Endpoint') . ': ' . $connection->endpoint;
$form['connection']['details'] = array(
'#value' => theme('item_list', array(
'items' => $items,
)),
);
$form['buttons'] = array(
'#tree' => TRUE,
);
$buttons = $connection
->getTestOperations($form_state, $connection->name);
drupal_alter('client_connection_test_buttons', $buttons, $form_state, $connection->name);
foreach ($buttons as $key => $button) {
if ($button['#type'] == 'submit') {
$form['buttons'][$key] = array(
'#type' => 'fieldset',
);
$form['buttons'][$key]['button'] = $button;
}
else {
$form['buttons'][$key] = $button;
}
$form['buttons'][$key]['#description'] = $form['buttons'][$key]['button']['#description'];
$form['buttons'][$key]['#tree'] = TRUE;
$form['buttons'][$key]['button']['#key'] = $key;
}
foreach (array_keys($form['buttons']) as $button_id) {
if (isset($form_state['storage'][$button_id])) {
$form['buttons'][$button_id]['results'] = array(
'#type' => 'fieldset',
'#title' => t('Results'),
'#collapsible' => TRUE,
);
$title = t($form['buttons'][$button_id]['button']['#value']);
$data = check_plain(print_r($form_state['storage'][$button_id], TRUE));
$form['buttons'][$button_id]['results'][$button_id] = array(
'#type' => 'markup',
'#value' => "<h3>{$title}</h3>" . "<pre>{$data}</pre>",
);
unset($form_state['storage'][$button_id]);
}
}
return $form;
}
function clients_connection_test_form_validate($form, &$form_state) {
$connection = $form['#connection'];
$button_key = $form_state['clicked_button']['#key'];
$button_form_values = $form_state['values']['buttons'][$button_key];
if (isset($form_state['clicked_button']['#action_validate'])) {
$validate_handler = $form_state['clicked_button']['#action_validate'];
$connection
->{$validate_handler}($button_form_values);
}
}
function clients_connection_test_form_submit($form, &$form_state) {
$connection = $form['#connection'];
$button_key = $form_state['clicked_button']['#key'];
$submit_handler = $form_state['clicked_button']['#action_submit'];
$button_form_values = $form_state['values']['buttons'][$button_key];
if ($form_state['clicked_button']['#action_type'] == 'method') {
$result = $connection
->{$submit_handler}($button_form_values);
}
else {
$result = $submit_handler($button_form_values);
}
$form_state['storage'][$button_key] = $result;
}