BxTasksCmts.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 BxTasksCmts extends BxTemplCmts
  11. {
  12. protected $_sModule;
  13. protected $_oModule;
  14. protected $_iAuthorAuto;
  15. public function __construct($sSystem, $iId, $iInit = 1)
  16. {
  17. parent::__construct($sSystem, $iId, $iInit);
  18. $this->_sModule = 'bx_tasks';
  19. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  20. $this->_iAuthorAuto = (int)getParam('sys_profile_bot');
  21. $this->_aT = array_merge($this->_aT, [
  22. 'block_comments_title' => '_bx_tasks_page_block_title_entry_comments'
  23. ]);
  24. }
  25. public function addAuto($aText)
  26. {
  27. $aValues = [
  28. 'cmt_author_id' => $this->_iAuthorAuto,
  29. 'cmt_parent_id' => 0,
  30. 'cmt_text' => json_encode($aText)
  31. ];
  32. return $this->add($aValues);
  33. }
  34. public function getViewText($mixedItem)
  35. {
  36. if(!is_array($mixedItem))
  37. $mixedItem = $this->getCommentSimple((int)$mixedItem);
  38. if(empty($mixedItem) || !is_array($mixedItem))
  39. return '';
  40. if($this->_isAutoComment($mixedItem) && isset($mixedItem['cmt_text']))
  41. $mixedItem['cmt_text'] = $this->_getAutoCommentText($mixedItem['cmt_text']);
  42. return $this->_prepareTextForOutput($mixedItem['cmt_text'], (int)$mixedItem['cmt_id']);
  43. }
  44. public function getComment($mixedCmt, $aBp = [], $aDp = [])
  45. {
  46. $aCmt = !is_array($mixedCmt) ? $this->getCommentRow((int)$mixedCmt) : $mixedCmt;
  47. if(!$aCmt)
  48. return '';
  49. if($this->_isAutoComment($aCmt))
  50. $aDp = array_merge($aDp ?? [], [
  51. 'class_comment' => $this->_sStylePrefix . '-auto'
  52. ]);
  53. return parent::getComment($aCmt, $aBp, $aDp);
  54. }
  55. public function getDataAPI($aData, $aParams = [])
  56. {
  57. $aResult = parent::getDataAPI($aData, $aParams);
  58. if($this->_isAutoComment($aData)) {
  59. if(isset($aResult['cmt_text']))
  60. $aResult['cmt_text'] = $this->_getAutoCommentText($aResult['cmt_text'], $aData['cmt_time']);
  61. unset($aResult['menu_actions'], $aResult['menu_manage']);
  62. }
  63. return $aResult;
  64. }
  65. protected function _getHeaderBox(&$aCmt, $aBp = [], $aDp = [])
  66. {
  67. return !$this->_isAutoComment($aCmt) ? parent::_getHeaderBox($aCmt, $aBp, $aDp) : '';
  68. }
  69. protected function _getCountersBox(&$aCmt, $aBp = [], $aDp = [])
  70. {
  71. return !$this->_isAutoComment($aCmt) ? parent::_getCountersBox($aCmt, $aBp, $aDp) : '';
  72. }
  73. protected function _getActionsBox(&$aCmt, $aBp = [], $aDp = [])
  74. {
  75. return !$this->_isAutoComment($aCmt) ? parent::_getActionsBox($aCmt, $aBp, $aDp) : '';
  76. }
  77. protected function _getAttachments($aCmt)
  78. {
  79. return !$this->_isAutoComment($aCmt) ? parent::_getAttachments($aCmt) : ($this->_bIsApi ? [] : '');
  80. }
  81. protected function _getTmplVarsAuthor($aCmt)
  82. {
  83. return !$this->_isAutoComment($aCmt) ? parent::_getTmplVarsAuthor($aCmt) : [
  84. 'author_unit' => ''
  85. ];
  86. }
  87. protected function _getTmplVarsText($aCmt)
  88. {
  89. $aTmplVarsText = parent::_getTmplVarsText($aCmt);
  90. if($this->_isAutoComment($aCmt) && isset($aTmplVarsText['text']))
  91. $aTmplVarsText['text'] = $this->_getAutoCommentText($aTmplVarsText['text'], $aCmt['cmt_time']);
  92. return $aTmplVarsText;
  93. }
  94. protected function _prepareAlertParams($aCmt)
  95. {
  96. $aResult = parent::_prepareAlertParams($aCmt);
  97. if($this->_isAutoComment($aCmt) && isset($aResult['comment_text']))
  98. $aResult['comment_text'] = $this->_getAutoCommentText($aResult['comment_text']);
  99. return $aResult;
  100. }
  101. protected function _isAutoComment($aCmt)
  102. {
  103. return (int)$aCmt['cmt_author_id'] == $this->_iAuthorAuto;
  104. }
  105. protected function _getAutoCommentText($sTextCode, $iDate = false)
  106. {
  107. $aText = json_decode($sTextCode, true);
  108. $sText = isset($aText['key']) ? _t($aText['key']) : '';
  109. if(($aMarkers = $aText['markers'] ?? false) && is_array($aMarkers)) {
  110. if(($sValue = $aMarkers['value'] ?? false) && substr($sValue, 0, 1) == '_')
  111. $aMarkers['value'] = _t($sValue);
  112. $sText = bx_replace_markers($sText, $aMarkers);
  113. }
  114. if($iDate !== false)
  115. $sText = _t('_bx_tasks_txt_msg_format', $sText, $this->_bIsApi ? bx_process_output((int)$iDate, BX_DATA_DATE_TS) : bx_time_js((int)$iDate, BX_FORMAT_DATE, true));
  116. return $sText;
  117. }
  118. }
  119. /** @} */