403Webshell
Server IP : 80.87.202.40  /  Your IP : 216.73.216.169
Web Server : Apache
System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64
User : bitrix ( 600)
PHP Version : 8.2.27
Disable Function : NONE
MySQL : OFF |  cURL : ON |  WGET : ON |  Perl : ON |  Python : OFF |  Sudo : ON |  Pkexec : ON
Directory :  /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/burlakastudio.realcommenter/lib/

Upload File :
current_dir [ Writeable] document_root [ Writeable]

 

Command :


[ Back ]     

Current File : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/burlakastudio.realcommenter/lib/Voting.php
<?php
/**
 * ����� "��������� ����������� D7" ��� �������
 * �������� ���� �����: www.realcommenter.com
 * �������� ���� �����������: burlaka.studio
 * ����� � ����������: ������� ������� (AlexeyGfi) -> alexeygfi@gmail.com
 */

namespace Burlakastudio\Realcommenter;

use Bitrix\Main\Localization\Loc;
use Bitrix\Main\ORM\Fields\ExpressionField;
use Exception;

require_once 'VOTING_TABLE.php';

class Voting extends REALCOMMENTER_HL_GENERAL_PROVIDER
{

    /**
     * ��� ����� ��������, �� ������������ �� ���������� (������� � ����)
     *
     * ������� ����� ������������.
     * �� ��� �� �������������� ��������� true
     * � �������� �� �������� ���������� �� ������� � ����������
     * ������ ��������� ���-�� �������
     *
     * @param array $arParams
     * @param bool $userId
     *
     * @return bool
     */
    public static function mayVote(&$arParams = [], $userId = false)
    {

        if (!$userId) {
            $userId = USER::get_id();
        }

        if ($userId && USER::in_restricted_status($userId)) {
            return false;
        }

        if (!$userId && !$arParams['USE_VOTES_FOR_ALL']) {
            return false;
        }

        return true;

    }

    public static function restrictedByAuthor(&$commentInfo = 0, &$userId = false)
    {
        if (empty($commentInfo) || !$commentInfo['UF_USER_ID']) {
            return false;
        }

        if (!$userId) {
            $userId = USER::get_id();

            if (!$userId) {
                return false;
            }
        }

        return ($commentInfo['UF_USER_ID'] == $userId);

    }

    public static function votedYet(&$commentId = 0, &$userId = false)
    {

        if (!$commentId) {
            return false;
        }

        if (!$userId) {
            $userId = USER::get_id();
        }

        if (!$userId) {
            /**
             * TODO: ��������������� - ������ �� ����, �� ��������� �� �� �� �����������
             */
            return false;
        }

        /**
         * �� ���� � ���������� - ������ ������������, �� ������ �� ��� ���������.
         * ��� ���� ���������� �����.
         *
         * �������� ����� �����, ����� ��������� ���������� � ���-�� ��������
         */
        $userVotes = self::getUserVotes($userId);

        return !empty($userVotes) && in_array($commentId, $userVotes);

    }

    public static function getUserVotes(&$userId = null)
    {

        $userVotes = [];

        if (!$userId) {
            return $userVotes;
        }

        /**
         * �� ���� � ���������� - ������ ������������, �� ������ �� ��� ���������.
         * ��� ���� ���������� �����.
         *
         * �������� ����� �����, ����� ��������� ���������� � ���-�� ��������
         */
        $cacheObj = CACHE::getCacheObj();
        $cacheKey = CACHE::get_user_key_for_voting($userId);
        $cacheTtl = CACHE::getTtl();

        if ($cacheObj->initCache($cacheTtl, $cacheKey, TOOLS::getModuleName())) {
            $userVotes = $cacheObj->getVars();
        } else {

            $hlClass = self::getDataClass();
            $userVotes = $hlClass::getList([
                'filter' => ['UF_USER_ID' => $userId],
                'limit' => 1,
                'select' => [
                    new ExpressionField('COMMENTS_LIST', 'GROUP_CONCAT(UF_COMMENT_ID)')
                ],
            ])->fetchAll();

            if (!empty($userVotes)) {
                $userVotes = array_shift($userVotes);
                $userVotes = explode(',', $userVotes['COMMENTS_LIST']);
            }

            if ($cacheObj->startDataCache($cacheTtl, $cacheKey)) {
                // ���� � ��
                $cacheObj->endDataCache($userVotes);
            }

        }

        return $userVotes;
    }

    /**
     * @param int $commentId
     * @param int $voteWeight
     * @param array $arParams
     *
     * @return bool
     * @throws Exception
     */
    public static function voting(&$commentId = 0, &$voteWeight = 0, &$arParams = [])
    {

        if (!$commentId || !$voteWeight || !self::mayVote($arParams)) {
            return false;
        }

        if (!$commentId) {
            return false;
        }

        /**
         * ���� �����.
         * ����� ������������ ��� � ������ �������,
         * ...������ ���� ������ � ������� � �����������
         */

        EVENT::call('OnBeforeVoteAdd', [
            'comment_id' => &$commentId,
            'vote_weight' => &$voteWeight,
            'arParams' => &$arParams
        ]);

        /**
         * ���� ����� ������� ��������� ������, ����� ������ ������ ������� ������� ��� ������
         */
        if (!$voteWeight) {
            return false;
        }

        $userId = USER::get_id();

        $voteResult = self::add([
            'UF_COMMENT_ID' => $commentId,
            'UF_USER_ID' => $userId,
            'UF_VOTE' => $voteWeight > 0 ? 1 : -1
        ]);

        if (!$voteResult->isSuccess()) {
            throw new Exception(implode(', ', $voteResult->getErrorMessages()));
        }

        // ����� � ���������� � ������ ����������
        $votes = self::voteStatRecalc($commentId);

        EVENT::call('OnAfterVoteAdd', [
            'comment_id' => &$commentId,
            'votes' => &$votes,
            'user_id' => $userId,
            'arParams' => &$arParams
        ]);

        return true;

    }

    /**
     * @param int $commentId
     *
     * @return array|bool
     */
    private static function voteStatRecalc($commentId = 0)
    {

        if (!$commentId) {
            return false;
        }

        $commentInfo = Comments::getCommentInfo($commentId);

        $votesArr = self::getList_EX(
            [
                'UF_COMMENT_ID' => $commentId
            ],
            [
                'UF_VOTE' => 'asc'
            ]
        );

        $votes = [
            'PLUS' => 0,
            'MINUS' => 0
        ];

        foreach ($votesArr as $v) {
            $votes[($v['UF_VOTE'] > 0 ? 'PLUS' : 'MINUS')]++;
        }

        $updateFields = [];

        if ($votes['PLUS'] != $commentInfo['UF_VOTES_PLUS']) {
            $updateFields['UF_VOTES_PLUS'] = $votes['PLUS'];
        }

        if ($votes['MINUS'] != $commentInfo['UF_VOTES_MINUS']) {
            $updateFields['UF_VOTES_MINUS'] = $votes['MINUS'];
        }

        if (!empty($updateFields)) {
            Comments::updateSingle($commentId, $updateFields);
        }

        return $votes;

    }

    public static function getVoteUpTitle()
    {
        return Loc::getMessage('VOTE_UP_TITLE');
    }

    public static function getVoteDownTitle()
    {
        return Loc::getMessage('VOTE_DOWN_TITLE');
    }
}

Youez - 2016 - github.com/yon3zu
LinuXploit