WorkflowScheduledTransition.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * @file
  4. * Contains workflow\includes\Entity\WorkflowScheduledTransition.
  5. */
  6. /**
  7. * Implements a scheduled transition, as shown on Workflow form.
  8. */
  9. class WorkflowScheduledTransition extends WorkflowTransition {
  10. // Scheduled timestamp of state change.
  11. public $scheduled;
  12. /**
  13. * Constructor.
  14. */
  15. public function __construct(array $values = array(), $entityType = 'WorkflowScheduledTransition') {
  16. // Please be aware that $entity_type and $entityType are different things!
  17. parent::__construct($values, $entityType);
  18. $this->is_scheduled = TRUE;
  19. $this->is_executed = FALSE;
  20. }
  21. public function setValues($entity_type, $entity, $field_name, $old_sid, $new_sid, $uid = NULL, $scheduled = REQUEST_TIME, $comment = '') {
  22. // A scheduled transition does not have a timestamp, yet.
  23. $stamp = 0;
  24. parent::setValues($entity_type, $entity, $field_name, $old_sid, $new_sid, $uid, $stamp, $comment);
  25. // Set the scheduled timestamp of state change.
  26. $this->scheduled = $scheduled;
  27. }
  28. /**
  29. * Given a node, get all scheduled transitions for it.
  30. *
  31. * @param string $entity_type
  32. * @param int $entity_id
  33. * @param string $field_name
  34. * Optional.
  35. *
  36. * @return array
  37. * An array of WorkflowScheduledTransitions.
  38. *
  39. * deprecated: workflow_get_workflow_scheduled_transition_by_nid() --> WorkflowScheduledTransition::load()
  40. */
  41. public static function load($entity_type, $entity_id, $field_name = '', $limit = NULL) {
  42. if (!$entity_id) {
  43. return array();
  44. }
  45. $query = db_select('workflow_scheduled_transition', 'wst');
  46. $query->fields('wst');
  47. $query->condition('entity_type', $entity_type, '=');
  48. $query->condition('nid', $entity_id, '=');
  49. if ($field_name !== NULL) {
  50. $query->condition('field_name', $field_name, '=');
  51. }
  52. $query->orderBy('scheduled', 'ASC');
  53. $query->addTag('workflow_scheduled_transition');
  54. if ($limit) {
  55. $query->range(0, $limit);
  56. }
  57. $result = $query->execute()->fetchAll(PDO::FETCH_CLASS, 'WorkflowScheduledTransition');
  58. return $result;
  59. }
  60. /**
  61. * Given a timeframe, get all scheduled transitions.
  62. *
  63. * deprecated: workflow_get_workflow_scheduled_transition_by_between() --> WorkflowScheduledTransition::loadBetween()
  64. */
  65. public static function loadBetween($start = 0, $end = 0) {
  66. $query = db_select('workflow_scheduled_transition', 'wst');
  67. $query->fields('wst');
  68. $query->orderBy('scheduled', 'ASC');
  69. $query->addTag('workflow_scheduled_transition');
  70. if ($start) {
  71. $query->condition('scheduled', $start, '>');
  72. }
  73. if ($end) {
  74. $query->condition('scheduled', $end, '<');
  75. }
  76. $result = $query->execute()->fetchAll(PDO::FETCH_CLASS, 'WorkflowScheduledTransition');
  77. return $result;
  78. }
  79. /**
  80. * Save a scheduled transition. If the transition is executed, save in history.
  81. */
  82. public function save() {
  83. // If executed, save in history.
  84. if ($this->is_executed) {
  85. // Be careful, we are not a WorkflowScheduleTransition anymore!
  86. $this->entityType = 'WorkflowTransition';
  87. $this->setUp();
  88. return parent::save(); // <--- exit !!
  89. }
  90. // Since we do not have an entity_id here, we cannot use entity_delete.
  91. // @todo: Add an 'entity id' to WorkflowScheduledTransition entity class.
  92. // $result = parent::save();
  93. // Avoid duplicate entries.
  94. $clone = clone $this;
  95. $clone->delete();
  96. // Save (insert or update) a record to the database based upon the schema.
  97. drupal_write_record('workflow_scheduled_transition', $this);
  98. // Create user message.
  99. if ($state = $this->getNewState()) {
  100. $entity_type = $this->entity_type;
  101. $entity = $this->getEntity();
  102. $message = '%entity_title scheduled for state change to %state_name on %scheduled_date';
  103. $args = array(
  104. '@entity_type' => $entity_type,
  105. '%entity_title' => entity_label($entity_type, $entity),
  106. '%state_name' => entity_label('WorkflowState', $state),
  107. '%scheduled_date' => format_date($this->scheduled),
  108. );
  109. $uri = entity_uri($entity_type, $entity);
  110. watchdog('workflow', $message, $args, WATCHDOG_NOTICE, l('view', $uri['path'] . '/workflow'));
  111. drupal_set_message(t($message, $args));
  112. }
  113. }
  114. /**
  115. * Given a node, delete transitions for it.
  116. *
  117. * deprecated: workflow_delete_workflow_scheduled_transition_by_nid() --> WorkflowScheduledTransition::delete()
  118. */
  119. public function delete() {
  120. // Support translated Workflow Field workflows by including the language.
  121. db_delete($this->entityInfo['base table'])
  122. ->condition('entity_type', $this->entity_type)
  123. ->condition('nid', $this->entity_id)
  124. ->condition('field_name', $this->field_name)
  125. ->condition('language', $this->language)
  126. ->execute();
  127. }
  128. /**
  129. * Property functions.
  130. */
  131. /**
  132. * If a scheduled transition has no comment, a default comment is added before executing it.
  133. */
  134. public function addDefaultComment() {
  135. $this->comment = t('Scheduled by user @uid.', array('@uid' => $this->uid));
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function getTimestamp() {
  141. return $this->scheduled;
  142. }
  143. }