BxTasksDb.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. 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)
  40. {
  41. $CNF = &$this->_oConfig->CNF;
  42. $aBindings = [
  43. $CNF['FIELD_ALLOW_VIEW_TO'] => $iContextId,
  44. $CNF['FIELD_TASKLIST'] => $iListId
  45. ];
  46. $sWhereClause = " AND `" . $CNF['FIELD_ALLOW_VIEW_TO'] . "` = :" . $CNF['FIELD_ALLOW_VIEW_TO'] . " AND `" . $CNF['FIELD_TASKLIST'] . "` = :" . $CNF['FIELD_TASKLIST'];
  47. $oCf = BxDolContentFilter::getInstance();
  48. if($oCf->isEnabled())
  49. $sWhereClause .= $oCf->getSQLParts($CNF['TABLE_ENTRIES'], $CNF['FIELD_CF']);
  50. return $this->getAll("SELECT * FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE 1" . $sWhereClause, $aBindings);
  51. }
  52. public function getEntriesByDate($sDateFrom, $sDateTo, $aSQLPart = array())
  53. {
  54. // validate input data
  55. if (false === ($oDateFrom = date_create($sDateFrom, new DateTimeZone('UTC'))))
  56. return array();
  57. if (false === ($oDateTo = date_create($sDateTo, new DateTimeZone('UTC'))))
  58. return array();
  59. if ($oDateFrom > $oDateTo)
  60. return array();
  61. // increase start and end date to cover timezones
  62. $oDateFrom = $oDateFrom->sub(new DateInterval("P1D"));
  63. $oDateTo = $oDateTo->add(new DateInterval("P1D"));
  64. // look throught all days in the interval
  65. $oDateIter = clone($oDateFrom);
  66. $aEntries = array();
  67. while ($oDateIter->format('Y-m-d') != $oDateTo->format('Y-m-d')) {
  68. $oDateMin = date_create($oDateIter->format('Y-m-d') . '00:00:00', new DateTimeZone('UTC'));
  69. $oDateMax = date_create($oDateIter->format('Y-m-d') . '23:59:59', new DateTimeZone('UTC'));
  70. // get all events for the specific day
  71. $oDateMonthBegin = date_create($oDateIter->format('Y-m-01'), new DateTimeZone('UTC'));
  72. $iWeekOfMonth = $oDateIter->format('W') - $oDateMonthBegin->format('W') + 1;
  73. $aBindings = array(
  74. 'timestamp_min' => $oDateMin->getTimestamp(),
  75. 'timestamp_max' => $oDateMax->getTimestamp(),
  76. );
  77. $sWhere = isset($aSQLPart['where']) ? $aSQLPart['where'] : '';
  78. $a = $this->getAll("SELECT DISTINCT `bx_tasks_tasks`.`id`, `bx_tasks_tasks`.`title` AS `title`, `bx_tasks_tasks`.`due_date`
  79. FROM `bx_tasks_tasks`
  80. WHERE (`bx_tasks_tasks`.`due_date` >= :timestamp_min AND `bx_tasks_tasks`.`due_date` <= :timestamp_max ) $sWhere
  81. ", $aBindings);
  82. // prepare variables for each event
  83. $sCurrentDay = $oDateIter->format('Y-m-d');
  84. foreach ($a as $k => $r) {
  85. $oDateStart = new DateTime();
  86. $oDateStart->setTimestamp($r['due_date']);
  87. $oDateStart->setTimezone(new DateTimeZone('UTC'));
  88. $oDateEnd = new DateTime();
  89. $oDateEnd->setTimestamp($r['due_date']);
  90. $oDateEnd->setTimezone(new DateTimeZone('UTC'));
  91. $oDuration = $oDateStart->diff($oDateEnd);
  92. $sHoursStart = $oDateStart->format('H:i:s');
  93. $oStart = date_create($sCurrentDay . ' ' . $sHoursStart, new DateTimeZone('UTC'));
  94. $oEnd = $oStart ? clone($oStart) : null;
  95. $oEnd = $oEnd ? $oEnd->add($oDuration) : null;
  96. $a[$k]['start'] = $oStart ? $oStart->format('c') : 0;
  97. $a[$k]['end'] = $oEnd ? $oEnd->format('c') : 0;
  98. $a[$k]['start_utc'] = $oStart ? $oStart->getTimestamp() : 0;
  99. $a[$k]['end_utc'] = $oEnd ? $oEnd->getTimestamp() : 0;
  100. $a[$k]['url'] = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $this->_oConfig->CNF['URI_VIEW_ENTRY'] . '&id=' . $r['id']));
  101. }
  102. // merge with all other events
  103. $aEntries = array_merge($aEntries, $a);
  104. // go to the next day
  105. $oDateIter = $oDateIter->add(new DateInterval("P1D"));
  106. }
  107. return $aEntries;
  108. }
  109. public function expireEntries()
  110. {
  111. $CNF = $this->_oConfig->CNF;
  112. $aResult = $this->getColumn("SELECT `id` FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `" . $CNF['FIELD_DUEDATE'] . "` < UNIX_TIMESTAMP() AND `" . $CNF['FIELD_EXPIRED'] . "` = '0'");
  113. if(empty($aResult) || !is_array($aResult))
  114. return false;
  115. return count($aResult) == (int)$this->query("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET `" . $CNF['FIELD_EXPIRED'] . "` = '1' WHERE `id` IN (" . $this->implode_escape($aResult) . ")") ? $aResult : false;
  116. }
  117. }
  118. /** @} */