Skip to content

Instantly share code, notes, and snippets.

@pcambra
Created February 10, 2016 11:36
Show Gist options
  • Save pcambra/49a580968647049b4a8d to your computer and use it in GitHub Desktop.
Save pcambra/49a580968647049b4a8d to your computer and use it in GitHub Desktop.
function hr_decommissioner_drush_command() {
$items['hr-decommissioner-list-hr-contacts-panes'] = array(
'description' => "List the contact panes from all spaces",
'drupal dependencies' => array(),
'aliases' => array(),
);
return $items;
}
/**
* Callback to get the hr contacts panes in spaces.
*/
function drush_hr_decommissioner_list_hr_contacts_panes() {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', array('hr_operation', 'hr_bundle', 'hr_disaster', 'hr_sector', 'hr_space'), 'IN');
$result = $query->execute();
if (empty($result['node'])) {
drush_log('No spaces found', 'ok');
return;
}
$batch = array(
'operations' => array(
array(
'drush_hr_decommissioner_list_hr_contacts_panes_callback',
array(
array_keys($result['node']),
),
),
),
'finished' => 'drush_hr_decommissioner_finished',
'title' => t('Calculating'),
'init_message' => t('Preparing calculation...'),
'progress_message' => t('Calculating...'),
'error_message' => t('Process resulted in an error.'),
);
batch_set($batch);
drush_backend_batch_process();
}
/**
* Generates a listing of the panels.
*/
function drush_hr_decommissioner_list_hr_contacts_panes_callback($nids, &$context) {
$limit = 500;
$context['finished'] = 0;
if (!isset($context['sandbox']['file'])) {
$headers = array(
'Id',
'Title',
'Url',
'Position'
);
// Create the file and print the labels in the header row.
$file_path = file_directory_temp() . '/hr-contacts-panes-report.csv';
$handle = fopen($file_path, 'w');
fputcsv($handle, $headers);
fclose($handle);
$context['sandbox']['nids'] = $nids;
$context['sandbox']['total_nids'] = count($nids);
$context['sandbox']['file'] = $file_path;
$context['results']['count'] = 0;
}
$handle = fopen($context['sandbox']['file'], 'a');
if ($contacts_pending = count($context['sandbox']['nids'])) {
$actual_limit = min($nids, $limit);
$current_nids = array_slice(
$context['sandbox']['nids'],
$context['results']['count'],
$actual_limit,
TRUE
);
// Load all the nodes for the iteration.
$nodes = node_load_multiple($current_nids);
foreach ($nodes as $node) {
if (!empty($node->panelizer)) {
foreach ($node->panelizer as $panel) {
foreach ($panel->display->content as $item) {
if ($item->type == 'hr_contacts_key_contacts') {
$row = array();
$row[] = $node->nid;
$row[] = $node->title;
$row[] = 'https://www.humanitarianresponse.info' . url('node/' . $node->nid);
$row[] = $item->panel;
fputcsv($handle, $row);
}
}
}
}
}
$context['results']['count'] += $actual_limit;
$context['finished'] = $context['results']['count'] / $context['sandbox']['total_nids'];
}
else {
$context['finished'] = 1;
}
fclose($handle);
$context['message'] = t(
'Processed @count of @total nodes.',
array(
'@count' => $context['results']['count'],
'@total' => $context['sandbox']['total_nids'],
)
);
/**
* Batch finished callback.
*/
function drush_hr_decommissioner_finished($success, $results, $operations) {
drush_log('Process finished');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment