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