Index: workflow_ng_workflow_ng.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/workflow_ng/workflow_ng/modules/Attic/workflow_ng_workflow_ng.inc,v retrieving revision 1.1.2.2 diff -u -r1.1.2.2 workflow_ng_workflow_ng.inc --- workflow_ng_workflow_ng.inc 4 Dec 2007 19:40:42 -0000 1.1.2.2 +++ workflow_ng_workflow_ng.inc 11 Dec 2007 17:55:00 -0000 @@ -51,3 +51,125 @@ $configurations['config1'] = workflow_ng_configure($configurations['config1'], $conditions, $action); return $configurations; } + +function workflow_ng_action_info() { + return array( + 'workflow_ng_action_custom_php' => array( + '#label' => t('Execute custom PHP code'), + '#arguments' => array(), + '#module' => t('Token'), + ), + ); +} + +function workflow_ng_condition_info() { + return array( + 'workflow_ng_condition_custom_php' => array( + '#label' => t('Execute custom PHP code'), + '#arguments' => array(), + '#module' => t('Token'), + ), + ); +} + +/* + * Returns an array of all used PHP arguments in $code + * + * @param $code The $code, which will be processed + * @param $argument_info The argument definitions we work with + * @return The array of names of the used arguments + */ +function workflow_ng_custom_php_get_used_php_arguments($code, $argument_info) { + $used_args = array(); + foreach ($argument_info as $name => $argument) { + if (strpos($code, '$'. $name) !== FALSE) { + $used_args[] = $name; + } + } + return $used_args; +} + +function workflow_ng_custom_php_prepend_php_arguments(&$code, $used_arguments, &$arguments, &$log) { + $argument_data = array(); + if (is_array($used_arguments)) { + foreach($used_arguments as $name) { + $data = workflow_ng_element_get_argument($arguments, $name, $log); + $argument_data[$name] = $data; + $code = '$'. $name .' = $argument_data["'. $name .'"]; '. $code; + } + } + return $argument_data; +} + +function workflow_ng_custom_php_eval($code, $argument_data = NULL) { + echo $code; + ob_start(); + print eval('?>'. $code); + $output = ob_get_contents(); + ob_end_clean(); + return $output; +} + +function workflow_ng_custom_php_eval_normal_output($code, $argument_data = NULL) { + return eval('?>'. $code); +} + +function workflow_ng_action_custom_php($settings, &$arguments, &$log) { + $php = workflow_ng_token_replace($settings['php'], $settings['used_arguments'], $arguments, $log); + $argument_data = workflow_ng_custom_php_prepend_php_arguments($php, $settings['used_php_arguments'], $arguments, $log); + $output = workflow_ng_custom_php_eval('', $argument_data); + drupal_set_message($output); + return $argument_data; +} + +function workflow_ng_condition_custom_php($settings, &$arguments, &$log) { + $php = workflow_ng_token_replace($settings['php'], $settings['used_arguments'], $arguments, $log); + if (strpos($php, 'return ') === FALSE) { + $php = 'return '. $php; + } + $argument_data = workflow_ng_custom_php_prepend_php_arguments($php, $settings['used_php_arguments'], $arguments, $log); + $php = ''; + return workflow_ng_custom_php_eval_normal_output($php, $argument_data); +} + +function workflow_ng_action_custom_php_form($settings = array(), $argument_info) { + $form = array(); + $form['php'] = array( + '#type' => 'textarea', + '#rows' => 12, + '#title' => t('PHP code'), + '#default_value' => $settings['php'], + '#description' => t('The code that should be executed. You can use Tokens as variable values (e.g.: print "hello [user:user]").'), + ); + workflow_ng_token_replacement_help($form, $argument_info); + return $form; +} + +function workflow_ng_condition_custom_php_form($settings = array(), $argument_info) { + $form = array(); + $form['php'] = array( + '#type' => 'textarea', + '#rows' => 12, + '#title' => t('PHP code'), + '#default_value' => $settings['php'], + '#description' => t('The code that should be executed. You can use Tokens as variable values (e.g.: [node:nid] < 3 || [node:nid] > 5). For conditions with multiple PHP statements, you must explicitly use "return".'), + ); + workflow_ng_token_replacement_help($form, $argument_info); + return $form; +} + +function workflow_ng_action_custom_php_submit($form_id, $form_values) { + return array( + 'php' => $form_values['php'], + 'used_arguments' => workflow_ng_token_get_used_arguments($form_values['php'], $form_values['arguments']), + 'used_php_arguments' => workflow_ng_custom_php_get_used_php_arguments($form_values['php'], $form_values['arguments']), + ); +} + +function workflow_ng_condition_custom_php_submit($form_id, $form_values) { + return array( + 'php' => $form_values['php'], + 'used_arguments' => workflow_ng_token_get_used_arguments($form_values['php'], $form_values['arguments']), + 'used_php_arguments' => workflow_ng_custom_php_get_used_php_arguments($form_values['php'], $form_values['arguments']), + ); +}