<?php
// $Id: content_copy_service.module,v 1.1.2.2 2008/06/05 04:17:05 heyrocker Exp $

include_once ('./'. drupal_get_path('module', 'node'    ) .'/content_types.inc');
include_once ('./'. drupal_get_path('module', 'content') .'/content_admin.inc');

/**
 * Implementation of hook_help().
 */
function content_copy_service_help($section) {
  switch ($section) {
    case 'admin/help#services_content_copy':
      return t('<p>Provides content_copy services. Requires services.module and content_copy.module.</p>');
    case 'admin/modules#description':
      return t('Provides content_copy services. Requires services.module and content_copy.module.');
  }
}

/**
 * Implementation of hook_service()
 */
function content_copy_service_service() {
  return array(
    
    // content_copy.import
    array(
      '#method'   => 'content_copy.import',
      '#callback' => 'content_copy_service_import',
      '#args'     => array(
        array(
          '#name'         => 'type_name',
          '#type'         => 'string',
          '#description'  => t('Content type name to import fields into (or "create" to create new).')),
        array(
          '#name'         => 'export',
          '#type'         => 'text',
          '#description'  => t('Text of a content copy export'))),
      '#return'   => 'boolean',
      '#help'     => t('Import a content type macro')),
      
    // content_copy.export
    array(
      '#method'   => 'content_copy.export',
      '#callback' => 'content_copy_service_export',
      '#args'     => array(
        array(
          '#name'         => 'type_name',
          '#type'         => 'string',
          '#description'  => t('The content type to export'))),
      '#return'   => 'string',
      '#help'     => t('Export a content type macro')),
  );
}

/**
 * Content copy import service callback
 */
function content_copy_service_import($type_name, $export) {

  // if the type does not already exist, then we assume that
  // we are creating a new content type
  if (!array_key_exists($type_name, content_copy_types())) {
    $type_name = '<create>';
  }

  $values = array('type_name' => $type_name, 'macro' => $export, 'op' => 'Submit');
  drupal_execute('content_copy_import_form', $values);
  
  if ($errors = form_get_errors()) {
    foreach ($errors as $error) {
      $msg .= "$error ";
    }
    return services_error("Content Copy Import Error: $msg");
    watchdog('deploy', $msg);
  }
  else {
    watchdog("services", "Content Copy Import Service run.");
  }

  return TRUE;
}

/**
 * Content copy export service callback
 */
function content_copy_service_export($type_name) {
  if (!array_key_exists($type_name, node_get_types())) {
    return services_error("Content type $type_name does not exist");
  }
  
  $groups = array();
  if (module_exists('fieldgroup')) {
    $groups = content_copy_groups($type_name);
  }
 
  $fields = content_copy_fields($type_name);
  $fields = array_keys($fields);
  
  $values = array(
    'type_name' => $type_name,
    'groups' => $groups,
    'fields' => $fields
  );
  
  $retval = content_copy_export('content_copy_export_form', $values);
  
  watchdog("services", "Content Copy Export Service run for content type $type_name.");

  return $retval;
}
