1
0

BxTasksTime.php 11 KB

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