BxTasksGridTimeContextCommon.php 7.5 KB

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