В версии 6.1.1 появилась возможность ставить лайк/дизлайк за товар, информационный элемент и комментарии. Пример добавления для товара.
1. В XSL-шаблоне добавляется:
<xsl:if test="rate/node()">
<span id="shop_item_id_{@id}" class="thumbs">
<xsl:choose>
<xsl:when test="/shop/siteuser_id > 0">
<xsl:choose>
<xsl:when test="vote/value = 1">
<xsl:attribute name="class">thumbs up</xsl:attribute>
</xsl:when>
<xsl:when test="vote/value = -1">
<xsl:attribute name="class">thumbs down</xsl:attribute>
</xsl:when>
</xsl:choose>
<span id="shop_item_likes_{@id}"><xsl:value-of select="rate/@likes"></xsl:value-of></span>
<span class="inner_thumbs">
<a onclick="return $.sendVote({@id}, 1, 'shop_item')" href="{/shop/url}?id={@id}&vote=1&entity_type=shop_item" alt="Нравится"></a>
<span class="rate" id="shop_item_rate_{@id}"><xsl:value-of select="rate"></xsl:value-of></span>
<a onclick="return $.sendVote({@id}, 0, 'shop_item')" href="{/shop/url}?id={@id}&vote=0&entity_type=shop_item" alt="Не нравится"></a>
</span>
<span id="shop_item_dislikes_{@id}"><xsl:value-of select="rate/@dislikes"></xsl:value-of></span>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="class">thumbs inactive</xsl:attribute>
<span id="shop_item_likes_{@id}"><xsl:value-of select="rate/@likes"></xsl:value-of></span>
<span class="inner_thumbs">
<a alt="Нравится"></a>
<span class="rate" id="shop_item_rate_{@id}"><xsl:value-of select="rate"></xsl:value-of></span>
<a alt="Не нравится"></a>
</span>
<span id="shop_item_dislikes_{@id}"><xsl:value-of select="rate/@dislikes"></xsl:value-of></span>
</xsl:otherwise>
</xsl:choose>
</span>
</xsl:if>
2. В код настроек типовой динамической страницы магазина добавляется блок:
if (!is_null(Core_Array::getGet('vote'))) { $oSiteuser = Core_Entity::factory('Siteuser')->getCurrent(); $entity_id = intval(Core_Array::getGet('id')); if ($entity_id && !is_null($oSiteuser)) { $entity_type = strval(Core_Array::getGet('entity_type')); $vote = intval(Core_Array::getGet('vote')); $oObject = Vote_Controller::instance()->getVotedObject($entity_type, $entity_id); if (!is_null($oObject)) { $oVote = $oObject->Votes->getBySiteuser_Id($oSiteuser->id); $vote_value = $vote ? 1 : -1; $deleteVote = 0; // Пользователь не голосовал ранее if (is_null($oVote)) { $oVote = Core_Entity::factory('Vote'); $oVote->siteuser_id = $oSiteuser->id; $oVote->value = $vote_value; $oObject->add($oVote); } // Пользователь голосовал ранее, но поставил противоположную оценку elseif ($oVote->value != $vote_value) { $oVote->value = $vote_value; $oVote->save(); } // Пользователь голосовал ранее и поставил такую же оценку как и ранее, обнуляем его голосование, как будто он вообще не голосовал else { $deleteVote = 1; $oVote->delete(); } $aVotingStatistic = Vote_Controller::instance()->getRate($entity_type, $entity_id); Core_Page::instance()->response ->body( json_encode(array('value' => $oVote->value, 'item' => $oObject->id, 'entity_type' => $entity_type, 'likes' => $aVotingStatistic['likes'], 'dislikes' => $aVotingStatistic['dislikes'], 'rate' => $aVotingStatistic['rate'], 'delete_vote' => $deleteVote) ) ); } } Core_Page::instance()->response ->status(200) ->header('Pragma', "no-cache") ->header('Cache-Control', "private, no-cache") ->header('Vary', "Accept") ->header('Last-Modified', gmdate('D, d M Y H:i:s', time()) . ' GMT') ->header('X-Powered-By', 'HostCMS') ->header('Content-Disposition', 'inline; filename="files.json"'); if (strpos(Core_Array::get($_SERVER, 'HTTP_ACCEPT', ''), 'application/json') !== FALSE) { Core_Page::instance()->response->header('Content-type', 'application/json; charset=utf-8'); } else { Core_Page::instance()->response ->header('X-Content-Type-Options', 'nosniff') ->header('Content-type', 'text/plain; charset=utf-8'); } if(Core_Array::getRequest('_')) { Core_Page::instance()->response ->sendHeaders() ->showBody(); exit(); } }
3. Стили для макета:
.thumbs, .lock { display: inline-block; /* margin-right: 5px;*/ } span.thumbs{ /*padding-right: 0px;*/ font-size: 8pt; } .thumbs a { display: inline-block; height: 10px; margin-bottom: -1px; width: 11px; } .thumbs .inner_thumbs a:first-child { background-image: url("/images/thumbs_gray_up.png"); } .thumbs .inner_thumbs a:first-child + .rate + a { background-image: url("/images/thumbs_gray_down.png"); } .thumbs.up .inner_thumbs a:first-child { background-image: url("/images/thumbs_red_up.png"); /*cursor: default;*/ } .thumbs.down .inner_thumbs a:first-child + .rate + a { background-image: url("/images/thumbs_red_down.png"); /*cursor: default;*/ } .thumbs.inactive a{ cursor: default; } .thumbs span{ padding-right: 0px; } span.rate{ display: inline-block; font-weight: bold; margin: 0 10px; padding: 0; } span.inner_thumbs{ padding: 0 5px 0 3px; }