BxPollsModule.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. * Polls module
  13. */
  14. class BxPollsModule extends BxBaseModTextModule
  15. {
  16. function __construct(&$aModule)
  17. {
  18. parent::__construct($aModule);
  19. $CNF = &$this->_oConfig->CNF;
  20. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, array(
  21. $CNF['FIELD_ANONYMOUS_VOTING'],
  22. $CNF['FIELD_HIDDEN_RESULTS']
  23. ));
  24. }
  25. /**
  26. * ACTION METHODS
  27. */
  28. public function actionGetBlock()
  29. {
  30. $iContentId = (int)bx_get('content_id');
  31. $sBlock = bx_process_input(bx_get('block'));
  32. $sMethod = 'serviceGetBlock' . bx_gen_method_name($sBlock);
  33. if(!method_exists($this, $sMethod))
  34. return echoJson(array());
  35. $aBlock = $this->$sMethod($iContentId, true);
  36. if(empty($aBlock) || !is_array($aBlock))
  37. return echoJson(array());
  38. return echoJson(array(
  39. 'content' => $aBlock['content']
  40. ));
  41. }
  42. public function actionEmbed($iContentId, $sUnitTemplate = '', $sAddCode = '')
  43. {
  44. $this->_oTemplate->addJs(array('entry.js'));
  45. $this->_oTemplate->addCss(array('entry.css'));
  46. return parent::actionEmbed($iContentId, 'unit_embed_common.html', $this->_oTemplate->getJsCode('entry'));
  47. }
  48. public function actionEmbedEntry($iId = 0)
  49. {
  50. list($iContentId, $aContentInfo) = $this->_getContent($iId);
  51. if($iContentId === false)
  52. return;
  53. $aParams = bx_get_with_prefix('param');
  54. array_walk($aParams, function(&$sValue) {
  55. $sValue = bx_process_input($sValue);
  56. });
  57. $this->_oTemplate->embedEntry($aContentInfo, $aParams);
  58. }
  59. /**
  60. * SERVICE METHODS
  61. */
  62. /**
  63. * @page service Service Calls
  64. * @section bx_polls Polls
  65. * @subsection bx_polls-page_blocks Page Blocks
  66. * @subsubsection bx_polls-get_results_search_extended get_results_search_extended
  67. *
  68. * @code bx_srv('bx_polls', 'get_results_search_extended', [...]); @endcode
  69. *
  70. * Get page block with the results of Extended Search.
  71. *
  72. * @param $aParams an array with search params.
  73. * @return HTML string with block content to display on the site. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  74. *
  75. * @see BxPollsModule::serviceGetResultsSearchExtended
  76. */
  77. /**
  78. * @ref bx_polls-get_results_search_extended "get_results_search_extended"
  79. */
  80. public function serviceGetResultsSearchExtended($aParams)
  81. {
  82. $this->_oTemplate->addJs(array('entry.js'));
  83. $this->_oTemplate->addCss(array('entry.css'));
  84. return $this->_oTemplate->getJsCode('entry') . BxDolService::call('system', 'get_results', array($aParams), 'TemplSearchExtendedServices');
  85. }
  86. /**
  87. * @page service Service Calls
  88. * @section bx_polls Polls
  89. * @subsection bx_polls-page_blocks Page Blocks
  90. * @subsubsection bx_polls-get_block_subentries get_block_subentries
  91. *
  92. * @code bx_srv('bx_polls', 'get_block_subentries', [...]); @endcode
  93. *
  94. * Get page block with poll questions.
  95. *
  96. * @param $iContentId (optional) poll's ID. If empty value is provided, an attempt to get it from GET/POST arrays will be performed.
  97. * @param $bForceDisplay (optional) if true is passed then the block will be displayed as is (without checking whether user answered the poll or not).
  98. * @return HTML string with block content to display on the site or false if there is no enough input data. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  99. *
  100. * @see BxPollsModule::serviceGetBlockSubentries
  101. */
  102. /**
  103. * @ref bx_polls-get_block_subentries "get_block_subentries"
  104. */
  105. public function serviceGetBlockSubentries($iContentId = 0, $bForceDisplay = false)
  106. {
  107. if (!$iContentId)
  108. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  109. if (!$iContentId)
  110. return false;
  111. if(!$bForceDisplay && $this->isPerformed($iContentId))
  112. return $this->serviceGetBlockResults($iContentId);
  113. return $this->_serviceTemplateFunc('entrySubentries', $iContentId);
  114. }
  115. /**
  116. * @page service Service Calls
  117. * @section bx_polls Polls
  118. * @subsection bx_polls-page_blocks Page Blocks
  119. * @subsubsection bx_polls-get_block_results get_block_results
  120. *
  121. * @code bx_srv('bx_polls', 'get_block_results', [...]); @endcode
  122. *
  123. * Get page block with poll results.
  124. *
  125. * @param $iContentId (optional) poll's ID. If empty value is provided, an attempt to get it from GET/POST arrays will be performed.
  126. * @return HTML string with block content to display on the site or false if there is no enough input data. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  127. *
  128. * @see BxPollsModule::serviceGetBlockResults
  129. */
  130. /**
  131. * @ref bx_polls-get_block_results "get_block_results"
  132. */
  133. public function serviceGetBlockResults($iContentId = 0)
  134. {
  135. return $this->_serviceTemplateFunc('entryResults', $iContentId);
  136. }
  137. /**
  138. * @page service Service Calls
  139. * @section bx_polls Polls
  140. * @subsection bx_polls-page_blocks Page Blocks
  141. * @subsubsection bx_polls-get_block_text_and_subentries get_block_text_and_subentries
  142. *
  143. * @code bx_srv('bx_polls', 'get_block_text_and_subentries', [...]); @endcode
  144. *
  145. * Get page block with poll question and answers with possibility to switch to results.
  146. *
  147. * @param $iContentId (optional) poll's ID. If empty value is provided, an attempt to get it from GET/POST arrays will be performed.
  148. * @param $bForceDisplay (optional) if true is passed then the block will be displayed as is (without checking whether user answered the poll or not).
  149. * @return HTML string with block content to display on the site or false if there is no enough input data. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  150. *
  151. * @see BxPollsModule::serviceGetBlockTextAndSubentries
  152. */
  153. /**
  154. * @ref bx_polls-get_block_text_and_subentries "get_block_text_and_subentries"
  155. */
  156. public function serviceGetBlockTextAndSubentries($iContentId = 0, $bForceDisplaySubentries = false)
  157. {
  158. if (!$iContentId)
  159. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  160. if (!$iContentId)
  161. return false;
  162. $mixedContent = $this->_getContent($iContentId);
  163. if($mixedContent === false)
  164. return false;
  165. list($iContentId, $aContentInfo) = $mixedContent;
  166. $mixedResult = $this->_oTemplate->entryTextAndSubentries($aContentInfo, $bForceDisplaySubentries);
  167. return $this->_bIsApi ? [bx_api_get_block('entity_poll', $mixedResult)] : $mixedResult;
  168. }
  169. /**
  170. * PERMISSION METHODS
  171. */
  172. public function isAllowedVote($isPerformAction = false)
  173. {
  174. $aCheck = checkActionModule($this->_iProfileId, 'vote entry', $this->getName(), false);
  175. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  176. return $aCheck[CHECK_ACTION_MESSAGE];
  177. return CHECK_ACTION_RESULT_ALLOWED;
  178. }
  179. public function isPerformed($iObjectId, $iAuthorId = 0, $iAuthorIp = 0)
  180. {
  181. if(empty($iAuthorId)) {
  182. $iAuthorId = bx_get_logged_profile_id();
  183. $iAuthorIp = bx_get_ip_hash(getVisitorIP());
  184. }
  185. return $this->_oDb->isPerformed($iObjectId, $iAuthorId, $iAuthorIp);
  186. }
  187. /**
  188. * INTERNAL METHODS
  189. */
  190. protected function _getContent($iContentId = 0, $sFuncGetContent = 'getContentInfoById')
  191. {
  192. $mixedResult = parent::_getContent($iContentId, $sFuncGetContent);
  193. if(empty($mixedResult) || !is_array($mixedResult))
  194. return $mixedResult;
  195. list($iContentId, $aContentInfo) = $mixedResult;
  196. if(bx_get('salt') !== false)
  197. $aContentInfo['salt'] = bx_process_input(bx_get('salt'));
  198. return array($iContentId, $aContentInfo);
  199. }
  200. protected function _getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams = array())
  201. {
  202. $CNF = &$this->_oConfig->CNF;
  203. $bDynamic = isset($aBrowseParams['dynamic_mode']) && $aBrowseParams['dynamic_mode'] === true;
  204. $sInclude = '';
  205. $sInclude .= $this->_oTemplate->addCss(array('main.css'), $bDynamic);
  206. $aBlock = $this->_oTemplate->{$this->isPerformed($aContentInfo[$CNF['FIELD_ID']]) ? 'entryResults' : 'entrySubentries'}($aContentInfo, $bDynamic);
  207. $aResult = parent::_getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams);
  208. $aResult['title'] = $this->_oConfig->getTitle($aContentInfo);
  209. $aResult['text'] = '';
  210. $aResult['raw'] = ($bDynamic ? $sInclude : '') . $this->_oTemplate->parseHtmlByName('unit_embed.html', array(
  211. 'html_id' => $this->_oConfig->getHtmlIds('embed') . (int)$aContentInfo[$CNF['FIELD_ID']],
  212. 'embed_url' => BX_DOL_URL_ROOT . bx_append_url_params($this->_oConfig->getBaseUri() . 'embed_entry/', array(
  213. 'id' => (int)$aContentInfo[$CNF['FIELD_ID']]
  214. ))
  215. ));
  216. return $aResult;
  217. }
  218. }
  219. /** @} */