tmgmt_local.controller.task_item.inc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * @file
  4. * Contains the task item controller.
  5. */
  6. /**
  7. * Controller class for the local task entity.
  8. *
  9. * @ingroup tmgmt_local_task
  10. */
  11. class TMGMTLocalTaskItemController extends EntityAPIController {
  12. /**
  13. * {@inheritdoc}
  14. *
  15. * @todo Eliminate the need to flatten and unflatten the TaskItem data.
  16. */
  17. public function save($entity, DatabaseTransaction $transaction = NULL) {
  18. // Consider everything translated when the job item is translated.
  19. if ($entity->isCompleted()) {
  20. $entity->count_untranslated = 0;
  21. $entity->count_translated = count(tmgmt_flatten_data($entity->data));
  22. $entity->count_completed = 0;
  23. }
  24. // Consider everything completed if the job is completed.
  25. elseif ($entity->isClosed()) {
  26. $entity->count_untranslated = 0;
  27. $entity->count_translated = 0;
  28. $entity->count_completed = count(tmgmt_flatten_data($entity->data));
  29. }
  30. // Count the data item states.
  31. else {
  32. // Start with assuming that all data is untranslated, then go through it
  33. // and count translated data.
  34. $entity->count_untranslated = count(array_filter(tmgmt_flatten_data($entity->getJobItem()->getData()), '_tmgmt_filter_data'));
  35. $entity->count_translated = 0;
  36. $entity->count_completed = 0;
  37. $this->count($entity->data, $entity);
  38. }
  39. return parent::save($entity, $transaction);
  40. }
  41. /**
  42. * Parse all data items recursively and sums up the counters for
  43. * accepted, translated and pending items.
  44. *
  45. * @param $item
  46. * The current data item.
  47. * @param $entity
  48. * The job item the count should be calculated.
  49. */
  50. protected function count(&$item, $entity) {
  51. if (!empty($item['#text'])) {
  52. if (_tmgmt_filter_data($item)) {
  53. // Set default states if no state is set.
  54. if (!isset($item['#status'])) {
  55. $item['#status'] = TMGMT_DATA_ITEM_STATE_UNTRANSLATED;
  56. }
  57. switch ($item['#status']) {
  58. case TMGMT_DATA_ITEM_STATE_TRANSLATED:
  59. $entity->count_untranslated--;
  60. $entity->count_translated++;
  61. break;
  62. }
  63. }
  64. }
  65. else {
  66. foreach (element_children($item) as $key) {
  67. $this->count($item[$key], $entity);
  68. }
  69. }
  70. }
  71. }