<commit_before><?php

namespace Backend\Core\Engine;

use Backend\Core\Engine\Model as BackendModel;
use Backend\Core\Language\Language as BackendLanguage;
use SpoonDate;
use SpoonFilter;

/**
 * A set of commonly used functions that will be applied on rows or columns
 */
class DataGridFunctions
{
    protected static $dataGridUsers = [];

    /**
     * Formats plain text as HTML, links will be detected, paragraphs will be inserted
     *
     * @param string $var The data to cleanup.
     *
     * @return string
     */
    public static function cleanupPlainText(string $var): string
    {
        // detect links
        $var = SpoonFilter::replaceURLsWithAnchors($var);

        // replace newlines
        $var = str_replace("\r", '', $var);
        $var = preg_replace('/(?<!.)(\r\n|\r|\n){3,}$/m', '', $var);

        // replace br's into p's
        $var = '<p>' . str_replace("\n", '</p><p>', $var) . '</p>';

        // cleanup
        $var = str_replace("\n", '', $var);
        $var = str_replace('<p></p>', '', $var);

        return $var;
    }

    /**
     * Format a number as a float
     *
     * @param float $number The number to format.
     * @param int $decimals The number of decimals.
     *
     * @return string
     */
    public static function formatFloat(float $number, int $decimals = 2): string
    {
        return number_format($number, $decimals, '.', ' ');
    }

    /**
     * Format a date according the users' settings
     *
     * @param int $timestamp The UNIX-timestamp to format as a human readable date.
     *
     * @return string
     */
    public static function getDate(int $timestamp): string
    {
        // if invalid timestamp return an empty string
        if ($timestamp <= 0) {
            return '';
        }

        // get user setting for long dates
        $format = Authentication::getUser()->getSetting('date_format');

        // format the date according the user his settings
        return SpoonDate::getDate($format, $timestamp, BackendLanguage::getInterfaceLanguage());
    }

    /**
     * Format a date as a long representation according the users' settings
     *
     * @param int $timestamp The UNIX-timestamp to format as a human readable date.
     *
     * @return string
     */
    public static function getLongDate(int $timestamp): string
    {
        // if invalid timestamp return an empty string
        if ($timestamp <= 0) {
            return '';
        }

        // get user setting for long dates
        $format = Authentication::getUser()->getSetting('datetime_format');

        // format the date according the user his settings
        return SpoonDate::getDate($format, $timestamp, BackendLanguage::getInterfaceLanguage());
    }

    /**
     * Format a time according the users' settings
     *
     * @param int $timestamp The UNIX-timestamp to format as a human readable time.
     *
     * @return string
     */
    public static function getTime(int $timestamp): string
    {
        // if invalid timestamp return an empty string
        if ($timestamp <= 0) {
            return '';
        }

        // get user setting for long dates
        $format = Authentication::getUser()->getSetting('time_format');

        // format the date according the user his settings
        return SpoonDate::getDate($format, $timestamp, BackendLanguage::getInterfaceLanguage());
    }

    /**
     * Get time ago as a string for use in a datagrid
     *
     * @param int $timestamp The UNIX-timestamp to convert in a time-ago-string.
     *
     * @return string
     */
    public static function getTimeAgo(int $timestamp): string
    {
        // get user setting for long dates
        $format = Authentication::getUser()->getSetting('datetime_format');

        // get the time ago as a string
        $timeAgo = SpoonDate::getTimeAgo($timestamp, BackendLanguage::getInterfaceLanguage(), $format);

        return '<time tabindex="0" data-toggle="tooltip" datetime="'
               . SpoonDate::getDate('Y-m-d H:i:s', $timestamp)
               . '" title="' . SpoonDate::getDate($format, $timestamp, BackendLanguage::getInterfaceLanguage())
               . '">' . $timeAgo . '</time>';
    }

