| 1 |
<?php
|
| 2 |
// $Id: mm_custom_command.module,v 1.1 2008/12/14 11:10:59 anantagati Exp $
|
| 3 |
|
| 4 |
define(MM_CUSTOM_COMMAND_PROCESS, 1);
|
| 5 |
define(MM_CUSTOM_COMMAND_STORAGE, 2);
|
| 6 |
define(MM_CUSTOM_COMMAND_COMPLETE, 3);
|
| 7 |
|
| 8 |
/**
|
| 9 |
* @file
|
| 10 |
* Module allow user to define custom command for Media Mover
|
| 11 |
*/
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Implementation of media_mover hook
|
| 15 |
*/
|
| 16 |
function mm_custom_command_media_mover($op, $action = NULL, $configuration = NULL, &$file = array(), $running_config = NULL) {
|
| 17 |
switch ($op) {
|
| 18 |
|
| 19 |
// give your module a distinct name
|
| 20 |
case 'name':
|
| 21 |
return "Custom Command";
|
| 22 |
break;
|
| 23 |
|
| 24 |
case 'actions':
|
| 25 |
return array(
|
| 26 |
'process' => array(
|
| 27 |
MM_CUSTOM_COMMAND_PROCESS => t('Use custom command'),
|
| 28 |
),
|
| 29 |
'storage' => array(
|
| 30 |
MM_CUSTOM_COMMAND_STORAGE => t('Use custom command'),
|
| 31 |
),
|
| 32 |
'complete' => array(
|
| 33 |
MM_CUSTOM_COMMAND_COMPLETE => t('Use custom command'),
|
| 34 |
),
|
| 35 |
);
|
| 36 |
break;
|
| 37 |
|
| 38 |
case 'config':
|
| 39 |
switch ($action) {
|
| 40 |
case MM_CUSTOM_COMMAND_PROCESS:
|
| 41 |
return mm_custom_command_config($action, $configuration, 'process');
|
| 42 |
break;
|
| 43 |
case MM_CUSTOM_COMMAND_STORAGE:
|
| 44 |
return mm_custom_command_config($action, $configuration, 'storage');
|
| 45 |
break;
|
| 46 |
case MM_CUSTOM_COMMAND_COMPLETE:
|
| 47 |
return mm_custom_command_config($action, $configuration, 'complete');
|
| 48 |
break;
|
| 49 |
}
|
| 50 |
break;
|
| 51 |
|
| 52 |
// functions called on process
|
| 53 |
case 'process':
|
| 54 |
switch ($action) {
|
| 55 |
case MM_CUSTOM_COMMAND_PROCESS:
|
| 56 |
return mm_custom_command_action($action, $configuration, $file, 'process');
|
| 57 |
break;
|
| 58 |
}
|
| 59 |
break;
|
| 60 |
|
| 61 |
// functions called on storage
|
| 62 |
case 'storage':
|
| 63 |
switch ($action) {
|
| 64 |
case MM_CUSTOM_COMMAND_STORAGE:
|
| 65 |
return mm_custom_command_action($action, $configuration, $file, 'storage');
|
| 66 |
break;
|
| 67 |
}
|
| 68 |
break;
|
| 69 |
|
| 70 |
// functions called on complete
|
| 71 |
case 'complete':
|
| 72 |
switch ($action) {
|
| 73 |
case MM_CUSTOM_COMMAND_COMPLETE:
|
| 74 |
return mm_custom_command_action($action, $configuration, $file, 'complete');
|
| 75 |
break;
|
| 76 |
}
|
| 77 |
break;
|
| 78 |
|
| 79 |
}
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
* Configuration for $verb action
|
| 84 |
*/
|
| 85 |
function mm_custom_command_config($action, $configuration, $verb) {
|
| 86 |
// set title for current action
|
| 87 |
$title = array(
|
| 88 |
'process' => t('Process Configuration'),
|
| 89 |
'storage' => t('Storage Configuration'),
|
| 90 |
'complete' => t('Complete Configuration'),
|
| 91 |
);
|
| 92 |
|
| 93 |
$form = array();
|
| 94 |
$form['mm_'. $verb .'_conf'] = array(
|
| 95 |
'#type' => 'fieldset',
|
| 96 |
'#title' => $title[$verb],
|
| 97 |
'#description' => t('This module runs custom command'),
|
| 98 |
);
|
| 99 |
|
| 100 |
$form['mm_'. $verb .'_conf']['output_file'] = array(
|
| 101 |
'#type' => 'textfield',
|
| 102 |
'#title' => t('Output file'),
|
| 103 |
'#description' => t('Define output file.'),
|
| 104 |
'#default_value' => $configuration['output_file'],
|
| 105 |
);
|
| 106 |
$form['mm_'. $verb .'_conf']['command'] = array(
|
| 107 |
'#type' => 'textfield',
|
| 108 |
'#title' => t('Command'),
|
| 109 |
'#description' => t("Define command for this action. If you want to use 'Output file' value from previous field use [output_file]."),
|
| 110 |
'#default_value' => $configuration['command'],
|
| 111 |
);
|
| 112 |
if (module_exists('token')) {
|
| 113 |
$form['mm_'. $verb .'_conf']['token_help'] = array(
|
| 114 |
'#title' => t('Replacement patterns'),
|
| 115 |
'#type' => 'fieldset',
|
| 116 |
'#collapsible' => TRUE,
|
| 117 |
'#collapsed' => TRUE,
|
| 118 |
);
|
| 119 |
$form['mm_'. $verb .'_conf']['token_help']['help'] = array(
|
| 120 |
'#value' => theme('token_help', 'mm_file'),
|
| 121 |
);
|
| 122 |
}
|
| 123 |
|
| 124 |
return $form;
|
| 125 |
}
|
| 126 |
|
| 127 |
/**
|
| 128 |
* Run custom command
|
| 129 |
*/
|
| 130 |
function mm_custom_command_action($action, $configuration, $file, $verb) {
|
| 131 |
// replace tokens
|
| 132 |
$output_file = token_replace($configuration['output_file'], 'mm_file', $file);
|
| 133 |
$command = token_replace($configuration['command'], 'mm_file', $file);
|
| 134 |
|
| 135 |
// change [output_file] to value from $output_file
|
| 136 |
$command = str_replace('[output_file]', $output_file, $command);
|
| 137 |
|
| 138 |
ob_start();
|
| 139 |
passthru($command ." 2>&1", $command_return);
|
| 140 |
$command_output = ob_get_contents();
|
| 141 |
ob_end_clean();
|
| 142 |
|
| 143 |
return $output_file;
|
| 144 |
}
|