1
0

BxBaseModGeneralVotePollAnswers.php 5.6 KB

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