BxTasksGridTimeContextCommon.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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(BX_DOL_DIR_STUDIO_INC . 'utils.inc.php');
  11. require_once('BxTasksGridTimeContextAdministration.php');
  12. class BxTasksGridTimeContextCommon extends BxTasksGridTimeContextAdministration
  13. {
  14. protected $_iLogged;
  15. public function __construct ($aOptions, $oTemplate = false)
  16. {
  17. parent::__construct ($aOptions, $oTemplate);
  18. $this->_iLogged = bx_get_logged_profile_id();
  19. }
  20. public function getFormBlockTitleAPI($sAction, $iId = 0)
  21. {
  22. $sResult = '';
  23. switch($sAction) {
  24. case 'add':
  25. $sResult = _t('_bx_tasks_grid_popup_title_tm_add');
  26. break;
  27. case 'edit':
  28. $sResult = _t('_bx_tasks_grid_popup_title_tm_edit');
  29. break;
  30. default:
  31. parent::getFormBlockTitleAPI($sAction, $iId);
  32. }
  33. return $sResult;
  34. }
  35. public function performActionAdd()
  36. {
  37. $sAction = 'add';
  38. $oForm = $this->_getFormObject($sAction);
  39. $oForm->initChecker();
  40. if($oForm->isSubmittedAndValid()) {
  41. $iNow = time();
  42. $iValue = $this->_oModule->_oConfig->timeA2I([(int)$oForm->getCleanValue('value_h'), (int)$oForm->getCleanValue('value_m')]);
  43. $aValsToAdd = [
  44. 'author_id' => $this->_iLogged,
  45. 'author_nip' => bx_get_ip_hash(getVisitorIP()),
  46. 'value' => $iValue,
  47. 'value_date' => $oForm->getCleanValue('value_date') ?: $iNow,
  48. 'date' => $iNow
  49. ];
  50. $iTrackId = $oForm->insert($aValsToAdd);
  51. if(!$iTrackId)
  52. return $this->_getActionResult(['msg' => _t('_bx_tasks_txt_err_cannot_perform_action')]);
  53. $sSystem = $oForm->getCleanValue('sys');
  54. $iObjectId = $oForm->getCleanValue('object_id');
  55. if(($oTime = BxDolReport::getObjectInstance($sSystem, $iObjectId)) && $oTime->isEnabled())
  56. $oTime->putReport($iObjectId, $this->_iLogged, $iTrackId);
  57. $this->_oModule->updateBudgetByContentId($iObjectId, $iValue);
  58. return $this->_bIsApi ? [] : echoJson(['grid' => $this->getCode(false), 'blink' => $iTrackId]);
  59. }
  60. if($this->_bIsApi)
  61. return $this->getFormBlockAPI($oForm, $sAction);
  62. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('time_popup'), _t('_bx_tasks_grid_popup_title_tm_add'), $this->_oModule->_oTemplate->parseHtmlByName('popup_time.html', [
  63. 'form_id' => $oForm->getId(),
  64. 'form' => $oForm->getCode(true),
  65. 'object' => $this->_sObject,
  66. 'action' => $sAction
  67. ]));
  68. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  69. }
  70. public function performActionEdit()
  71. {
  72. $CNF = &$this->_oModule->_oConfig->CNF;
  73. $sAction = 'edit';
  74. $aIds = $this->_getIds();
  75. if($aIds === false)
  76. return $this->_getActionResult([]);
  77. $iTrackId = array_shift($aIds);
  78. $aTrack = $this->_oModule->_oDb->getTimeTracks(['sample' => 'id', 'id' => $iTrackId]);
  79. if(empty($aTrack) || !is_array($aTrack))
  80. return $this->_getActionResult([]);
  81. $oForm = $this->_getFormObject($sAction, $aTrack);
  82. $oForm->initChecker($aTrack);
  83. if($oForm->isSubmittedAndValid()) {
  84. $aValsToAdd = [
  85. 'value' => $this->_oModule->_oConfig->timeA2I([(int)$oForm->getCleanValue('value_h'), (int)$oForm->getCleanValue('value_m')]),
  86. ];
  87. if(!$oForm->update($iTrackId, $aValsToAdd))
  88. return $this->_getActionResult(['msg' => _t('_bx_tasks_txt_err_cannot_perform_action')]);
  89. $iObjectId = (int)$aTrack['object_id'];
  90. $iAuthorId = (int)$aTrack['author_id'];
  91. if(($oTime = BxDolReport::getObjectInstance($CNF['OBJECT_REPORTS_TIME'], $iObjectId)) && $oTime->isEnabled()) {
  92. $oTime->putReport($iObjectId, $iAuthorId, $aTrack, true); //--- Revoke old value
  93. $oTime->putReport($iObjectId, $iAuthorId, $iTrackId); //--- Process new value
  94. }
  95. $this->_oModule->updateBudgetByContentId($iObjectId, $aValsToAdd['value'] - $aTrack['value']);
  96. return $this->_bIsApi ? [] : echoJson(['grid' => $this->getCode(false), 'blink' => $iTrackId]);
  97. }
  98. if($this->_bIsApi)
  99. return $this->getFormBlockAPI($oForm, $sAction, $iTrackId);
  100. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('time_popup'), _t('_bx_tasks_grid_popup_title_tm_edit'), $this->_oModule->_oTemplate->parseHtmlByName('popup_time.html', [
  101. 'form_id' => $oForm->getId(),
  102. 'form' => $oForm->getCode(true),
  103. 'object' => $this->_sObject,
  104. 'action' => $sAction
  105. ]));
  106. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  107. }
  108. protected function _getFormObject($sAction, $aTrack = [])
  109. {
  110. $CNF = &$this->_oModule->_oConfig->CNF;
  111. $bActionAdd = $sAction == 'add';
  112. $bActionEdit = $sAction == 'edit';
  113. $bTrack = !empty($aTrack) && is_array($aTrack);
  114. $aActionParams = [
  115. 'o' => $this->_sObject,
  116. 'a' => $sAction,
  117. 'context_pid' => $this->_iContextPid
  118. ];
  119. if($bActionEdit && $bTrack)
  120. $aActionParams['id'] = (int)$aTrack['id'];
  121. $sForm = $CNF['OBJECT_FORM_TIME_DISPLAY_' . strtoupper($sAction)];
  122. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_TIME'], $sForm);
  123. $oForm->setId($sForm);
  124. $oForm->setName($sForm);
  125. $oForm->setAction(BX_DOL_URL_ROOT . bx_append_url_params('grid.php', $aActionParams));
  126. $oForm->aParams['db']['table'] = $CNF['TABLE_TIME_TRACK'];
  127. if($bActionAdd) {
  128. $oForm->aInputs['sys']['value'] = $CNF['OBJECT_REPORTS_TIME'];
  129. $oForm->aInputs['action']['value'] = 'Report';
  130. $oForm->aInputs['object_id'] = array_merge($oForm->aInputs['object_id'], [
  131. 'type' => 'select',
  132. 'values' => [['key' => 0, 'value' => _t('_sys_please_select')]],
  133. 'required' => true,
  134. 'checker' => [
  135. 'func' => 'Avail',
  136. 'error' => _t('_bx_tasks_form_time_input_object_id_err')
  137. ]
  138. ]);
  139. $aTasks = $this->_oModule->_oDb->getTasks($this->_iContextPid);
  140. foreach($aTasks as $aTask) {
  141. if($aTask[$CNF['FIELD_STATUS_ADMIN']] !== 'active')
  142. continue;
  143. $oForm->aInputs['object_id']['values'][] = [
  144. 'key' => $aTask[$CNF['FIELD_ID']],
  145. 'value' => $aTask[$CNF['FIELD_TITLE']]
  146. ];
  147. }
  148. }
  149. else if($bActionEdit) {
  150. if($bTrack) {
  151. list($value_h, $value_m) = $this->_oModule->_oConfig->timeI2A($aTrack['value']);
  152. foreach($oForm->aInputs['value'] as $mixedKey => $mixedValue) {
  153. if(!is_numeric($mixedKey) || !is_array($mixedValue))
  154. continue;
  155. if(($iValue = ${$mixedValue['name']} ?? false) !== false)
  156. $oForm->aInputs['value'][$mixedKey]['value'] = (int)$iValue;
  157. }
  158. }
  159. }
  160. if($this->_bIsApi && ($sK = 'value') && isset($oForm->aInputs[$sK])) {
  161. foreach($oForm->aInputs[$sK] as $mixedKey => $mixedValue)
  162. if(($sName = $mixedValue['name'] ?? false) && in_array($sName, ['value_h', 'value_m']) && ($sCaptionSrc = '_bx_tasks_form_time_input_' . $sName))
  163. $oForm->aInputs = bx_array_insert_before([$sName => array_merge(
  164. $mixedValue, [
  165. 'caption_src' => $sCaptionSrc,
  166. 'caption' => _t($sCaptionSrc)
  167. ]
  168. )], $oForm->aInputs, 'value_date');
  169. unset($oForm->aInputs[$sK]);
  170. }
  171. return $oForm;
  172. }
  173. protected function _getFilterControls()
  174. {
  175. $this->_getFcDefault();
  176. $sContent = $this->_getFilterSelectOne($this->_sFilter2Name, $this->_sFilter2Value, $this->_aFilter2Values, '_bx_tasks_grid_filter_item_title_tm_select_one_object_id');
  177. $sContent .= $this->_getFcDateSearch();
  178. return $sContent;
  179. }
  180. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  181. {
  182. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `ttt`.`author_id`=?", $this->_iLogged);
  183. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  184. }
  185. }
  186. /** @} */