BxTasksGridTime.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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('BxTasksGridTimeAdministration.php');
  11. class BxTasksGridTime extends BxTasksGridTimeAdministration
  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->timeS2I($oForm->getCleanValue('value')),
  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, $iTrack);
  64. $oForm->initChecker(array_merge($aTrack, ['value' => $this->_oModule->_oConfig->timeI2S($aTrack['value'])]));
  65. if($oForm->isSubmittedAndValid()) {
  66. $aValsToAdd = [
  67. 'value' => $this->_oModule->_oConfig->timeS2I($oForm->getCleanValue('value')),
  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 _delete ($mixedId)
  90. {
  91. $CNF = &$this->_oModule->_oConfig->CNF;
  92. $aTrack = $this->_oModule->_oDb->getTimeTracks(['sample' => 'id', 'id' => (int)$mixedId]);
  93. if(!is_array($aTrack) || empty($aTrack))
  94. return false;
  95. $iObjectId = (int)$aTrack['object_id'];
  96. $iAuthorId = (int)$aTrack['author_id'];
  97. $bResult = parent::_delete($mixedId);
  98. if($bResult && ($oTime = BxDolReport::getObjectInstance($CNF['OBJECT_REPORTS_TIME'], $iObjectId)) && $oTime->isEnabled())
  99. $oTime->putReport($iObjectId, $iAuthorId, $aTrack, true);
  100. return $bResult;
  101. }
  102. protected function _getFormObject($sAction, $iId = 0)
  103. {
  104. $CNF = &$this->_oModule->_oConfig->CNF;
  105. $bActionAdd = $sAction == 'add';
  106. $bActionEdit = $sAction == 'edit';
  107. $aActionParams = [
  108. 'o' => $this->_sObject,
  109. 'a' => $sAction,
  110. 'context_pid' => $this->_iContextPid
  111. ];
  112. if($bActionEdit && !empty($iId))
  113. $aActionParams['id'] = $iId;
  114. $sForm = $CNF['OBJECT_FORM_TIME_DISPLAY_' . strtoupper($sAction)];
  115. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_TIME'], $sForm);
  116. $oForm->setId($sForm);
  117. $oForm->setName($sForm);
  118. $oForm->setAction(BX_DOL_URL_ROOT . bx_append_url_params('grid.php', $aActionParams));
  119. $oForm->aParams['db']['table'] = $CNF['TABLE_TIME_TRACK'];
  120. if($bActionAdd) {
  121. $oForm->aInputs['sys']['value'] = $CNF['OBJECT_REPORTS_TIME'];
  122. $oForm->aInputs['action']['value'] = 'Report';
  123. $oForm->aInputs['object_id'] = array_merge($oForm->aInputs['object_id'], [
  124. 'type' => 'select',
  125. 'values' => [['key' => 0, 'value' => _t('_sys_please_select')]],
  126. 'required' => true,
  127. 'checker' => [
  128. 'func' => 'Avail',
  129. 'error' => _t('_bx_tasks_form_time_input_object_id_err')
  130. ]
  131. ]);
  132. $aTasks = $this->_oModule->_oDb->getTasks(-$this->_iContextPid);
  133. foreach($aTasks as $aTask) {
  134. if($aTask[$CNF['FIELD_STATUS_ADMIN']] !== 'active')
  135. continue;
  136. $oForm->aInputs['object_id']['values'][] = [
  137. 'key' => $aTask[$CNF['FIELD_ID']],
  138. 'value' => $aTask[$CNF['FIELD_TITLE']]
  139. ];
  140. }
  141. }
  142. return $oForm;
  143. }
  144. protected function _getFilterControls()
  145. {
  146. $this->__getFilterControls();
  147. $sContent = $this->_getFilterSelectOne($this->_sFilter2Name, $this->_sFilter2Value, $this->_aFilter2Values);
  148. $sContent .= $this->_getSearchInput();
  149. return $sContent;
  150. }
  151. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  152. {
  153. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `ttt`.`author_id`=?", $this->_iLogged);
  154. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  155. }
  156. }
  157. /** @} */