1
0

BxTasksGridTimeContextAdministration.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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('BxTasksGridTime.php');
  11. class BxTasksGridTimeContextAdministration extends BxTasksGridTime
  12. {
  13. protected $_iContextPid;
  14. public function __construct ($aOptions, $oTemplate = false)
  15. {
  16. parent::__construct ($aOptions, $oTemplate);
  17. $this->_sPaginateType = 'load_more';
  18. if(($iContextPid = bx_get('context_pid')) !== false)
  19. $this->setContextPid($iContextPid);
  20. }
  21. public function setContextPid($iContextPid)
  22. {
  23. $this->_iContextPid = (int)$iContextPid;
  24. $this->_aQueryAppend['context_pid'] = $this->_iContextPid;
  25. /*
  26. * Filter by author_id
  27. */
  28. if($this->_isAdministration())
  29. $this->_initFilter(1, $this->_oModule->getContextMembers($this->_iContextPid));
  30. /*
  31. * Filter by object_id
  32. */
  33. $this->_initFilter(2, $this->_oModule->getContextEntries($this->_iContextPid));
  34. }
  35. public function getFormCallBackUrlAPI($sAction, $iId = 0)
  36. {
  37. return '/api.php?r=system/perfom_action_api/TemplServiceGrid/&params[]=&o=' . $this->_sObject . '&a=' . $sAction . '&context_pid=' . $this->_iContextPid . '&id=' . $iId;
  38. }
  39. public function performActionCalculate()
  40. {
  41. if($this->_isAdministration() && !$this->_isAllowedByConext())
  42. return $this->_getActionResult([]);
  43. return parent::performActionCalculate();
  44. }
  45. public function performActionExport()
  46. {
  47. $CNF = &$this->_oModule->_oConfig->CNF;
  48. $aFields = explode(',', getParam($CNF['PARAM_EXPORT_TIME_FIELDS']));
  49. if(!$aFields || !is_array($aFields)) {
  50. echoJson([]);
  51. return;
  52. }
  53. $aTitles = $aConverters = [];
  54. foreach($aFields as $sField) {
  55. $aTitles[$sField] = _t('_bx_tasks_option_export_time_field_' . $sField);
  56. $sConverter = '_exportFieldConverter' . bx_gen_method_name($sField);
  57. if(method_exists($this, $sConverter))
  58. $aConverters[$sField] = $sConverter;
  59. }
  60. $sMethod = '_exportTo' . bx_gen_method_name(getParam($CNF['PARAM_EXPORT_TIME_TO']));
  61. if(!method_exists($this, $sMethod)) {
  62. echoJson([]);
  63. return;
  64. }
  65. $aIds = $this->_getIds();
  66. if(!$aIds || !is_array($aIds)) {
  67. echoJson([]);
  68. return;
  69. }
  70. if(($aFile = $this->$sMethod($aIds, $aFields, $aTitles, $aConverters)) === false) {
  71. echoJson([]);
  72. return;
  73. }
  74. list($sFilePath, $sFileUrl) = $aFile;
  75. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE_EXPORT_FILES']);
  76. if($oStorage && ($iFileId = $oStorage->storeFileFromPath($sFilePath, true, bx_get_logged_profile_id())) !== false) {
  77. $sFileUrl = $oStorage->getFileUrlById($iFileId);
  78. @unlink($sFilePath);
  79. }
  80. echoJson(['redirect' => $sFileUrl]);
  81. return;
  82. }
  83. public function getCode ($isDisplayHeader = true)
  84. {
  85. if($this->_isAdministration() && !$this->_isAllowedByConext())
  86. return $this->_getActionResult([]);
  87. return parent::getCode($isDisplayHeader);
  88. }
  89. protected function _isAllowedByConext($iContextPid = 0)
  90. {
  91. if(!$iContextPid)
  92. $iContextPid = $this->_iContextPid;
  93. if(!$iContextPid)
  94. return false;
  95. return $this->_oModule->isAllowManageByContext($this->_iContextPid);
  96. }
  97. protected function _getFilterControls()
  98. {
  99. $this->_getFcDefault();
  100. $sContent = $this->_getFilterSelectOne($this->_sFilter1Name, $this->_sFilter1Value, $this->_aFilter1Values, '_bx_tasks_grid_filter_item_title_tm_select_one_author_id');
  101. $sContent .= $this->_getFilterSelectOne($this->_sFilter2Name, $this->_sFilter2Value, $this->_aFilter2Values, '_bx_tasks_grid_filter_item_title_tm_select_one_object_id');
  102. $sContent .= $this->_getFcDateSearch();
  103. return $sContent;
  104. }
  105. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  106. {
  107. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND ABS(`tt`.`allow_view_to`)=?", $this->_iContextPid);
  108. $this->_parseFilterValue($sFilter);
  109. if(!empty($this->_sFilter1Value))
  110. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `ttt`.`author_id`=?", $this->_sFilter1Value);
  111. if(!empty($this->_sFilter2Value))
  112. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `ttt`.`object_id`=?", $this->_sFilter2Value);
  113. if(!empty($this->_sFilter3Value))
  114. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `ttt`.`value_date`>=?", strtotime($this->_sFilter3Value));
  115. if(!empty($this->_sFilter4Value))
  116. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `ttt`.`value_date`<=?", strtotime($this->_sFilter4Value));
  117. return parent::__getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  118. }
  119. protected function _exportToCsv($aIds, $aFields, $aTitles, $aConverters)
  120. {
  121. $sFileDiv = ';';
  122. $sFileName = 'tasks-reported-time-' . date('Y.m.d') . '.csv';
  123. $sFilePath = BX_DIRECTORY_PATH_TMP . $sFileName;
  124. $oFileHandler = fopen($sFilePath, 'w');
  125. if(!$oFileHandler)
  126. return false;
  127. fputcsv($oFileHandler, $aTitles, $sFileDiv);
  128. $aFieldsFlip = array_flip($aFields);
  129. foreach($aIds as $iId) {
  130. $aTrack = $this->_oModule->_oDb->getTimeTracks(['sample' => 'id', 'id' => $iId]);
  131. if(!$aTrack || !is_array($aTrack))
  132. continue;
  133. $aConvert = array_intersect_key($aConverters, $aTrack);
  134. foreach($aConvert as $sField => $sMethod)
  135. $aTrack[$sField] = $this->$sMethod($aTrack[$sField]);
  136. fputcsv($oFileHandler, array_intersect_key($aTrack, $aFieldsFlip), $sFileDiv);
  137. }
  138. fclose($oFileHandler);
  139. return [$sFilePath, BX_DOL_URL_ROOT . 'tmp/' . $sFileName];
  140. }
  141. protected function _exportToXml($aIds, $aFields, $aTitles, $aConverters)
  142. {
  143. $sFileName = 'tasks-reported-time-' . date('Y.m.d') . '.xml';
  144. $sFilePath = BX_DIRECTORY_PATH_TMP . $sFileName;
  145. $oFileHandler = fopen($sFilePath, 'w');
  146. if(!$oFileHandler)
  147. return false;
  148. $aFieldsFlip = array_flip($aFields);
  149. $oXML = xmlwriter_open_memory();
  150. xmlwriter_set_indent($oXML, 1);
  151. xmlwriter_set_indent_string($oXML, ' ');
  152. xmlwriter_start_document($oXML, '1.0', 'UTF-8');
  153. xmlwriter_start_element($oXML, 'items');
  154. foreach($aIds as $iId) {
  155. $aTrack = $this->_oModule->_oDb->getTimeTracks(['sample' => 'id', 'id' => $iId]);
  156. if(!$aTrack || !is_array($aTrack))
  157. continue;
  158. $aConvert = array_intersect_key($aConverters, $aTrack);
  159. foreach($aConvert as $sField => $sMethod)
  160. $aTrack[$sField] = $this->$sMethod($aTrack[$sField]);
  161. xmlwriter_start_element($oXML, 'item');
  162. $aTrackFields = array_intersect_key($aTrack, $aFieldsFlip);
  163. foreach($aTrackFields as $sTrackField => $sTrackValue) {
  164. xmlwriter_start_element($oXML, $sTrackField);
  165. xmlwriter_write_cdata($oXML, $sTrackValue);
  166. xmlwriter_end_element($oXML);
  167. }
  168. xmlwriter_end_element($oXML);
  169. }
  170. xmlwriter_end_document($oXML);
  171. fwrite($oFileHandler, xmlwriter_output_memory($oXML));
  172. fclose($oFileHandler);
  173. return [$sFilePath, BX_DOL_URL_ROOT . 'tmp/' . $sFileName];
  174. }
  175. protected function _exportFieldConverterAuthorId($mixedValue)
  176. {
  177. return BxDolProfile::getInstanceMagic($mixedValue)->getDisplayName();
  178. }
  179. protected function _exportFieldConverterObjectId($mixedValue)
  180. {
  181. return $this->_oModule->serviceGetTitle($mixedValue);
  182. }
  183. protected function _exportFieldConverterValue($mixedValue)
  184. {
  185. return $this->__exportFieldConverterTime($mixedValue);
  186. }
  187. protected function _exportFieldConverterValueDate($mixedValue)
  188. {
  189. return $this->__exportFieldConverterDate($mixedValue);
  190. }
  191. protected function _exportFieldConverterDate($mixedValue)
  192. {
  193. return $this->__exportFieldConverterDateTime($mixedValue);
  194. }
  195. protected function __exportFieldConverterDate($mixedValue)
  196. {
  197. return date('Y.m.d', $mixedValue);
  198. }
  199. protected function __exportFieldConverterDateTime($mixedValue)
  200. {
  201. return date('Y.m.d H:i', $mixedValue);
  202. }
  203. protected function __exportFieldConverterTime($mixedValue)
  204. {
  205. return $this->_oModule->_oConfig->timeI2S($mixedValue);
  206. }
  207. }
  208. /** @} */