TFC-Plex

Internet Explorer

Internet Explorer is not supported. Please upgrade to a more modern browser.

ff

<?php
/*
 *  Made by Samerton
 *  https://github.com/NamelessMC/Nameless/
 *  NamelessMC version 2.0.0-pr13
 *
 *  License: MIT
 *
 *  Resources index page
 */

// Always define page name
define('PAGE', 'resources');

require(ROOT_PATH . '/modules/Resources/classes/Resources.php');
$resources = new Resources();

// Initialise
$timeago = new TimeAgo(TIMEZONE);

$sort_types = [];
$sort_types['updated'] = ['type' => 'updated', 'sort' => $resource_language->get('resources', 'last_updated'), 'link' => URL::build('/resources', 'sort=updated')];
$sort_types['newest'] = ['type' => 'created', 'sort' => $resource_language->get('resources', 'newest'), 'link' => URL::build('/resources', 'sort=newest')];
$sort_types['downloads'] = ['type' => 'downloads', 'sort' => $resource_language->get('resources', 'downloads'), 'link' => URL::build('/resources', 'sort=downloads')];

if(isset($_GET['sort']) && array_key_exists($_GET['sort'], $sort_types)){
    $sort_type = $_GET['sort'];
    $sort_by = $sort_types[$sort_type]['type'];
    $sort_by_text = $sort_types[$sort_type]['sort'];
    $url = $sort_types[$sort_type]['link'];
} else {
    $sort_by = 'updated';
    $sort_by_text = $resource_language->get('resources', 'last_updated');
    $url = URL::build('/resources', true);
}

// Get page
if(isset($_GET['p'])){
    if(!is_numeric($_GET['p'])){
        Redirect::to($url);
    } else {
        if($_GET['p'] == 1){
            // Avoid bug in pagination class
            Redirect::to($url);
        }
        $p = $_GET['p'];
    }
} else {
    $p = 1;
}

if ($user->isLoggedIn()) {
    $groups = [];
    foreach ($user->getGroups() as $group) {
        $groups[] = $group->id;
    }
} else {
    $groups = [0];
}

$page_title = $resource_language->get('resources', 'resources') . ' - ' . $language->get('general', 'page_x', ['page' => $p]);
require_once(ROOT_PATH . '/core/templates/frontend_init.php');

$categories = $resources->getCategories($groups);
// Assign to Smarty array
$category_array = [];
foreach($categories as $category){
    // Get category count
    $category_count = DB::getInstance()->get('resources', ['category_id', '=', $category->id]);
    $category_count = $category_count->count();
    $category_array[] = [
        'name' => Output::getClean($category->name),
        'link' => URL::build('/resources/category/' . $category->id . '-' . URL::urlSafe($category->name)),
        'count' => Output::getClean($category_count)
    ];
}
$categories = null;

// Get latest releases
$latest_releases = $resources->getResourcesList($groups, $sort_by);

// Array to hold resources
$resources_array = [];

if(count($latest_releases)){
    // Display the correct number of resources
    $n = 0;

    while ($n < count($latest_releases)) {
        // Get actual resource info
        $resource = $latest_releases[$n];

        // Get category
        $category = DB::getInstance()->get('resources_categories', ['id', '=', $resource->category_id]);
        if ($category->count()){
              $category = Output::getClean($category->first()->name);
        } else {
              $category = 'n/a';
        }

        $resource_author = new User($resource->creator_id);
        $resources_array[] = [
            'link' => URL::build('/resources/resource/' . $resource->id . '-' . URL::urlSafe($resource->name)),
            'name' => Output::getClean($resource->name),
            'short_description' => Output::getClean($resource->short_description),
            'description' => mb_substr(strip_tags(Output::getDecoded($resource->description)), 0, 60) . '...',
            'author' => Output::getClean($resource_author->getDisplayname()),
            'downloads' => $resource_language->get('resources', 'x_downloads', ['count' => $resource->downloads]),
            'views' => $resource_language->get('resources', 'x_views', ['count' => $resource->views]),
            'rating' => round($resource->rating / 10),
            'version' => Output::getClean($resource->latest_version),
            'category' => $resource_language->get('resources', 'in_category_x', ['category' => $category]),
            'updated' => $resource_language->get('resources', 'updated_x', ['updated' => $timeago->inWords(date('d M Y, H:i', $resource->updated), $language)]),
            'updated_full' => date('d M Y, H:i', $resource->updated)
        ];

        if ($resource->type == 1) {
            $resources_array[count($resources_array) - 1]['price'] = Output::getClean($resource->price);
        }

        // Check if resource icon uploaded
        if ($resource->has_icon == 1) {
            $resources_array[count($resources_array) - 1]['icon'] = $resource->icon;
        } else {
            $resources_array[count($resources_array) - 1]['icon'] = rtrim(URL::getSelfURL(), '/') . (defined('CONFIG_PATH') ? CONFIG_PATH . '/' : '/') . 'uploads/resources_icons/default.png';
        }

        $n++;
    }
}

// Convert array to JSON
$json_output = json_encode($resources_array, JSON_PRETTY_PRINT);

// Output JSON
header('Content-Type: application/json');
echo $json_output;