    /**
     * Get the HTML for a user to use in a datagrid
     *
     * @param int $id The Id of the user.
     *
     * @return string
     */
    public static function getUser(int $id): string
    {
        // nothing in cache
        if (!isset(self::$dataGridUsers[$id])) {
            // create user instance
            $user = new User($id);

            // get settings
            $avatar = $user->getSetting('avatar', 'no-avatar.gif');
            $nickname = $user->getSetting('nickname');
            $allowed = Authentication::isAllowedAction('Edit', 'Users');

            // build html
            $html = '<div class="fork-data-grid-avatar">' . "\n";
            if ($allowed) {
                $html .= '     <a href="' .
                BackendModel::createUrlForAction(
                    'Edit',
                    'Users'
                ) . '&amp;id=' . $id . '">' . "\n";
            }
            $html .= '          <img class="img-circle" src="' . FRONTEND_FILES_URL . '/Users/avatars/32x32/' .
                $avatar . '" width="24" height="24" alt="' . $nickname . '" />' . "\n";<commit_msg>Fix image rounded in datagrid<commit_after><?php

namespace Backend\Core\Engine;

use Backend\Core\Engine\Model as BackendModel;
use Backend\Core\Language\Language as BackendLanguage;
use SpoonDate;
use SpoonFilter;

/**
 * A set of commonly used functions that will be applied on rows or columns
 */
class DataGridFunctions
{
    protected static $dataGridUsers = [];

    /**
     * Formats plain text as HTML, links will be detected, paragraphs will be inserted
     *
     * @param string $var The data to cleanup.
     *
     * @return string
     */
    public static function cleanupPlainText(string $var): string
    {
        // detect links
        $var = SpoonFilter::replaceURLsWithAnchors($var);

        // replace newlines
        $var = str_replace("\r", '', $var);
        $var = preg_replace('/(?<!.)(\r\n|\r|\n){3,}$/m', '', $var);

        // replace br's into p's
        $var = '<p>' . str_replace("\n", '</p><p>', $var) . '</p>';

        // cleanup
        $var = str_replace("\n", '', $var);
        $var = str_replace('<p></p>', '', $var);

        return $var;
    }

    /**
     * Format a number as a float
     *
     * @param float $number The number to format.
     * @param int $decimals The number of decimals.
     *
     * @return string
     */
    public static function formatFloat(float $number, int $decimals = 2): string
    {
        return number_format($number, $decimals, '.', ' ');
    }

    /**
     * Format a date according the users' settings
     *
     * @param int $timestamp The UNIX-timestamp to format as a human readable date.
     *
     * @return string
     */
    public static function getDate(int $timestamp): string
    {
        // if invalid timestamp return an empty string
        if ($timestamp <= 0) {
            return '';
        }

        // get user setting for long dates
        $format = Authentication::getUser()->getSetting('date_format');

        // format the date according the user his settings
        return SpoonDate::getDate($format, $timestamp, BackendLanguage::getInterfaceLanguage());
    }

    /**
     * Format a date as a long representation according the users' settings
     *
     * @param int $timestamp The UNIX-timestamp to format as a human readable date.
     *
     * @return string
     */
    public static function getLongDate(int $timestamp): string
    {
        // if invalid timestamp return an empty string
        if ($timestamp <= 0) {
            return '';
        }

        // get user setting for long dates
        $format = Authentication::getUser()->getSetting('datetime_format');

        // format the date according the user his settings
        return SpoonDate::getDate($format, $timestamp, BackendLanguage::getInterfaceLanguage());
    }

    /**
     * Format a time according the users' settings
     *
     * @param int $timestamp The UNIX-timestamp to format as a human readable time.
     *
     * @return string
     */
    public static function getTime(int $timestamp): string
    {
        // if invalid timestamp return an empty string
        if ($timestamp <= 0) {
            return '';
        }

        // get user setting for long dates
        $format = Authentication::getUser()->getSetting('time_format');

        // format the date according the user his settings
        return SpoonDate::getDate($format, $timestamp, BackendLanguage::getInterfaceLanguage());
    }

    /**
     * Get time ago as a string for use in a datagrid
     *
     * @param int $timestamp The UNIX-timestamp to convert in a time-ago-string.
     *
     * @return string
     */
    public static function getTimeAgo(int $timestamp): string
    {
        // get user setting for long dates
        $format = Authentication::getUser()->getSetting('datetime_format');

        // get the time ago as a string
        $timeAgo = SpoonDate::getTimeAgo($timestamp, BackendLanguage::getInterfaceLanguage(), $format);

        return '<time tabindex="0" data-toggle="tooltip" datetime="'
               . SpoonDate::getDate('Y-m-d H:i:s', $timestamp)
               . '" title="' . SpoonDate::getDate($format, $timestamp, BackendLanguage::getInterfaceLanguage())
               . '">' . $timeAgo . '</time>';
    }

    /**
     * Get the HTML for a user to use in a datagrid
     *
     * @param int $id The Id of the user.
     *
     * @return string
     */
    public static function getUser(int $id): string
    {
        // nothing in cache
        if (!isset(self::$dataGridUsers[$id])) {
            // create user instance
            $user = new User($id);

            // get settings
            $avatar = $user->getSetting('avatar', 'no-avatar.gif');
            $nickname = $user->getSetting('nickname');
            $allowed = Authentication::isAllowedAction('Edit', 'Users');

            // build html
            $html = '<div class="fork-data-grid-avatar">' . "\n";
            if ($allowed) {
                $html .= '     <a href="' .
                BackendModel::createUrlForAction(
                    'Edit',
                    'Users'
                ) . '&amp;id=' . $id . '">' . "\n";
            }
            $html .= '          <img class="rounded-circle" src="' . FRONTEND_FILES_URL . '/Users/avatars/32x32/' .
                $avatar . '" width="24" height="24" alt="' . $nickname . '" />' . "\n";
{'fraction_non_alphanumeric': 0.1324106975874322, 'fraction_numerical': 0.002618290630259959, 'mean_word_length': 2.065348237317283, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4033678', 'n_tokens_mistral': 3004, 'n_tokens_neox': 2906, 'n_words': 891}