action.class.php

  1. nittany7 modules/contrib/views_bulk_operations/plugins/operation_types/action.class.php
  2. cis7 modules/contrib/views_bulk_operations/plugins/operation_types/action.class.php
  3. mooc7 modules/contrib/views_bulk_operations/plugins/operation_types/action.class.php

Defines the class for core actions. Belongs to the "action" operation type plugin.

Classes

File

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