BxPollsModule.php 9.0 KB

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