views_bulk_operations_handler_field_operations.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. 'row_clickable' => array('default' => TRUE),
  50. 'force_single' => array('default' => FALSE),
  51. 'entity_load_capacity' => array('default' => 10),
  52. 'skip_batching' => array('default' => 0),
  53. ),
  54. );
  55. $options['vbo_operations'] = array(
  56. 'default' => array(),
  57. 'unpack_translatable' => 'unpack_operations',
  58. 'export' => 'export_vbo_operations',
  59. );
  60. return $options;
  61. }
  62. function export_vbo_operations($indent, $prefix, $storage, $option, $definition, $parents) {
  63. // Anti-recursion, since we use the parent export helper.
  64. unset($definition['export']);
  65. // Find and remove all unselected/disabled operations.
  66. foreach ($storage['vbo_operations'] as $operation_id => $operation) {
  67. if (empty($operation['selected'])) {
  68. unset($storage['vbo_operations'][$operation_id]);
  69. }
  70. }
  71. return parent::export_option($indent, $prefix, $storage, $option, $definition, $parents);
  72. }
  73. function unpack_operations(&$translatable, $storage, $option, $definition, $parents, $keys) {
  74. $translatable[] = array(
  75. 'value' => t('- Choose an operation -'),
  76. 'keys' => array_merge($keys, array('noop')),
  77. );
  78. foreach ($storage[$option] as $key => $operation) {
  79. if (!empty($operation['override_label']) && !empty($operation['label'])) {
  80. $translatable[] = array(
  81. 'value' => $operation['label'],
  82. 'keys' => array_merge($keys, array($key)),
  83. );
  84. }
  85. }
  86. }
  87. function options_form(&$form, &$form_state) {
  88. parent::options_form($form, $form_state);
  89. $form['vbo_settings'] = array(
  90. '#type' => 'fieldset',
  91. '#title' => t('Bulk operations settings'),
  92. '#collapsible' => TRUE,
  93. '#collapsed' => TRUE,
  94. );
  95. $form['vbo_settings']['display_type'] = array(
  96. '#type' => 'radios',
  97. '#title' => t('Display operations as'),
  98. '#default_value' => $this->options['vbo_settings']['display_type'],
  99. '#options' => array(
  100. t('Dropdown selectbox with Submit button'),
  101. t('Each action as a separate button'),
  102. ),
  103. );
  104. $form['vbo_settings']['enable_select_all_pages'] = array(
  105. '#type' => 'checkbox',
  106. '#title' => t('Enable "Select all items on all pages"'),
  107. '#default_value' => $this->options['vbo_settings']['enable_select_all_pages'],
  108. '#description' => t('Check this box to enable the ability to select all items on all pages.'),
  109. );
  110. $form['vbo_settings']['row_clickable'] = array(
  111. '#type' => 'checkbox',
  112. '#title' => t('Make the whole row clickable'),
  113. '#default_value' => $this->options['vbo_settings']['row_clickable'],
  114. '#description' => t('Check this box to select an item when its row has been clicked. Requires JavaScript.'),
  115. );
  116. $form['vbo_settings']['force_single'] = array(
  117. '#type' => 'checkbox',
  118. '#title' => t('Force single'),
  119. '#default_value' => $this->options['vbo_settings']['force_single'],
  120. '#description' => t('Check this box to restrict selection to a single value.'),
  121. );
  122. $form['vbo_settings']['entity_load_capacity'] = array(
  123. '#type' => 'textfield',
  124. '#title' => t('Number of entities to load at once'),
  125. '#description' => t("Improve execution performance at the cost of memory usage. Set to '1' if you're having problems."),
  126. '#default_value' => $this->options['vbo_settings']['entity_load_capacity'],
  127. );
  128. $form['vbo_settings']['skip_batching'] = array(
  129. '#type' => 'checkbox',
  130. '#title' => t('Skip batching'),
  131. '#default_value' => $this->options['vbo_settings']['skip_batching'],
  132. '#description' => '<b>' . t('Warning:') . '</b> ' . t('This will cause timeouts for larger amounts of selected items.'),
  133. );
  134. // Display operations and their settings.
  135. $form['vbo_operations'] = array(
  136. '#tree' => TRUE,
  137. '#type' => 'fieldset',
  138. '#title' => t('Selected bulk operations'),
  139. '#collapsible' => TRUE,
  140. '#collapsed' => FALSE,
  141. );
  142. $entity_type = $this->get_entity_type();
  143. $options = $this->options['vbo_operations'];
  144. foreach (views_bulk_operations_get_applicable_operations($entity_type, $options) as $operation_id => $operation) {
  145. $operation_options = !empty($options[$operation_id]) ? $options[$operation_id] : array();
  146. $dom_id = 'edit-options-vbo-operations-' . str_replace(array('_', ':'), array('-', ''), $operation_id);
  147. $form['vbo_operations'][$operation_id]['selected'] = array(
  148. '#type' => 'checkbox',
  149. '#title' => $operation->adminLabel(),
  150. '#default_value' => !empty($operation_options['selected']),
  151. );
  152. if (!$operation->aggregate()) {
  153. $form['vbo_operations'][$operation_id]['postpone_processing'] = array(
  154. '#type' => 'checkbox',
  155. '#title' => t('Enqueue the operation instead of executing it directly'),
  156. '#default_value' => !empty($operation_options['postpone_processing']),
  157. '#dependency' => array(
  158. $dom_id . '-selected' => array(1),
  159. ),
  160. );
  161. }
  162. $form['vbo_operations'][$operation_id]['skip_confirmation'] = array(
  163. '#type' => 'checkbox',
  164. '#title' => t('Skip confirmation step'),
  165. '#default_value' => !empty($operation_options['skip_confirmation']),
  166. '#dependency' => array(
  167. $dom_id . '-selected' => array(1),
  168. ),
  169. );
  170. $form['vbo_operations'][$operation_id] += $operation->adminOptionsForm($dom_id, $this);
  171. }
  172. }
  173. function options_validate(&$form, &$form_state) {
  174. parent::options_validate($form, $form_state);
  175. $entity_type = $this->get_entity_type();
  176. foreach ($form_state['values']['options']['vbo_operations'] as $operation_id => &$options) {
  177. if (empty($options['selected'])) {
  178. continue;
  179. }
  180. $operation = views_bulk_operations_get_operation($operation_id, $entity_type, $options);
  181. $fake_form = $form['vbo_operations'][$operation_id];
  182. $fake_form_state = array('values' => &$options);
  183. $error_element_base = 'vbo_operations][' . $operation_id . '][';
  184. $operation->adminOptionsFormValidate($fake_form, $fake_form_state, $error_element_base);
  185. }
  186. }
  187. function options_submit(&$form, &$form_state) {
  188. parent::options_submit($form, $form_state);
  189. $entity_type = $this->get_entity_type();
  190. foreach ($form_state['values']['options']['vbo_operations'] as $operation_id => &$options) {
  191. if (empty($options['selected'])) {
  192. continue;
  193. }
  194. $operation = views_bulk_operations_get_operation($operation_id, $entity_type, $options);
  195. $fake_form = $form['vbo_operations'][$operation_id];
  196. $fake_form_state = array('values' => &$options);
  197. $operation->adminOptionsFormSubmit($fake_form, $fake_form_state);
  198. }
  199. }
  200. /**
  201. * Returns the value of a vbo option.
  202. */
  203. function get_vbo_option($key, $default = NULL) {
  204. return isset($this->options['vbo_settings'][$key]) ? $this->options['vbo_settings'][$key] : $default;
  205. }
  206. /**
  207. * If the view is using a table style, provide a
  208. * placeholder for a "select all" checkbox.
  209. */
  210. function label() {
  211. if (!empty($this->view->style_plugin) && $this->view->style_plugin instanceof views_plugin_style_table && !$this->options['vbo_settings']['force_single']) {
  212. return '<!--views-bulk-operations-select-all-->';
  213. }
  214. else {
  215. return parent::label();
  216. }
  217. }
  218. function render($values) {
  219. return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
  220. }
  221. /**
  222. * The form which replaces the placeholder from render().
  223. */
  224. function views_form(&$form, &$form_state) {
  225. // The view is empty, abort.
  226. if (empty($this->view->result)) {
  227. return;
  228. }
  229. $form[$this->options['id']] = array(
  230. '#tree' => TRUE,
  231. );
  232. // At this point, the query has already been run, so we can access the results
  233. // in order to get the base key value (for example, nid for nodes).
  234. foreach ($this->view->result as $row_index => $row) {
  235. $entity_id = $this->get_value($row);
  236. if ($this->options['vbo_settings']['force_single']) {
  237. $form[$this->options['id']][$row_index] = array(
  238. '#type' => 'radio',
  239. '#parents' => array($this->options['id']),
  240. '#return_value' => $entity_id,
  241. );
  242. }
  243. else {
  244. $form[$this->options['id']][$row_index] = array(
  245. '#type' => 'checkbox',
  246. '#return_value' => $entity_id,
  247. '#default_value' => FALSE,
  248. '#attributes' => array('class' => array('vbo-select')),
  249. );
  250. }
  251. }
  252. }
  253. public function get_selected_operations() {
  254. global $user;
  255. $selected = drupal_static(__FUNCTION__);
  256. if (!isset($selected)) {
  257. $entity_type = $this->get_entity_type();
  258. $selected = array();
  259. foreach ($this->options['vbo_operations'] as $operation_id => $options) {
  260. if (empty($options['selected'])) {
  261. continue;
  262. }
  263. $operation = views_bulk_operations_get_operation($operation_id, $entity_type, $options);
  264. if (!$operation || !$operation->access($user)) {
  265. continue;
  266. }
  267. $selected[$operation_id] = $operation;
  268. }
  269. }
  270. return $selected;
  271. }
  272. /**
  273. * Returns the options stored for the provided operation id.
  274. */
  275. public function get_operation_options($operation_id) {
  276. $options = $this->options['vbo_operations'];
  277. return isset($options[$operation_id]) ? $options[$operation_id] : array();
  278. }
  279. /**
  280. * Determine the base table of the VBO field, and then use it to determine
  281. * the entity type that VBO is operating on.
  282. */
  283. public function get_entity_type() {
  284. $base_table = $this->view->base_table;
  285. // If the current field is under a relationship you can't be sure that the
  286. // base table of the view is the base table of the current field.
  287. // For example a field from a node author on a node view does have users as base table.
  288. if (!empty($this->options['relationship']) && $this->options['relationship'] != 'none') {
  289. $relationships = $this->view->display_handler->get_option('relationships');
  290. $options = $relationships[$this->options['relationship']];
  291. $data = views_fetch_data($options['table']);
  292. $base_table = $data[$options['field']]['relationship']['base'];
  293. }
  294. // The base table is now known, use it to determine the entity type.
  295. foreach (entity_get_info() as $entity_type => $info) {
  296. if (isset($info['base table']) && $info['base table'] == $base_table) {
  297. return $entity_type;
  298. }
  299. elseif (isset($info['revision table']) && $info['revision table'] == $base_table) {
  300. $this->revision = TRUE;
  301. return $entity_type;
  302. }
  303. }
  304. // This should never happen.
  305. _views_bulk_operations_report_error("Could not determine the entity type for VBO field on views base table %table", array('%table' => $base_table));
  306. return FALSE;
  307. }
  308. }