entity_share_ui_client.share.admin.inc in Entity Share 7
Entity Share UI Client Admin Share file.
File
modules/entity_share_ui/modules/entity_share_ui_client/entity_share_ui_client.share.admin.incView source
<?php
/**
* @file
* Entity Share UI Client Admin Share file.
*/
/**
* Node operation or action callback.
*
* @param array $nids
* Node ids to share.
*/
function entity_share_ui_share_action(array $nids) {
$nids = array_unique(array_keys($nids));
$page = entity_share_ui_share_page($nids);
print drupal_render_page($page);
drupal_exit();
}
/**
* Page builder.
*
* @param array $nids
* Node ids to share.
*
* @return array|string
* Render.
*/
function entity_share_ui_share_page(array $nids = array()) {
drupal_set_title(t('Share the contents'), PASS_THROUGH);
// Check nodes.
if (empty($nids)) {
if (empty($_POST['nids'])) {
drupal_set_message(t('Invalid parameters !'), 'error');
return array(
'#markup' => '',
);
}
else {
$nids = explode(',', $_POST['nids']);
}
}
$nodes_table = entity_share_ui_node_list($nids);
// Form.
$form = drupal_get_form('entity_share_ui_share_action_form', $nids);
return theme('entity_share_form_choose_endpoint_page', array(
'nodes_table' => $nodes_table,
'form' => $form,
));
}
/**
* Node list table.
*
* @param array $nids
* Node ids to share.
*
* @return array
* Render array.
*/
function entity_share_ui_node_list(array $nids) {
// Table header.
$header = array(
array(
'data' => t('Id'),
),
array(
'data' => t('Name'),
),
array(
'data' => t('Type'),
),
array(
'data' => t('Language'),
),
);
$nodes = node_load_multiple($nids);
$rows = array();
foreach ($nodes as $node) {
$rows[] = array(
'data' => array(
$node->nid,
$node->title,
node_type_get_name($node),
$node->language,
),
);
}
// Create a render array ($build) which will be
// themed as a table with a pager.
$build['table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('There is no node selected...'),
);
return $build;
}
/**
* Form generation.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state.
*
* @return array
* Form render array.
*/
function entity_share_ui_share_action_form(array $form, array &$form_state) {
if (isset($form_state['build_info']['args'][0])) {
$nids = $form_state['build_info']['args'][0];
}
else {
// Note: $form_state['values'] is not available there.
$nids = !empty($form_state['input']['nids']) ? explode(',', $form_state['input']['nids']) : array();
}
$form = array();
$form['#action'] = url('admin/content/entity_share');
$form['endpoints'] = array(
'#type' => 'fieldset',
'#title' => t('Enpoints'),
);
$endpoints_options = array();
$endpoints = EntityShareUiClientEndpoint::loadAll(NULL, TRUE);
foreach ($endpoints as $endpoint) {
$endpoints_options[$endpoint['eid']] = $endpoint['title'];
}
if (count($endpoints_options) == 0) {
$endpoint_chkbx_title = t('No endpoint is available or enabled, create one or enable an existing endpoint.');
}
else {
$endpoint_chkbx_title = t('Select the @endpoint that will be targeted for the sharing process', array(
'@endpoint' => format_plural(count($endpoints_options), 'endpoint', 'endpoints'),
));
}
$form['endpoints']['endpoints'] = array(
'#type' => 'checkboxes',
'#title' => check_plain($endpoint_chkbx_title),
'#options' => $endpoints_options,
);
// Hidden.
$form['nids'] = array(
'#type' => 'hidden',
'#value' => implode(',', $nids),
);
// Actions.
if (count($endpoints_options) > 0) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Share contents'),
'#validate' => array(
'entity_share_ui_share_action_form_validate',
),
'#submit' => array(
'entity_share_ui_share_action_form_submit',
),
);
}
$form['cancel'] = array(
'#type' => 'link',
'#title' => t('Cancel'),
'#href' => url('admin/content', array(
'absolute' => TRUE,
)),
);
return $form;
}
/**
* Validate the submitted datas.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state.
*/
function entity_share_ui_share_action_form_validate(array $form, array &$form_state) {
if (empty($form_state['values']['nids'])) {
form_set_error('', t('No nid items selected.'));
}
$endpoints = array_filter($form_state['values']['endpoints']);
if (empty($endpoints)) {
form_set_error('', t('No endpoint was selected.'));
}
}
/**
* Action when the form is submitted.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state.
*/
function entity_share_ui_share_action_form_submit(array $form, array &$form_state) {
$nids = explode(',', $form_state['values']['nids']);
if (!is_array($nids)) {
$nids = explode(',', $form_state['values']['nids']);
}
$endpoints = array_values(array_filter($form_state['values']['endpoints']));
$operations = array();
foreach ($nids as $nid) {
foreach ($endpoints as $endpoint_id) {
$operations[] = array(
'entity_share_ui_share_node',
array(
$nid,
$endpoint_id,
),
);
}
}
$batch = array(
'title' => t('Processing Sharing Nodes'),
'operations' => $operations,
'progress_message' => t('Processed @percentage%'),
'error_message' => t('Sharing process has encountered an error.'),
'finished' => 'entity_share_ui_share_node_finished',
);
batch_set($batch);
batch_process('admin/content');
}
/**
* Batch operation to share a node.
*
* @param int $nid
* Node to share.
* @param int $endpoint_id
* Endpoint id to target.
* @param array $context
* Context of the batch.
*/
function entity_share_ui_share_node($nid, $endpoint_id, array &$context) {
// Load the node to export.
$node = node_load($nid);
if ($node) {
// Export the node.
$export = new EntityShareEntityExport(clone $node);
$exported_node = $export
->execute();
// Load the targeted endpoint.
$endpoint = EntityShareUiClientEndpoint::load((int) $endpoint_id);
if (!$endpoint['enabled']) {
_entity_share_ui_share_results_format($node, $endpoint, $context['results'], 'errors');
if ($endpoint['debug']) {
watchdog('es_ui_client', 'The endpoint "@endpoint_title" is disabled.', array(
'@endpoint_title' => $endpoint['title'],
), WATCHDOG_ALERT);
}
}
else {
// Import the node on the remote drupal instance.
$client = new EntityShareClient($endpoint['url'], $endpoint['debug']);
if ($client
->login($endpoint['login'], $endpoint['password'])) {
$response = $client
->create('node', drupal_json_encode($exported_node));
if ($response->code == '200') {
$result = drupal_json_decode($response->data);
if ($result['status'] == 'OK') {
_entity_share_ui_share_results_format($node, $endpoint, $context['results'], 'success');
}
else {
// Error.
_entity_share_ui_share_results_format($node, $endpoint, $context['results'], 'errors');
if ($endpoint['debug']) {
watchdog('es_ui_client', 'Error while calling the endpoint "@endpoint_title": <pre>@endpoint_error</pre>', array(
'@endpoint_title' => $endpoint['title'],
'@endpoint_error' => print_r($result, TRUE),
), WATCHDOG_ERROR);
}
}
}
else {
// Error.
_entity_share_ui_share_results_format($node, $endpoint, $context['results'], 'errors');
if ($endpoint['debug']) {
watchdog('es_ui_client', 'Error while calling the endpoint "@endpoint_title": <pre>@endpoint_error</pre>', array(
'@endpoint_title' => $endpoint['title'],
'@endpoint_error' => print_r($response, TRUE),
), WATCHDOG_ERROR);
}
}
}
else {
// Authentication error.
_entity_share_ui_share_results_format($node, $endpoint, $context['results'], 'errors');
if ($endpoint['debug']) {
watchdog('es_ui_client', 'Error while connecting to endpoint "@endpoint_title". Please check the url of the endpoint, the account you provided and that your IP is allowed on the entity share server.', array(
'@endpoint_title' => $endpoint['title'],
), WATCHDOG_ERROR);
}
}
}
// Update our progress information.
$context['sandbox']['current_node'] = $node->nid;
$context['sandbox']['current_endpoint'] = $endpoint['name'];
$context['message'] = t('Now processing %node on %endpoint', array(
'%node' => $node->title,
'%endpoint' => $endpoint['title'],
));
}
}
/**
* When the batch is finished, treat success and errors.
*
* @param int $success
* HTTP success or not of the operations.
* @param array $results
* Results of the operations.
* @param array $operations
* The operations remaining.
*/
function entity_share_ui_share_node_finished($success, array $results, array $operations) {
// HTTP success.
if ($success) {
$message = 'Sharing process completed ' . (!empty($results['errors']) ? 'with errors' : 'successfully');
// Treatment success.
if (!empty($results['success'])) {
$node_str = format_plural(count($results['success']['nodes']), '1 node', '@count nodes');
$endpoint_str = format_plural(count($results['success']['endpoints']), '1 endpoint', '@count endpoints');
$message .= ' (' . $node_str . ' processed on ' . $endpoint_str . ')';
$message .= theme('item_list', array(
'items' => $results['success']['items'],
));
drupal_set_message(filter_xss($message));
}
// Errors treatment.
if (!empty($results['errors'])) {
$node_str = format_plural(count($results['errors']['nodes']), '1 node error', '@count nodes errors');
$endpoint_str = format_plural(count($results['errors']['endpoints']), '1 endpoint', '@count endpoints');
$message .= ' (' . $node_str . ' on ' . $endpoint_str . ')';
$message .= theme('item_list', array(
'items' => $results['errors']['items'],
));
drupal_set_message(filter_xss($message), 'error');
}
}
else {
// A general error occurred (exception, fatal error...)
// $operations contains the operations that remained unprocessed.
$error_operation = reset($operations);
$message = t('An error occurred while processing %error_operation with arguments: @arguments', array(
'%error_operation' => $error_operation[0],
'@arguments' => print_r($error_operation[1], TRUE),
));
drupal_set_message(filter_xss($message), 'error');
}
}
/**
* Format the batch results.
*
* @param object $node
* The current node object to format results.
* @param array $endpoint
* The current endpoint.
* @param array $results
* The results array reference.
* @param string $type
* The type of the result (success, errors).
*/
function _entity_share_ui_share_results_format($node, array $endpoint, array &$results, $type = 'success') {
$type = $type != 'success' ? 'errors' : $type;
$results[$type]['nodes'][$node->nid] = check_plain($node->title);
$results[$type]['endpoints'][$endpoint['eid']] = check_plain($endpoint['title']);
$results[$type]['items'][$endpoint['eid']]['data'] = check_plain($endpoint['title']);
$results[$type]['items'][$endpoint['eid']]['children'][] = check_plain($node->title);
}
Functions
Name![]() |
Description |
---|---|
entity_share_ui_node_list | Node list table. |
entity_share_ui_share_action | Node operation or action callback. |
entity_share_ui_share_action_form | Form generation. |
entity_share_ui_share_action_form_submit | Action when the form is submitted. |
entity_share_ui_share_action_form_validate | Validate the submitted datas. |
entity_share_ui_share_node | Batch operation to share a node. |
entity_share_ui_share_node_finished | When the batch is finished, treat success and errors. |
entity_share_ui_share_page | Page builder. |
_entity_share_ui_share_results_format | Format the batch results. |