Monday, September 22, 2014

Drupal batch process for custom function callback

Drush is a very powerful command line tool to manage Drupal site.

Other than core Drush command, we can also write our own custom drush command to perform some task like cron setup, etc, ...

we can do it by using below 3 steps.

Steps :
1) Write hook_drush_help.
2) Write hook_drush_command.
3) Write drush_hook_command.

1) hook_drush_help.
/**
 * Implements hook_drush_help().
 */
function my_module_drush_help($command) {
  switch ($command) {
    case 'drush:test-import':
     return dt('Import test contents.');
  }
}

2) hook_drush_command.
/**
 * Implements hook_drush_command().
 */
function my_module_drush_command() {
  $items = array();
  $items['test-import'] = array(
    'description' => dt('Import test contents.'),
    'arguments'   => array(
      'arg1'    => dt('An optional example argument'),
    ),
    'examples' => array(
      'Standard example' => 'drush test-import',
      'Argument example' => 'drush test-import 5',
    ),
    'aliases' => array('ti'),
  );
  return $items;
}

3) drush_hook_command().
/**
 * Implements drush_hook_command().
 */
function drush_my_module_test_import($arg = NULL) {
  batch_custom_function();
}

4) batch_custom_function().
/**
 * Batch start function. In this example, I'm downloading XML file.
 */
function batch_custom_function() {
  $batch = array(
    'title' => t('Download XML file.'),
    'operations' => array(
      array('_batch_xml_download', array()),
    ),
    'progress_message' => t('Downloading XML file ...'),
    'error_message' => t('Error! while downloading XML file ...'),
    'finished' => '_batch_download_finished',
  );
  //Get the batch process all ready!
  batch_set($batch);
  $batch = &batch_get();

  //Because we are doing this on the back-end, we set progressive to false.
  $batch['progressive'] = FALSE;

  //Start processing the batch operations.
  drush_backend_batch_process();
}

5) _batch_xml_download callback.
/**
 * XML download.
 */
function _batch_xml_download(&$context) {
  $xml_read_return = '';
  $context['message'] = 'Downloading XML file ...';
  $xml_read_return = custom_callback_to_download_xml();

  if($xml_read_return == 200) {
    drush_log('XML file downloaded and saved successfully.', 'ok');
  }
  elseif (!empty($xml_read_return)) {
    drush_log('Unable to download XML file. (' . $xml_read_return . ')', 'error');
  }
}

6) _batch_download_finished callback.
/**
 * Finish of butch. Messagess
 */
function _batch_download_finished($success, $results, $operations) {
  if ($success) {
    drupal_set_message(t('Your success msg.'));
  }
  else {
    drupal_set_message(t('An error occurred while processing operation.'));
  }
}

Friday, September 19, 2014

PHP create XML file

Pragmatically create XML using PHP.

  $xml = "<company>";
  $xml .= "<employee>";
  $xml .= "<id>100</id>";  
  $xml .= "<name>name</name>";
  $xml .= "<email>email</email>";  
  $xml .= "<salary>salary</salary>";    
  $xml .= "</employee>";  
  $xml .= "</company>";
  $xml_obj = new SimpleXMLElement($xml);
  $xml_obj->asXML('public://myfile.xml'); // Drupal file path.