BxDolCmtsReviews.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * Reviews for any content
  11. */
  12. class BxDolCmtsReviews extends BxTemplCmts
  13. {
  14. protected $_iMoodMinValue;
  15. protected $_iMoodMaxValue;
  16. protected $_sMoodLegendId;
  17. protected $_sRatingLegendId;
  18. /*
  19. * Determines whether an object author is allowed
  20. * to post review to his own content.
  21. */
  22. protected $_bOaPostAllowed;
  23. /*
  24. * Determines whether an object author is allowed
  25. * to reply to somebody's review.
  26. */
  27. protected $_bOaReplyAllowed;
  28. /*
  29. * Determines whether a review author is allowed
  30. * to reply to his own review or to somebody's reply
  31. * to his own review.
  32. */
  33. protected $_bRaReplyAllowed;
  34. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  35. {
  36. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  37. $this->_sType = BX_DOL_CMT_TYPE_REVIEWS;
  38. $this->_oQuery = new BxDolCmtsReviewsQuery($this);
  39. $this->_sFormObject = 'sys_review';
  40. $this->_sFormDisplayPost = 'sys_review_post';
  41. $this->_sFormDisplayEdit = 'sys_review_edit';
  42. $this->_aT = array(
  43. 'block_comments_title' => '_cmt_rvw_block_reviews_title',
  44. 'block_rating_title' => '_cmt_rvw_block_rating_title',
  45. 'txt_sample_single' => '_cmt_rvw_txt_sample_review_single',
  46. 'txt_sample_vote_single' => '_cmt_rvw_txt_sample_vote_single',
  47. 'txt_sample_reaction_single' => '_cmt_rvw_txt_sample_reaction_single',
  48. 'txt_sample_score_up_single' => '_cmt_rvw_txt_sample_score_up_single',
  49. 'txt_sample_score_down_single' => '_cmt_rvw_txt_sample_score_down_single',
  50. 'txt_min_form_placeholder' => '_cmt_rvw_txt_min_form_placeholder'
  51. );
  52. $this->_iMoodMinValue = 1;
  53. $this->_iMoodMaxValue = 5;
  54. $this->_sMoodLegendId = "cmt-legend-%s-%d-%d";
  55. $this->_sRatingLegendId = "cmt-legend-%s-%d";
  56. $this->_bOaPostAllowed = true;
  57. $this->_bOaReplyAllowed = true;
  58. $this->_bRaReplyAllowed = true;
  59. }
  60. public function isPostAllowed($isPerformAction = false)
  61. {
  62. $iUsrId = $this->_getAuthorId();
  63. $iObjAthrId = $this->getObjectAuthorId();
  64. if($iUsrId == $iObjAthrId && !$this->_bOaPostAllowed)
  65. return false;
  66. if($this->isReviewed($iUsrId))
  67. return false;
  68. if($this->isPostAllowedCustom($isPerformAction) === false)
  69. return false;
  70. return parent::isPostAllowed($isPerformAction);
  71. }
  72. public function isReplyAllowed ($mixedCmt, $isPerformAction = false)
  73. {
  74. $iUsrId = $this->_getAuthorId();
  75. $iObjAthrId = $this->getObjectAuthorId();
  76. $iRvwAthrId = $this->getReviewAuthorId($mixedCmt);
  77. if(!in_array($iUsrId, array($iObjAthrId, $iRvwAthrId)))
  78. return false;
  79. if($iUsrId == $iObjAthrId && $iUsrId != $iRvwAthrId && !$this->_bOaReplyAllowed)
  80. return false;
  81. if($iUsrId == $iRvwAthrId && !$this->_bRaReplyAllowed)
  82. return false;
  83. return parent::isPostAllowed($isPerformAction);
  84. }
  85. /**
  86. * Can be overwritten if some custom check is needed.
  87. * @param boolean $isPerformAction
  88. * @return boolean
  89. */
  90. public function isPostAllowedCustom($isPerformAction = false)
  91. {
  92. return true;
  93. }
  94. public function isReviewed($mixedCmt)
  95. {
  96. return $this->_oQuery->isReviewed($this->getId(), $mixedCmt);
  97. }
  98. public function getMoodMinValue()
  99. {
  100. return $this->_iMoodMinValue;
  101. }
  102. public function getMoodMaxValue()
  103. {
  104. return $this->_iMoodMaxValue;
  105. }
  106. public function getMoodLegendId($iItemId)
  107. {
  108. return sprintf($this->_sMoodLegendId, str_replace('_', '-', $this->getSystemName()), $this->getId(), $iItemId);
  109. }
  110. public function getRatingLegendId()
  111. {
  112. return sprintf($this->_sRatingLegendId, str_replace('_', '-', $this->getSystemName()), $this->getId());
  113. }
  114. public function getReviewAuthorId($mixedCmt)
  115. {
  116. return $this->_oQuery->getReviewAuthorId($this->getId(), $mixedCmt);
  117. }
  118. public function onEditAfter($iCmtId, $aDp = [])
  119. {
  120. $mixedResult = parent::onEditAfter($iCmtId, $aDp);
  121. if($mixedResult === false)
  122. return $mixedResult;
  123. $aCmt = $this->getCommentSimple($iCmtId);
  124. return array_merge($mixedResult, [
  125. 'mood' => (int)$aCmt['cmt_mood'],
  126. 'mood_legend_id' => $this->getMoodLegendId($iCmtId)
  127. ]);
  128. }
  129. protected function _triggerComment()
  130. {
  131. if(!$this->_aSystem['trigger_table'])
  132. return false;
  133. $iId = $this->getId();
  134. if(!$iId)
  135. return false;
  136. $aStats = $this->_oQuery->getReviewsStats($iId);
  137. return $this->_oQuery->updateTriggerTable($iId, (int)$aStats['count']) !== false && $this->_oQuery->updateTriggerTableAvg($iId, (float)$aStats['avg']) !== false;
  138. }
  139. protected function _getIconDo()
  140. {
  141. return 'far comment-dots';
  142. }
  143. protected function _getTitleDo()
  144. {
  145. return '_cmt_rvw_txt_do';
  146. }
  147. }
  148. /** @} */