BxTasksDb.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 getTasks ($iContextId = 0, $iListId = 0)
  32. {
  33. $CNF = &$this->_oConfig->CNF;
  34. $sQuery = $this->prepare ("SELECT * FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `" . $CNF['FIELD_ALLOW_VIEW_TO'] . "` = ? AND `" . $CNF['FIELD_TASKLIST'] . "` = ?", $iContextId, $iListId);
  35. return $this->getAll($sQuery);
  36. }
  37. public function getEntriesByDate($sDateFrom, $sDateTo, $aSQLPart = array())
  38. {
  39. // validate input data
  40. if (false === ($oDateFrom = date_create($sDateFrom, new DateTimeZone('UTC'))))
  41. return array();
  42. if (false === ($oDateTo = date_create($sDateTo, new DateTimeZone('UTC'))))
  43. return array();
  44. if ($oDateFrom > $oDateTo)
  45. return array();
  46. // increase start and end date to cover timezones
  47. $oDateFrom = $oDateFrom->sub(new DateInterval("P1D"));
  48. $oDateTo = $oDateTo->add(new DateInterval("P1D"));
  49. // look throught all days in the interval
  50. $oDateIter = clone($oDateFrom);
  51. $aEntries = array();
  52. while ($oDateIter->format('Y-m-d') != $oDateTo->format('Y-m-d')) {
  53. $oDateMin = date_create($oDateIter->format('Y-m-d') . '00:00:00', new DateTimeZone('UTC'));
  54. $oDateMax = date_create($oDateIter->format('Y-m-d') . '23:59:59', new DateTimeZone('UTC'));
  55. // get all events for the specific day
  56. $oDateMonthBegin = date_create($oDateIter->format('Y-m-01'), new DateTimeZone('UTC'));
  57. $iWeekOfMonth = $oDateIter->format('W') - $oDateMonthBegin->format('W') + 1;
  58. $aBindings = array(
  59. 'timestamp_min' => $oDateMin->getTimestamp(),
  60. 'timestamp_max' => $oDateMax->getTimestamp(),
  61. );
  62. $sWhere = isset($aSQLPart['where']) ? $aSQLPart['where'] : '';
  63. $a = $this->getAll("SELECT DISTINCT `bx_tasks_tasks`.`id`, `bx_tasks_tasks`.`title` AS `title`, `bx_tasks_tasks`.`due_date`
  64. FROM `bx_tasks_tasks`
  65. WHERE (`bx_tasks_tasks`.`due_date` >= :timestamp_min AND `bx_tasks_tasks`.`due_date` <= :timestamp_max ) $sWhere
  66. ", $aBindings);
  67. // prepare variables for each event
  68. $sCurrentDay = $oDateIter->format('Y-m-d');
  69. foreach ($a as $k => $r) {
  70. $oDateStart = new DateTime();
  71. $oDateStart->setTimestamp($r['due_date']);
  72. $oDateStart->setTimezone(new DateTimeZone('UTC'));
  73. $oDateEnd = new DateTime();
  74. $oDateEnd->setTimestamp($r['due_date']);
  75. $oDateEnd->setTimezone(new DateTimeZone('UTC'));
  76. $oDuration = $oDateStart->diff($oDateEnd);
  77. $sHoursStart = $oDateStart->format('H:i:s');
  78. $oStart = date_create($sCurrentDay . ' ' . $sHoursStart, new DateTimeZone('UTC'));
  79. $oEnd = $oStart ? clone($oStart) : null;
  80. $oEnd = $oEnd ? $oEnd->add($oDuration) : null;
  81. $a[$k]['start'] = $oStart ? $oStart->format('c') : 0;
  82. $a[$k]['end'] = $oEnd ? $oEnd->format('c') : 0;
  83. $a[$k]['start_utc'] = $oStart ? $oStart->getTimestamp() : 0;
  84. $a[$k]['end_utc'] = $oEnd ? $oEnd->getTimestamp() : 0;
  85. $a[$k]['url'] = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $this->_oConfig->CNF['URI_VIEW_ENTRY'] . '&id=' . $r['id']);
  86. }
  87. // merge with all other events
  88. $aEntries = array_merge($aEntries, $a);
  89. // go to the next day
  90. $oDateIter = $oDateIter->add(new DateInterval("P1D"));
  91. }
  92. return $aEntries;
  93. }
  94. }
  95. /** @} */