BxTasksDb.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. * @defgroup Tasks Tasks
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. /*
  11. * Module database queries
  12. */
  13. class BxTasksDb extends BxBaseModTextDb
  14. {
  15. public function __construct(&$oConfig)
  16. {
  17. parent::__construct($oConfig);
  18. }
  19. public function getLists ($iContextId = 0)
  20. {
  21. $CNF = &$this->_oConfig->CNF;
  22. $sQuery = $this->prepare ("SELECT * FROM `" . $CNF['TABLE_LISTS'] . "` WHERE `context_id` = ?", $iContextId);
  23. return $this->getAll($sQuery);
  24. }
  25. public function getList ($iId = 0)
  26. {
  27. $CNF = &$this->_oConfig->CNF;
  28. $sQuery = $this->prepare ("SELECT * FROM `" . $CNF['TABLE_LISTS'] . "` WHERE `id` = ?", $iId);
  29. return $this->getRow($sQuery);
  30. }
  31. public function deleteList ($iId = 0)
  32. {
  33. $CNF = &$this->_oConfig->CNF;
  34. $sQuery = $this->prepare ("DELETE FROM `" . $CNF['TABLE_LISTS'] . "` WHERE `id` = ?", $iId);
  35. $this->query($sQuery);
  36. $sQuery = $this->prepare ("DELETE FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `" . $CNF['FIELD_TASKLIST'] . "` = ?", $iId);
  37. $this->query($sQuery);
  38. }
  39. public function getTasks ($iContextId = 0, $iListId = 0, $bWithStats = false)
  40. {
  41. $CNF = &$this->_oConfig->CNF;
  42. $aBindings = [];
  43. $sSelectClause = "`te`.*";
  44. $sJoinClause = $sWhereClause = "";
  45. if(($sField = $CNF['FIELD_ALLOW_VIEW_TO']) && $iContextId) {
  46. $aBindings[$sField] = $iContextId;
  47. $sWhereClause .= " AND `te`.`" . $sField . "` = :" . $sField;
  48. }
  49. if(($sField = $CNF['FIELD_TASKLIST']) && $iListId) {
  50. $aBindings[$sField] = $iListId;
  51. $sWhereClause .= " AND `te`.`" . $sField . "` = :" . $sField;
  52. }
  53. if(($oCf = BxDolContentFilter::getInstance()) && $oCf->isEnabled())
  54. $sWhereClause .= $oCf->getSQLParts('te', $CNF['FIELD_CF']);
  55. if($bWithStats) {
  56. $sSelectClause .= $this->prepareAsString(", (SELECT SUM(`value`) FROM `" . $CNF['TABLE_TIME_TRACK'] . "` WHERE `object_id`=`te`.`id` AND `author_id`=?) AS `time`, `tt`.`sum` AS `time_total`", bx_get_logged_profile_id());
  57. $sJoinClause .= " LEFT JOIN `" . $CNF['TABLE_TIME'] . "` AS `tt` ON `te`.`id`=`tt`.`object_id`";
  58. }
  59. return $this->getAll("SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_ENTRIES'] . "` AS `te`" . $sJoinClause . " WHERE 1" . $sWhereClause, $aBindings);
  60. }
  61. public function getEntriesByDate($sDateFrom, $sDateTo, $aSQLPart = array())
  62. {
  63. // validate input data
  64. if (false === ($oDateFrom = date_create($sDateFrom, new DateTimeZone('UTC'))))
  65. return array();
  66. if (false === ($oDateTo = date_create($sDateTo, new DateTimeZone('UTC'))))
  67. return array();
  68. if ($oDateFrom > $oDateTo)
  69. return array();
  70. // increase start and end date to cover timezones
  71. $oDateFrom = $oDateFrom->sub(new DateInterval("P1D"));
  72. $oDateTo = $oDateTo->add(new DateInterval("P1D"));
  73. // look throught all days in the interval
  74. $oDateIter = clone($oDateFrom);
  75. $aEntries = array();
  76. while ($oDateIter->format('Y-m-d') != $oDateTo->format('Y-m-d')) {
  77. $oDateMin = date_create($oDateIter->format('Y-m-d') . '00:00:00', new DateTimeZone('UTC'));
  78. $oDateMax = date_create($oDateIter->format('Y-m-d') . '23:59:59', new DateTimeZone('UTC'));
  79. // get all events for the specific day
  80. $oDateMonthBegin = date_create($oDateIter->format('Y-m-01'), new DateTimeZone('UTC'));
  81. $iWeekOfMonth = $oDateIter->format('W') - $oDateMonthBegin->format('W') + 1;
  82. $aBindings = array(
  83. 'timestamp_min' => $oDateMin->getTimestamp(),
  84. 'timestamp_max' => $oDateMax->getTimestamp(),
  85. );
  86. $sWhere = isset($aSQLPart['where']) ? $aSQLPart['where'] : '';
  87. $a = $this->getAll("SELECT DISTINCT `bx_tasks_tasks`.`id`, `bx_tasks_tasks`.`title` AS `title`, `bx_tasks_tasks`.`due_date`
  88. FROM `bx_tasks_tasks`
  89. WHERE (`bx_tasks_tasks`.`due_date` >= :timestamp_min AND `bx_tasks_tasks`.`due_date` <= :timestamp_max ) $sWhere
  90. ", $aBindings);
  91. // prepare variables for each event
  92. $sCurrentDay = $oDateIter->format('Y-m-d');
  93. foreach ($a as $k => $r) {
  94. $oDateStart = new DateTime();
  95. $oDateStart->setTimestamp($r['due_date']);
  96. $oDateStart->setTimezone(new DateTimeZone('UTC'));
  97. $oDateEnd = new DateTime();
  98. $oDateEnd->setTimestamp($r['due_date']);
  99. $oDateEnd->setTimezone(new DateTimeZone('UTC'));
  100. $oDuration = $oDateStart->diff($oDateEnd);
  101. $sHoursStart = $oDateStart->format('H:i:s');
  102. $oStart = date_create($sCurrentDay . ' ' . $sHoursStart, new DateTimeZone('UTC'));
  103. $oEnd = $oStart ? clone($oStart) : null;
  104. $oEnd = $oEnd ? $oEnd->add($oDuration) : null;
  105. $a[$k]['start'] = $oStart ? $oStart->format('Y-m-d') : 0;
  106. $a[$k]['start_utc'] = $oStart ? $oStart->getTimestamp() : 0;
  107. $a[$k]['url'] = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $this->_oConfig->CNF['URI_VIEW_ENTRY'] . '&id=' . $r['id']));
  108. }
  109. // merge with all other events
  110. $aEntries = array_merge($aEntries, $a);
  111. // go to the next day
  112. $oDateIter = $oDateIter->add(new DateInterval("P1D"));
  113. }
  114. return $aEntries;
  115. }
  116. public function expireEntries()
  117. {
  118. $CNF = $this->_oConfig->CNF;
  119. $aResult = $this->getColumn("SELECT `id` FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `" . $CNF['FIELD_DUE_DATE'] . "` < UNIX_TIMESTAMP() AND `" . $CNF['FIELD_EXPIRED'] . "` = '0'");
  120. if(empty($aResult) || !is_array($aResult))
  121. return false;
  122. return count($aResult) == (int)$this->query("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET `" . $CNF['FIELD_EXPIRED'] . "` = '1' WHERE `id` IN (" . $this->implode_escape($aResult) . ")") ? $aResult : false;
  123. }
  124. public function getTimeTracks($aParams = [])
  125. {
  126. $CNF = &$this->_oConfig->CNF;
  127. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  128. $sSelectClause = '`ttt`.*';
  129. $sJoinClause = $sWhereClause = $sOrderClause = '';
  130. if(!empty($aParams))
  131. switch($aParams['sample']) {
  132. case 'id':
  133. $aMethod['name'] = 'getRow';
  134. $aMethod['params'][1] = [
  135. 'id' => $aParams['id']
  136. ];
  137. $sWhereClause = "AND `ttt`.`id` = :id";
  138. break;
  139. case 'assignees':
  140. $aMethod['name'] = 'getColumn';
  141. $sSelectClause = 'DISTINCT `ttt`.`author_id`';
  142. break;
  143. }
  144. if(!empty($sOrderClause))
  145. $sOrderClause = "ORDER BY " . $sOrderClause;
  146. $aMethod['params'][0] = "SELECT
  147. " . $sSelectClause . "
  148. FROM `" . $CNF['TABLE_TIME_TRACK'] . "` AS `ttt` " . $sJoinClause . "
  149. WHERE 1 " . $sWhereClause . " " . $sOrderClause;
  150. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  151. }
  152. }
  153. /** @} */