Встраиваем рейтинг в компоненту. Цель, если пользователю все нравится он ставит оценку от 1 до 5 звездочками
«bitrix:forum.topic.reviews»
У меня вот в этой папке
/local/templates/ваш шаблон/components/bitrix/forum.topic.reviews/main/template.php
в него засовываем такой код: это будет вывод рейтинга, который нельзя изменить. Класс fa-star — это класс шрифта авезоме.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//этот блок можно переносить, лишь бы изменялось id $rating = getRatingByIdMessage($res['ID']); ?> <div class="star_block starrr" id=""><?php for ($i = 1; $i <= 5; $i++) { if ($i <= $rating) { echo ' <a href="#" class="fa-star fa"></a>'; } else { echo '<a href="#" class="fa-star-o fa"></a>'; } } ?> </div> <?php |
Вот вывод. Звездочки не кликабельны!!!
Подрубаем скрипты плагина
1 2 |
<link rel="stylesheet" href="/design/starrr.css"> <script src="/design/starrr.js"></script> |
css
1 2 3 4 5 6 7 8 9 10 11 12 |
.starrr { display: inline-block; } .starrr a { font-size: 16px; padding: 0 1px; cursor: pointer; color: #FFD119; text-decoration: none; } #star_form a{ font-size: 34px; } |
код плагина. Можете скачать его по ссылке
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
//https://github.com/dobtco/starrr var slice = [].slice; (function($, window) { var Starrr; window.Starrr = Starrr = (function() { Starrr.prototype.defaults = { rating: void 0, max: 5, readOnly: false, emptyClass: 'fa fa-star-o', fullClass: 'fa fa-star', change: function(e, value) {} }; function Starrr($el, options) { this.options = $.extend({}, this.defaults, options); this.$el = $el; this.createStars(); this.syncRating(); if (this.options.readOnly) { return; } this.$el.on('mouseover.starrr', 'a', (function(_this) { return function(e) { return _this.syncRating(_this.getStars().index(e.currentTarget) + 1); }; })(this)); this.$el.on('mouseout.starrr', (function(_this) { return function() { return _this.syncRating(); }; })(this)); this.$el.on('click.starrr', 'a', (function(_this) { return function(e) { e.preventDefault(); return _this.setRating(_this.getStars().index(e.currentTarget) + 1); }; })(this)); this.$el.on('starrr:change', this.options.change); } Starrr.prototype.getStars = function() { return this.$el.find('a'); }; Starrr.prototype.createStars = function() { var j, ref, results; results = []; for (j = 1, ref = this.options.max; 1 <= ref ? j <= ref : j >= ref; 1 <= ref ? j++ : j--) { results.push(this.$el.append("<a href='#' />")); } return results; }; Starrr.prototype.setRating = function(rating) { if (this.options.rating === rating) { rating = void 0; } this.options.rating = rating; this.syncRating(); return this.$el.trigger('starrr:change', rating); }; Starrr.prototype.getRating = function() { return this.options.rating; }; Starrr.prototype.syncRating = function(rating) { var $stars, i, j, ref, results; rating || (rating = this.options.rating); $stars = this.getStars(); results = []; for (i = j = 1, ref = this.options.max; 1 <= ref ? j <= ref : j >= ref; i = 1 <= ref ? ++j : --j) { results.push($stars.eq(i - 1).removeClass(rating >= i ? this.options.emptyClass : this.options.fullClass).addClass(rating >= i ? this.options.fullClass : this.options.emptyClass)); } return results; }; return Starrr; })(); return $.fn.extend({ starrr: function() { var args, option; option = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : []; return this.each(function() { var data; data = $(this).data('starrr'); if (!data) { $(this).data('starrr', (data = new Starrr($(this), option))); } if (typeof option === 'string') { return data[option].apply(data, args); } }); } }); })(window.jQuery, window); |
Теперь я воспользовался классом для работы с hiload block
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
<?php if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true) die(); /** * Created by PhpStorm. * User: Denis * Date: 10.01.2019 * Time: 7:49 */ use Bitrix\Main\Loader; Loader::includeModule("highloadblock"); use Bitrix\Highloadblock as HL; use Bitrix\Highloadblock\HighloadBlockTable as HLBT; CModule::IncludeModule('highloadblock'); class HLBData { static function GetEntityDataClass($HlBlockId) { if (empty($HlBlockId) || $HlBlockId < 1) { return false; } $hlblock = HLBT::getById($HlBlockId)->fetch(); $entity = HLBT::compileEntity($hlblock); $entity_data_class = $entity->getDataClass(); return $entity_data_class; } static function addHLBData ($hlbId, $arData) { $entity_data_class = self::GetEntityDataClass($hlbId); $result = $entity_data_class::add($arData); return $result; } static function deleteHLBData ($hlbId, $ID) { $entity_data_class = self::GetEntityDataClass($hlbId); $result = $entity_data_class::delete($ID); return $result; } static function getHLBData ($hlbId, $arData) { $entity_data_class = self::GetEntityDataClass($hlbId); $userTempData = $entity_data_class::getList($arData); $result = ''; while($elTemp = $userTempData->fetch()) { $result = $elTemp; } return $result; } static function getHLBDatas ($hlbId, $arData) { $entity_data_class = self::GetEntityDataClass($hlbId); $userTempData = $entity_data_class::getList($arData); $result = array(); while($elTemp = $userTempData->fetch()) { $result[] = $elTemp; } return $result; } static function updateHLBData ($hlbId, $entityId, $arData) { $entity_data_class = self::GetEntityDataClass($hlbId); $result = $entity_data_class::update($entityId, $arData); } } |
Теперь Php init
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<? CModule::AddAutoloadClasses( '', // не указываем имя модуля array( 'HLBData' => '/local/classes/HLBData.php', // функции работы с Highload блоками ) ); define("RATING_HL_BLOCK_ID", 4); CModule::IncludeModule("forum"); AddEventHandler("forum", "onAfterMessageAdd", "onAfterMessageAddHandler"); //добавляем 1 поледля рейтинга function onAfterMessageAddHandler($id, $arFields){ addRatingMessage($id,$_REQUEST['UF_RATING']); // file_put_contents($_SERVER["DOCUMENT_ROOT"]."/add_message.log", date("d-m-Y")."; ID=".print_r($id,1)."; ".print_r($arFields,1).";\n", FILE_APPEND); } //получаем рейтинг сообщения по id из Hiload function getRatingByIdMessage($MESSAGE_ID){ $RATING=5; $arFilter = array( 'UF_MESSAGE_ID'=>$MESSAGE_ID ); //задаете фильтр по вашим полям //получаем данные от текущего пользователя $entity_data_class = HLBData::GetEntityDataClass(RATING_HL_BLOCK_ID); $rsData = $entity_data_class::getList(array( 'select' => array('*'), "filter" => $arFilter, )); $ret = false; //детальные данные о пользователе выводятся в шблоне if($elIntesity = $rsData->fetch()){ $ret=$elIntesity; $RATING=$ret['UF_RATING']; } return $RATING; } //добавляем рейтинг function addRatingMessage($MESSAGE_ID ,$RATING){ $RATING=(int)$RATING; $arData=array( 'UF_MESSAGE_ID'=>$MESSAGE_ID, 'UF_RATING'=>$RATING, ); $result_add = HLBData::addHLBData (RATING_HL_BLOCK_ID, $arData); } ?> |
Еще есть файлик /local/templates/вашшаблон/components/bitrix/forum.topic.reviews/main/form.php . В нем через js мы выставлем значения
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<input style="display: none;" name="UF_RATING" id="UF_RATING" size="30" type="text" value="5" /> <div class="reviews-reply-header"> <span>Вам понравился наш сайт?</span> </div> <div id="star_form" class='starrr'></div> <script> $('#star_form').starrr({ rating: 0, readOnly:false, change: function(e, value){ $('#UF_RATING').val(value); } }); </script> |
И последний момент. Сайм hiloadblock
UF_MESSAGE_ID — сюда помещаем id сообщения
UF_RATING — сюда рейтиг 1.
И еще. В коде плагина для js есть ссылка. Читаем использование.