You are here

function clients_export_object in Web Service Clients 6.2

Same name and namespace in other branches
  1. 7.2 clients.module \clients_export_object()

Export an object into code.

This is our alternative to ctools_export_object(). We need to follow the same pattern in code as in loading from the database and from CTools: load the data into a plain object, then pass that to the constructor of the desired class. This is because classes' constructors may do different things and need the data first to do them with.

In the interests of sanity, places where this function deviates from the model are marked.

1 string reference to 'clients_export_object'
clients_schema in ./clients.install
Implementation of hook_schema

File

./clients.module, line 461
Clients module provides a UI, storage, and an API for handling connections to remote webservices, including those provided by Services module on other Drupal sites.

Code

function clients_export_object($object, $indent = '', $connection_identifier = NULL, $additions = array(), $additions2 = array()) {

  // Change: We need to specify this ourselves.
  $table = 'clients_connections';

  // Change: In the interest of not deviating too much, we just repurpose
  // $identifier to 'object' and hence change the name of the variable for the
  // actual export, since we only use it once.
  $schema = ctools_export_get_schema($table);
  if (!isset($connection_identifier)) {
    $connection_identifier = $schema['export']['identifier'];
  }

  // Change: Just create a stdClass object for now.
  $identifier = 'object';
  $output = $indent . '$' . $identifier . " = new stdClass;\n";
  if ($schema['export']['can disable']) {
    $output .= $indent . '$' . $identifier . '->disabled = FALSE; /* Edit this to true to make a default ' . $identifier . ' disabled initially */' . "\n";
  }
  if (!empty($schema['export']['api']['current_version'])) {
    $output .= $indent . '$' . $identifier . '->api_version = ' . $schema['export']['api']['current_version'] . ";\n";
  }

  // Put top additions here:
  foreach ($additions as $field => $value) {
    $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n";
  }
  $fields = $schema['fields'];
  if (!empty($schema['join'])) {
    foreach ($schema['join'] as $join) {
      if (!empty($join['load'])) {
        foreach ($join['load'] as $join_field) {
          $fields[$join_field] = $join['fields'][$join_field];
        }
      }
    }
  }

  // Go through our schema and joined tables and build correlations.
  foreach ($fields as $field => $info) {
    if (!empty($info['no export'])) {
      continue;
    }
    if (!isset($object->{$field})) {
      if (isset($info['default'])) {
        $object->{$field} = $info['default'];
      }
      else {
        $object->{$field} = '';
      }
    }

    // Note: This is the *field* export callback, not the table one!
    if (!empty($info['export callback']) && function_exists($info['export callback'])) {
      $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . $info['export callback']($object, $field, $object->{$field}, $indent) . ";\n";
    }
    else {
      $value = $object->{$field};
      if ($info['type'] == 'int') {
        $value = isset($info['size']) && $info['size'] == 'tiny' ? (bool) $value : (int) $value;
      }
      $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n";
    }
  }

  // And bottom additions here
  foreach ($additions2 as $field => $value) {
    $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n";
  }

  // Change: Add the actual connection object.
  $output .= $indent . '$' . $connection_identifier . ' = new ' . get_class($object) . '($' . $identifier . ')' . ";\n";
  return $output;
}