Helpers.php 3.59 KB
<?php

namespace FootyRoom\Support\Utils;

use Illuminate\Http\Request;

class Helpers
{
    /**
     * Returns URL for user's avatar.
     *
     * @param mixed $userId This can be set to "default" to display default avatar.
     *
     * @return string
     */
    public static function avatarUrl($userId)
    {
        return 'https://'.app('FootyRoom\Config')->get('imagesHost').'/avatars/'.$userId;
    }

    public static function dateSelect()
    {
        $html = '';

        $months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

        //Day
        $html .= '<select id="dobDay" name="dobDay" class="form-control day-select">';
        for ($day=1; $day<32; $day++) {
            $html .= '<option>'.$day.'</option>';
        }
        $html .= '</select>';

        //Month
        $html .= '<select id="dobMonth" name="dobMonth" class="form-control month-select">';
        foreach ($months as $key => $month) {
            $html .= '<option value="'.($key+1).'">'.$month.'</option>';
        }
        $html .= '</select>';

        //Year
        $html .= '<select id="dobYear" name="dobYear" class="form-control year-select">';
        for ($year=1900; $year<2012; $year++) {
            $html .= '<option>'.$year.'</option>';
        }
        $html .= '</select>';

        return $html;
    }

    /**
     * Returns html for rel links with.
     *
     * @param bool $hasNext
     * @param string $resource URL of the resource
     * @param int $page
     * @param bool $hasPrev
     *
     * @return string
     */
    public static function relLinks($hasNext, $resource = null, $page = null, $hasPrev = true)
    {
        if (is_null($resource)) {
            $resource = app()->make(\Illuminate\Http\Request::class)->fullUrl();
        }

        if (is_null($page)) {
            parse_str(parse_url($resource, PHP_URL_QUERY), $params);

            $page = isset($params['page']) ? (int) $params['page'] : 1;
        }

        // Remove `page` query string parameter from the resource URL.
        $resource = rtrim(preg_replace('/&?page=\d+/i', '', $resource), '?');

        if (strpos($resource, '?') !== false) {
            $separator = '&';
        } else {
            $separator = '?';
        }

        $html = '';

        if ($hasNext) {
            $html .= '<link rel="next" href="'.$resource.$separator.'page='.($page + 1).'">';
        }

        if ($page > 2 && $hasPrev) {
            $html .= '<link rel="prev" href="'.$resource.$separator.'page='.($page - 1).'">';
        }

        if ($page === 2 && $hasPrev) {
            $html .= '<link rel="prev" href="'.$resource.'">';
        }

        return $html;
    }

    /**
     * Formats page title to site wide standard.
     */
    public static function title($title)
    {
        return $title.' - FootyRoom';
    }

    /**
     * Returns url for team's badge.
     *
     * @param id $teamId
     *
     * @return string
     */
    public static function teamBadgeUrl($teamId)
    {
        return 'https://'.app('FootyRoom\Config')->get('imagesHost').'/badges/'.$teamId;
    }

    public static function nl2p($string)
    {
        $string = str_replace(['<p>', '</p>', '<br>', '<br />'], '', $string);

        return '<p>'.preg_replace(
            ["/([\n]{2,})/i", "/([^>])\n([^<])/i"],
            ["</p>\n<p>", '$1<br />$2'],
            trim($string)
        ).'</p>';
    }

    /**
     * Get the CSRF token value.
     */
    public static function csrfToken(): string
    {
        $request = app(Request::class);

        return $request->session()->token();
    }
}