BxTasksGridTime.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. class BxTasksGridTime extends BxBaseModGeneralGrid
  11. {
  12. protected $_sFilter1Name; //--- Assignees
  13. protected $_sFilter1Value;
  14. protected $_aFilter1Values;
  15. protected $_sFilter2Name; //--- Contexts/Tasks
  16. protected $_sFilter2Value;
  17. protected $_aFilter2Values;
  18. protected $_sFilter3Name; //--- Date from
  19. protected $_sFilter3Value;
  20. protected $_sFilter4Name; //--- Date to
  21. protected $_sFilter4Value;
  22. public function __construct ($aOptions, $oTemplate = false)
  23. {
  24. $this->_sModule = 'bx_tasks';
  25. parent::__construct ($aOptions, $oTemplate);
  26. $this->_sTableAlias = 'ttt';
  27. $this->_sDefaultSortingOrder = 'DESC';
  28. /*
  29. * Filters by date from/to
  30. */
  31. $this->_sFilter3Name = 'filter3';
  32. $this->_sFilter4Name = 'filter4';
  33. }
  34. public function getFormBlockTitleAPI($sAction, $iId = 0)
  35. {
  36. $sResult = '';
  37. switch($sAction) {
  38. case 'calculate':
  39. $sResult = _t('_bx_tasks_grid_popup_title_tm_calculate');
  40. break;
  41. }
  42. return $sResult;
  43. }
  44. public function getFormCallBackUrlAPI($sAction, $iId = 0)
  45. {
  46. return '/api.php?r=system/perfom_action_api/TemplServiceGrid/&params[]=&o=' . $this->_sObject . '&a=' . $sAction . '&id=' . $iId;
  47. }
  48. public function performActionCalculate()
  49. {
  50. $sAction = 'calculate';
  51. $aIds = $this->_getIds();
  52. if($aIds === false)
  53. return $this->_getActionResult([]);
  54. $iTotal = 0;
  55. foreach($aIds as $iId) {
  56. $aTrack = $this->_oModule->_oDb->getTimeTracks(['sample' => 'id', 'id' => $iId]);
  57. if(empty($aTrack) || !is_array($aTrack))
  58. continue;
  59. $iTotal += $aTrack['value'];
  60. }
  61. $sTotal = $iTotal ? $this->_oModule->_oConfig->timeI2S($iTotal) : '';
  62. if($this->_bIsApi)
  63. return [
  64. bx_api_get_block('msg', [
  65. 'total' => $iTotal,
  66. 'total_f' => $sTotal
  67. ])
  68. ];
  69. $sContent = BxTemplFunctions::getInstance()->transBox($this->_oModule->_oConfig->getHtmlIds('total_popup'), $this->_oModule->_oTemplate->parseHtmlByName('popup_total.html', [
  70. 'total' => $sTotal
  71. ]));
  72. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => true]]]);
  73. }
  74. protected function _delete ($mixedId)
  75. {
  76. $CNF = &$this->_oModule->_oConfig->CNF;
  77. $aTrack = $this->_oModule->_oDb->getTimeTracks(['sample' => 'id', 'id' => (int)$mixedId]);
  78. if(!is_array($aTrack) || empty($aTrack))
  79. return false;
  80. $iObjectId = (int)$aTrack['object_id'];
  81. $iAuthorId = (int)$aTrack['author_id'];
  82. $bResult = parent::_delete($mixedId);
  83. if($bResult && ($oTime = BxDolReport::getObjectInstance($CNF['OBJECT_REPORTS_TIME'], $iObjectId)) && $oTime->isEnabled())
  84. $oTime->putReport($iObjectId, $iAuthorId, $aTrack, true);
  85. $this->_oModule->spendBudgetByContentId($iObjectId, -$aTrack['value']);
  86. return $bResult;
  87. }
  88. protected function _getCellAuthorId($mixedValue, $sKey, $aField, $aRow)
  89. {
  90. if($this->_bIsApi)
  91. return ['type' => 'profile', 'data' => BxDolProfile::getData($mixedValue)];
  92. if(($oProfile = BxDolProfile::getInstanceMagic($mixedValue)) !== false)
  93. $mixedValue = $oProfile->getUnit();
  94. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  95. }
  96. protected function _getCellObjectId($mixedValue, $sKey, $aField, $aRow)
  97. {
  98. $CNF = &$this->_oModule->_oConfig->CNF;
  99. $sTitle = $aRow[$CNF['FIELD_TITLE']];
  100. if((int)$aField['chars_limit'] > 0)
  101. $sTitle = strmaxtextlen($sTitle, (int)$aField['chars_limit']);
  102. if($sTitle == '')
  103. $sTitle = _t('_sys_txt_no_title');
  104. $sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $mixedValue));
  105. if($this->_bIsApi)
  106. return ['type' => 'link', 'data' => [
  107. 'text' => $aRow[$CNF['FIELD_TITLE']],
  108. 'url' => bx_api_get_relative_url($sUrl)
  109. ]];
  110. return parent::_getCellDefault($this->_getEntryLink($sTitle, $sUrl, $aRow), $sKey, $aField, $aRow);
  111. }
  112. protected function _getCellValue($mixedValue, $sKey, $aField, $aRow)
  113. {
  114. return parent::_getCellDefault($this->_oModule->_oConfig->timeI2S($mixedValue), $sKey, $aField, $aRow);
  115. }
  116. protected function _getCellValueDate($mixedValue, $sKey, $aField, $aRow)
  117. {
  118. return $this->_bIsApi ? [
  119. 'type' => 'time',
  120. 'data' => $mixedValue
  121. ] : parent::_getCellDefault($mixedValue ? bx_time_js($mixedValue, BX_FORMAT_DATE, true) : '', $sKey, $aField, $aRow);
  122. }
  123. protected function _getActionDelete($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  124. {
  125. if($this->_isAdministration())
  126. return '';
  127. return parent::_getActionDelete($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  128. }
  129. protected function _getFcDefault()
  130. {
  131. return parent::_getFilterControls();
  132. }
  133. protected function _getFcDateSearch()
  134. {
  135. $sContent = $this->_getFilterDatePicker($this->_sFilter3Name, $this->_sFilter3Value);
  136. $sContent .= $this->_getFilterLabel('-');
  137. $sContent .= $this->_getFilterDatePicker($this->_sFilter4Name, $this->_sFilter4Value);
  138. $sContent .= $this->_getSearchInput();
  139. return $sContent;
  140. }
  141. protected function _getFilterOnChange()
  142. {
  143. return $this->_oModule->_oConfig->getJsObject('time') . '.onChangeFilter(this)';
  144. }
  145. protected function __getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  146. {
  147. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  148. }
  149. protected function _getEntryLink($sTitle, $sUrl, $aRow)
  150. {
  151. return $this->_oTemplate->parseHtmlByName('title_link.html', [
  152. 'href' => $sUrl,
  153. 'title' => bx_html_attribute($sTitle),
  154. 'content' => bx_process_output($sTitle),
  155. 'target' => '_blank'
  156. ]);
  157. }
  158. protected function _isCommon()
  159. {
  160. $CNF = &$this->_oModule->_oConfig->CNF;
  161. return in_array($this->_sObject, [$CNF['OBJECT_GRID_TIME_COMMON'], $CNF['OBJECT_GRID_TIME_CONTEXT_COMMON']]);
  162. }
  163. protected function _isAdministration()
  164. {
  165. $CNF = &$this->_oModule->_oConfig->CNF;
  166. return in_array($this->_sObject, [$CNF['OBJECT_GRID_TIME_ADMINISTRATION'], $CNF['OBJECT_GRID_TIME_CONTEXT_ADMINISTRATION']]);
  167. }
  168. protected function _isContext()
  169. {
  170. $CNF = &$this->_oModule->_oConfig->CNF;
  171. return in_array($this->_sObject, [$CNF['OBJECT_GRID_TIME_CONTEXT_ADMINISTRATION'], $CNF['OBJECT_GRID_TIME_CONTEXT_COMMON']]);
  172. }
  173. protected function _getIds()
  174. {
  175. $aIds = bx_get('ids');
  176. if(!$aIds || !is_array($aIds)) {
  177. $iId = (int)bx_get('id');
  178. if(!$iId)
  179. return false;
  180. $aIds = [$iId];
  181. }
  182. return $aIds;
  183. }
  184. }
  185. /** @} */