You are here

function ctools_export_object in Chaos Tool Suite (ctools) 7

Same name and namespace in other branches
  1. 6 includes/export.inc \ctools_export_object()

Export an object into code.

3 calls to ctools_export_object()
ctools_export_crud_export in includes/export.inc
Get the exported code of a single exportable object.
page_manager_export_task_handler in page_manager/page_manager.module
Export a task handler into code suitable for import or use as a default task handler.
page_manager_page_export in page_manager/plugins/tasks/page.inc
Export a page subtask.

File

includes/export.inc, line 905
Contains code to make it easier to have exportable objects.

Code

function ctools_export_object($table, $object, $indent = '', $identifier = NULL, $additions = array(), $additions2 = array()) {
  $schema = ctools_export_get_schema($table);
  if (!isset($identifier)) {
    $identifier = $schema['export']['identifier'];
  }
  $output = $indent . '$' . $identifier . ' = new ' . get_class($object) . "();\n";
  if ($schema['export']['can disable']) {
    $disabled = !isset($object->disabled) || $object->disabled != TRUE ? 'FALSE' : 'TRUE';
    $output .= $indent . '$' . $identifier . '->disabled = ' . $disabled . '; /* 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') {
        if (isset($info['size']) && $info['size'] == 'tiny') {
          $info['boolean'] = !isset($info['boolean']) ? $schema['export']['boolean'] : $info['boolean'];
          $value = $info['boolean'] ? (bool) $value : (int) $value;
        }
        else {
          $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";
  }
  return $output;
}