BxTasksGridTimeAdministration.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 BxTemplGrid
  11. {
  12. protected $_sModule;
  13. protected $_oModule;
  14. protected $_iContextPid;
  15. protected $_sParamsDivider;
  16. protected $_sFilter1Name;
  17. protected $_sFilter1Value;
  18. protected $_aFilter1Values;
  19. protected $_sFilter2Name;
  20. protected $_sFilter2Value;
  21. protected $_aFilter2Values;
  22. public function __construct ($aOptions, $oTemplate = false)
  23. {
  24. $this->_sModule = 'bx_tasks';
  25. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  26. if(!$oTemplate)
  27. $oTemplate = $this->_oModule->_oTemplate;
  28. parent::__construct ($aOptions, $oTemplate);
  29. $this->_sDefaultSortingOrder = 'DESC';
  30. if(($iContextPid = bx_get('context_pid')) !== false)
  31. $this->setContextPid($iContextPid);
  32. $this->_sParamsDivider = '#-#';
  33. }
  34. public function setContextPid($iContextPid)
  35. {
  36. $CNF = &$this->_oModule->_oConfig->CNF;
  37. $this->_iContextPid = (int)$iContextPid;
  38. $this->_aQueryAppend['context_pid'] = $this->_iContextPid;
  39. /*
  40. * Filter by author_id
  41. */
  42. $this->_sFilter1Name = 'filter1';
  43. $this->_aFilter1Values = [];
  44. $this->_sFilter1Value = '';
  45. if($this->_sObject == $CNF['OBJECT_GRID_TIME_ADMINISTRATION']) {
  46. if(($oContext = BxDolProfile::getInstance($this->_iContextPid)) !== false) {
  47. $aPids = bx_srv($oContext->getModule(), 'fans', [$oContext->getContentId(), true]);
  48. foreach($aPids as $iPid)
  49. $this->_aFilter1Values[$iPid] = $oContext->getDisplayName($iPid);
  50. }
  51. if(($sFilter1 = bx_get($this->_sFilter1Name)) !== false) {
  52. $this->_sFilter1Value = bx_process_input($sFilter1);
  53. $this->_aQueryAppend[$this->_sFilter1Name] = $this->_sFilter1Value;
  54. }
  55. }
  56. /*
  57. * Filter by object_id
  58. */
  59. $this->_sFilter2Name = 'filter2';
  60. $this->_aFilter2Values = [];
  61. $this->_sFilter2Value = '';
  62. $aTasks = $this->_oModule->_oDb->getTasks(-$this->_iContextPid);
  63. foreach($aTasks as $aTask)
  64. $this->_aFilter2Values[$aTask[$CNF['FIELD_ID']]] = $aTask[$CNF['FIELD_TITLE']];
  65. if(($sFilter2 = bx_get($this->_sFilter2Name)) !== false) {
  66. $this->_sFilter2Value = bx_process_input($sFilter2);
  67. $this->_aQueryAppend[$this->_sFilter2Name] = $this->_sFilter2Value;
  68. }
  69. }
  70. public function performActionCalculate()
  71. {
  72. $sAction = 'calculate';
  73. $aIds = $this->_getIds();
  74. if($aIds === false)
  75. return $this->_getActionResult([]);
  76. $iTotal = 0;
  77. foreach($aIds as $iId) {
  78. $aTrack = $this->_oModule->_oDb->getTimeTracks(['sample' => 'id', 'id' => $iId]);
  79. if(empty($aTrack) || !is_array($aTrack))
  80. continue;
  81. $iTotal += $aTrack['value'];
  82. }
  83. $sContent = BxTemplFunctions::getInstance()->transBox($this->_oModule->_oConfig->getHtmlIds('total_popup'), $this->_oModule->_oTemplate->parseHtmlByName('popup_total.html', [
  84. 'total' => $this->_oModule->_oConfig->timeI2S($iTotal)
  85. ]));
  86. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => true]]]);
  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 _getActionEdit($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::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  129. }
  130. protected function _getActionDelete($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  131. {
  132. $CNF = &$this->_oModule->_oConfig->CNF;
  133. if($this->_sObject == $CNF['OBJECT_GRID_TIME_ADMINISTRATION'])
  134. $isDisabled = true;
  135. return parent::_getActionDelete($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  136. }
  137. protected function _getFilterControls()
  138. {
  139. $this->__getFilterControls();
  140. $sContent = $this->_getFilterSelectOne($this->_sFilter1Name, $this->_sFilter1Value, $this->_aFilter1Values);
  141. $sContent .= $this->_getFilterSelectOne($this->_sFilter2Name, $this->_sFilter2Value, $this->_aFilter2Values);
  142. $sContent .= $this->_getSearchInput();
  143. return $sContent;
  144. }
  145. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  146. {
  147. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND ABS(`tt`.`allow_view_to`)=?", $this->_iContextPid);
  148. $aFilterParts = explode($this->_sParamsDivider, $sFilter);
  149. switch (substr_count($sFilter, $this->_sParamsDivider)) {
  150. case 1:
  151. list($this->_sFilter1Value, $sFilter) = $aFilterParts;
  152. break;
  153. case 2:
  154. list($this->_sFilter1Value, $this->_sFilter2Value, $sFilter) = $aFilterParts;
  155. break;
  156. }
  157. if(!empty($this->_sFilter1Value))
  158. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `ttt`.`author_id`=?", $this->_sFilter1Value);
  159. if(!empty($this->_sFilter2Value))
  160. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `ttt`.`object_id`=?", $this->_sFilter2Value);
  161. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  162. }
  163. protected function _getFilterSelectOne($sFilterName, $sFilterValue, $aFilterValues, $bAddSelectOne = true)
  164. {
  165. if(empty($sFilterName))
  166. return '';
  167. $CNF = &$this->_oModule->_oConfig->CNF;
  168. $sJsObject = $this->_oModule->_oConfig->getJsObject('manage_tools');
  169. $aInputValues = [];
  170. if($bAddSelectOne)
  171. $aInputValues[''] = _t($CNF['T']['filter_item_select_one_' . $sFilterName]);
  172. foreach($aFilterValues as $sKey => $sValue)
  173. $aInputValues[$sKey] = _t($sValue);
  174. $aInputModules = [
  175. 'type' => 'select',
  176. 'name' => $sFilterName,
  177. 'attrs' => [
  178. 'id' => 'bx-grid-' . $sFilterName . '-' . $this->_sObject,
  179. 'onChange' => 'javascript:' . $sJsObject . '.onChangeFilter(this)'
  180. ],
  181. 'value' => $sFilterValue,
  182. 'values' => $aInputValues
  183. ];
  184. $oForm = new BxTemplFormView([]);
  185. return $oForm->genRow($aInputModules);
  186. }
  187. protected function _getSearchInput()
  188. {
  189. $sJsObject = $this->_oModule->_oConfig->getJsObject('manage_tools');
  190. $aInputSearch = [
  191. 'type' => 'text',
  192. 'name' => 'search',
  193. 'attrs' => [
  194. 'id' => 'bx-grid-search-' . $this->_sObject,
  195. 'onKeyup' => 'javascript:$(this).off(\'keyup focusout\'); ' . $sJsObject . '.onChangeFilter(this)',
  196. 'onBlur' => 'javascript:' . $sJsObject . '.onChangeFilter(this)',
  197. ]
  198. ];
  199. $oForm = new BxTemplFormView([]);
  200. return $oForm->genRow($aInputSearch);
  201. }
  202. protected function _getEntryLink($sTitle, $sUrl, $aRow)
  203. {
  204. return $this->_oTemplate->parseHtmlByName('title_link.html', [
  205. 'href' => $sUrl,
  206. 'title' => bx_html_attribute($sTitle),
  207. 'content' => bx_process_output($sTitle),
  208. 'target' => '_blank'
  209. ]);
  210. }
  211. protected function __getFilterControls()
  212. {
  213. parent::_getFilterControls();
  214. }
  215. protected function _getIds()
  216. {
  217. $aIds = bx_get('ids');
  218. if(!$aIds || !is_array($aIds)) {
  219. $iId = (int)bx_get('id');
  220. if(!$iId)
  221. return false;
  222. $aIds = [$iId];
  223. }
  224. return $aIds;
  225. }
  226. }
  227. /** @} */