View source
<?php
define('ENDPOINT_MAIN_PATH', 'admin/config/entity_share/client');
function entity_share_ui_endpoint_list() {
drupal_set_title(t('Entity Share Client - Endpoints'));
$header = array(
'title' => array(
'data' => t('Title'),
),
'description' => array(
'data' => t('Description'),
),
'url' => array(
'data' => t('URL'),
),
'enable' => array(
'data' => t('Enable'),
),
'debug' => array(
'data' => t('Debug'),
),
'actions' => array(
'data' => t('Action'),
),
);
$endpoints = EntityShareUiClientEndpoint::loadAll();
$rows = array();
if ($endpoints) {
foreach ($endpoints as $endpoint) {
$rows[$endpoint['eid']] = array(
'title' => array(
'data' => array(
'#type' => 'link',
'#title' => $endpoint['title'],
'#href' => 'admin/entity_share/endpoint/' . $endpoint['name'] . '/edit',
),
),
'description' => check_plain($endpoint['description']),
'url' => $endpoint['url'],
'enable' => $endpoint['enabled'] ? t('Enabled') : t('Disabled'),
'debug' => $endpoint['debug'] ? t('Enabled') : t('Disabled'),
);
$rows[$endpoint['eid']]['actions'] = array(
'data' => array(
'#theme' => 'links',
'#attributes' => array(
'class' => array(
'links',
'inline',
),
),
'#links' => array(
'edit' => array(
'title' => t('edit'),
'href' => 'admin/entity_share/endpoint/' . $endpoint['name'] . '/edit',
),
'delete' => array(
'title' => t('delete'),
'href' => 'admin/entity_share/endpoint/' . $endpoint['name'] . '/delete',
),
),
),
);
}
}
$build = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('There is no endpoint...'),
);
return $build;
}
function entity_share_ui_endpoint_edit_form(array $form, array &$form_state, $name = NULL) {
if (!empty($name)) {
$endpoint = EntityShareUiClientEndpoint::load($name);
$button_name = t('Update');
$action = 'update';
drupal_set_title(t('Edit the endpoint @endpoint', array(
'@endpoint' => !empty($endpoint['title']) ? $endpoint['title'] : $name,
)));
}
else {
$button_name = t('Create');
$action = 'create';
drupal_set_title(t('Create a new endpoint'));
}
$form = array();
$form['name'] = array(
'#type' => 'machine_name',
'#machine_name' => array(
'exists' => '_entity_share_ui_endpoint_name_exists',
),
'#default_value' => isset($endpoint['name']) ? $endpoint['name'] : '',
'#disabled' => !empty($endpoint['name']),
);
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#maxlength' => 128,
'#required' => TRUE,
'#default_value' => isset($endpoint['title']) ? $endpoint['title'] : '',
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#default_value' => isset($endpoint['description']) ? $endpoint['description'] : '',
);
$form['url'] = array(
'#type' => 'textfield',
'#title' => t('Url'),
'#maxlength' => 255,
'#required' => TRUE,
'#default_value' => isset($endpoint['url']) ? $endpoint['url'] : '',
);
$form['authentication'] = array(
'#type' => 'fieldset',
'#title' => t('Authentication'),
);
$form['authentication']['login'] = array(
'#type' => 'textfield',
'#title' => t('Login'),
'#description' => t('User login to access the endpoint on the server side'),
'#maxlength' => 128,
'#required' => TRUE,
'#default_value' => isset($endpoint['login']) ? $endpoint['login'] : '',
);
$form['authentication']['password'] = array(
'#type' => 'password',
'#title' => t('Password'),
'#description' => t('User password to access the endpoint on the server side'),
'#maxlength' => 128,
'#required' => $action == 'create' ? TRUE : FALSE,
'#default_value' => isset($endpoint['password']) ? $endpoint['password'] : '',
);
$form['enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enable endpoint'),
'#default_value' => isset($endpoint['enabled']) ? $endpoint['enabled'] : 1,
);
$form['debug'] = array(
'#type' => 'checkbox',
'#title' => t('Debug mode'),
'#default_value' => isset($endpoint['debug']) ? $endpoint['debug'] : 0,
);
$form['eid'] = array(
'#type' => 'hidden',
'#value' => isset($endpoint['eid']) ? $endpoint['eid'] : '',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => $button_name,
'#submit' => array(
'entity_share_ui_endpoint_edit_form_submit',
),
);
$form['cancel'] = array(
'#type' => 'link',
'#title' => t('Cancel'),
'#href' => ENDPOINT_MAIN_PATH,
);
return $form;
}
function entity_share_ui_endpoint_edit_form_submit(array $form, array &$form_state) {
$endpoint = array(
'name' => $form_state['values']['name'],
'title' => $form_state['values']['title'],
'description' => $form_state['values']['description'],
'url' => $form_state['values']['url'],
'login' => $form_state['values']['login'],
'password' => $form_state['values']['password'],
'enabled' => $form_state['values']['enabled'],
'debug' => $form_state['values']['debug'],
);
if (!empty($form_state['values']['eid'])) {
$endpoint['eid'] = $form_state['values']['eid'];
}
$result = EntityShareUiClientEndpoint::save($endpoint);
if ($result) {
drupal_set_message(t('Endpoint @endpoint was successfully saved', array(
'@endpoint' => $endpoint['title'],
)));
}
else {
drupal_set_message(t('An error occurred while saving endpoint @endpoint.', array(
'@endpoint' => $endpoint['title'],
)), 'error');
}
$form_state['redirect'] = ENDPOINT_MAIN_PATH;
}
function _entity_share_ui_endpoint_name_exists($name) {
return is_array(EntityShareUiClientEndpoint::load($name)) ? TRUE : FALSE;
}
function entity_share_ui_endpoint_delete_form(array $form, array &$form_state, $name) {
$endpoint = EntityShareUiClientEndpoint::load($name);
if (!$endpoint) {
drupal_set_message(t('Invalid endpoint @endpoint', array(
'@endpoint' => $name,
)), 'error');
drupal_goto('admin/config/entity_share/client');
}
$form = array();
$form['endpoint'] = array(
'#type' => 'hidden',
'#value' => $name,
);
$form['#submit'] = array(
'entity_share_ui_endpoint_delete_form_submit',
);
return confirm_form($form, t('Do you want to delete the endpoint @endpoint ?', array(
'@endpoint' => $endpoint['title'],
)), 'admin/config/entity_share/client', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
}
function entity_share_ui_endpoint_delete_form_submit(array $form, array &$form_state) {
$endpoint = $form_state['values']['endpoint'];
if ($form_state['values']['confirm']) {
if (EntityShareUiClientEndpoint::delete($endpoint)) {
drupal_set_message(t('Endpoint @endpoint successfully deleted', array(
'@endpoint' => $endpoint,
)));
}
else {
drupal_set_message(t('An error occurred while deleting endpoint @endpoint', array(
'@endpoint' => $endpoint,
)), 'error');
}
}
$form_state['redirect'] = ENDPOINT_MAIN_PATH;
}