/app/Model/TaskExport.php

https://gitlab.com/x33n/kanboard · PHP · 143 lines · 91 code · 14 blank · 38 comment · 3 complexity · f720aa78578a2f75aabd338373022779 MD5 · raw file

  1. <?php
  2. namespace Model;
  3. use PDO;
  4. /**
  5. * Task Export model
  6. *
  7. * @package model
  8. * @author Frederic Guillot
  9. */
  10. class TaskExport extends Base
  11. {
  12. /**
  13. * Fetch tasks and return the prepared CSV
  14. *
  15. * @access public
  16. * @param integer $project_id Project id
  17. * @param mixed $from Start date (timestamp or user formatted date)
  18. * @param mixed $to End date (timestamp or user formatted date)
  19. * @return array
  20. */
  21. public function export($project_id, $from, $to)
  22. {
  23. $tasks = $this->getTasks($project_id, $from, $to);
  24. $swimlanes = $this->swimlane->getList($project_id);
  25. $results = array($this->getColumns());
  26. foreach ($tasks as &$task) {
  27. $results[] = array_values($this->format($task, $swimlanes));
  28. }
  29. return $results;
  30. }
  31. /**
  32. * Get the list of tasks for a given project and date range
  33. *
  34. * @access public
  35. * @param integer $project_id Project id
  36. * @param mixed $from Start date (timestamp or user formatted date)
  37. * @param mixed $to End date (timestamp or user formatted date)
  38. * @return array
  39. */
  40. public function getTasks($project_id, $from, $to)
  41. {
  42. $sql = '
  43. SELECT
  44. tasks.id,
  45. projects.name AS project_name,
  46. tasks.is_active,
  47. project_has_categories.name AS category_name,
  48. tasks.swimlane_id,
  49. columns.title AS column_title,
  50. tasks.position,
  51. tasks.color_id,
  52. tasks.date_due,
  53. creators.username AS creator_username,
  54. users.username AS assignee_username,
  55. tasks.score,
  56. tasks.title,
  57. tasks.date_creation,
  58. tasks.date_modification,
  59. tasks.date_completed,
  60. tasks.date_started,
  61. tasks.time_estimated,
  62. tasks.time_spent
  63. FROM tasks
  64. LEFT JOIN users ON users.id = tasks.owner_id
  65. LEFT JOIN users AS creators ON creators.id = tasks.creator_id
  66. LEFT JOIN project_has_categories ON project_has_categories.id = tasks.category_id
  67. LEFT JOIN columns ON columns.id = tasks.column_id
  68. LEFT JOIN projects ON projects.id = tasks.project_id
  69. WHERE tasks.date_creation >= ? AND tasks.date_creation <= ? AND tasks.project_id = ?
  70. ORDER BY tasks.id ASC
  71. ';
  72. if (! is_numeric($from)) {
  73. $from = $this->dateParser->removeTimeFromTimestamp($this->dateParser->getTimestamp($from));
  74. }
  75. if (! is_numeric($to)) {
  76. $to = $this->dateParser->removeTimeFromTimestamp(strtotime('+1 day', $this->dateParser->getTimestamp($to)));
  77. }
  78. $rq = $this->db->execute($sql, array($from, $to, $project_id));
  79. return $rq->fetchAll(PDO::FETCH_ASSOC);
  80. }
  81. /**
  82. * Format the output of a task array
  83. *
  84. * @access public
  85. * @param array $task Task properties
  86. * @param array $swimlanes List of swimlanes
  87. * @return array
  88. */
  89. public function format(array &$task, array &$swimlanes)
  90. {
  91. $colors = $this->color->getList();
  92. $task['is_active'] = $task['is_active'] == Task::STATUS_OPEN ? e('Open') : e('Closed');
  93. $task['color_id'] = $colors[$task['color_id']];
  94. $task['score'] = $task['score'] ?: 0;
  95. $task['swimlane_id'] = isset($swimlanes[$task['swimlane_id']]) ? $swimlanes[$task['swimlane_id']] : '?';
  96. $this->dateParser->format($task, array('date_due', 'date_modification', 'date_creation', 'date_started', 'date_completed'), 'Y-m-d');
  97. return $task;
  98. }
  99. /**
  100. * Get column titles
  101. *
  102. * @access public
  103. * @return string[]
  104. */
  105. public function getColumns()
  106. {
  107. return array(
  108. e('Task Id'),
  109. e('Project'),
  110. e('Status'),
  111. e('Category'),
  112. e('Swimlane'),
  113. e('Column'),
  114. e('Position'),
  115. e('Color'),
  116. e('Due date'),
  117. e('Creator'),
  118. e('Assignee'),
  119. e('Complexity'),
  120. e('Title'),
  121. e('Creation date'),
  122. e('Modification date'),
  123. e('Completion date'),
  124. e('Start date'),
  125. e('Time estimated'),
  126. e('Time spent'),
  127. );
  128. }
  129. }