You are here

function _ctools_drush_export_op_command_logic in Chaos Tool Suite (ctools) 7

Helper function to abstract logic for selecting exportable types/objects from individual commands as they will all share this same error handling/arguments for returning list of exportables.

Parameters

$table_name:

$object_names:

Return value

Array of exportable objects (filtered if necessary, by name etc..) or FALSE if not.

1 call to _ctools_drush_export_op_command_logic()
ctools_drush_export_op_command in drush/ctools.drush.inc
Drush callback: Acts as the hub for all op commands to keep all arg handling etc in one place.

File

drush/ctools.drush.inc, line 547
CTools Drush commands.

Code

function _ctools_drush_export_op_command_logic($op = '', $table_name = NULL, array $object_names = array()) {
  if (!$table_name) {
    drush_log(dt('Exportable table name empty. Use the --all command if you want to perform this operation on all tables.'), 'error');
    return FALSE;
  }

  // Get export info based on table name.
  $info = _ctools_drush_export_info(array(
    $table_name,
  ), TRUE);
  if (!isset($info['exportables'][$table_name])) {
    drush_log(dt('Exportable table name not found.'), 'error');
    return FALSE;
  }
  $exportables =& $info['exportables'];
  if (empty($object_names)) {
    $all = drush_confirm(dt('No object names entered. Would you like to try and !op all exportables of type !type', array(
      '!op' => _ctools_drush_export_op_alias($op),
      '!type' => $table_name,
    )));
    if (!$all) {
      drush_log(dt('Command cancelled'), 'success');
      return FALSE;
    }
  }
  else {

    // Iterate through object names and check they exist in exportables array.
    // Log error and unset them if they don't.
    foreach ($object_names as $object_name) {
      if (!isset($exportables[$table_name][$object_name])) {
        drush_log(dt('Invalid exportable: !exportable', array(
          '!exportable' => $object_name,
        )), 'error');
        unset($object_names[$table_name][$object_name]);
      }
    }

    // Iterate through exportables to get just a list of selected ones.
    foreach (array_keys($exportables[$table_name]) as $exportable) {
      if (!in_array($exportable, $object_names)) {
        unset($exportables[$table_name][$exportable]);
      }
    }
  }
  $export_module = drush_get_option('module', FALSE);
  if (is_string($export_module)) {
    $exportables = _ctools_drush_export_module_filter($exportables, $export_module);
  }
  return $exportables[$table_name];
}