tmgmt.controller.job_item.inc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * @file
  4. * Contains the job item entity controller class.
  5. */
  6. /**
  7. * Controller class for the job item entity.
  8. *
  9. * @ingroup tmgmt_job
  10. */
  11. class TMGMTJobItemController extends EntityAPIController {
  12. /**
  13. * {@inheritdoc}
  14. *
  15. * @todo Eliminate the need to flatten and unflatten the JobItem data.
  16. */
  17. public function save($entity, DatabaseTransaction $transaction = NULL) {
  18. $entity->changed = REQUEST_TIME;
  19. if (!empty($entity->tjid)) {
  20. $entity->recalculateStatistics();
  21. }
  22. return parent::save($entity, $transaction);
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function delete($ids, $transaction = NULL) {
  28. parent::delete($ids, $transaction);
  29. // Since we are deleting one or multiple job items here we also need to
  30. // delete the attached messages.
  31. $query = new EntityFieldQuery();
  32. $result = $query->entityCondition('entity_type', 'tmgmt_message')
  33. ->propertyCondition('tjiid', $ids)
  34. ->execute();
  35. if (!empty($result['tmgmt_message'])) {
  36. $controller = entity_get_controller('tmgmt_message');
  37. // We need to directly query the entity controller so we can pass on
  38. // the transaction object.
  39. $controller->delete(array_keys($result['tmgmt_message']), $transaction);
  40. }
  41. $query = new EntityFieldQuery();
  42. $result = $query->entityCondition('entity_type', 'tmgmt_remote')
  43. ->propertyCondition('tjiid', $ids)
  44. ->execute();
  45. if (!empty($result['tmgmt_remote'])) {
  46. $controller = entity_get_controller('tmgmt_remote');
  47. $controller->delete(array_keys($result['tmgmt_remote']), $transaction);
  48. }
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function invoke($hook, $entity) {
  54. // We need to check whether the state of the job is affected by this
  55. // deletion.
  56. if ($hook == 'delete' && $job = $entity->getJob()) {
  57. // We only care for active jobs.
  58. if ($job->isActive() && tmgmt_job_check_finished($job->tjid)) {
  59. // Mark the job as finished.
  60. $job->finished();
  61. }
  62. }
  63. parent::invoke($hook, $entity);
  64. }
  65. }