root / drupal7 / sites / all / modules / views_bulk_operations / plugins / operation_types / action.class.php @ 388c412d
1 | 85ad3d82 | Assos Assos | <?php
|
---|---|---|---|
2 | |||
3 | /**
|
||
4 | * @file
|
||
5 | * Defines the class for core actions.
|
||
6 | * Belongs to the "action" operation type plugin.
|
||
7 | */
|
||
8 | |||
9 | class ViewsBulkOperationsAction extends ViewsBulkOperationsBaseOperation { |
||
10 | |||
11 | /**
|
||
12 | * Contains the options provided by the user in the configuration form.
|
||
13 | *
|
||
14 | * @var array
|
||
15 | */
|
||
16 | public $formOptions = array(); |
||
17 | |||
18 | /**
|
||
19 | * Returns the access bitmask for the operation, used for entity access checks.
|
||
20 | */
|
||
21 | public function getAccessMask() { |
||
22 | // Assume edit by default.
|
||
23 | 9df8b457 | Assos Assos | if (empty($this->operationInfo['behavior'])) { |
24 | 85ad3d82 | Assos Assos | $this->operationInfo['behavior'] = array('changes_property'); |
25 | } |
||
26 | |||
27 | $mask = 0; |
||
28 | if (in_array('views_property', $this->operationInfo['behavior'])) { |
||
29 | $mask |= VBO_ACCESS_OP_VIEW; |
||
30 | } |
||
31 | if (in_array('changes_property', $this->operationInfo['behavior'])) { |
||
32 | $mask |= VBO_ACCESS_OP_UPDATE; |
||
33 | } |
||
34 | if (in_array('creates_property', $this->operationInfo['behavior'])) { |
||
35 | $mask |= VBO_ACCESS_OP_CREATE; |
||
36 | } |
||
37 | if (in_array('deletes_property', $this->operationInfo['behavior'])) { |
||
38 | $mask |= VBO_ACCESS_OP_DELETE; |
||
39 | } |
||
40 | return $mask; |
||
41 | } |
||
42 | |||
43 | /**
|
||
44 | * Returns whether the provided account has access to execute the operation.
|
||
45 | *
|
||
46 | * @param $account
|
||
47 | */
|
||
48 | public function access($account) { |
||
49 | // Use actions_permissions if enabled.
|
||
50 | if (module_exists('actions_permissions')) { |
||
51 | $perm = actions_permissions_get_perm($this->operationInfo['label'], $this->operationInfo['key']); |
||
52 | if (!user_access($perm, $account)) { |
||
53 | return FALSE; |
||
54 | } |
||
55 | } |
||
56 | // Check against additional permissions.
|
||
57 | if (!empty($this->operationInfo['permissions'])) { |
||
58 | foreach ($this->operationInfo['permissions'] as $perm) { |
||
59 | if (!user_access($perm, $account)) { |
||
60 | return FALSE; |
||
61 | } |
||
62 | } |
||
63 | } |
||
64 | // Access granted.
|
||
65 | return TRUE; |
||
66 | } |
||
67 | |||
68 | /**
|
||
69 | * Returns the configuration form for the operation.
|
||
70 | * Only called if the operation is declared as configurable.
|
||
71 | *
|
||
72 | * @param $form
|
||
73 | * The views form.
|
||
74 | * @param $form_state
|
||
75 | * An array containing the current state of the form.
|
||
76 | * @param $context
|
||
77 | * An array of related data provided by the caller.
|
||
78 | */
|
||
79 | public function form($form, &$form_state, array $context) { |
||
80 | // Some modules (including this one) place their action callbacks
|
||
81 | // into separate files. At this point those files might no longer be
|
||
82 | // included due to an #ajax rebuild, so we call actions_list() to trigger
|
||
83 | // inclusion. The same thing is done by actions_do() on execute.
|
||
84 | actions_list(); |
||
85 | |||
86 | $context['settings'] = $this->getAdminOption('settings', array()); |
||
87 | $form_callback = $this->operationInfo['callback'] . '_form'; |
||
88 | return $form_callback($context, $form_state); |
||
89 | } |
||
90 | |||
91 | /**
|
||
92 | * Validates the configuration form.
|
||
93 | * Only called if the operation is declared as configurable.
|
||
94 | *
|
||
95 | * @param $form
|
||
96 | * The views form.
|
||
97 | * @param $form_state
|
||
98 | * An array containing the current state of the form.
|
||
99 | */
|
||
100 | public function formValidate($form, &$form_state) { |
||
101 | // Some modules (including this one) place their action callbacks
|
||
102 | // into separate files. At this point those files might no longer be
|
||
103 | // included due to a page reload, so we call actions_list() to trigger
|
||
104 | // inclusion. The same thing is done by actions_do() on execute.
|
||
105 | actions_list(); |
||
106 | |||
107 | $validation_callback = $this->operationInfo['callback'] . '_validate'; |
||
108 | if (function_exists($validation_callback)) { |
||
109 | $validation_callback($form, $form_state); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | /**
|
||
114 | * Handles the submitted configuration form.
|
||
115 | * This is where the operation can transform and store the submitted data.
|
||
116 | * Only called if the operation is declared as configurable.
|
||
117 | *
|
||
118 | * @param $form
|
||
119 | * The views form.
|
||
120 | * @param $form_state
|
||
121 | * An array containing the current state of the form.
|
||
122 | */
|
||
123 | public function formSubmit($form, &$form_state) { |
||
124 | // Some modules (including this one) place their action callbacks
|
||
125 | // into separate files. At this point those files might no longer be
|
||
126 | // included due to a page reload, so we call actions_list() to trigger
|
||
127 | // inclusion. The same thing is done by actions_do() on execute.
|
||
128 | actions_list(); |
||
129 | |||
130 | $submit_callback = $this->operationInfo['callback'] . '_submit'; |
||
131 | $this->formOptions = $submit_callback($form, $form_state); |
||
132 | } |
||
133 | |||
134 | /**
|
||
135 | * Returns the admin options form for the operation.
|
||
136 | *
|
||
137 | * The admin options form is embedded into the VBO field settings and used
|
||
138 | * to configure operation behavior. The options can later be fetched
|
||
139 | * through the getAdminOption() method.
|
||
140 | *
|
||
141 | * @param $dom_id
|
||
142 | * The dom path to the level where the admin options form is embedded.
|
||
143 | * Needed for #dependency.
|
||
144 | * @param $field_handler
|
||
145 | * The Views field handler object for the VBO field.
|
||
146 | */
|
||
147 | public function adminOptionsForm($dom_id, $field_handler) { |
||
148 | $form = parent::adminOptionsForm($dom_id, $field_handler); |
||
149 | |||
150 | $settings_form_callback = $this->operationInfo['callback'] . '_views_bulk_operations_form'; |
||
151 | if (function_exists($settings_form_callback)) { |
||
152 | $settings = $this->getAdminOption('settings', array()); |
||
153 | |||
154 | $form['settings'] = array( |
||
155 | '#type' => 'fieldset', |
||
156 | '#title' => t('Operation settings'), |
||
157 | '#collapsible' => TRUE, |
||
158 | '#dependency' => array( |
||
159 | $dom_id . '-selected' => array(1), |
||
160 | ), |
||
161 | ); |
||
162 | $settings_dom_id = $dom_id . '-settings'; |
||
163 | $form['settings'] += $settings_form_callback($settings, $this->entityType, $settings_dom_id); |
||
164 | } |
||
165 | |||
166 | return $form; |
||
167 | } |
||
168 | |||
169 | /**
|
||
170 | * Validates the admin options form.
|
||
171 | *
|
||
172 | * @param $form
|
||
173 | * The admin options form.
|
||
174 | * @param $form_state
|
||
175 | * An array containing the current state of the form. Note that this array
|
||
176 | * is constructed by the VBO views field handler, so it's not a real form
|
||
177 | * state, it contains only the 'values' key.
|
||
178 | * @param $error_element_base
|
||
179 | * The base to prepend to field names when using form_set_error().
|
||
180 | * Needed because the admin settings form is embedded into a bigger form.
|
||
181 | */
|
||
182 | public function adminOptionsFormValidate($form, &$form_state, $error_element_base) { |
||
183 | parent::adminOptionsFormValidate($form, $form_state, $error_element_base); |
||
184 | |||
185 | if (!empty($form['settings'])) { |
||
186 | $settings_validation_callback = $this->operationInfo['callback'] . '_views_bulk_operations_form_validate'; |
||
187 | if (function_exists($settings_validation_callback)) { |
||
188 | $fake_form = $form['settings']; |
||
189 | $fake_form_state = array('values' => &$form_state['values']['settings']); |
||
190 | $error_element_base .= 'settings]['; |
||
191 | |||
192 | $settings_validation_callback($fake_form, $fake_form_state, $error_element_base); |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | |||
197 | /**
|
||
198 | * Handles the submitted admin options form.
|
||
199 | * Note that there is no need to handle saving the options, that is done
|
||
200 | * by the VBO views field handler, which also injects the options into the
|
||
201 | * operation object upon instantiation.
|
||
202 | *
|
||
203 | * @param $form
|
||
204 | * The admin options form.
|
||
205 | * @param $form_state
|
||
206 | * An array containing the current state of the form. Note that this array
|
||
207 | * is constructed by the VBO views field handler, so it's not a real form
|
||
208 | * state, it contains only the 'values' key.
|
||
209 | */
|
||
210 | public function adminOptionsFormSubmit($form, &$form_state) { |
||
211 | parent::adminOptionsFormSubmit($form, $form_state); |
||
212 | |||
213 | if (!empty($form['settings'])) { |
||
214 | $settings_submit_callback = $this->operationInfo['callback'] . '_views_bulk_operations_form_submit'; |
||
215 | if (function_exists($settings_submit_callback)) { |
||
216 | $fake_form = $form['settings']; |
||
217 | $fake_form_state = array('values' => &$form_state['values']['settings']); |
||
218 | |||
219 | $settings_submit_callback($form, $form_state); |
||
220 | } |
||
221 | } |
||
222 | } |
||
223 | |||
224 | /**
|
||
225 | * Returns whether the operation needs the full selected views rows to be
|
||
226 | * passed to execute() as a part of $context.
|
||
227 | */
|
||
228 | public function needsRows() { |
||
229 | return !empty($this->operationInfo['pass rows']); |
||
230 | } |
||
231 | |||
232 | /**
|
||
233 | * Executes the selected operation on the provided data.
|
||
234 | *
|
||
235 | * @param $data
|
||
236 | * The data to to operate on. An entity or an array of entities.
|
||
237 | * @param $context
|
||
238 | * An array of related data (selected views rows, etc).
|
||
239 | */
|
||
240 | public function execute($data, array $context) { |
||
241 | $context['entity_type'] = $this->entityType; |
||
242 | $context['settings'] = $this->getAdminOption('settings', array()); |
||
243 | $context += $this->formOptions; |
||
244 | $context += $this->operationInfo['parameters']; |
||
245 | // Actions provided by the Drupal system module require the entity to be
|
||
246 | // present in $context, keyed by entity type.
|
||
247 | if (is_object($data)) { |
||
248 | $context[$this->entityType] = $data; |
||
249 | } |
||
250 | |||
251 | actions_do($this->operationInfo['callback'], $data, $context); |
||
252 | |||
253 | // The action might need to have its entities saved after execution.
|
||
254 | if (in_array('changes_property', $this->operationInfo['behavior'])) { |
||
255 | $data = is_array($data) ? $data : array($data); |
||
256 | foreach ($data as $entity) { |
||
257 | entity_save($this->entityType, $entity); |
||
258 | } |
||
259 | } |
||
260 | } |
||
261 | } |