BxTasksGridTimeAdministration.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 BxTasksGridTimeAdministration extends BxBaseModGeneralGrid
  11. {
  12. protected $_iContextPid;
  13. protected $_sFilter1Name;
  14. protected $_sFilter1Value;
  15. protected $_aFilter1Values;
  16. protected $_sFilter2Name;
  17. protected $_sFilter2Value;
  18. protected $_aFilter2Values;
  19. public function __construct ($aOptions, $oTemplate = false)
  20. {
  21. $this->_sModule = 'bx_tasks';
  22. parent::__construct ($aOptions, $oTemplate);
  23. $this->_sDefaultSortingOrder = 'DESC';
  24. if(($iContextPid = bx_get('context_pid')) !== false)
  25. $this->setContextPid($iContextPid);
  26. }
  27. public function setContextPid($iContextPid)
  28. {
  29. $CNF = &$this->_oModule->_oConfig->CNF;
  30. $this->_iContextPid = (int)$iContextPid;
  31. $this->_aQueryAppend['context_pid'] = $this->_iContextPid;
  32. /*
  33. * Filter by author_id
  34. */
  35. $this->_sFilter1Name = 'filter1';
  36. $this->_aFilter1Values = [];
  37. $this->_sFilter1Value = '';
  38. if($this->_sObject == $CNF['OBJECT_GRID_TIME_ADMINISTRATION']) {
  39. if(($oContext = BxDolProfile::getInstance($this->_iContextPid)) !== false) {
  40. $aPids = bx_srv($oContext->getModule(), 'fans', [$oContext->getContentId(), true]);
  41. foreach($aPids as $iPid)
  42. $this->_aFilter1Values[$iPid] = $oContext->getDisplayName($iPid);
  43. }
  44. if(($sFilter1 = bx_get($this->_sFilter1Name)) !== false) {
  45. $this->_sFilter1Value = bx_process_input($sFilter1);
  46. $this->_aQueryAppend[$this->_sFilter1Name] = $this->_sFilter1Value;
  47. }
  48. }
  49. /*
  50. * Filter by object_id
  51. */
  52. $this->_sFilter2Name = 'filter2';
  53. $this->_aFilter2Values = [];
  54. $this->_sFilter2Value = '';
  55. $aTasks = $this->_oModule->_oDb->getTasks(-$this->_iContextPid);
  56. foreach($aTasks as $aTask)
  57. $this->_aFilter2Values[$aTask[$CNF['FIELD_ID']]] = $aTask[$CNF['FIELD_TITLE']];
  58. if(($sFilter2 = bx_get($this->_sFilter2Name)) !== false) {
  59. $this->_sFilter2Value = bx_process_input($sFilter2);
  60. $this->_aQueryAppend[$this->_sFilter2Name] = $this->_sFilter2Value;
  61. }
  62. }
  63. public function performActionCalculate()
  64. {
  65. $sAction = 'calculate';
  66. $aIds = $this->_getIds();
  67. if($aIds === false)
  68. return $this->_getActionResult([]);
  69. $iTotal = 0;
  70. foreach($aIds as $iId) {
  71. $aTrack = $this->_oModule->_oDb->getTimeTracks(['sample' => 'id', 'id' => $iId]);
  72. if(empty($aTrack) || !is_array($aTrack))
  73. continue;
  74. $iTotal += $aTrack['value'];
  75. }
  76. $sContent = BxTemplFunctions::getInstance()->transBox($this->_oModule->_oConfig->getHtmlIds('total_popup'), $this->_oModule->_oTemplate->parseHtmlByName('popup_total.html', [
  77. 'total' => $this->_oModule->_oConfig->timeI2S($iTotal)
  78. ]));
  79. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => true]]]);
  80. }
  81. protected function _getCellAuthorId($mixedValue, $sKey, $aField, $aRow)
  82. {
  83. if($this->_bIsApi)
  84. return ['type' => 'profile', 'data' => BxDolProfile::getData($mixedValue)];
  85. if(($oProfile = BxDolProfile::getInstanceMagic($mixedValue)) !== false)
  86. $mixedValue = $oProfile->getUnit();
  87. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  88. }
  89. protected function _getCellObjectId($mixedValue, $sKey, $aField, $aRow)
  90. {
  91. $CNF = &$this->_oModule->_oConfig->CNF;
  92. $sTitle = $aRow[$CNF['FIELD_TITLE']];
  93. if((int)$aField['chars_limit'] > 0)
  94. $sTitle = strmaxtextlen($sTitle, (int)$aField['chars_limit']);
  95. if($sTitle == '')
  96. $sTitle = _t('_sys_txt_no_title');
  97. $sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $mixedValue));
  98. if($this->_bIsApi)
  99. return ['type' => 'link', 'data' => [
  100. 'text' => $aRow[$CNF['FIELD_TITLE']],
  101. 'url' => bx_api_get_relative_url($sUrl)
  102. ]];
  103. return parent::_getCellDefault($this->_getEntryLink($sTitle, $sUrl, $aRow), $sKey, $aField, $aRow);
  104. }
  105. protected function _getCellValue($mixedValue, $sKey, $aField, $aRow)
  106. {
  107. return parent::_getCellDefault($this->_oModule->_oConfig->timeI2S($mixedValue), $sKey, $aField, $aRow);
  108. }
  109. protected function _getCellValueDate($mixedValue, $sKey, $aField, $aRow)
  110. {
  111. return $this->_bIsApi ? [
  112. 'type' => 'time',
  113. 'data' => $mixedValue
  114. ] : parent::_getCellDefault($mixedValue ? bx_time_js($mixedValue, BX_FORMAT_DATE, true) : '', $sKey, $aField, $aRow);
  115. }
  116. protected function _getActionEdit($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  117. {
  118. $CNF = &$this->_oModule->_oConfig->CNF;
  119. if($this->_sObject == $CNF['OBJECT_GRID_TIME_ADMINISTRATION'])
  120. $isDisabled = true;
  121. return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  122. }
  123. protected function _getActionDelete($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  124. {
  125. $CNF = &$this->_oModule->_oConfig->CNF;
  126. if($this->_sObject == $CNF['OBJECT_GRID_TIME_ADMINISTRATION'])
  127. $isDisabled = true;
  128. return parent::_getActionDelete($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  129. }
  130. protected function _getFilterControls()
  131. {
  132. $this->__getFilterControls();
  133. $sContent = $this->_getFilterSelectOne($this->_sFilter1Name, $this->_sFilter1Value, $this->_aFilter1Values);
  134. $sContent .= $this->_getFilterSelectOne($this->_sFilter2Name, $this->_sFilter2Value, $this->_aFilter2Values);
  135. $sContent .= $this->_getSearchInput();
  136. return $sContent;
  137. }
  138. protected function _getFilterOnChange()
  139. {
  140. return $this->_oModule->_oConfig->getJsObject('manage_tools') . '.onChangeFilter(this)';
  141. }
  142. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  143. {
  144. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND ABS(`tt`.`allow_view_to`)=?", $this->_iContextPid);
  145. $aFilterParts = explode($this->_sParamsDivider, $sFilter);
  146. switch (substr_count($sFilter, $this->_sParamsDivider)) {
  147. case 1:
  148. list($this->_sFilter1Value, $sFilter) = $aFilterParts;
  149. break;
  150. case 2:
  151. list($this->_sFilter1Value, $this->_sFilter2Value, $sFilter) = $aFilterParts;
  152. break;
  153. }
  154. if(!empty($this->_sFilter1Value))
  155. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `ttt`.`author_id`=?", $this->_sFilter1Value);
  156. if(!empty($this->_sFilter2Value))
  157. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `ttt`.`object_id`=?", $this->_sFilter2Value);
  158. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  159. }
  160. protected function _getEntryLink($sTitle, $sUrl, $aRow)
  161. {
  162. return $this->_oTemplate->parseHtmlByName('title_link.html', [
  163. 'href' => $sUrl,
  164. 'title' => bx_html_attribute($sTitle),
  165. 'content' => bx_process_output($sTitle),
  166. 'target' => '_blank'
  167. ]);
  168. }
  169. protected function __getFilterControls()
  170. {
  171. parent::_getFilterControls();
  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. /** @} */