script.action.inc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. function views_bulk_operations_script_action_info() {
  3. $actions = array();
  4. $actions['views_bulk_operations_script_action'] = array(
  5. 'type' => 'entity',
  6. 'label' => t('Execute arbitrary PHP script'),
  7. 'configurable' => TRUE,
  8. 'triggers' => array('any'),
  9. );
  10. // Provide a strict default permission if actions_permissions is disabled.
  11. if (!module_exists('actions_permissions')) {
  12. $actions['views_bulk_operations_script_action']['permissions'] = array('administer site configuration');
  13. }
  14. return $actions;
  15. }
  16. function views_bulk_operations_script_action($entity, $context) {
  17. $return = eval($context['script']);
  18. if ($return === FALSE) {
  19. $msg = 'Error in script.';
  20. $arg = array();
  21. $error = error_get_last();
  22. if ($error) {
  23. $msg = '!err in script: !msg in line \'%line\'.';
  24. $arg = array(
  25. '!msg' => $error['message'],
  26. '%line' => _views_bulk_operations_script_action_error_line($context['script'], $error['line']),
  27. '!err' => _views_bulk_operations_script_action_error_type($error['type']),
  28. );
  29. }
  30. drupal_set_message(t($msg, $arg), 'error', FALSE);
  31. watchdog('actions', $msg, $arg, WATCHDOG_ERROR);
  32. }
  33. }
  34. function views_bulk_operations_script_action_form($context) {
  35. $form['script'] = array(
  36. '#type' => 'textarea',
  37. '#title' => t('PHP script'),
  38. '#description' => t('Type the PHP snippet that will run upon execution of this action. You can use variables <code>$entity</code> and <code>$context</code> in your snippet.
  39. Note that it is up to the script to save the $entity once it\'s done modifying it.'),
  40. '#default_value' => @$context['script'],
  41. );
  42. return $form;
  43. }
  44. function views_bulk_operations_script_action_validate($form, $form_state) {
  45. }
  46. function views_bulk_operations_script_action_submit($form, $form_state) {
  47. return array(
  48. 'script' => $form_state['values']['script'],
  49. );
  50. }
  51. function _views_bulk_operations_script_action_error_line($script, $line) {
  52. $lines = preg_split("/(\r?\n)/", $script);
  53. if (isset($lines[$line-1])) {
  54. return $lines[$line-1];
  55. }
  56. else {
  57. return t('Line !line', array('!line' => $line));
  58. }
  59. }
  60. function _views_bulk_operations_script_action_error_type($type) {
  61. $types = array(
  62. E_ERROR => 'Error',
  63. E_WARNING => 'Warning',
  64. E_PARSE => 'Parsing Error',
  65. E_NOTICE => 'Notice',
  66. E_CORE_ERROR => 'Core Error',
  67. E_CORE_WARNING => 'Core Warning',
  68. E_COMPILE_ERROR => 'Compile Error',
  69. E_COMPILE_WARNING => 'Compile Warning',
  70. E_USER_ERROR => 'User Error',
  71. E_USER_WARNING => 'User Warning',
  72. E_USER_NOTICE => 'User Notice',
  73. E_STRICT => 'Runtime Notice',
  74. E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
  75. );
  76. if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
  77. $types += array(
  78. E_DEPRECATED => 'Deprecated Notice',
  79. E_USER_DEPRECATED => 'User Deprecated Notice',
  80. );
  81. }
  82. return t($types[$type]);
  83. }