base.class.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * @file
  4. * Defines the base class for operations.
  5. */
  6. abstract class ViewsBulkOperationsBaseOperation {
  7. /**
  8. * The id of the operation.
  9. *
  10. * Composed of the operation_type plugin name and the operation key,
  11. * divided by '::'. For example: action::node_publish_action.
  12. */
  13. public $operationId;
  14. /**
  15. * The entity type that the operation is operating on.
  16. *
  17. * Not the same as $operationInfo['type'] since that value can be just
  18. * "entity", which means "available to every entity type".
  19. */
  20. public $entityType;
  21. /**
  22. * Contains information about the current operation, as generated
  23. * by the "list callback" function in the plugin file.
  24. *
  25. * @var array
  26. */
  27. protected $operationInfo;
  28. /**
  29. * Contains the options set by the admin for the current operation.
  30. *
  31. * @var array
  32. */
  33. protected $adminOptions;
  34. /**
  35. * Constructs an operation object.
  36. *
  37. * @param $operationId
  38. * The id of the operation.
  39. * @param $entityType
  40. * The entity type that the operation is operating on.
  41. * @param $operationInfo
  42. * An array of information about the operation.
  43. * @param $adminOptions
  44. * An array of options set by the admin.
  45. */
  46. public function __construct($operationId, $entityType, array $operationInfo, array $adminOptions) {
  47. $this->operationId = $operationId;
  48. $this->entityType = $entityType;
  49. $this->operationInfo = $operationInfo;
  50. $this->adminOptions = $adminOptions;
  51. }
  52. /**
  53. * Returns the value of an admin option.
  54. */
  55. public function getAdminOption($key, $default = NULL) {
  56. return isset($this->adminOptions[$key]) ? $this->adminOptions[$key] : $default;
  57. }
  58. /**
  59. * Returns the access bitmask for the operation, used for entity access checks.
  60. */
  61. public function getAccessMask() {
  62. // Assume edit by default.
  63. return VBO_ACCESS_OP_UPDATE;
  64. }
  65. /**
  66. * Returns the id of the operation.
  67. */
  68. public function id() {
  69. return $this->operationId;
  70. }
  71. /**
  72. * Returns the name of the operation_type plugin that provides the operation.
  73. */
  74. public function type() {
  75. return $this->operationInfo['operation_type'];
  76. }
  77. /**
  78. * Returns the human-readable name of the operation, meant to be shown
  79. * to the user.
  80. */
  81. public function label() {
  82. $admin_label = $this->getAdminOption('label');
  83. if (!empty($admin_label)) {
  84. $label = t($admin_label);
  85. }
  86. else {
  87. // If the admin didn't specify any label, fallback to the default one.
  88. $label = $this->operationInfo['label'];
  89. }
  90. return $label;
  91. }
  92. /**
  93. * Returns the human-readable name of the operation, meant to be shown
  94. * to the admin.
  95. */
  96. public function adminLabel() {
  97. return $this->operationInfo['label'];
  98. }
  99. /**
  100. * Returns whether the operation is configurable. Used to determine
  101. * whether the operation's form methods should be invoked.
  102. */
  103. public function configurable() {
  104. return !empty($this->operationInfo['configurable']);
  105. }
  106. /**
  107. * Returns whether the provided account has access to execute the operation.
  108. *
  109. * @param $account
  110. */
  111. public function access($account) {
  112. return TRUE;
  113. }
  114. /**
  115. * Returns the configuration form for the operation.
  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. * @param $context
  123. * An array of related data provided by the caller.
  124. */
  125. abstract function form($form, &$form_state, array $context);
  126. /**
  127. * Validates the configuration form.
  128. * Only called if the operation is declared as configurable.
  129. *
  130. * @param $form
  131. * The views form.
  132. * @param $form_state
  133. * An array containing the current state of the form.
  134. */
  135. abstract function formValidate($form, &$form_state);
  136. /**
  137. * Handles the submitted configuration form.
  138. * This is where the operation can transform and store the submitted data.
  139. * Only called if the operation is declared as configurable.
  140. *
  141. * @param $form
  142. * The views form.
  143. * @param $form_state
  144. * An array containing the current state of the form.
  145. */
  146. abstract function formSubmit($form, &$form_state);
  147. /**
  148. * Returns the admin options form for the operation.
  149. *
  150. * The admin options form is embedded into the VBO field settings and used
  151. * to configure operation behavior. The options can later be fetched
  152. * through the getAdminOption() method.
  153. *
  154. * @param $dom_id
  155. * The dom path to the level where the admin options form is embedded.
  156. * Needed for #dependency.
  157. * @param $field_handler
  158. * The Views field handler object for the VBO field.
  159. */
  160. public function adminOptionsForm($dom_id, $field_handler) {
  161. $label = $this->getAdminOption('label', '');
  162. $form = array();
  163. $form['override_label'] = array(
  164. '#type' => 'checkbox',
  165. '#title' => t('Override label'),
  166. '#default_value' => $label !== '',
  167. '#dependency' => array(
  168. $dom_id . '-selected' => array(1),
  169. ),
  170. );
  171. $form['label'] = array(
  172. '#type' => 'textfield',
  173. '#title' => t('Provide label'),
  174. '#title_display' => 'invisible',
  175. '#default_value' => $label,
  176. '#dependency' => array(
  177. $dom_id . '-selected' => array(1),
  178. $dom_id . '-override-label' => array(1),
  179. ),
  180. '#dependency_count' => 2,
  181. );
  182. return $form;
  183. }
  184. /**
  185. * Validates the admin options form.
  186. *
  187. * @param $form
  188. * The admin options form.
  189. * @param $form_state
  190. * An array containing the current state of the form. Note that this array
  191. * is constructed by the VBO views field handler, so it's not a real form
  192. * state, it contains only the 'values' key.
  193. * @param $error_element_base
  194. * The base to prepend to field names when using form_set_error().
  195. * Needed because the admin options form is embedded into a bigger form.
  196. */
  197. public function adminOptionsFormValidate($form, &$form_state, $error_element_base) {
  198. // No need to do anything, but make the function have a body anyway
  199. // so that it's callable by overriding methods.
  200. }
  201. /**
  202. * Handles the submitted admin options form.
  203. * Note that there is no need to handle saving the options, that is done
  204. * by the VBO views field handler, which also injects the options into the
  205. * operation object upon instantiation.
  206. *
  207. * @param $form
  208. * The admin options form.
  209. * @param $form_state
  210. * An array containing the current state of the form. Note that this array
  211. * is constructed by the VBO views field handler, so it's not a real form
  212. * state, it contains only the 'values' key.
  213. */
  214. public function adminOptionsFormSubmit($form, &$form_state) {
  215. // If the "Override label" checkbox was deselected, clear the entered value.
  216. if (empty($form_state['values']['override_label'])) {
  217. $form_state['values']['label'] = '';
  218. }
  219. }
  220. /**
  221. * Returns whether the selected entities should be aggregated
  222. * (loaded in bulk and passed in together).
  223. * To be avoided if possible, since aggregation makes it impossible to use
  224. * Batch API or the Drupal Queue for execution.
  225. */
  226. public function aggregate() {
  227. return !empty($this->operationInfo['aggregate']);
  228. }
  229. /**
  230. * Returns whether the operation needs the full selected views rows to be
  231. * passed to execute() as a part of $context.
  232. */
  233. public function needsRows() {
  234. return FALSE;
  235. }
  236. /**
  237. * Executes the selected operation on the provided data.
  238. *
  239. * @param $data
  240. * The data to to operate on. An entity or an array of entities.
  241. * @param $context
  242. * An array of related data (selected views rows, etc).
  243. */
  244. abstract function execute($data, array $context);
  245. }