BxPollsModule.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. public function serviceGetSafeServices()
  63. {
  64. return array_merge(parent::serviceGetSafeServices(), [
  65. 'GetBlockSubentries' => '',
  66. 'GetBlockResults' => '',
  67. ]);
  68. }
  69. /**
  70. * @page service Service Calls
  71. * @section bx_polls Polls
  72. * @subsection bx_polls-page_blocks Page Blocks
  73. * @subsubsection bx_polls-get_results_search_extended get_results_search_extended
  74. *
  75. * @code bx_srv('bx_polls', 'get_results_search_extended', [...]); @endcode
  76. *
  77. * Get page block with the results of Extended Search.
  78. *
  79. * @param $aParams an array with search params.
  80. * @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.
  81. *
  82. * @see BxPollsModule::serviceGetResultsSearchExtended
  83. */
  84. /**
  85. * @ref bx_polls-get_results_search_extended "get_results_search_extended"
  86. */
  87. public function serviceGetResultsSearchExtended($aParams)
  88. {
  89. $this->_oTemplate->addJs(array('entry.js'));
  90. $this->_oTemplate->addCss(array('entry.css'));
  91. return $this->_oTemplate->getJsCode('entry') . BxDolService::call('system', 'get_results', array($aParams), 'TemplSearchExtendedServices');
  92. }
  93. /**
  94. * @page service Service Calls
  95. * @section bx_polls Polls
  96. * @subsection bx_polls-page_blocks Page Blocks
  97. * @subsubsection bx_polls-get_block_subentries get_block_subentries
  98. *
  99. * @code bx_srv('bx_polls', 'get_block_subentries', [...]); @endcode
  100. *
  101. * Get page block with poll questions.
  102. *
  103. * @param $iContentId (optional) poll's ID. If empty value is provided, an attempt to get it from GET/POST arrays will be performed.
  104. * @param $bForceDisplay (optional) if true is passed then the block will be displayed as is (without checking whether user answered the poll or not).
  105. * @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.
  106. *
  107. * @see BxPollsModule::serviceGetBlockSubentries
  108. */
  109. /**
  110. * @ref bx_polls-get_block_subentries "get_block_subentries"
  111. */
  112. public function serviceGetBlockSubentries($iContentId = 0, $bForceDisplay = false)
  113. {
  114. if (!$iContentId)
  115. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  116. if (!$iContentId)
  117. return false;
  118. if(!$bForceDisplay && $this->isPerformed($iContentId))
  119. return $this->serviceGetBlockResults($iContentId);
  120. return $this->_serviceTemplateFunc('entrySubentries', $iContentId);
  121. }
  122. /**
  123. * @page service Service Calls
  124. * @section bx_polls Polls
  125. * @subsection bx_polls-page_blocks Page Blocks
  126. * @subsubsection bx_polls-get_block_results get_block_results
  127. *
  128. * @code bx_srv('bx_polls', 'get_block_results', [...]); @endcode
  129. *
  130. * Get page block with poll results.
  131. *
  132. * @param $iContentId (optional) poll's ID. If empty value is provided, an attempt to get it from GET/POST arrays will be performed.
  133. * @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.
  134. *
  135. * @see BxPollsModule::serviceGetBlockResults
  136. */
  137. /**
  138. * @ref bx_polls-get_block_results "get_block_results"
  139. */
  140. public function serviceGetBlockResults($iContentId = 0)
  141. {
  142. return $this->_serviceTemplateFunc('entryResults', $iContentId);
  143. }
  144. /**
  145. * @page service Service Calls
  146. * @section bx_polls Polls
  147. * @subsection bx_polls-page_blocks Page Blocks
  148. * @subsubsection bx_polls-get_block_text_and_subentries get_block_text_and_subentries
  149. *
  150. * @code bx_srv('bx_polls', 'get_block_text_and_subentries', [...]); @endcode
  151. *
  152. * Get page block with poll question and answers with possibility to switch to results.
  153. *
  154. * @param $iContentId (optional) poll's ID. If empty value is provided, an attempt to get it from GET/POST arrays will be performed.
  155. * @param $bForceDisplay (optional) if true is passed then the block will be displayed as is (without checking whether user answered the poll or not).
  156. * @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.
  157. *
  158. * @see BxPollsModule::serviceGetBlockTextAndSubentries
  159. */
  160. /**
  161. * @ref bx_polls-get_block_text_and_subentries "get_block_text_and_subentries"
  162. */
  163. public function serviceGetBlockTextAndSubentries($iContentId = 0, $bForceDisplaySubentries = false)
  164. {
  165. if (!$iContentId)
  166. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  167. if (!$iContentId)
  168. return false;
  169. $mixedContent = $this->_getContent($iContentId);
  170. if($mixedContent === false)
  171. return false;
  172. list($iContentId, $aContentInfo) = $mixedContent;
  173. $mixedResult = $this->_oTemplate->entryTextAndSubentries($aContentInfo, $bForceDisplaySubentries);
  174. return $this->_bIsApi ? [bx_api_get_block('entity_poll', $mixedResult)] : $mixedResult;
  175. }
  176. /**
  177. * PERMISSION METHODS
  178. */
  179. public function isAllowedVote($isPerformAction = false)
  180. {
  181. $aCheck = checkActionModule($this->_iProfileId, 'vote entry', $this->getName(), false);
  182. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  183. return $aCheck[CHECK_ACTION_MESSAGE];
  184. return CHECK_ACTION_RESULT_ALLOWED;
  185. }
  186. public function isPerformed($iObjectId, $iAuthorId = 0, $iAuthorIp = 0)
  187. {
  188. if(empty($iAuthorId)) {
  189. $iAuthorId = bx_get_logged_profile_id();
  190. $iAuthorIp = bx_get_ip_hash(getVisitorIP());
  191. }
  192. return $this->_oDb->isPerformed($iObjectId, $iAuthorId, $iAuthorIp);
  193. }
  194. public function getPerformedValue($iObjectId, $iAuthorId = 0)
  195. {
  196. if(empty($iAuthorId))
  197. $iAuthorId = bx_get_logged_profile_id();
  198. return $this->_oDb->getPerformedValue($iObjectId, $iAuthorId);
  199. }
  200. /**
  201. * INTERNAL METHODS
  202. */
  203. protected function _getContent($iContentId = 0, $sFuncGetContent = 'getContentInfoById')
  204. {
  205. $mixedResult = parent::_getContent($iContentId, $sFuncGetContent);
  206. if(empty($mixedResult) || !is_array($mixedResult))
  207. return $mixedResult;
  208. list($iContentId, $aContentInfo) = $mixedResult;
  209. if(bx_get('salt') !== false)
  210. $aContentInfo['salt'] = bx_process_input(bx_get('salt'));
  211. return array($iContentId, $aContentInfo);
  212. }
  213. protected function _getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams = array())
  214. {
  215. $aResult = parent::_getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams);
  216. if($this->_bIsApi)
  217. return array_merge($aResult, $this->_oTemplate->entryTextAndSubentries($aContentInfo));
  218. $CNF = &$this->_oConfig->CNF;
  219. $bDynamic = isset($aBrowseParams['dynamic_mode']) && $aBrowseParams['dynamic_mode'] === true;
  220. $sInclude = '';
  221. $sInclude .= $this->_oTemplate->addCss(array('main.css'), $bDynamic);
  222. $aBlock = $this->_oTemplate->{$this->isPerformed($aContentInfo[$CNF['FIELD_ID']]) ? 'entryResults' : 'entrySubentries'}($aContentInfo, $bDynamic);
  223. $aResult['title'] = $this->_oConfig->getTitle($aContentInfo);
  224. $aResult['text'] = '';
  225. $aResult['raw'] = ($bDynamic ? $sInclude : '') . $this->_oTemplate->parseHtmlByName('unit_embed.html', array(
  226. 'html_id' => $this->_oConfig->getHtmlIds('embed') . (int)$aContentInfo[$CNF['FIELD_ID']],
  227. 'embed_url' => BX_DOL_URL_ROOT . bx_append_url_params($this->_oConfig->getBaseUri() . 'embed_entry/', array(
  228. 'id' => (int)$aContentInfo[$CNF['FIELD_ID']]
  229. ))
  230. ));
  231. return $aResult;
  232. }
  233. public function decodeDataAPI($aData, $aParams = [])
  234. {
  235. $CNF = $this->_oConfig->CNF;
  236. $aResult = parent::decodeDataAPI($aData, $aParams);
  237. if(is_array($aResult))
  238. $aResult = array_merge($aResult, [
  239. 'title' => strip_tags($aData[$CNF['FIELD_TEXT']]),
  240. ]);
  241. return $aResult;
  242. }
  243. }
  244. /** @} */