View source
<?php
interface ClientsConnectionAdminUIInterface {
function connectionSettingsFormAlter(&$form, &$form_state);
}
interface ClientsConnectionTestingInterface {
function testLabels();
function test($connection, &$button_form_values);
}
function clients_connection_form($form, &$form_state, $connection, $op = 'edit') {
$environment_name = variable_get('environment_name', NULL);
if (isset($environment_name)) {
$replacement_connection_name = $connection->name . '_' . $environment_name;
if ($replacement_connection = entity_load_single('clients_connection', $replacement_connection_name)) {
$message = t("This connection is inactive due to the site's environment setting. The <a href=\"!url\">%connection connection</a> will be substituted for it.", array(
'!url' => url('admin/structure/clients/connections/manage/' . $replacement_connection_name),
'%connection' => $replacement_connection->label,
));
}
elseif (substr($connection->name, -1 * strlen($environment_name)) == $environment_name) {
$original_connection_name = substr($connection->name, 0, -1 * (strlen($environment_name) + 1));
if ($original_connection = entity_load_single('clients_connection', $original_connection_name)) {
$message = t("Due to the site's environment setting, this connection will substitute for the <a href=\"!url\">%connection connection</a>.", array(
'!url' => url('admin/structure/clients/connections/manage/' . $original_connection_name),
'%connection' => $original_connection->label,
));
}
}
if (isset($message)) {
drupal_set_message($message);
}
}
$form = array();
$type = $connection->type;
$class = 'clients_connection_' . $type;
if ($op == 'clone') {
$connection->label .= ' (cloned)';
$connection->name = '';
}
$form['#connection'] = clone $connection;
$form['fake_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['label'] = array(
'#type' => 'textfield',
'#title' => t('Connection label'),
'#default_value' => isset($connection->label) ? $connection->label : '',
'#size' => 50,
'#maxlength' => 100,
'#description' => t('The human-readable name for the connection.'),
'#required' => TRUE,
);
$form['name'] = array(
'#type' => 'machine_name',
'#default_value' => isset($connection->name) ? $connection->name : '',
'#maxlength' => 32,
'#machine_name' => array(
'exists' => 'clients_connection_load',
'source' => array(
'label',
),
),
'#description' => t('A unique machine-readable name for this connection. It must only contain lowercase letters, numbers, and underscores.'),
);
$form['endpoint'] = array(
'#type' => 'textfield',
'#title' => t('Connection endpoint'),
'#default_value' => isset($connection->endpoint) ? $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['configuration']['debug'] = array(
'#type' => 'checkbox',
'#title' => t('Debugging mode'),
'#description' => t('Outputs debug data for this connection. (Not all connection types support this.)'),
'#weight' => 100,
);
$form['credentials'] = array(
'#type' => 'fieldset',
'#title' => t('Credentials'),
'#tree' => TRUE,
);
$form['#connection_credentials_storage'] = isset($connection->configuration['credentials_storage']) ? $connection->configuration['credentials_storage'] : NULL;
$form['credentials']['credentials_storage'] = array(
'#type' => 'radios',
'#title' => t('Credentials storage'),
'#required' => TRUE,
'#default_value' => $form['#connection_credentials_storage'],
);
ctools_include('plugins');
$credentials_plugins = ctools_get_plugins('clients', 'clients_credentials_storage');
$options = array();
foreach ($credentials_plugins as $credentials_plugin => $credentials_plugin_info) {
$options[$credentials_plugin] = $credentials_plugin_info['label'];
$form['credentials']['credentials_storage'][$credentials_plugin]['#description'] = $credentials_plugin_info['description'];
}
$form['credentials']['credentials_storage']['#options'] = $options;
$credentials_storage_plugin = $connection
->get_credentials_storage_plugin();
$credentials_storage_plugin
->credentialsLoad($connection);
$form['#connection_type'] = $type;
$form['#connection_class'] = $class;
$connection
->connectionSettingsFormAlter($form, $form_state);
foreach (element_children($form['configuration']) as $configuration_key) {
if (isset($connection->configuration[$configuration_key])) {
$form['configuration'][$configuration_key]['#default_value'] = $connection->configuration[$configuration_key];
}
}
$credentials_properties = $connection
->credentialsProperties();
foreach ($credentials_properties as $property_name) {
if (isset($connection->credentials[$property_name])) {
$form['credentials'][$property_name]['#default_value'] = $connection->credentials[$property_name];
}
}
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save connection'),
);
$form['actions']['delete'] = array(
'#name' => 'delete',
'#type' => 'submit',
'#value' => t('Delete connection'),
);
return $form;
}
function clients_connection_form_submit($form, &$form_state) {
if ($form_state['triggering_element']['#name'] == 'delete') {
$form_state['redirect'] = 'admin/structure/clients/connections/manage/' . $form['#connection']->name . '/delete';
return;
}
$connection = entity_ui_form_submit_build_entity($form, $form_state);
ctools_include('plugins');
$connection->configuration['credentials_storage'] = $form_state['values']['credentials']['credentials_storage'];
$connection
->connectionSettingsForm_submit($form, $form_state);
$credentials_storage_plugin = $connection
->get_credentials_storage_plugin();
$credentials_storage_plugin
->credentialsSave($connection);
if (isset($form['#connection_credentials_storage']) && $form['#connection_credentials_storage'] != $form_state['values']['credentials']['credentials_storage']) {
$old_credentials_storage_plugin = $connection
->get_credentials_storage_plugin($form['#connection_credentials_storage']);
$old_credentials_storage_plugin
->credentialsDelete($connection);
}
$connection
->save();
drupal_set_message(t('Connection saved.'));
$form_state['redirect'] = 'admin/structure/clients/connections';
}
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 clients_connection_test_form($form, &$form_state, $connection) {
$type_definition = clients_get_connection_types($connection->type);
$form['#connection'] = $connection;
$form['connection'] = array(
'#type' => 'fieldset',
'#title' => t('Connection details'),
);
$items[] = t('Type') . ': ' . $type_definition['label'];
$items[] = t('Name') . ': ' . check_plain($connection->name);
$items[] = t('Endpoint') . ': ' . $connection->endpoint;
$resources = clients_resource_load_for_connection($connection->name);
$resource_labels = array();
foreach ($resources as $resource) {
$uri = $resource
->uri();
$resource_labels[] = l($resource->label, $uri['path']);
}
$items[] = t('Resources') . ': ' . implode(', ', $resource_labels);
$form['connection']['details'] = array(
'#markup' => theme('item_list', array(
'items' => $items,
)),
);
$form['buttons'] = array(
'#tree' => TRUE,
);
if (empty($type_definition['tests'])) {
$type_definition['tests'] = array();
}
drupal_alter('client_connection_tests', $type_definition['tests'], $connection);
$form['#connection_type'] = $type_definition;
if (!empty($type_definition['tests'])) {
foreach ($type_definition['tests'] as $test_name => $test_class) {
$test_handler = new $test_class();
$labels = $test_handler
->testLabels();
$element = array(
'#type' => 'fieldset',
'#title' => $labels['label'],
);
$element['description'] = array(
'#prefix' => '<div>',
'#markup' => $labels['description'],
'#suffix' => '</div>',
);
$element['params'] = array();
$element['submit'] = array(
'#type' => 'button',
'#value' => $labels['button'],
'#name' => $test_name,
'#ajax' => array(
'event' => 'click',
'callback' => 'clients_connection_test_js',
'wrapper' => $test_name . '-results',
'name' => $test_name,
),
'#limit_validation_errors' => array(
array(
'buttons',
$test_name,
),
),
);
$element['results'] = array(
'#prefix' => '<div id="' . $test_name . '-results">',
'#suffix' => '</div>',
);
if (method_exists($test_handler, 'testForm')) {
$form['buttons'][$test_name] = $test_handler
->testForm($element);
}
else {
$form['buttons'][$test_name] = $element;
}
}
}
else {
}
return $form;
}
function clients_connection_test_form_validate($form, &$form_state) {
$connection = $form['#connection'];
$type_definition = $form['#connection_type'];
$test_name = $form_state['clicked_button']['#name'];
if (isset($form_state['clicked_button']['#name'])) {
$test_class = $type_definition['tests'][$test_name];
$test_handler = new $test_class();
if (method_exists($test_handler, 'formValidate')) {
$test_handler
->formValidate($form_state['values']['buttons'][$test_name]);
}
}
}
function clients_connection_test_js($form, &$form_state) {
$connection = $form['#connection'];
$type_definition = $form['#connection_type'];
$test_name = $form_state['clicked_button']['#name'];
$test_class = $type_definition['tests'][$test_name];
$test_handler = new $test_class();
$errors = form_get_errors();
if (!$errors) {
$results = $test_handler
->test($connection, $form_state['values']['buttons'][$test_name]);
$form['buttons'][$test_name]['results']['output'] = array(
'#markup' => '<pre>' . check_plain(print_r($results, TRUE)) . '</pre>',
);
}
return $form['buttons'][$test_name]['results'];
}
function clients_connection_page_devel($connection) {
$output = kprint_r($connection, TRUE);
return $output;
}