1
0

BxReviewsTemplate.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 Reviews Reviews
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Module representation.
  13. */
  14. class BxReviewsTemplate extends BxBaseModTextTemplate
  15. {
  16. function __construct(&$oConfig, &$oDb)
  17. {
  18. $this->MODULE = 'bx_reviews';
  19. parent::__construct($oConfig, $oDb);
  20. }
  21. protected function _addCommonJs() {
  22. $sRes = $this->addJs(['BxDolVoteStars.js', 'BxReviewsVoteStars.js'], bx_is_dynamic_request());
  23. $sRes .= $this->addCss(['main.css'], bx_is_dynamic_request());
  24. if (bx_is_dynamic_request()) return $sRes;
  25. }
  26. /**
  27. * Use Gallery image for both because currently there is no Unit types with small thumbnails.
  28. */
  29. protected function getUnitThumbAndGallery ($aData)
  30. {
  31. list($sPhotoThumb, $sPhotoGallery) = parent::getUnitThumbAndGallery($aData);
  32. return array($sPhotoGallery, $sPhotoGallery);
  33. }
  34. public function getMultiVoting($aValue, $isAllowedVote) {
  35. $aOptions = $this->_oModule->_oDb->getVotingOptions();
  36. if (empty($aOptions)) return '';
  37. $CNF = &$this->_oConfig->CNF;
  38. $sJsObject = $this->_oConfig->getJsObject('multi_voting');
  39. $sJsCode = $this->getJsCode('multi_voting', ['review_id' => 0]);
  40. $iMinValue = 1;
  41. $iMaxValue = $this->_oDb->getParam($CNF['PARAM_MAX_STARS']);
  42. $aTmplVarsStars = $aTmplVarsSlider = $aTmplVarsButtons = [];
  43. for($i = $iMinValue; $i <= $iMaxValue; $i++) {
  44. $aTmplVarsStars[] = ['style_prefix' => 'bx-vote', 'value' => $i];
  45. $aTmplVarsSlider[] = ['style_prefix' => 'bx-vote'];
  46. if($isAllowedVote) $aTmplVarsButtons[] = [
  47. 'style_prefix' => 'bx-vote',
  48. 'js_object' => $sJsObject,
  49. 'value' => $i
  50. ];
  51. }
  52. $sStars = $this->parseHtmlByName('vote_do_vote_stars.html', [
  53. 'style_prefix' => 'bx-vote',
  54. 'bx_repeat:stars' => $aTmplVarsStars,
  55. 'bx_repeat:slider' => $aTmplVarsSlider,
  56. 'bx_repeat:buttons' => $aTmplVarsButtons,
  57. 'bx_if:show_legend' => ['condition' => false, 'content' => []],
  58. ]);
  59. $aTmplOptions = [];
  60. foreach ($aOptions as $aOption) {
  61. $iRate = isset($aValue[$aOption['id']]) ? $aValue[$aOption['id']] : 0;
  62. if (!$isAllowedVote && !$iRate) continue;
  63. $aTmplOptions[] = [
  64. 'voting_option_name' => htmlspecialchars_adv(_t($aOption['lkey'])),
  65. 'style_prefix' => 'bx-vote',
  66. 'rate' => $iRate,
  67. 'stars' => $sStars,
  68. 'bx_if:vote_allowed' => ['condition' => $isAllowedVote, 'content' => [
  69. 'option_id' => $aOption['id'],
  70. 'rate' => $iRate,
  71. ]],
  72. ];
  73. }
  74. if (empty($aTmplOptions)) return false;
  75. return $this->parseHtmlByName('voting_multi_stars.html', [
  76. 'js_code' => $this->_addCommonJs().$sJsCode,
  77. 'bx_repeat:voting_options' => $aTmplOptions,
  78. ]);
  79. }
  80. public function getOverallRating($sEntryId, $fRate, $bShowDetails = true) {
  81. if (!$fRate) return '';
  82. $sJsCode = $this->getJsCode(['type' => 'multi_voting', 'id' => $sEntryId], ['review_id' => $sEntryId]);
  83. $iMinValue = 1;
  84. $iMaxValue = 5;
  85. $aTmplVarsStars = $aTmplVarsSlider = $aTmplVarsButtons = [];
  86. for($i = $iMinValue; $i <= $iMaxValue; $i++) {
  87. $aTmplVarsStars[] = ['style_prefix' => 'bx-vote', 'value' => $i];
  88. $aTmplVarsSlider[] = ['style_prefix' => 'bx-vote'];
  89. }
  90. $sStars = $this->parseHtmlByName('vote_do_vote_stars.html', [
  91. 'style_prefix' => 'bx-vote',
  92. 'bx_repeat:stars' => $aTmplVarsStars,
  93. 'bx_repeat:slider' => $aTmplVarsSlider,
  94. 'bx_repeat:buttons' => $aTmplVarsButtons,
  95. 'bx_if:show_legend' => ['condition' => false, 'content' => []],
  96. ]);
  97. return $this->parseHtmlByName('unit_rating.html', [
  98. 'id' => $sEntryId,
  99. 'rate' => $fRate,
  100. 'stars' => $sStars,
  101. 'bx_if:details' => [
  102. 'condition' => $bShowDetails,
  103. 'content' => [
  104. 'details_action_url' => BX_DOL_URL_ROOT.$this->_oConfig->getBaseUri().'get_review_rating_details/'.$sEntryId,
  105. ]
  106. ],
  107. 'js_code' => $this->_addCommonJs().$sJsCode,
  108. ]);
  109. }
  110. protected function getUnit($aData, $aParams = array())
  111. {
  112. $CNF = &$this->_oConfig->CNF;
  113. $aResult = parent::getUnit($aData, $aParams);
  114. $aContext = [];
  115. if ($aData[$CNF['FIELD_ALLOW_VIEW_TO']] < 0) {
  116. $oContextProfile = BxDolProfile::getInstance(-$aData[$CNF['FIELD_ALLOW_VIEW_TO']]);
  117. if ($oContextProfile) {
  118. $oModule = BxDolModule::getInstance($oContextProfile->getModule());
  119. $aContext = [
  120. 'content_icon' => !empty($oModule->_oConfig->CNF) && isset($oModule->_oConfig->CNF['ICON']) ? $oModule->_oConfig->CNF['ICON'] : 'landmark',
  121. 'content_type' => !empty($oModule->_oConfig->CNF) && isset($oModule->_oConfig->CNF['T']['txt_sample_single']) ? _t($oModule->_oConfig->CNF['T']['txt_sample_single']) : _t('_bx_reviews_txt_related'),
  122. 'content_link' => $oContextProfile->getUrl(),
  123. 'content_name' => $oContextProfile->getDisplayName(),
  124. ];
  125. }
  126. }
  127. $aProduct = [];
  128. if (!empty($aData[$CNF['FIELD_PRODUCT']])) {
  129. $sProduct = $aData[$CNF['FIELD_PRODUCT']];
  130. $aProduct = [
  131. 'product_name' => bx_process_output($sProduct),
  132. 'product_link' => bx_append_url_params(BX_DOL_URL_ROOT . $this->_oModule->_oConfig->CNF['URI_SEARCH_PRODUCT'], ['keyword' => $sProduct]),
  133. ];
  134. }
  135. $aResultExtra = [
  136. 'bx_if:context' => [
  137. 'condition' => !bx_is_empty_array($aContext),
  138. 'content' => $aContext,
  139. ],
  140. 'bx_if:product' => [
  141. 'condition' => !bx_is_empty_array($aProduct),
  142. 'content' => $aProduct,
  143. ],
  144. 'bx_if:rating' => [
  145. 'condition' => !empty($aData[$CNF['FIELD_VOTING_AVG']]),
  146. 'content' => [
  147. 'stars' => $this->getOverallRating($aData[$CNF['FIELD_ID']], $aData[$CNF['FIELD_VOTING_AVG']]),
  148. ],
  149. ],
  150. ];
  151. $sText = $this->getText($aData);
  152. $sSummaryQuickPlain = isset($CNF['PARAM_CHARS_SUMMARY_PLAIN_SHORT']) && $CNF['PARAM_CHARS_SUMMARY_PLAIN_SHORT'] ? BxTemplFunctions::getInstance()->getStringWithLimitedLength(strip_tags($sText), (int)getParam($CNF['PARAM_CHARS_SUMMARY_PLAIN_SHORT'])) : '';
  153. $aResult['bx_if:thumb_plus_quick_desc'] = [
  154. 'condition' => $aResult['bx_if:thumb']['condition'],
  155. 'content' => [
  156. 'summary_quick_plain' => $sSummaryQuickPlain,
  157. ],
  158. ];
  159. return array_merge($aResult, $aResultExtra);
  160. }
  161. public function browseReviewedContent() {
  162. $CNF = &$this->_oConfig->CNF;
  163. $sPageUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i='.bx_get('i')));
  164. $sOrderBy = bx_get('order_by');
  165. if ($sOrderBy != 'avg_rating' && $sOrderBy != 'reviews_num') $sOrderBy = 'avg_rating';
  166. bx_import('BxTemplPaginate');
  167. $oPaginate = new BxTemplPaginate(array(
  168. 'page_url' => $sPageUrl,
  169. 'on_change_page' => "return !loadDynamicBlockAutoPaginate(this, {start}, {per_page}, 'order_by={$sOrderBy}');",
  170. 'start' => (int)bx_get('start'),
  171. 'per_page' => getParam($CNF['PARAM_PER_PAGE_BROWSE']),
  172. ));
  173. $aProfiles = $this->_oDb->getReviewedContent($oPaginate->getStart(), $oPaginate->getPerPage() + 1, $sOrderBy);
  174. if (empty($aProfiles)) return;
  175. $oPaginate->setNumFromDataArray($aProfiles);
  176. $sReviewsLinkTmpl = BxDolPermalinks::getInstance()->permalink('page.php?i='.$CNF['URI_ENTRIES_BY_CONTEXT']);
  177. $aDataTmpl = [];
  178. foreach ($aProfiles as $aDataRow) {
  179. $oProfile = BxDolProfile::getInstance($aDataRow[$CNF['FIELD_REVIEWED_PROFILE']]);
  180. if (!$oProfile) continue;
  181. $aDataTmpl[] = [
  182. 'profile_id' => $aDataRow[$CNF['FIELD_REVIEWED_PROFILE']],
  183. 'profile_link' => $oProfile->getUrl(),
  184. 'profile_name' => bx_process_output($oProfile->getDisplayName()),
  185. 'profile_pic' => $oProfile->getPicture(),
  186. 'reviews' => $aDataRow['reviews_num'],
  187. 'bx_if:rating' => [
  188. 'condition' => $aDataRow['avg_rating'] > 0,
  189. 'content' => [
  190. 'stars' => $this->getOverallRating(0, $aDataRow['avg_rating'], false),
  191. ],
  192. ],
  193. 'reviews_link' => bx_append_url_params($sReviewsLinkTmpl, ['profile_id' => $aDataRow[$CNF['FIELD_REVIEWED_PROFILE']]]),
  194. ];
  195. }
  196. $sContent = $this->parseHtmlByName('reviewed_profiles.html',[
  197. 'bx_repeat:profiles' => $aDataTmpl,
  198. 'pagination' => $oPaginate->getSimplePaginate(),
  199. ]);
  200. $aMenu = [
  201. [
  202. 'name' => 'sort-by-rating',
  203. 'link' => bx_append_url_params($sPageUrl, ['order_by' => 'avg_rating']),
  204. 'onclick' => 'return !loadDynamicBlockAutoPaginate(this, 0, '.getParam($CNF['PARAM_PER_PAGE_BROWSE']).', \'order_by=avg_rating\');',
  205. 'title' => _t('_bx_reviews_txt_order_by_rating'),
  206. ],
  207. [
  208. 'name' => 'sort-by-reviews',
  209. 'link' => bx_append_url_params($sPageUrl, ['order_by' => 'reviews_num']),
  210. 'onclick' => 'return !loadDynamicBlockAutoPaginate(this, 0, '.getParam($CNF['PARAM_PER_PAGE_BROWSE']).', \'order_by=reviews_num\');',
  211. 'title' => _t('_bx_reviews_txt_order_by_reviews'),
  212. ]
  213. ];
  214. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> 'bx-reviews-view-reviewed-profiles', 'menu_items' => $aMenu));
  215. $oMenu->setSelected('', $sOrderBy == 'reviews_num' ? 'sort-by-reviews' : 'sort-by-rating');
  216. return [
  217. 'content' => $sContent,
  218. 'menu' => $oMenu,
  219. ];
  220. }
  221. public function entryRating($aData)
  222. {
  223. $CNF = &$this->getModule()->_oConfig->CNF;
  224. $sVotes = '';
  225. $oVotes = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES_STARS'], $aData[$CNF['FIELD_ID']]);
  226. if($oVotes) {
  227. $sVotes = $oVotes->getElementBlock(array('show_counter' => true, 'show_legend' => true));
  228. if(!empty($sVotes))
  229. $sVotes = $this->parseHtmlByName('entry-rating.html', array(
  230. 'content' => $sVotes,
  231. ));
  232. }
  233. return $sVotes;
  234. }
  235. }
  236. /** @} */