BxTasksTime.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. require_once('BxTasksTimeQuery.php');
  11. class BxTasksTime extends BxTemplReport
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  16. {
  17. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  18. $this->_sModule = 'bx_tasks';
  19. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  20. $CNF = &$this->_oModule->_oConfig->CNF;
  21. $this->_oQuery = new BxTasksTimeQuery($this);
  22. $this->_bUndo = false;
  23. $this->_bProcessible = false;
  24. $this->_sFormObject = $CNF['OBJECT_FORM_TIME'];
  25. $this->_sFormDisplayPost = $CNF['OBJECT_FORM_TIME_DISPLAY_ADD'];
  26. array_walk($this->_aHtmlIds, function(&$sVa1ue) {
  27. $sVa1ue = str_replace('bx-report', 'bx-tasks', $sVa1ue);
  28. });
  29. }
  30. public function report($aParams = [])
  31. {
  32. $mixedResult = parent::report($aParams);
  33. if(!empty($mixedResult) && is_array($mixedResult) && isset($mixedResult['popup'], $mixedResult['popup_id']))
  34. $mixedResult['popup'] = [
  35. 'html' => $mixedResult['popup'],
  36. 'options' => ['closeOnOuterClick' => false]
  37. ];
  38. return $mixedResult;
  39. }
  40. /**
  41. * Should always return false to allow any number of time reports per user.
  42. */
  43. public function isPerformed($iObjectId, $iAuthorId, $iAuthorIp = 0)
  44. {
  45. return false;
  46. }
  47. public function putReport($iObjectId, $iAuthorId, $mixedTrack, $bUndo = false)
  48. {
  49. if(!$this->_oQuery->putReport($iObjectId, $iAuthorId, $bUndo))
  50. return false;
  51. $aTrack = is_array($mixedTrack) ? $mixedTrack : $this->_oQuery->getTrackBy(['type' => 'id', 'id' => (int)$mixedTrack]);
  52. if(empty($aTrack) || !is_array($aTrack))
  53. return false;
  54. if(!$bUndo && empty($aTrack['value_date']) && !$this->_oQuery->updateTrack(['value_date' => time()], ['id' => $aTrack['id']]))
  55. return false;
  56. if(!$this->_oQuery->updateReport($iObjectId, $aTrack['value'], $bUndo))
  57. return false;
  58. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
  59. /*
  60. * TODO: May be we need to send notification to Context admins.
  61. *
  62. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_Reported', [
  63. 'report_type' => $sType,
  64. 'report_text' => $sText,
  65. 'report_url' => $this->getBaseUrl(),
  66. ]);
  67. if($aTemplate)
  68. sendMail(getParam('site_email'), $aTemplate['Subject'], $aTemplate['Body']);
  69. */
  70. $this->_trigger();
  71. /**
  72. * @hooks
  73. * @hookdef bx_tasks-report_time '{module_name}', 'report_time' - hook on create new time report
  74. * - $unit_name - module name
  75. * - $action - equals `report_time`
  76. * - $object_id - reported entry ID
  77. * - $sender_id - profile id for report's author
  78. * - $extra_params - array of additional params with the following array keys:
  79. * - `object_system` - [string] system name
  80. * - `object_author_id` - [int] author's profile_id for reported object_id
  81. * - `report_id` - [int] report id
  82. * - `report_author_id` - [int] profile id for report's author
  83. * - `type` - [string] reported time
  84. * @hook @ref hook-bx_tasks-report_time
  85. */
  86. bx_alert($this->_sModule, 'report_time', $iObjectId, $iAuthorId, [
  87. 'object_system' => $this->_sSystem,
  88. 'object_author_id' => $iObjectAuthorId,
  89. 'report_id' => $aTrack['id'],
  90. 'report_author_id' => $iAuthorId,
  91. 'value' => $aTrack['value']
  92. ]);
  93. return true;
  94. }
  95. protected function _report($bPerformed, $aParams, &$oForm)
  96. {
  97. $iAuthorId = $this->_getAuthorId();
  98. $iAuthorNip = bx_get_ip_hash($this->_getAuthorIp());
  99. $iObjectId = $this->_bApi ? $this->_iId : $oForm->getCleanValue('object_id');
  100. if(!$this->isAllowedReport(true))
  101. return ['code' => 2, 'message' => $this->msgErrAllowedReport()];
  102. $iVh = ($sKey = 'value_h') && $this->_bApi ? $aParams[$sKey] : $oForm->getCleanValue($sKey);
  103. $iVm = ($sKey = 'value_m') && $this->_bApi ? $aParams[$sKey] : $oForm->getCleanValue($sKey);
  104. $iValue = $this->_oModule->_oConfig->timeA2I([$iVh, $iVm]);
  105. $sText = $this->_bApi ? $aParams['text'] : $oForm->getCleanValue('text');
  106. $sText = bx_process_input($sText);
  107. $iId = (int)$oForm->insert(['object_id' => $iObjectId, 'author_id' => $iAuthorId, 'author_nip' => $iAuthorNip, 'value' => $iValue, 'text' => $sText, 'date' => time()]);
  108. if($iId != 0 && $this->putReport($iObjectId, $iAuthorId, $iId)) {
  109. $aReport = $this->_getReport($iObjectId, true);
  110. $aResult = $this->_returnReportData($iObjectId, $iAuthorId, $iId, $aReport, !$bPerformed);
  111. if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled())
  112. $oSockets->sendEvent($this->getSocketName(), $iObjectId, 'reported', json_encode($this->_returnReportDataForSocket($aResult)));
  113. return $aResult;
  114. }
  115. return ['code' => 3, 'message' => _t('_report_err_cannot_perform_action')];
  116. }
  117. protected function _returnReportData($iObjectId, $iAuthorId, $iReportId, $aData, $bPerformed)
  118. {
  119. return parent::_returnReportData($iObjectId, $iAuthorId, $iReportId, $aData, false);
  120. }
  121. protected function _getReportedBy()
  122. {
  123. $aTmplReports = [];
  124. $aReports = $this->_oQuery->getPerformedBy($this->getId());
  125. foreach($aReports as $aReport) {
  126. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($aReport['author_id']);
  127. $sText = bx_process_output($aReport['text'], BX_DATA_TEXT_MULTILINE);
  128. $aTmplReports[] = [
  129. 'style_prefix' => $this->_sStylePrefix,
  130. 'user_unit' => $sUserUnit,
  131. 'value' => $this->_oModule->_oConfig->timeI2S($aReport['value']),
  132. 'date' => $aReport['value_date'] ? bx_time_js($aReport['value_date'], BX_FORMAT_DATE, true) : '',
  133. 'bx_if:show_text' => [
  134. 'condition' => strlen($sText) > 0,
  135. 'content' => [
  136. 'text' => $sText
  137. ]
  138. ]
  139. ];
  140. }
  141. if(empty($aTmplReports))
  142. $aTmplReports = MsgBox(_t('_Empty'));
  143. return $this->_oModule->_oTemplate->parseHtmlByName('report_by_list.html', [
  144. 'style_prefix' => $this->_sStylePrefix,
  145. 'bx_repeat:list' => $aTmplReports
  146. ]);
  147. }
  148. protected function _getIconDoReport($bPerformed)
  149. {
  150. return $bPerformed && $this->isUndo() ? 'stopwatch' : 'stopwatch';
  151. }
  152. protected function _getTitleDoReport($bPerformed)
  153. {
  154. return ['_bx_tasks_report_time_do_' . ($bPerformed && $this->isUndo() ? 'un' : '') . 'report'];
  155. }
  156. }
  157. /** @} */