index.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. if (cfr('TASKMANTIMING')) {
  3. class TaskmanTimingReport {
  4. /**
  5. * Contains task real done dates as taskid=>data
  6. *
  7. * @var array
  8. */
  9. protected $doneTasks = array();
  10. /**
  11. * Base module URL
  12. */
  13. const URL_ME = '?module=taskmantiming';
  14. /**
  15. * Task preview URL
  16. */
  17. const URL_TASK = '?module=taskman&edittask=';
  18. /**
  19. * Creates new taskman timing report instance
  20. */
  21. public function __construct() {
  22. $this->loadTasksDone();
  23. }
  24. /**
  25. * Loads task done dates from database
  26. *
  27. * @return void
  28. */
  29. protected function loadTasksDone() {
  30. $qeury = "SELECT * from `taskmandone`";
  31. $all = simple_queryall($qeury);
  32. if (!empty($all)) {
  33. foreach ($all as $io => $each) {
  34. $this->doneTasks[$each['taskid']] = $each;
  35. }
  36. }
  37. }
  38. /**
  39. * Returns default date search form
  40. *
  41. * @return string
  42. */
  43. public function renderSearchForm() {
  44. $result = '';
  45. $dateFrom = (wf_CheckPost(array('datefrom'))) ? $_POST['datefrom'] : date("Y-m-") . '01';
  46. $dateTo = (wf_CheckPost(array('dateto'))) ? $_POST['dateto'] : curdate();
  47. $inputs = wf_DatePickerPreset('datefrom', $dateFrom, true) . ' ' . __('From') . ' ';
  48. $inputs.=wf_DatePickerPreset('dateto', $dateTo, true) . ' ' . __('To') . ' ';
  49. $inputs.= wf_Submit(__('Show'));
  50. $result.=wf_Form('', 'POST', $inputs, 'glamour');
  51. return ($result);
  52. }
  53. /**
  54. * Renders container for report
  55. *
  56. * @return string
  57. */
  58. public function renderReportContainer() {
  59. $dateFrom = (wf_CheckPost(array('datefrom'))) ? $_POST['datefrom'] : date("Y-m-") . '01';
  60. $dateTo = (wf_CheckPost(array('dateto'))) ? $_POST['dateto'] : curdate();
  61. $result = '';
  62. /**
  63. * Shizukesa ga shimikomu you de iki wo tometa gozen goji
  64. * Hijou kaidan de tsume wo kamu asu wa docchi da? THE DAY HAS COME
  65. */
  66. $columns = array('ID', 'Task address', 'Job type', 'Task creation date', 'Target date', 'Finish date', 'Time', 'From creation', 'Reality');
  67. $opts = '"order": [[ 0, "desc" ]]';
  68. $result.=wf_JqDtLoader($columns, self::URL_ME . '&ajax=true&datefrom=' . $dateFrom . '&dateto=' . $dateTo, false, __('Tasks'), 100, $opts);
  69. return ($result);
  70. }
  71. /**
  72. * Returns array of done tasks by some period
  73. *
  74. * @param string $dateFrom
  75. * @param string $dateTo
  76. *
  77. * @return array
  78. */
  79. protected function getDoneTasks($dateFrom, $dateTo) {
  80. $result = array();
  81. $query = "SELECT * from `taskman` WHERE `status`='1' AND `date` BETWEEN '" . $dateFrom . "' AND '" . $dateTo . " 23:59:59'";
  82. $all = simple_queryall($query);
  83. if (!empty($all)) {
  84. foreach ($all as $io => $each) {
  85. $result[$each['id']] = $each;
  86. }
  87. }
  88. return ($result);
  89. }
  90. /**
  91. * Renders time duration in seconds into formatted human-readable view in days
  92. *
  93. * @param int $seconds
  94. *
  95. * @return string
  96. */
  97. function formatTimeDays($seconds) {
  98. $init = $seconds;
  99. $days = floor($seconds / 86400);
  100. if ($init >= 86400) {
  101. $result = $days . ' ' . __('days');
  102. } else {
  103. if ($init > 0) {
  104. $result = '0 ' . __('days');
  105. } else {
  106. $result = $days . ' ' . __('days');
  107. }
  108. }
  109. return ($result);
  110. }
  111. /**
  112. * Renders JSON data for report
  113. *
  114. * @return void
  115. */
  116. public function ajaxSearchReply() {
  117. $dateFrom = (wf_CheckGet(array('datefrom'))) ? $_GET['datefrom'] : date("Y-m-") . '01';
  118. $dateTo = (wf_CheckGet(array('dateto'))) ? $_GET['dateto'] : curdate();
  119. $json = new wf_JqDtHelper();
  120. $allTasks = $this->getDoneTasks($dateFrom, $dateTo);
  121. if (!empty($allTasks)) {
  122. $allJobTypes = ts_GetAllJobtypes();
  123. foreach ($allTasks as $io => $each) {
  124. $startTimestamp = strtotime($each['startdate']);
  125. $endTimestamp = strtotime($each['enddate']);
  126. $creationTimestamp = strtotime($each['date']);
  127. $creationDays = date("Y-m-d", $creationTimestamp);
  128. $creationDays = strtotime($creationDays);
  129. $deltaPlanned = ($endTimestamp >= $startTimestamp) ? $endTimestamp - $startTimestamp : '-' . ($startTimestamp - $endTimestamp);
  130. $deltaReal = ($endTimestamp >= $creationDays) ? $endTimestamp - $creationDays : '-' . ($creationDays - $endTimestamp);
  131. $cruelReality = '';
  132. if (isset($this->doneTasks[$each['id']])) {
  133. $taskDoneDate = $this->doneTasks[$each['id']]['date'];
  134. $taskDoneTimestamp = strtotime($taskDoneDate);
  135. $cruelReality = $taskDoneTimestamp - $creationTimestamp;
  136. if ($cruelReality > 0) {
  137. $cruelReality = zb_formatTime($cruelReality);
  138. }
  139. }
  140. $data[] = wf_Link(self::URL_TASK . $each['id'], $each['id']);
  141. $data[] = $each['address'];
  142. $data[] = $allJobTypes[$each['jobtype']];
  143. $data[] = $each['date'];
  144. $data[] = $each['startdate'];
  145. $data[] = $each['enddate'];
  146. $data[] = $this->formatTimeDays($deltaPlanned);
  147. $data[] = $this->formatTimeDays($deltaReal);
  148. $data[] = $cruelReality;
  149. $json->addRow($data);
  150. unset($data);
  151. }
  152. }
  153. $json->getJson();
  154. }
  155. }
  156. $taskmanTiming = new TaskmanTimingReport();
  157. if (wf_CheckGet(array('ajax', 'datefrom', 'dateto'))) {
  158. $taskmanTiming->ajaxSearchReply();
  159. }
  160. show_window('', wf_BackLink('?module=taskman'));
  161. show_window(__('Task timing report'), $taskmanTiming->renderSearchForm());
  162. show_window('', $taskmanTiming->renderReportContainer());
  163. } else {
  164. show_error(__('Access denied'));
  165. }
  166. ?>