You are here

function entity_share_ui_share_node in Entity Share 7

Batch operation to share a node.

Parameters

int $nid: Node to share.

int $endpoint_id: Endpoint id to target.

array $context: Context of the batch.

1 string reference to 'entity_share_ui_share_node'
entity_share_ui_share_action_form_submit in modules/entity_share_ui/modules/entity_share_ui_client/entity_share_ui_client.share.admin.inc
Action when the form is submitted.

File

modules/entity_share_ui/modules/entity_share_ui_client/entity_share_ui_client.share.admin.inc, line 241
Entity Share UI Client Admin Share file.

Code

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'],
    ));
  }
}