EntityWorkflowUIController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * @file
  4. * Contains workflow_admin_ui\includes\Entity\EntityWorkflowUIController.
  5. */
  6. class EntityWorkflowUIController extends EntityDefaultUIController {
  7. /**
  8. * Provides definitions for implementing hook_menu().
  9. */
  10. public function hook_menu() {
  11. $items = parent::hook_menu();
  12. // Set this on the object so classes that extend hook_menu() can use it.
  13. $admin_path = $this->path;
  14. $id_count = count(explode('/', $admin_path));
  15. $wildcard = isset($this->entityInfo['admin ui']['menu wildcard']) ? $this->entityInfo['admin ui']['menu wildcard'] : '%entity_object';
  16. $plural_label = isset($this->entityInfo['plural label']) ? $this->entityInfo['plural label'] : $this->entityInfo['label'] . 's';
  17. $entityType = $this->entityInfo['entity class'];
  18. // @todo: Allow modules to insert their own action links to the 'workflow',
  19. // $workflow_operations = module_invoke_all('workflow_operations', 'workflow', NULL);
  20. $item = array(
  21. 'file path' => isset($this->entityInfo['admin ui']['file path']) ? $this->entityInfo['admin ui']['file path'] : drupal_get_path('module', $this->entityInfo['module']),
  22. 'access arguments' => array('administer workflow'),
  23. 'type' => MENU_LOCAL_TASK,
  24. );
  25. $items["$admin_path/manage/$wildcard/states"] = $item + array(
  26. 'file' => 'workflow_admin_ui/workflow_admin_ui.page.states.inc',
  27. 'title' => 'States',
  28. 'weight' => '11',
  29. 'page callback' => 'drupal_get_form',
  30. 'page arguments' => array('workflow_admin_ui_states_form', $id_count + 1, $id_count + 2),
  31. );
  32. $items["$admin_path/manage/$wildcard/transitions"] = $item + array(
  33. 'file' => 'workflow_admin_ui/workflow_admin_ui.page.transitions.inc',
  34. 'title' => 'Transitions',
  35. 'weight' => '12',
  36. 'page callback' => 'drupal_get_form',
  37. 'page arguments' => array('workflow_admin_ui_transitions_form', $id_count + 1, $id_count + 2),
  38. );
  39. $items["$admin_path/manage/$wildcard/labels"] = $item + array(
  40. 'file' => 'workflow_admin_ui/workflow_admin_ui.page.labels.inc',
  41. 'title' => 'Labels',
  42. 'weight' => '13',
  43. 'page callback' => 'drupal_get_form',
  44. 'page arguments' => array('workflow_admin_ui_labels_form', $id_count + 1, $id_count + 2),
  45. );
  46. $items["$admin_path/manage/$wildcard/permissions"] = $item + array(
  47. 'file' => 'workflow_admin_ui/workflow_admin_ui.page.permissions.inc',
  48. 'title' => 'Permission summary',
  49. 'weight' => '14',
  50. 'page callback' => 'workflow_admin_ui_view_permissions_form',
  51. 'page arguments' => array($id_count + 1, $id_count + 2),
  52. // @todo: convert to drupal_get_form('workflow_admin_ui_view_permissions_form');
  53. // 'page callback' => 'drupal_get_form',
  54. // 'page arguments' => array('workflow_admin_ui_view_permissions_form', $id_count + 1, $id_count + 2),
  55. );
  56. return $items;
  57. }
  58. protected function operationCount() {
  59. // Add more then enough colspan.
  60. return parent::operationCount() + 8;
  61. }
  62. /*
  63. public function operationForm($form, &$form_state, $entity, $op) {}
  64. */
  65. public function overviewForm($form, &$form_state) {
  66. // Add table and pager.
  67. $form = parent::overviewForm($form, $form_state);
  68. // Allow modules to insert their own action links to the 'table', like cleanup module.
  69. $top_actions = module_invoke_all('workflow_operations', 'top_actions', NULL);
  70. // Allow modules to insert their own workflow operations.
  71. foreach ($form['table']['#rows'] as &$row) {
  72. $url = $row[0]['data']['#url'];
  73. $workflow = $url['options']['entity'];
  74. foreach ($actions = module_invoke_all('workflow_operations', 'workflow', $workflow) as $action) {
  75. $action['attributes'] = isset($action['attributes']) ? $action['attributes'] : array();
  76. $row[] = l(strtolower($action['title']), $action['href'], $action['attributes']);
  77. }
  78. }
  79. // @todo: add these top actions next to the core 'Add workflow' action.
  80. $top_actions_args = array(
  81. 'links' => $top_actions,
  82. 'attributes' => array('class' => array('inline', 'action-links')),
  83. );
  84. $form['action-links'] = array(
  85. '#type' => 'markup',
  86. '#markup' => theme('links', $top_actions_args),
  87. '#weight' => -1,
  88. );
  89. if (module_exists('workflownode')) {
  90. // Append the type_map form, changing the form by reference.
  91. // The 'type_map' form is only valid for Workflow Node API.
  92. module_load_include('inc', 'workflow_admin_ui', 'workflow_admin_ui.page.type_map');
  93. workflow_admin_ui_type_map_form($form);
  94. }
  95. // Add a submit button. The submit functions are added in the sub-forms.
  96. $form['submit'] = array(
  97. '#type' => 'submit',
  98. '#value' => t('Save'),
  99. '#weight' => 100,
  100. );
  101. return $form;
  102. }
  103. /*
  104. * Avoids the 'Delete' action if the Workflow is used somewhere.
  105. */
  106. protected function overviewTableRow($conditions, $id, $entity, $additional_cols = array()) {
  107. // Avoid the 'delete' operation if the Workflow is used somewhere.
  108. $status = $entity->status;
  109. // @see parent::overviewTableRow() how to determine a deletable entity.
  110. if (!entity_has_status($this->entityType, $entity, ENTITY_IN_CODE) && !$entity->isDeletable()) {
  111. // Set to a state that does not allow deleting, but allows other actions.
  112. $entity->status = ENTITY_IN_CODE;
  113. }
  114. $row = parent::overviewTableRow($conditions, $id, $entity, $additional_cols);
  115. // Just to be sure: reset status.
  116. $entity->status = $status;
  117. return $row;
  118. }
  119. /**
  120. * Overrides the 'revert' action, to not delete the workflows.
  121. *
  122. * @see https://www.drupal.org/node/2051079
  123. * @see https://www.drupal.org/node/1043634
  124. */
  125. public function applyOperation($op, $entity) {
  126. $admin_path = $this->path;
  127. $label = entity_label($this->entityType, $entity);
  128. $vars = array('%entity' => $this->entityInfo['label'], '%label' => $label);
  129. $wid = entity_id($this->entityType, $entity);
  130. $edit_link = l(t('edit'), "$admin_path/manage/$wid/edit");
  131. switch ($op) {
  132. case 'revert':
  133. $defaults = workflow_get_defaults($entity->module);
  134. workflow_revert($defaults, $entity->getName());
  135. watchdog($this->entityType, 'Reverted %entity %label to the defaults.', $vars, WATCHDOG_NOTICE, $edit_link);
  136. return t('Reverted %entity %label to the defaults.', $vars);
  137. case 'delete':
  138. case 'import':
  139. default:
  140. return parent::applyOperation($op, $entity);
  141. }
  142. }
  143. }