BxTasksGridTime.php 7.3 KB

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