BxBaseModGeneralVotePollAnswers.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 BaseGeneral Base classes for modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxBaseModGeneralVotePollAnswers extends BxTemplVoteLikes
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. protected $_aObjectInfo;
  16. protected $_aPollInfo;
  17. protected $_bContentInfo;
  18. protected $_aContentInfo;
  19. protected $_bFormMode;
  20. protected $_bHiddenResults;
  21. protected $_bAnonymousVoting;
  22. protected $_sTmplNameElementBlock;
  23. // A poll vote follows the object's IsUndo setting, unlike a like.
  24. protected $_bUndoAlways = false;
  25. function __construct($sSystem, $iId, $iInit = 1)
  26. {
  27. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  28. parent::__construct($sSystem, $iId, $iInit, $this->_oModule->_oTemplate);
  29. $CNF = &$this->_oModule->_oConfig->CNF;
  30. $this->_aElementDefaults = array_merge($this->_aElementDefaults, array(
  31. 'show_do_vote_label' => true,
  32. 'show_counter' => false
  33. ));
  34. $this->_aObjectInfo = $this->_oModule->_oDb->getPollAnswers(array('type' => 'id', 'id' => $iId));
  35. $this->_aPollInfo = $this->_oModule->_oDb->getPolls(array('type' => 'answer_id', 'answer_id' => $iId));
  36. $this->_aContentInfo = [];
  37. if(!empty($this->_aPollInfo[$CNF['FIELD_POLL_CONTENT_ID']]))
  38. $this->_aContentInfo = $this->_oModule->_oDb->getContentInfoById($this->_aPollInfo[$CNF['FIELD_POLL_CONTENT_ID']]);
  39. $this->_bContentInfo = !empty($this->_aContentInfo) && is_array($this->_aContentInfo);
  40. $this->_bFormMode = false;
  41. $this->_bHiddenResults = $CNF['PARAM_POLL_HIDDEN_RESULTS'];
  42. $this->_bAnonymousVoting = $CNF['PARAM_POLL_ANONYMOUS_VOTING'];
  43. $this->_sTmplNameElementBlock = 'poll_answer_ve_block.html';
  44. }
  45. public function getJsClick($iValue = 0)
  46. {
  47. $CNF = &$this->_oModule->_oConfig->CNF;
  48. $sJsObjectVote = $this->getJsObjectName();
  49. $sJsObjectPoll = $this->_bFormMode ? $this->_oModule->_oConfig->getJsObjectPoll($this->_bContentInfo ? (int)$this->_aPollInfo[$CNF['FIELD_POLL_CONTENT_ID']] : 0) : $this->_oModule->_oConfig->getJsObject('poll');
  50. return $sJsObjectVote . '.vote(this, ' . $this->getValue() . ', function(oLink, oData) {' . $sJsObjectPoll . '.onPollAnswerVote(oLink, oData, ' . $this->_aPollInfo[$CNF['FIELD_POLL_ID']] . ');})';
  51. }
  52. public function getElement($aParams = [])
  53. {
  54. $this->_bFormMode = isset($aParams['form_mode']) ? $aParams['form_mode'] : false;
  55. return parent::getElement($aParams);
  56. }
  57. public function getCounter($aParams = [])
  58. {
  59. $this->_bFormMode = isset($aParams['form_mode']) ? $aParams['form_mode'] : false;
  60. $iObjectId = $this->getId();
  61. $iAuthorId = $this->_getAuthorId();
  62. if($this->_bHiddenResults && !$this->isPerformed($iObjectId, $iAuthorId))
  63. return '';
  64. $sResult = parent::getCounter($aParams);
  65. if(!isset($aParams['show_counter_in_brackets']) || $aParams['show_counter_in_brackets'] == true)
  66. $sResult = '(' . $sResult . ')';
  67. return $sResult;
  68. }
  69. public function getObjectAuthorId($iObjectId = 0)
  70. {
  71. if(empty($this->_aSystem['trigger_field_author']))
  72. return 0;
  73. $aPoll = $this->_oModule->_oDb->getPolls(array('type' => 'answer_id', 'answer_id' => $iObjectId ? $iObjectId : $this->getId()));
  74. if(empty($aPoll) || !is_array($aPoll))
  75. return 0;
  76. return $aPoll[$this->_aSystem['trigger_field_author']];
  77. }
  78. public function isPerformed($iObjectId, $iAuthorId, $iAuthorIp = 0)
  79. {
  80. $CNF = &$this->_oModule->_oConfig->CNF;
  81. return $this->_oModule->isPollPerformed((int)$this->_aPollInfo[$CNF['FIELD_POLL_ID']], $iAuthorId, $iAuthorIp);
  82. }
  83. /**
  84. * Permissions functions
  85. */
  86. public function isAllowedVote($isPerformAction = false)
  87. {
  88. if($this->_bContentInfo && $this->_oModule->checkAllowedView($this->_aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  89. return false;
  90. return parent::isAllowedVote($isPerformAction);
  91. }
  92. public function isAllowedVoteViewVoters($isPerformAction = false)
  93. {
  94. if($this->_bAnonymousVoting)
  95. return false;
  96. return parent::isAllowedVoteViewVoters($isPerformAction);
  97. }
  98. /**
  99. * Internal functions
  100. */
  101. protected function _isAllowedVoteByObject($aObject)
  102. {
  103. /**
  104. * If a poll wasn't attached to any content yet,
  105. * then we cannot check content's visibility.
  106. */
  107. if(!$this->_bContentInfo)
  108. return true;
  109. return parent::_isAllowedVoteByObject($this->_aContentInfo);
  110. }
  111. protected function _getIconDo($bVoted)
  112. {
  113. return $bVoted ? 'far dot-circle' : 'far circle';
  114. }
  115. protected function _getTitleDo($bVoted)
  116. {
  117. return bx_process_output($this->_aObjectInfo['title']);
  118. }
  119. protected function _getTitleDoBy($aParams = array())
  120. {
  121. $CNF = &$this->_oModule->_oConfig->CNF;
  122. return _t($CNF['T']['txt_poll_answer_vote_do_by']);
  123. }
  124. protected function _getCounterLabel($iCount, $aParams = array())
  125. {
  126. $CNF = &$this->_oModule->_oConfig->CNF;
  127. return _t($CNF['T']['txt_poll_answer_vote_counter'], $iCount);
  128. }
  129. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  130. {
  131. return !isset($aParams['show_do_vote']) || $aParams['show_do_vote'] == true;
  132. }
  133. protected function _getTmplContentElementBlock()
  134. {
  135. return $this->_oTemplate->getHtml($this->_sTmplNameElementBlock);
  136. }
  137. }
  138. /** @} */