BxPollsTemplate.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 Polls Polls
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Module representation.
  13. */
  14. class BxPollsTemplate extends BxBaseModTextTemplate
  15. {
  16. /**
  17. * Constructor
  18. */
  19. function __construct(&$oConfig, &$oDb)
  20. {
  21. $this->MODULE = 'bx_polls';
  22. parent::__construct($oConfig, $oDb);
  23. }
  24. public function embedEntry($mixedContentInfo, $aParams = array())
  25. {
  26. $oModule = $this->getModule();
  27. $CNF = &$oModule->_oConfig->CNF;
  28. $aContentInfo = is_array($mixedContentInfo) ? $mixedContentInfo : $this->_oDb->getContentInfoById((int)$mixedContentInfo);
  29. if(empty($aContentInfo) || !is_array($aContentInfo))
  30. return;
  31. /**
  32. * Anonymous voting MUST be enabled for Embeded poll,
  33. * because a list of voters cannot be correctly displayed
  34. * in Embeded iframe.
  35. */
  36. $aContentInfo[$CNF['FIELD_ANONYMOUS_VOTING']] = 1;
  37. $aBlock = $this->{$oModule->isPerformed((int)$aContentInfo[$CNF['FIELD_ID']]) ? 'entryResults' : 'entrySubentries'}($aContentInfo);
  38. $this->addJs(array('entry.js'));
  39. $this->addCss(array('entry.css'));
  40. $oTemplate = BxDolTemplate::getInstance();
  41. $oTemplate->addCssStyle($CNF['STYLES_POLLS_EMBED_CLASS'], $CNF['STYLES_POLLS_EMBED_CONTENT']);
  42. $oTemplate->setPageNameIndex(BX_PAGE_EMBED);
  43. $oTemplate->setPageHeader($this->getTitleAuto($aContentInfo));
  44. $oTemplate->setPageContent('page_main_code', $this->getJsCode('entry', array(
  45. 'bEmbed' => true
  46. )) . $aBlock['content']);
  47. $oTemplate->getPageCode();
  48. exit;
  49. }
  50. public function getJsCode($sType, $aParams = array(), $bWrap = true)
  51. {
  52. $aParams = array_merge(array(
  53. 'aHtmlIds' => $this->_oConfig->getHtmlIds()
  54. ), $aParams);
  55. return parent::getJsCode($sType, $aParams, $bWrap);
  56. }
  57. public function entrySubentries ($aData, $bDynamic = false)
  58. {
  59. $mixedContent = $this->_getGetBlockContentSubentries($aData, $bDynamic);
  60. if($this->_bIsApi)
  61. return $mixedContent;
  62. if(empty($mixedContent))
  63. return '';
  64. return array(
  65. 'content' => $mixedContent,
  66. 'menu' => $this->_getGetBlockMenu($aData, 'subentries')
  67. );
  68. }
  69. public function entryResults($aData, $bDynamic = false)
  70. {
  71. $mixedContent = $this->_getGetBlockContentResults($aData, $bDynamic);
  72. if($this->_bIsApi)
  73. return $mixedContent;
  74. if(empty($mixedContent))
  75. return '';
  76. return array(
  77. 'content' => $mixedContent,
  78. 'menu' => $this->_getGetBlockMenu($aData, 'results')
  79. );
  80. }
  81. public function entryTextAndSubentries($aData, $bForceDisplaySubentries = false)
  82. {
  83. $oModule = $this->getModule();
  84. $CNF = &$oModule->_oConfig->CNF;
  85. $aTmplVars = parent::getTmplVarsText($aData);
  86. if($this->_bIsApi) {
  87. $bPerformed = $oModule->isPerformed($aData['id']);
  88. return [
  89. 'id' => $aData['id'],
  90. 'title' => $this->_oConfig->getTitle($aData),
  91. 'image' => !empty($aTmplVars['image']) ? $aTmplVars['image'] : '',
  92. 'object' => $CNF['OBJECT_VOTES_SUBENTRIES'],
  93. 'subentries' => $this->_getGetBlockContentSubentries($aData),
  94. 'results' => $this->_getGetBlockContentResults($aData),
  95. 'is_hidden_results' => (int)$aData[$CNF['FIELD_HIDDEN_RESULTS']] == 1,
  96. 'is_performed' => $bPerformed,
  97. 'value' => $bPerformed ? $oModule->getPerformedValue($aData['id']) : 0
  98. ];
  99. }
  100. $sMethod = '_getGetBlockContent';
  101. $sMenuItem = '';
  102. if(!$bForceDisplaySubentries && $oModule->isPerformed($aData[$CNF['FIELD_ID']])) {
  103. $sMethod .= 'Results';
  104. $sMenuItem = 'results';
  105. }
  106. else {
  107. $sMethod .= 'Subentries';
  108. $sMenuItem = 'subentries';
  109. };
  110. $sSubentries = $this->$sMethod($aData);
  111. $aTmplVars = array_merge($aTmplVars, array(
  112. 'menu' => 'delete',
  113. 'bx_if:show_subentries' => array(
  114. 'condition' => !empty($sSubentries),
  115. 'content' => array(
  116. 'entry_subentries' => $sSubentries
  117. )
  118. )
  119. ));
  120. return array(
  121. 'content' => $this->parseHtmlByName('entry-text-subentries.html', $aTmplVars),
  122. 'menu' => $this->_getGetBlockMenu($aData, $sMenuItem)
  123. );
  124. }
  125. public function getTitleAuto($aData, $iMaxLen = 32, $sEllipsisSign = '...')
  126. {
  127. $sResult = $this->getTitle($aData);
  128. if(strlen($sResult) > 0 && $iMaxLen > 0)
  129. $sResult = strmaxtextlen($sResult, $iMaxLen, $sEllipsisSign);
  130. return $sResult;
  131. }
  132. public function getTitle($aData, $mixedProcessOutput = BX_DATA_TEXT)
  133. {
  134. $sResult = $this->_oConfig->getTitle($aData);
  135. if($mixedProcessOutput !== false && !empty($sResult))
  136. $sResult = bx_process_output($sResult, (int)$mixedProcessOutput);
  137. return $sResult;
  138. }
  139. public function getSummary($aData, $sTitle = '', $sText = '', $sUrl = '')
  140. {
  141. $oModule = $this->getModule();
  142. $CNF = &$oModule->_oConfig->CNF;
  143. $aBlock = $this->{$oModule->isPerformed($aData[$CNF['FIELD_ID']]) ? 'entryResults' : 'entrySubentries'}($aData);
  144. return $aBlock['content'];
  145. }
  146. protected function getUnit ($aData, $aParams = [])
  147. {
  148. $CNF = &$this->getModule()->_oConfig->CNF;
  149. $aResult = parent::getUnit($aData, $aParams);
  150. $sTemplate = 'unit';
  151. if(isset($aParams['template_name']))
  152. $sTemplate = $this->_getUnitName($aData, $aParams['template_name']);
  153. if(in_array($sTemplate, ['unit', 'unit_full']) && $aResult['bx_if:thumb']['condition']) {
  154. $oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_COVER']);
  155. if($oImagesTranscoder && ($sPhotoCover = $oImagesTranscoder->getFileUrl($aData[$CNF['FIELD_THUMB']])))
  156. $aResult['bx_if:thumb']['content'] = array_merge($aResult['bx_if:thumb']['content'], [
  157. 'thumb_url' => $sPhotoCover,
  158. 'gallery_url' => $sPhotoCover
  159. ]);
  160. }
  161. if(($oMenuMeta = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SNIPPET_META'], $this)) !== false) {
  162. $oMenuMeta->setContent($aData);
  163. $aResult = array_merge($aResult, [
  164. 'bx_if:meta' => [
  165. 'condition' => true,
  166. 'content' => [
  167. 'meta' => $oMenuMeta->getCode()
  168. ]
  169. ],
  170. ]);
  171. }
  172. return $aResult;
  173. }
  174. protected function getUnitThumbAndGallery ($aData)
  175. {
  176. list($sPhotoThumb, $sPhotoGallery) = parent::getUnitThumbAndGallery($aData);
  177. return array($sPhotoGallery, $sPhotoGallery);
  178. }
  179. protected function _getGetBlockMenu($aData, $sSelected = '')
  180. {
  181. $oModule = $this->getModule();
  182. $CNF = &$oModule->_oConfig->CNF;
  183. $sJsObject = $this->_oConfig->getJsObject('entry');
  184. $iContentId = $aData[$CNF['FIELD_ID']];
  185. $aBlocks = array(
  186. 'subentries' => true,
  187. 'results' => (int)$aData[$CNF['FIELD_HIDDEN_RESULTS']] == 0 || $oModule->isPerformed($iContentId)
  188. );
  189. $aMenu = array();
  190. foreach($aBlocks as $sBlock => $bActive) {
  191. if(!$bActive)
  192. continue;
  193. $sId = $this->_oConfig->getHtmlIds('block_link_' . $sBlock) . $aData['salt'];
  194. if(!empty($sSelected) && $sSelected == $sBlock)
  195. $sSelected = $sId;
  196. $aMenu[] = array('id' => $sId, 'name' => $sId, 'class' => '', 'link' => 'javascript:void(0)', 'onclick' => 'javascript:' . $sJsObject . '.changeBlock(this, \'' . $sBlock . '\', ' . bx_js_string(json_encode(array('content_id' => $iContentId, 'salt' => $aData['salt']))) . ')', 'target' => '_self', 'title' => _t('_bx_polls_menu_item_view_' . $sBlock));
  197. }
  198. if(count($aMenu) <= 1)
  199. return '';
  200. $sMenu = $this->_oConfig->getHtmlIds('block_menu');
  201. $oMenu = new BxTemplMenuInteractive(array('object' => $sMenu, 'menu_id' => $sMenu . '-' . $aData['salt'], 'menu_items' => $aMenu, 'template' => 'menu_interactive_vertical.html'));
  202. if(!empty($sSelected))
  203. $oMenu->setSelected('', $sSelected);
  204. return $oMenu;
  205. }
  206. protected function _getGetBlockContentSubentries($aData, $bDynamic = false)
  207. {
  208. $CNF = &$this->getModule()->_oConfig->CNF;
  209. $aSubentries = $this->_oDb->getSubentries(array('type' => 'entry_id', 'entry_id' => $aData[$CNF['FIELD_ID']]));
  210. if($this->_bIsApi)
  211. return $aSubentries;
  212. if(empty($aSubentries) || !is_array($aSubentries))
  213. return '';
  214. $aTmplVarsSubentries = array();
  215. foreach($aSubentries as $aSubentry) {
  216. $oVotes = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES_SUBENTRIES'], $aSubentry['id']);
  217. $oVotes->setEntry($aData);
  218. $aTmplVarsSubentries[] = array(
  219. 'subentry' => $oVotes->getElementBlock(array(
  220. 'dynamic_mode' => $bDynamic
  221. ))
  222. );
  223. }
  224. return $this->parseHtmlByName('subentries.html', array(
  225. 'html_id' => $this->_oConfig->getHtmlIds('content') . $aData[$CNF['FIELD_ID']],
  226. 'bx_repeat:subentries' => $aTmplVarsSubentries,
  227. 'bx_if:show_public' => array(
  228. 'condition' => (int)$aData[$CNF['FIELD_ANONYMOUS_VOTING']] == 0,
  229. 'content' => array()
  230. )
  231. ));
  232. }
  233. protected function _getGetBlockContentResults($aData, $bDynamic = false)
  234. {
  235. $oModule = $this->getModule();
  236. $CNF = &$oModule->_oConfig->CNF;
  237. $bAnonymous = (int)$aData[$CNF['FIELD_ANONYMOUS_VOTING']] == 1;
  238. $bHiddenResults = (int)$aData[$CNF['FIELD_HIDDEN_RESULTS']] == 1;
  239. $iContentId = (int)$aData[$CNF['FIELD_ID']];
  240. $aSubentries = $this->_oDb->getSubentries(['type' => 'entry_id', 'entry_id' => $iContentId]);
  241. if(empty($aSubentries) || !is_array($aSubentries))
  242. return $this->_bIsApi ? [] : '';
  243. if($bHiddenResults && !$oModule->isPerformed($iContentId))
  244. return $this->_bIsApi ? [] : '';
  245. $iTotal = 0;
  246. foreach($aSubentries as $aSubentry)
  247. $iTotal += $aSubentry['votes'];
  248. $aTmplVarsSubentries = array();
  249. foreach($aSubentries as $aSubentry) {
  250. $oVotes = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES_SUBENTRIES'], $aSubentry['id']);
  251. $oVotes->setEntry($aData);
  252. $fPercent = $iTotal > 0 ? 100 * (float)$aSubentry['votes']/$iTotal : 0;
  253. $aTmplVarsSubentries[] = array_merge([
  254. 'title' => bx_process_output($aSubentry['title']),
  255. 'width' => (int)round($fPercent) . '%',
  256. 'percent' => _t('_bx_polls_txt_subentry_vote_percent', $iTotal > 0 ? round($fPercent, 2) : 0),
  257. ], ($this->_bIsApi ? [
  258. 'votes' => $oVotes->getCounterAPI(['show_counter_empty' => true, 'show_counter_in_brackets' => false]),
  259. ] : [
  260. 'votes' => $oVotes->getCounter(['show_counter_empty' => true, 'show_counter_in_brackets' => false]),
  261. 'js_code' => $oVotes->getJsScript(['dynamic_mode' => $bDynamic])
  262. ]));
  263. }
  264. return $this->_bIsApi ? $aTmplVarsSubentries : $this->parseHtmlByName('subentries_results.html', array(
  265. 'html_id' => $this->_oConfig->getHtmlIds('content') . $iContentId,
  266. 'bx_repeat:subentries' => $aTmplVarsSubentries,
  267. ));
  268. }
  269. }
  270. /** @} */