Jump to content
MakeWebGames

Create new tab admin panel


URBANZ

Recommended Posts

So i ran in to an issue unless i've missed something. As the methods are pulled from the module.json file to display the tabs in admin panel i wanted to dynamically add another if a user activates a feature.

I thought i would post this if any other people have this issue.

$array = array(     
	//"hide" => true, //can also add hide element if needed 
	"text" => "TEXT HERE",
	"method" => "METHODNAME"
);
$json = file_get_contents('modules/installed/MODULEHERE/module.json');
$decoded = json_decode($json);
$decoded->admin[] = $array;
file_put_contents('modules/installed/MODULEHERE/module.json', json_encode($decoded));

I haven't tested this much but it is currently functioning as desired in one of my modules.

I'm sure this probably could be done better/another way. Let me know your thoughts.

This is full usage including removing the array when deactivated again.

if (isset($this->methodData->SETTINGSVALUE)) {
	$settings->update("SETTINGSVALUE", $this->methodData->SETTINGSVALUE);
	$json = file_get_contents('modules/installed/YOURMODULE/module.json');
	$decoded = json_decode($json);
	if($settings->loadSetting("SETTINGSVALUE")){
		$array = array(     
			"text" => "YOUR TEXT",
			"method" => "METHOD"
		);
		$decoded->admin[] = $array;
	} else {
		/* 
			This has -1 as starts 1 item from the last then counts 1 up and removes it can change to how many items you want.
			makes it much easier if you need to add multiple items then remove them later.
			just make sure you are removing the same amount you added or you will remove other arrays.
		*/
		array_splice($decoded->admin, -1, 1); 
	}	
	file_put_contents('modules/installed/YOURMODULE/module.json', json_encode($decoded));
	header("Location: /?page=admin&module=YOURMODULE");
}

 

 

Edited by urbanmafia
Link to comment
Share on other sites

7 minutes ago, Magictallguy said:

Nice!
Quick tip; appending an array is cheaper on the resources (and therefore faster) than array_push().
Recommendation: Alter

array_push($decoded->admin, $array);


array_push($decoded->admin, $array);

 to

$decoded->admin[] = $array;


$decoded->admin[] = $array;

 

Thankyou MTG much better way. updated in first post if someone uses it. 

Link to comment
Share on other sites

    /* This hook is run before any module is loaded */
    new Hook("moduleLoad", function ($loadedModule) {
        global $page;

        /* Only make this change if the user is on the admin page */
        if ($loadedModule == "admin") {
            /* Insert new admin menu link */
            $page->modules["crimes"]["admin"][] = array(
                "text" => "test", 
                "method" => "edit"
            );
        }
        /* 
            Always return the $loadedModule as this function can be used to change the module to load i.e.
            
            if ($loadedModule == "jail") {
                return "prison";
            }
            return $loadedModule;
         */
        return $loadedModule;
    });

Thius is a way to do it with hooks and bypassing file permission errors

Edited by Dayo
Link to comment
Share on other sites

1 minute ago, Dayo said:

/* This hook is run before any module is loaded */ new Hook("moduleLoad", function ($loadedModule) { global $page; /* Only make this change if the user is on the admin page */ if ($loadedModule == "admin") { /* Insert new admin menu link */ $page->modules["crimes"]["admin"][] = array( "text" => "test", "method" => "edit" ); } /* Always return the $loadedModule as this function can be used to change the module to load i.e. if ($loadedModule == "jail") { return "prison"; } return $loadedModule; */ return $loadedModule; });


    /* This hook is run before any module is loaded */
    new Hook("moduleLoad", function ($loadedModule) {
        global $page;

        /* Only make this change if the user is on the admin page */
        if ($loadedModule == "admin") {
            /* Insert new admin menu link */
            $page->modules["crimes"]["admin"][] = array(
                "text" => "test", 
                "method" => "edit"
            );
        }
        /* 
            Always return the $loadedModule as this function can be used to change the module to load i.e.
            
            if ($loadedModule == "jail") {
                return "prison";
            }
            return $loadedModule;
         */
        return $loadedModule;
    });

Thius is a way to do it with hooks and bypassing file permission errors

Thank you didn't know this existed tbh still getting my head around alot of the hooks

Link to comment
Share on other sites

2 hours ago, Dayo said:

Yeah i need to sit down and document them all there are loads that people don't know of 

Even if you don't want to document them all. Just supplying us with a list of them would be a good start, us users can document them of we get bored.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...