/**
     * Delete reputation page/action
     *
     * @param int $rid	Reputation ID taken from the URL
     * @return null
     * @access public
     */
    public function delete($rid)
    {
        $this->user->add_lang_ext('pico/reputation', 'reputation_system');
        $is_ajax = $this->request->is_ajax();
        $submit = false;
        $post_type_id = (int) $this->reputation_manager->get_reputation_type_id('post');
        $sql_array = array('SELECT' => 'r.*, rt.reputation_type_name, p.post_id, uf.username AS username_from, ut.username AS username_to', 'FROM' => array($this->reputations_table => 'r', $this->reputation_types_table => 'rt'), 'LEFT_JOIN' => array(array('FROM' => array(POSTS_TABLE => 'p'), 'ON' => 'p.post_id = r.reputation_item_id
						AND r.reputation_type_id = ' . $post_type_id), array('FROM' => array(USERS_TABLE => 'uf'), 'ON' => 'r.user_id_from = uf.user_id '), array('FROM' => array(USERS_TABLE => 'ut'), 'ON' => 'r.user_id_to = ut.user_id ')), 'WHERE' => 'r.reputation_id = ' . $rid);
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
        $result = $this->db->sql_query($sql);
        $row = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        //We couldn't find this reputation. May be it was deleted meanwhile?
        if (empty($row)) {
            $message = $this->user->lang('RS_NO_REPUTATION');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}index.{$this->php_ext}");
            $redirect_text = 'RETURN_INDEX';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        if ($this->request->is_set_post('cancel')) {
            redirect($this->helper->route('reputation_details_controller', array('uid' => $row['user_id_to'])));
        }
        if ($this->auth->acl_gets('m_rs_moderate') || $row['user_id_from'] == $this->user->data['user_id'] && $this->auth->acl_get('u_rs_delete')) {
            if ($is_ajax) {
                $submit = true;
            } else {
                $s_hidden_fields = build_hidden_fields(array('r' => $rid));
                if (confirm_box(true)) {
                    $submit = true;
                } else {
                    confirm_box(false, $this->user->lang('RS_REPUTATION_DELETE_CONFIRM'), $s_hidden_fields);
                }
            }
        } else {
            $message = $this->user->lang('RS_USER_CANNOT_DELETE');
            $json_data = array('error_msg' => $message);
            $redirect = $this->helper->route('reputation_details_controller', array('uid' => $row['user_id_to']));
            $redirect_text = 'RETURN_PAGE';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        if ($submit) {
            try {
                $this->reputation_manager->delete_reputation($row);
            } catch (\pico\reputation\exception\base $e) {
                // Catch exception
                trigger_error($e->get_message($this->user));
            }
            $user_reputation = $this->reputation_manager->get_user_reputation($row['user_id_to']);
            $message = $this->user->lang('RS_POINTS_DELETED');
            $json_data = array('rid' => $rid, 'user_reputation' => $user_reputation);
            if (isset($row['post_id'])) {
                $post_reputation = $this->reputation_manager->get_post_reputation($row['post_id']);
                $json_data = array_merge($json_data, array('poster_id' => $row['user_id_to'], 'post_id' => $row['post_id'], 'post_reputation' => $post_reputation, 'reputation_class' => $this->reputation_helper->reputation_class($post_reputation), 'own_vote' => $row['user_id_from'] == $this->user->data['user_id'] ? true : false));
            }
            $redirect = $this->helper->route('reputation_details_controller', array('uid' => $row['user_id_to']));
            $redirect_text = 'RETURN_PAGE';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
    }
    /**
     * Display the post rating page
     *
     * @param string $mode		Mode taken from the URL 
     * @param int $post_id		Post ID taken from the URL
     * @return Symfony\Component\HttpFoundation\Response A Symfony Response object
     * @access public
     */
    public function post($mode, $post_id)
    {
        $this->user->add_lang_ext('pico/reputation', 'reputation_rating');
        // Define basic variables
        $error = '';
        $is_ajax = $this->request->is_ajax();
        $referer = $this->symfony_request->get('_referer');
        if (empty($this->config['rs_enable'])) {
            if ($is_ajax) {
                $json_response = new \phpbb\json_response();
                $json_data = array('error_msg' => $this->user->lang('RS_DISABLED'));
                $json_response->send($json_data);
            }
            redirect(append_sid("{$this->root_path}index.{$this->php_ext}"));
        }
        $reputation_type_id = (int) $this->reputation_manager->get_reputation_type_id('post');
        $sql_array = array('SELECT' => 'p.forum_id, p.poster_id, p.post_subject, u.user_type, u.user_reputation, f.reputation_enabled, r.reputation_id, r.reputation_points', 'FROM' => array(POSTS_TABLE => 'p', USERS_TABLE => 'u', FORUMS_TABLE => 'f'), 'LEFT_JOIN' => array(array('FROM' => array($this->reputations_table => 'r'), 'ON' => 'p.post_id = r.reputation_item_id
						AND r.reputation_type_id = ' . $reputation_type_id . '
						AND r.user_id_from = ' . $this->user->data['user_id'])), 'WHERE' => 'p.post_id = ' . $post_id . '
				AND p.poster_id = u.user_id
				AND p.forum_id = f.forum_id');
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
        $result = $this->db->sql_query($sql);
        $row = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        // We couldn't find this post. May be it was deleted while user voted?
        if (!$row) {
            $message = $this->user->lang('RS_NO_POST');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}index.{$this->php_ext}");
            $redirect_text = 'RETURN_INDEX';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        // Cancel action
        if ($this->request->is_set_post('cancel')) {
            redirect(append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&p=' . $post_id) . '#p' . $post_id);
        }
        // Fire error if post rating is disabled
        if (!$this->config['rs_post_rating'] || !$this->config['rs_negative_point'] && $mode == 'negative' || !$row['reputation_enabled']) {
            $message = $this->user->lang('RS_DISABLED');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        // No anonymous voting is allowed
        if ($row['user_type'] == USER_IGNORE) {
            $message = $this->user->lang('RS_USER_ANONYMOUS');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        // You cannot rate your own post
        if ($row['poster_id'] == $this->user->data['user_id']) {
            $message = $this->user->lang('RS_SELF');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        // Don't allow to rate same post
        if ($row['reputation_id']) {
            $message = $this->user->lang('RS_SAME_POST', $row['reputation_points']);
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        // Check if user is allowed to vote
        if (!$this->auth->acl_get('f_rs_rate', $row['forum_id']) || !$this->auth->acl_get('f_rs_rate_negative', $row['forum_id']) && $mode == 'negative' || !$this->auth->acl_get('u_rs_rate_post')) {
            $message = $this->user->lang('RS_USER_DISABLED');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        //Check if user reputation is enough to give negative points
        if ($this->config['rs_min_rep_negative'] && $this->user->data['user_reputation'] < $this->config['rs_min_rep_negative'] && $mode == 'negative') {
            $message = $this->user->lang('RS_USER_NEGATIVE', $this->config['rs_min_rep_negative']);
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        // Anti-abuse behaviour
        if (!empty($this->config['rs_anti_time']) && !empty($this->config['rs_anti_post'])) {
            $anti_time = time() - $this->config['rs_anti_time'] * 3600;
            $sql_and = !$this->config['rs_anti_method'] ? 'AND user_id_to = ' . $row['poster_id'] : '';
            $sql = 'SELECT COUNT(reputation_id) AS reputation_per_day
				FROM ' . $this->reputations_table . '
				WHERE user_id_from = ' . $this->user->data['user_id'] . '
					' . $sql_and . '
					AND reputation_type_id = ' . $reputation_type_id . '
					AND reputation_time > ' . $anti_time;
            $result = $this->db->sql_query($sql);
            $anti_row = $this->db->sql_fetchrow($result);
            $this->db->sql_freeresult($result);
            if ($anti_row['reputation_per_day'] >= $this->config['rs_anti_post']) {
                $message = $this->user->lang('RS_ANTISPAM_INFO');
                $json_data = array('error_msg' => $message);
                $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
                $redirect_text = 'RETURN_TOPIC';
                $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
            }
        }
        // Disallow rating banned users
        if ($this->user->check_ban($row['poster_id'], false, false, true)) {
            $message = $this->user->lang('RS_USER_BANNED');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        // Prevent overrating one user by another
        if ($this->reputation_manager->prevent_rating($row['poster_id'])) {
            $message = $this->user->lang('RS_ANTISPAM_INFO');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        // Request variables
        $points = $this->request->variable('points', '');
        $comment = $this->request->variable('comment', '', true);
        // Submit vote
        $submit = false;
        if ($this->request->is_set_post('submit_vote')) {
            $submit = true;
        }
        // The comment
        if ($submit && $this->config['rs_enable_comment']) {
            // The comment is too long
            if (strlen($comment) > $this->config['rs_comment_max_chars']) {
                $submit = false;
                $error = $this->user->lang('RS_COMMENT_TOO_LONG', strlen($comment), $this->config['rs_comment_max_chars']);
                if ($is_ajax) {
                    $json_response = new \phpbb\json_response();
                    $json_data = array('comment_error' => $error);
                    $json_response->send($json_data);
                }
            }
            // Force the comment
            if (($this->config['rs_force_comment'] == self::RS_COMMENT_BOTH || $this->config['rs_force_comment'] == self::RS_COMMENT_POST) && empty($comment)) {
                $submit = false;
                $error = $this->user->lang('RS_NO_COMMENT');
                if ($is_ajax) {
                    $json_response = new \phpbb\json_response();
                    $json_data = array('comment_error' => $error);
                    $json_response->send($json_data);
                }
            }
        }
        // Sumbit vote when the comment and the reputation power are disabled
        if (!$this->config['rs_enable_comment'] && !$this->config['rs_enable_power']) {
            $submit = true;
            $points = $mode == 'negative' ? '-1' : '1';
        }
        // Get reputation power
        if ($this->config['rs_enable_power']) {
            $voting_power_pulldown = '';
            // Get details on user voting - how much power was used
            $used_power = $this->reputation_power->used($this->user->data['user_id']);
            // Calculate how much maximum power the user has
            $max_voting_power = $this->reputation_power->get($this->user->data['user_posts'], $this->user->data['user_regdate'], $this->user->data['user_reputation'], $this->user->data['user_warnings'], $this->user->data['group_id']);
            if ($max_voting_power < 1) {
                $message = $this->user->lang('RS_NO_POWER');
                $json_data = array('error_msg' => $message);
                $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
                $redirect_text = 'RETURN_TOPIC';
                $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
            }
            $voting_power_left = $max_voting_power - $used_power;
            // Don't allow to vote more than set in ACP per 1 vote
            $max_voting_allowed = $this->config['rs_power_renewal'] ? min($max_voting_power, $voting_power_left) : $max_voting_power;
            // If now voting power left - fire error and exit
            if ($voting_power_left <= 0 && $this->config['rs_power_renewal']) {
                $message = $this->user->lang('RS_NO_POWER_LEFT', $max_voting_power);
                $json_data = array('error_msg' => $message);
                $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
                $redirect_text = 'RETURN_TOPIC';
                $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
            }
            $this->template->assign_vars(array('RS_POWER_POINTS_LEFT' => $this->config['rs_power_renewal'] ? $this->user->lang('RS_VOTE_POWER_LEFT_OF_MAX', $voting_power_left, $max_voting_power, $max_voting_allowed) : '', 'RS_POWER_PROGRESS_EMPTY' => $this->config['rs_power_renewal'] && $max_voting_power ? round(($max_voting_power - $voting_power_left) / $max_voting_power * 100, 0) : ''));
            //Preparing HTML for voting by manual spending of user power
            for ($i = 1; $i <= $max_voting_allowed; ++$i) {
                if ($mode == 'negative') {
                    $voting_power_pulldown = '<option value="-' . $i . '">' . $this->user->lang('RS_NEGATIVE') . ' (-' . $i . ') </option>';
                } else {
                    $voting_power_pulldown = '<option value="' . $i . '">' . $this->user->lang('RS_POSITIVE') . ' (+' . $i . ')</option>';
                }
                $this->template->assign_block_vars('reputation', array('REPUTATION_POWER' => $voting_power_pulldown));
            }
        } else {
            $points = $mode == 'negative' ? '-1' : '1';
        }
        // Save vote
        if ($submit) {
            // Prevent cheater to break the forum permissions to give negative points or give more points than they can
            if (!$this->auth->acl_get('f_rs_rate_negative', $row['forum_id']) && $points < 0 || $points < 0 && $this->config['rs_min_rep_negative'] && $this->user->data['user_reputation'] < $this->config['rs_min_rep_negative'] || $this->config['rs_enable_power'] && ($points > $max_voting_allowed || $points < -$max_voting_allowed)) {
                $submit = false;
                $error = $this->user->lang('RS_USER_CANNOT_RATE');
                if ($is_ajax) {
                    $json_response = new \phpbb\json_response();
                    $json_data = array('comment_error' => $error);
                    $json_response->send($json_data);
                }
            }
        }
        if (!empty($error)) {
            $submit = false;
        }
        if ($submit) {
            $data = array('user_id_from' => $this->user->data['user_id'], 'user_id_to' => $row['poster_id'], 'reputation_type' => 'post', 'reputation_item_id' => $post_id, 'reputation_points' => $points, 'reputation_comment' => $comment);
            try {
                $this->reputation_manager->store_reputation($data);
            } catch (\pico\reputation\exception\base $e) {
                // Catch exception
                $error = $e->get_message($this->user);
            }
            // Notification data
            $notification_data = array('user_id_to' => $row['poster_id'], 'user_id_from' => $this->user->data['user_id'], 'post_id' => $post_id, 'post_subject' => $row['post_subject']);
            $notification_type = $points > 0 ? 'pico.reputation.notification.type.rate_post_positive' : 'pico.reputation.notification.type.rate_post_negative';
            $this->reputation_manager->add_notification($notification_type, $notification_data);
            // Get post reputation
            $post_reputation = $this->reputation_manager->get_post_reputation($post_id);
            $message = $this->user->lang('RS_VOTE_SAVED');
            $json_data = array('post_id' => $post_id, 'poster_id' => $row['poster_id'], 'post_reputation' => $post_reputation, 'user_reputation' => $this->reputation_manager->get_user_reputation($row['poster_id']), 'reputation_class' => $this->reputation_helper->reputation_class($post_reputation), 'reputation_vote' => $points > 0 ? 'rated_good' : 'rated_bad', 'success_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        $this->template->assign_vars(array('ERROR_MSG' => $error, 'S_CONFIRM_ACTION' => $this->helper->route('reputation_post_rating_controller', array('mode' => $mode, 'post_id' => $post_id)), 'S_ERROR' => !empty($error) ? true : false, 'S_RS_COMMENT_ENABLE' => $this->config['rs_enable_comment'] ? true : false, 'S_RS_POWER_ENABLE' => $this->config['rs_enable_power'] ? true : false, 'S_IS_AJAX' => $is_ajax, 'U_RS_REFERER' => $referer));
        return $this->helper->render('ratepost.html', $this->user->lang('RS_POST_RATING'));
    }