BxTasksTime.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. protected $_sUniqId;
  16. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  17. {
  18. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  19. $this->_sModule = 'bx_tasks';
  20. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  21. $this->_sUniqId = '';
  22. $CNF = &$this->_oModule->_oConfig->CNF;
  23. $this->_oQuery = new BxTasksTimeQuery($this);
  24. $this->_bUndo = false;
  25. $this->_bProcessible = false;
  26. $this->_sFormObject = $CNF['OBJECT_FORM_TIME'];
  27. $this->_sFormDisplayPost = $CNF['OBJECT_FORM_TIME_DISPLAY_ADD'];
  28. array_walk($this->_aHtmlIds, function(&$sVa1ue) {
  29. $sVa1ue = str_replace('bx-report', 'bx-tasks', $sVa1ue);
  30. });
  31. }
  32. public function getJsObjectName()
  33. {
  34. return parent::getJsObjectName() . $this->_sUniqId;
  35. }
  36. public function getJsScript($bDynamicMode = false, $aParams = [])
  37. {
  38. if($this->_iDoAuto($aParams)) {
  39. $aParams['js_params'] ??= [];
  40. $aParams['js_params']['oRequestParams'] ??= [];
  41. $aParams['js_params']['oRequestParams']['show_do_report_form'] = 0;
  42. }
  43. return parent::getJsScript($bDynamicMode, $aParams);
  44. }
  45. public function getElement($aParams = [])
  46. {
  47. $this->_sUniqId = $aParams['uniq_id'] ?? '';
  48. return parent::getElement($aParams);
  49. }
  50. public function report($aParams = [])
  51. {
  52. $iObjectId = $this->_iId;
  53. $iAuthorId = $this->_getAuthorId();
  54. $bPerformed = $this->isPerformed($iObjectId, $iAuthorId);
  55. $this->_oModule->serviceProcessTimer('pause', $iObjectId, $iAuthorId);
  56. if(($sK = 'show_do_report_form') && ($iShowDoReportForm = bx_get($sK)) !== false && !$iShowDoReportForm) {
  57. if(!$this->isEnabled())
  58. return ['code' => 1, 'message' => _t('_report_err_not_enabled')];
  59. if(!$this->isAllowedReport())
  60. return ['code' => 2, 'message' => $this->msgErrAllowedReport()];
  61. $aTimer = $this->_oModule->_oDb->getTimers([
  62. 'sample' => 'content_profile_ids',
  63. 'content_id' => $this->_iId,
  64. 'profile_id' => $iAuthorId
  65. ]);
  66. if(!$aTimer || !is_array($aTimer) || !($iDuration = (int)$aTimer['duration']))
  67. return ['code' => 3, 'message' => _t('_bx_tasks_txt_err_timer_not_found')];
  68. list($iHours, $iMinutes) = $this->_oModule->_oConfig->timeI2A($iDuration, true);
  69. $oForm = $this->_getFormObject();
  70. return $this->_report($bPerformed, array_merge($aParams, [
  71. 'object_id' => $iObjectId,
  72. 'value_h' => $iHours,
  73. 'value_m' => $iMinutes,
  74. 'text' => '',
  75. 'timer_id' => $aTimer['id']
  76. ]), $oForm);
  77. }
  78. $mixedResult = parent::report($aParams);
  79. if(!empty($mixedResult) && is_array($mixedResult) && isset($mixedResult['popup'], $mixedResult['popup_id']))
  80. $mixedResult['popup'] = [
  81. 'html' => $mixedResult['popup'],
  82. 'options' => ['closeOnOuterClick' => false]
  83. ];
  84. return $mixedResult;
  85. }
  86. /**
  87. * Should always return false to allow any number of time reports per user.
  88. */
  89. public function isPerformed($iObjectId, $iAuthorId, $iAuthorIp = 0)
  90. {
  91. return false;
  92. }
  93. public function putReport($iObjectId, $iAuthorId, $mixedTrack, $bUndo = false)
  94. {
  95. if(!$this->_oQuery->putReport($iObjectId, $iAuthorId, $bUndo))
  96. return false;
  97. $aTrack = is_array($mixedTrack) ? $mixedTrack : $this->_oQuery->getTrackBy(['type' => 'id', 'id' => (int)$mixedTrack]);
  98. if(empty($aTrack) || !is_array($aTrack))
  99. return false;
  100. if(!$bUndo && empty($aTrack['value_date']) && !$this->_oQuery->updateTrack(['value_date' => time()], ['id' => $aTrack['id']]))
  101. return false;
  102. if(!$this->_oQuery->updateReport($iObjectId, $aTrack['value'], $bUndo))
  103. return false;
  104. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
  105. /*
  106. * TODO: May be we need to send notification to Context admins.
  107. *
  108. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_Reported', [
  109. 'report_type' => $sType,
  110. 'report_text' => $sText,
  111. 'report_url' => $this->getBaseUrl(),
  112. ]);
  113. if($aTemplate)
  114. sendMail(getParam('site_email'), $aTemplate['Subject'], $aTemplate['Body']);
  115. */
  116. $this->_trigger();
  117. $this->_oModule->logActivity($iObjectId, [
  118. 'key' => '_bx_tasks_txt_msg_report_time_for_date',
  119. 'markers' => [
  120. 'time' => $this->_oModule->_oConfig->timeI2S($aTrack['value']),
  121. 'date' => bx_process_output($aTrack['value_date'] ?: $aTrack['date'], BX_DATA_DATE_TS)
  122. ]
  123. ]);
  124. /**
  125. * @hooks
  126. * @hookdef bx_tasks-report_time '{module_name}', 'report_time' - hook on create new time report
  127. * - $unit_name - module name
  128. * - $action - equals `report_time`
  129. * - $object_id - reported entry ID
  130. * - $sender_id - profile id for report's author
  131. * - $extra_params - array of additional params with the following array keys:
  132. * - `object_system` - [string] system name
  133. * - `object_author_id` - [int] author's profile_id for reported object_id
  134. * - `report_id` - [int] report id
  135. * - `report_author_id` - [int] profile id for report's author
  136. * - `type` - [string] reported time
  137. * @hook @ref hook-bx_tasks-report_time
  138. */
  139. bx_alert($this->_sModule, 'report_time', $iObjectId, $iAuthorId, [
  140. 'object_system' => $this->_sSystem,
  141. 'object_author_id' => $iObjectAuthorId,
  142. 'report_id' => $aTrack['id'],
  143. 'report_author_id' => $iAuthorId,
  144. 'value' => $aTrack['value']
  145. ]);
  146. return true;
  147. }
  148. protected function _getDoReport($aParams = [])
  149. {
  150. $mixedResult = parent::_getDoReport($aParams);
  151. if($this->_bApi)
  152. $mixedResult['is_do_auto'] = $this->_iDoAuto($aParams);
  153. return $mixedResult;
  154. }
  155. protected function _report($bPerformed, $aParams, &$oForm)
  156. {
  157. $iAuthorId = $this->_getAuthorId();
  158. $iAuthorNip = bx_get_ip_hash($this->_getAuthorIp());
  159. $iObjectId = ($sKey = 'object_id') && $this->_bApi ? $this->_iId : ($aParams[$sKey] ?? $oForm->getCleanValue($sKey));
  160. if(!$this->isAllowedReport(true))
  161. return ['code' => 2, 'message' => $this->msgErrAllowedReport()];
  162. $iVh = ($sKey = 'value_h') && $this->_bApi ? $aParams[$sKey] : ($aParams[$sKey] ?? $oForm->getCleanValue($sKey));
  163. $iVm = ($sKey = 'value_m') && $this->_bApi ? $aParams[$sKey] : ($aParams[$sKey] ?? $oForm->getCleanValue($sKey));
  164. $iValue = $this->_oModule->_oConfig->timeA2I([$iVh, $iVm]);
  165. $sText = ($sKey = 'text') && $this->_bApi ? $aParams[$sKey] : ($aParams[$sKey] ?? $oForm->getCleanValue($sKey));
  166. $sText = bx_process_input($sText);
  167. $iId = (int)$oForm->insert(['object_id' => $iObjectId, 'author_id' => $iAuthorId, 'author_nip' => $iAuthorNip, 'value' => $iValue, 'text' => $sText, 'date' => time()]);
  168. if($iId != 0 && $this->putReport($iObjectId, $iAuthorId, $iId)) {
  169. $aReport = $this->_getReport($iObjectId, true);
  170. $aResult = $this->_returnReportData($iObjectId, $iAuthorId, $iId, $aReport, !$bPerformed);
  171. if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled())
  172. $oSockets->sendEvent($this->getSocketName(), $iObjectId, 'reported', json_encode($this->_returnReportDataForSocket($aResult)));
  173. /*
  174. * If timer is attached, clear it and initiate reloading.
  175. */
  176. if(($sKey = 'timer_id') && ($iTimerId = (int)($aParams[$sKey] ?? $oForm->getCleanValue($sKey)))) {
  177. $aTimer = $this->_oModule->_oDb->getTimers([
  178. 'sample' => 'id',
  179. 'id' => $iTimerId
  180. ]);
  181. if($aTimer && ($iContentId = $aTimer['content_id'] ?? 0) && ($iProfileId = $aTimer['profile_id'] ?? 0)) {
  182. $this->_oModule->serviceProcessTimer('clear', $iContentId, $iProfileId);
  183. $aResult = array_merge($aResult, [
  184. 'label_title' => _t('_bx_tasks_txt_timer_log'),
  185. 'eval' => $aResult['eval'] . '; ' . $this->_oModule->_oConfig->getJsObject('timer'). '.reload(this, ' . $iContentId . ', ' . $iProfileId . ');'
  186. ]);
  187. }
  188. }
  189. return $aResult;
  190. }
  191. return ['code' => 3, 'message' => _t('_report_err_cannot_perform_action')];
  192. }
  193. protected function _returnReportData($iObjectId, $iAuthorId, $iReportId, $aData, $bPerformed)
  194. {
  195. return parent::_returnReportData($iObjectId, $iAuthorId, $iReportId, $aData, false);
  196. }
  197. protected function _getReportedBy()
  198. {
  199. $aTmplReports = [];
  200. $aReports = $this->_oQuery->getPerformedBy($this->getId());
  201. foreach($aReports as $aReport) {
  202. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($aReport['author_id']);
  203. $sText = bx_process_output($aReport['text'], BX_DATA_TEXT_MULTILINE);
  204. $aTmplReports[] = [
  205. 'style_prefix' => $this->_sStylePrefix,
  206. 'user_unit' => $sUserUnit,
  207. 'value' => $this->_oModule->_oConfig->timeI2S($aReport['value']),
  208. 'date' => $aReport['value_date'] ? bx_time_js($aReport['value_date'], BX_FORMAT_DATE, true) : '',
  209. 'bx_if:show_text' => [
  210. 'condition' => strlen($sText) > 0,
  211. 'content' => [
  212. 'text' => $sText
  213. ]
  214. ]
  215. ];
  216. }
  217. if(empty($aTmplReports))
  218. $aTmplReports = MsgBox(_t('_Empty'));
  219. return $this->_oModule->_oTemplate->parseHtmlByName('report_by_list.html', [
  220. 'style_prefix' => $this->_sStylePrefix,
  221. 'bx_repeat:list' => $aTmplReports
  222. ]);
  223. }
  224. protected function _getIconDoReport($bPerformed)
  225. {
  226. return $bPerformed && $this->isUndo() ? 'stopwatch' : 'stopwatch';
  227. }
  228. protected function _getTitleDoReport($bPerformed, $aParams = [])
  229. {
  230. if(($sTitle = $aParams['do_report_label'] ?? false))
  231. return [$sTitle];
  232. return ['_bx_tasks_report_time_do_' . ($bPerformed && $this->isUndo() ? 'un' : '') . 'report'];
  233. }
  234. protected function _iDoAuto($aParams)
  235. {
  236. return ($sK = 'show_do_report_form') && isset($aParams[$sK]) && $aParams[$sK] != true;
  237. }
  238. }
  239. /** @} */