views_bulk_operations_handler_field_operations.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. /**
  3. * @file
  4. * Views field handler. Contains all relevant VBO options and related logic.
  5. * Implements the Views Form API.
  6. */
  7. class views_bulk_operations_handler_field_operations extends views_handler_field {
  8. var $revision = FALSE;
  9. function init(&$view, &$options) {
  10. parent::init($view, $options);
  11. // Update old settings
  12. if (!empty($options['vbo']) && empty($this->options['vbo_operations'])) {
  13. $this->options['vbo_operations'] = $options['vbo']['operations'];
  14. unset($options['vbo']['operations']);
  15. $this->options['vbo_settings'] = $options['vbo'] + $this->options['vbo_settings'];
  16. }
  17. // When updating old Views it is possible for this value to stay empty.
  18. if (empty($this->options['vbo_settings']['entity_load_capacity'])) {
  19. $this->options['vbo_settings']['entity_load_capacity'] = 10;
  20. }
  21. foreach ($this->options['vbo_operations'] as $operation_id => &$operation_options) {
  22. // Prefix all un-prefixed operations.
  23. if (strpos($operation_id, '::') === FALSE) {
  24. $operations = views_bulk_operations_get_operation_info();
  25. // Basically, guess.
  26. foreach (array('action', 'rules_component') as $operation_type) {
  27. $new_operation_id = $operation_type . '::' . $operation_id;
  28. if (isset($operations[$new_operation_id])) {
  29. $this->options['vbo_operations'][$new_operation_id] = $operation_options;
  30. break;
  31. }
  32. }
  33. // Remove the old operation in any case.
  34. unset($this->options['vbo_operations'][$operation_id]);
  35. }
  36. // Rename the use_queue setting.
  37. if (isset($operation_options['use_queue']) && !isset($operation_options['postpone_processing'])) {
  38. $operation_options['postpone_processing'] = $operation_options['use_queue'];
  39. unset($operation_options['use_queue']);
  40. }
  41. }
  42. }
  43. function option_definition() {
  44. $options = parent::option_definition();
  45. $options['vbo_settings'] = array(
  46. 'contains' => array(
  47. 'display_type' => array('default' => 0),
  48. 'enable_select_all_pages' => array('default' => TRUE),
  49. 'force_single' => array('default' => FALSE),
  50. 'entity_load_capacity' => array('default' => 10),
  51. 'skip_batching' => array('default' => 0),
  52. ),
  53. );
  54. $options['vbo_operations'] = array(
  55. 'default' => array(),
  56. 'unpack_translatable' => 'unpack_operations',
  57. );
  58. return $options;
  59. }
  60. function unpack_operations(&$translatable, $storage, $option, $definition, $parents, $keys) {
  61. $translatable[] = array(
  62. 'value' => t('- Choose an operation -'),
  63. 'keys' => array_merge($keys, array('noop')),
  64. );
  65. foreach ($storage[$option] as $key => $operation) {
  66. if (!empty($operation['override_label']) && !empty($operation['label'])) {
  67. $translatable[] = array(
  68. 'value' => $operation['label'],
  69. 'keys' => array_merge($keys, array($key)),
  70. );
  71. }
  72. }
  73. }
  74. function options_form(&$form, &$form_state) {
  75. parent::options_form($form, $form_state);
  76. $form['vbo_settings'] = array(
  77. '#type' => 'fieldset',
  78. '#title' => t('Bulk operations settings'),
  79. '#collapsible' => TRUE,
  80. '#collapsed' => TRUE,
  81. );
  82. $form['vbo_settings']['display_type'] = array(
  83. '#type' => 'radios',
  84. '#title' => t('Display operations as'),
  85. '#default_value' => $this->options['vbo_settings']['display_type'],
  86. '#options' => array(
  87. t('Dropdown selectbox with Submit button'),
  88. t('Each action as a separate button'),
  89. ),
  90. );
  91. $form['vbo_settings']['enable_select_all_pages'] = array(
  92. '#type' => 'checkbox',
  93. '#title' => t('Enable "Select all items on all pages"'),
  94. '#default_value' => $this->options['vbo_settings']['enable_select_all_pages'],
  95. '#description' => t('Check this box to enable the ability to select all items on all pages.'),
  96. );
  97. $form['vbo_settings']['force_single'] = array(
  98. '#type' => 'checkbox',
  99. '#title' => t('Force single'),
  100. '#default_value' => $this->options['vbo_settings']['force_single'],
  101. '#description' => t('Check this box to restrict selection to a single value.'),
  102. );
  103. $form['vbo_settings']['entity_load_capacity'] = array(
  104. '#type' => 'textfield',
  105. '#title' => t('Number of entities to load at once'),
  106. '#description' => t("Improve execution performance at the cost of memory usage. Set to '1' if you're having problems."),
  107. '#default_value' => $this->options['vbo_settings']['entity_load_capacity'],
  108. );
  109. $form['vbo_settings']['skip_batching'] = array(
  110. '#type' => 'checkbox',
  111. '#title' => t('Skip batching'),
  112. '#default_value' => $this->options['vbo_settings']['skip_batching'],
  113. '#description' => '<b>' . t('Warning:') . '</b> ' . t('This will cause timeouts for larger amounts of selected items.'),
  114. );
  115. // Display operations and their settings.
  116. $form['vbo_operations'] = array(
  117. '#tree' => TRUE,
  118. '#type' => 'fieldset',
  119. '#title' => t('Selected bulk operations'),
  120. '#collapsible' => TRUE,
  121. '#collapsed' => FALSE,
  122. );
  123. $entity_type = $this->get_entity_type();
  124. $options = $this->options['vbo_operations'];
  125. foreach (views_bulk_operations_get_applicable_operations($entity_type, $options) as $operation_id => $operation) {
  126. $operation_options = !empty($options[$operation_id]) ? $options[$operation_id] : array();
  127. $dom_id = 'edit-options-vbo-operations-' . str_replace(array('_', ':'), array('-', ''), $operation_id);
  128. $form['vbo_operations'][$operation_id]['selected'] = array(
  129. '#type' => 'checkbox',
  130. '#title' => $operation->adminLabel(),
  131. '#default_value' => !empty($operation_options['selected']),
  132. );
  133. if (!$operation->aggregate()) {
  134. $form['vbo_operations'][$operation_id]['postpone_processing'] = array(
  135. '#type' => 'checkbox',
  136. '#title' => t('Enqueue the operation instead of executing it directly'),
  137. '#default_value' => !empty($operation_options['postpone_processing']),
  138. '#dependency' => array(
  139. $dom_id . '-selected' => array(1),
  140. ),
  141. );
  142. }
  143. $form['vbo_operations'][$operation_id]['skip_confirmation'] = array(
  144. '#type' => 'checkbox',
  145. '#title' => t('Skip confirmation step'),
  146. '#default_value' => !empty($operation_options['skip_confirmation']),
  147. '#dependency' => array(
  148. $dom_id . '-selected' => array(1),
  149. ),
  150. );
  151. $form['vbo_operations'][$operation_id] += $operation->adminOptionsForm($dom_id, $this);
  152. }
  153. }
  154. function options_validate(&$form, &$form_state) {
  155. parent::options_validate($form, $form_state);
  156. $entity_type = $this->get_entity_type();
  157. foreach ($form_state['values']['options']['vbo_operations'] as $operation_id => &$options) {
  158. if (empty($options['selected'])) {
  159. continue;
  160. }
  161. $operation = views_bulk_operations_get_operation($operation_id, $entity_type, $options);
  162. $fake_form = $form['vbo_operations'][$operation_id];
  163. $fake_form_state = array('values' => &$options);
  164. $error_element_base = 'vbo_operations][' . $operation_id . '][';
  165. $operation->adminOptionsFormValidate($fake_form, $fake_form_state, $error_element_base);
  166. }
  167. }
  168. function options_submit(&$form, &$form_state) {
  169. parent::options_submit($form, $form_state);
  170. $entity_type = $this->get_entity_type();
  171. foreach ($form_state['values']['options']['vbo_operations'] as $operation_id => &$options) {
  172. if (empty($options['selected'])) {
  173. continue;
  174. }
  175. $operation = views_bulk_operations_get_operation($operation_id, $entity_type, $options);
  176. $fake_form = $form['vbo_operations'][$operation_id];
  177. $fake_form_state = array('values' => &$options);
  178. $operation->adminOptionsFormSubmit($fake_form, $fake_form_state);
  179. }
  180. }
  181. /**
  182. * Returns the value of a vbo option.
  183. */
  184. function get_vbo_option($key, $default = NULL) {
  185. return isset($this->options['vbo_settings'][$key]) ? $this->options['vbo_settings'][$key] : $default;
  186. }
  187. /**
  188. * If the view is using a table style, provide a
  189. * placeholder for a "select all" checkbox.
  190. */
  191. function label() {
  192. if (!empty($this->view->style_plugin) && $this->view->style_plugin instanceof views_plugin_style_table && !$this->options['vbo_settings']['force_single']) {
  193. return '<!--views-bulk-operations-select-all-->';
  194. }
  195. else {
  196. return parent::label();
  197. }
  198. }
  199. function render($values) {
  200. return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
  201. }
  202. /**
  203. * The form which replaces the placeholder from render().
  204. */
  205. function views_form(&$form, &$form_state) {
  206. // The view is empty, abort.
  207. if (empty($this->view->result)) {
  208. return;
  209. }
  210. $form[$this->options['id']] = array(
  211. '#tree' => TRUE,
  212. );
  213. // At this point, the query has already been run, so we can access the results
  214. // in order to get the base key value (for example, nid for nodes).
  215. foreach ($this->view->result as $row_index => $row) {
  216. $entity_id = $this->get_value($row);
  217. if ($this->options['vbo_settings']['force_single']) {
  218. $form[$this->options['id']][$row_index] = array(
  219. '#type' => 'radio',
  220. '#parents' => array($this->options['id']),
  221. '#return_value' => $entity_id,
  222. );
  223. }
  224. else {
  225. $form[$this->options['id']][$row_index] = array(
  226. '#type' => 'checkbox',
  227. '#return_value' => $entity_id,
  228. '#default_value' => FALSE,
  229. '#attributes' => array('class' => array('vbo-select')),
  230. );
  231. }
  232. }
  233. }
  234. public function get_selected_operations() {
  235. global $user;
  236. $selected = drupal_static(__FUNCTION__);
  237. if (!isset($selected)) {
  238. $entity_type = $this->get_entity_type();
  239. $selected = array();
  240. foreach ($this->options['vbo_operations'] as $operation_id => $options) {
  241. if (empty($options['selected'])) {
  242. continue;
  243. }
  244. $operation = views_bulk_operations_get_operation($operation_id, $entity_type, $options);
  245. if (!$operation || !$operation->access($user)) {
  246. continue;
  247. }
  248. $selected[$operation_id] = $operation;
  249. }
  250. }
  251. return $selected;
  252. }
  253. /**
  254. * Returns the options stored for the provided operation id.
  255. */
  256. public function get_operation_options($operation_id) {
  257. $options = $this->options['vbo_operations'];
  258. return isset($options[$operation_id]) ? $options[$operation_id] : array();
  259. }
  260. /**
  261. * Determine the base table of the VBO field, and then use it to determine
  262. * the entity type that VBO is operating on.
  263. */
  264. public function get_entity_type() {
  265. $base_table = $this->view->base_table;
  266. // If the current field is under a relationship you can't be sure that the
  267. // base table of the view is the base table of the current field.
  268. // For example a field from a node author on a node view does have users as base table.
  269. if (!empty($this->options['relationship']) && $this->options['relationship'] != 'none') {
  270. $relationships = $this->view->display_handler->get_option('relationships');
  271. $options = $relationships[$this->options['relationship']];
  272. $data = views_fetch_data($options['table']);
  273. $base_table = $data[$options['field']]['relationship']['base'];
  274. }
  275. // The base table is now known, use it to determine the entity type.
  276. foreach (entity_get_info() as $entity_type => $info) {
  277. if (isset($info['base table']) && $info['base table'] == $base_table) {
  278. return $entity_type;
  279. }
  280. elseif (isset($info['revision table']) && $info['revision table'] == $base_table) {
  281. $this->revision = TRUE;
  282. return $entity_type;
  283. }
  284. }
  285. // This should never happen.
  286. _views_bulk_operations_report_error("Could not determine the entity type for VBO field on views base table %table", array('%table' => $base_table));
  287. return FALSE;
  288. }
  289. }