BxBaseModTextVotePollAnswers.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 BaseText Base classes for text modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxBaseModTextVotePollAnswers extends BxTemplVoteLikes
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. protected $_aObjectInfo;
  16. protected $_aPollInfo;
  17. protected $_bHiddenResults;
  18. protected $_bAnonymousVoting;
  19. protected $_sTmplNameElementBlock;
  20. protected $_sTmplNameCounterText;
  21. function __construct($sSystem, $iId, $iInit = 1)
  22. {
  23. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  24. parent::__construct($sSystem, $iId, $iInit, $this->_oModule->_oTemplate);
  25. $CNF = &$this->_oModule->_oConfig->CNF;
  26. $this->_aElementDefaults = array_merge($this->_aElementDefaults, array(
  27. 'show_do_vote_label' => true,
  28. 'show_counter' => false
  29. ));
  30. $this->_aObjectInfo = $this->_oModule->_oDb->getPollAnswers(array('type' => 'id', 'id' => $iId));
  31. $this->_aPollInfo = $this->_oModule->_oDb->getPolls(array('type' => 'answer_id', 'answer_id' => $iId));
  32. $this->_bHiddenResults = $CNF['PARAM_POLL_HIDDEN_RESULTS'];
  33. $this->_bAnonymousVoting = $CNF['PARAM_POLL_ANONYMOUS_VOTING'];
  34. $this->_sTmplNameElementBlock = 'poll_answer_ve_block.html';
  35. $this->_sTmplNameCounterText = 'poll_answer_vc_text.html';
  36. }
  37. public function getJsClick($iValue = 0)
  38. {
  39. $CNF = &$this->_oModule->_oConfig->CNF;
  40. $sJsObjectVote = $this->getJsObjectName();
  41. $sJsObjectPoll = $this->_oModule->_oConfig->getJsObject('poll');
  42. return $sJsObjectVote . '.vote(this, ' . $this->getValue() . ', function(oLink, oData) {' . $sJsObjectPoll . '.onPollAnswerVote(oLink, oData, ' . $this->_aPollInfo[$CNF['FIELD_POLL_ID']] . ');})';
  43. }
  44. public function getCounter($aParams = array())
  45. {
  46. $CNF = &$this->_oModule->_oConfig->CNF;
  47. $bShowInBrackets = !isset($aParams['show_counter_in_brackets']) || $aParams['show_counter_in_brackets'] == true;
  48. $iObjectId = $this->getId();
  49. $iAuthorId = $this->_getAuthorId();
  50. if($this->_bHiddenResults)
  51. if(!$this->isPerformed($iObjectId, $iAuthorId))
  52. return '';
  53. $sResult = parent::getCounter($aParams);
  54. if($bShowInBrackets)
  55. $sResult = '(' . $sResult . ')';
  56. return $sResult;
  57. }
  58. public function getObjectAuthorId($iObjectId = 0)
  59. {
  60. if(empty($this->_aSystem['trigger_field_author']))
  61. return 0;
  62. $aPoll = $this->_oModule->_oDb->getPolls(array('type' => 'answer_id', 'answer_id' => $iObjectId ? $iObjectId : $this->getId()));
  63. if(empty($aPoll) || !is_array($aPoll))
  64. return 0;
  65. return $aPoll[$this->_aSystem['trigger_field_author']];
  66. }
  67. public function isPerformed($iObjectId, $iAuthorId, $iAuthorIp = 0)
  68. {
  69. $CNF = &$this->_oModule->_oConfig->CNF;
  70. return $this->_oModule->_oDb->isPollPerformed($this->_aPollInfo[$CNF['FIELD_POLL_ID']], $iAuthorId);
  71. }
  72. /**
  73. * Permissions functions
  74. */
  75. public function isAllowedVote($isPerformAction = false)
  76. {
  77. if(!parent::isAllowedVote($isPerformAction))
  78. return false;
  79. $CNF = &$this->_oModule->_oConfig->CNF;
  80. $iContentId = $this->_aPollInfo[$CNF['FIELD_POLL_CONTENT_ID']];
  81. if(empty($iContentId))
  82. return true;
  83. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  84. return $this->_oModule->checkAllowedView($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED;
  85. }
  86. /**
  87. * Internal functions
  88. */
  89. protected function _getIconDoLike($bVoted)
  90. {
  91. return $bVoted ? 'far dot-circle' : 'far circle';
  92. }
  93. protected function _getTitleDoLike($bVoted)
  94. {
  95. return bx_process_output($this->_aObjectInfo['title']);
  96. }
  97. protected function _getTitleDoBy($aParams = array())
  98. {
  99. $CNF = &$this->_oModule->_oConfig->CNF;
  100. return _t($CNF['T']['txt_poll_answer_vote_do_by']);
  101. }
  102. protected function _getLabelCounter($iCount, $aParams = array())
  103. {
  104. $CNF = &$this->_oModule->_oConfig->CNF;
  105. return _t($CNF['T']['txt_poll_answer_vote_counter'], $iCount);
  106. }
  107. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  108. {
  109. return !isset($aParams['show_do_vote']) || $aParams['show_do_vote'] == true;
  110. }
  111. protected function _getTmplContentElementBlock()
  112. {
  113. return $this->_oTemplate->getHtml($this->_sTmplNameElementBlock);
  114. }
  115. protected function _getTmplContentCounter()
  116. {
  117. if($this->_bAnonymousVoting)
  118. return $this->_oTemplate->getHtml($this->_sTmplNameCounterText);
  119. return self::$_sTmplContentCounter;
  120. }
  121. }
  122. /** @} */