Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
Add delete user hook
Fix permissions not being respected in widgets
Fix user account resources tabs
  • Loading branch information
samerton committed Jul 11, 2022
1 parent df1ac9e commit df64607
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 10 deletions.
28 changes: 28 additions & 0 deletions upload/modules/Resources/hooks/DeleteUserResourcesHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/*
* Made by Samerton
* https://github.com/NamelessMC/Nameless/
* NamelessMC version 2.0.0-pr13
*
* Delete user event listener for Resources module
*/

class DeleteUserResourcesHook {
public static function execute(array $params = []): void {
if (isset($params['user_id']) && $params['user_id'] > 1) {
$db = DB::getInstance();

// Delete the user's resources
$db->delete('resources', ['creator_id', $params['user_id']]);

// Delete the user's resource comments
$db->delete('resources_comments', ['author_id', $params['user_id']]);

// Resource payments
$db->delete('resources_payments', ['user_id', $params['user_id']]);

// Resource premium details
$db->delete('resources_users_premium_details', ['user_id', $params['user_id']]);
}
}
}
5 changes: 3 additions & 2 deletions upload/modules/Resources/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
// Initialise module language
$resource_language = new Language(ROOT_PATH . '/modules/Resources/language', LANGUAGE);

require_once(ROOT_PATH . '/modules/Resources/classes/Resources_Sitemap.php');
require_once ROOT_PATH . '/modules/Resources/classes/Resources_Sitemap.php';
require_once ROOT_PATH . '/modules/Resources/hooks/DeleteUserResourcesHook.php';

require_once(ROOT_PATH . '/modules/Resources/module.php');
require_once ROOT_PATH . '/modules/Resources/module.php';
$module = new Resources_Module($pages, $language, $resource_language);
2 changes: 2 additions & 0 deletions upload/modules/Resources/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function __construct($pages, $language, $resource_language){
$pages->add('Resources', '/resources/icon_upload', 'pages/resources/icon_upload.php');
$pages->add('Resources', '/user/resources/licenses', 'pages/user/licenses.php');

EventHandler::registerListener('deleteUser', 'DeleteUserResourcesHook::execute');

}

public function onInstall(){
Expand Down
5 changes: 5 additions & 0 deletions upload/modules/Resources/pages/user/resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@

require(ROOT_PATH . '/core/templates/cc_navbar.php');

// Temporary fix until v2.0.0
if (NAMELESS_VERSION == '2.0.0-pr13' && !defined('FOMANTIC_RESOURCES_FIX')) {
$template->addJSScript('$(\'.menu .item\').tab();');
}

$template->onPageLoad();

require(ROOT_PATH . '/core/templates/navbar.php');
Expand Down
24 changes: 22 additions & 2 deletions upload/modules/Resources/widgets/LatestResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,27 @@ public function initialise(): void {

$timeago = new TimeAgo(TIMEZONE);

$latestResources = DB::getInstance()->orderAll('resources', 'updated', 'DESC LIMIT 5');
$groups = '0';
if (($user = new User())->isLoggedIn()) {
$groups = implode(',', array_keys($user->getAllGroupIds()));
}

$latestResources = DB::getInstance()->query(
<<<SQL
SELECT * FROM nl2_resources
WHERE
category_id
IN (
SELECT category_id FROM nl2_resources_categories_permissions
WHERE
group_id
IN (
{$groups}
)
AND `view` = 1
) ORDER BY updated DESC LIMIT 5
SQL
);
$latestResourcesArr = [];

foreach ($latestResources->results() as $resource) {
Expand Down Expand Up @@ -80,4 +100,4 @@ public function initialise(): void {

$this->_content = $this->_smarty->fetch('widgets/resources/latest_resources.tpl');
}
}
}
29 changes: 23 additions & 6 deletions upload/modules/Resources/widgets/TopResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,30 @@ public function initialise(): void {

$timeago = new TimeAgo(TIMEZONE);

$topResources = DB::getInstance()->orderAll('resources', 'rating', 'DESC LIMIT 5');
$groups = '0';
if (($user = new User())->isLoggedIn()) {
$groups = implode(',', array_keys($user->getAllGroupIds()));
}

$topResources = DB::getInstance()->query(
<<<SQL
SELECT * FROM nl2_resources
WHERE
category_id
IN (
SELECT category_id FROM nl2_resources_categories_permissions
WHERE
group_id
IN (
{$groups}
)
AND `view` = 1
) ORDER BY rating DESC LIMIT 5
SQL
);
$topResourcesArr = [];

foreach ($topResources->results() as $resource) {
// check if resource rating > 0
if ($resource->rating == 0) continue;

$author = new User($resource->creator_id);

if (!$author->exists()) {
Expand All @@ -72,7 +89,7 @@ public function initialise(): void {
if($resource->has_icon == 1 ) {
$topResourcesArr[$resource->id]['icon'] = Output::getClean($resource->icon);
} else {
$topResourcesArr[$resource->id]['icon'] = rtrim(Util::getSelfURL(), '/') . (defined('CONFIG_PATH') ? CONFIG_PATH . '/' : '/') . 'uploads/resources_icons/default.png';
$topResourcesArr[$resource->id]['icon'] = rtrim(URL::getSelfURL(), '/') . (defined('CONFIG_PATH') ? CONFIG_PATH . '/' : '/') . 'uploads/resources_icons/default.png';
}
}

Expand All @@ -84,4 +101,4 @@ public function initialise(): void {

$this->_content = $this->_smarty->fetch('widgets/resources/top_resources.tpl');
}
}
}

0 comments on commit df64607

Please sign in to comment.