need example for how to add mod configuration settings to admin cpanel

Started by Deprecated, August 11, 2008, 04:03:19 PM

Previous topic - Next topic

[SiNaN]

Not at all, you're always welcome. Would be glad if you mark the topic solved if you get it working. ;)
Former SMF Core Developer | My Mods | SimplePortal

Deprecated

Just another question Sinan, on a higher level. Should I explode my array while ManageSettings is still running, like explode on comma, then clean it up, implode it to get a purely numeric array, then store that in the database? I'm wondering if that's what that code snippet you gave me does.

In the same area I'm in, can I store a third value calculated upon the entered values? And can that value be an array of integers?


And I was holding off marking it solved until I'm fully sure I'm done, probably a few hours from now.

Deprecated

Actually I think I want to explode on comma, then unset any space elements, and store the result as an array. It should be an array of just the numbers, right? Can you store that in the db?

[SiNaN]

That example was actually different. But this should help you with your question:

// Check if it is filled in.
if(!empty($_POST['deprecated_text_1'])) {

// Okay, get ready for checking; convert to an array
$values = explode(',', $_POST['deprecated_text_1']);

// Make sure every entry is integer.
foreach ($values as $index => $value)
$values[(int) $index] = (int) $value;

// Sum all the ids and add to the end
$values[] = array_sum($values);

// Ready to go! WHOOO HOOOO!
$_POST['deprecated_text_1'] = implode(',', $values);
}
else {
// No luck, just return 0.
$_POST['deprecated_text_1'] = 0;
}
Former SMF Core Developer | My Mods | SimplePortal

Deprecated

Your example is imploding it back into a CSV list. I'd rather instead save a third value as the array of numbers, and just let whatever the user typed in stay as is. Here's my idea but it's not finished.


$no_spaces = str_replace(" ", "", $deprecated_text_1);
$pieces = explode (",", $no_spaces)

foreach ($pieces as $key => $>value) if ($value == "") unset $pieces[$key];

$deprecated_array_1 = $pieces;

// and then how do I tell save settings to save it?

First I strip the spaces, then I explode on comma, then I delete the null elements. At that point I should have an array with just the numbers. I hope I can store that in yet another variable that is not exposed to the user directly. Can that be done?

[SiNaN]

In your codes, do not forget to make sure things between commas are integer too.

You mean adding a setting without asking the admin? Just would calculate something with admin entered and save it into the db for further use, right?
Former SMF Core Developer | My Mods | SimplePortal

Deprecated

What I want to do is take the two CSV strings of numbers and explode them and get rid of anything that isn't the actual integers the user entered, then I want to store those as an array of integers. The strings would also be stored, so that when the settings are changed next time the user sees what he had entered before.

The guts of my modification needs an array of integers to work on, and I don't want to calculate it every time it is used. It's better to make the array once when the admin is updating his selection, and then the stored array can be used in the test.

So the user (admin) enters two alpha fields of CSV numbers, but the cpanel stores an alpha string for each, and stores a corresponding array of integers for each also.

[SiNaN]

You talk too complex for me to understand it. I can say that I am a noob in programming.

What I understand is, you want to store old value too.
Former SMF Core Developer | My Mods | SimplePortal

Deprecated

Okay, the way I have it now, the admin enters a series of comma separated integers. But my ultimate use requires an array of integers, not a character string. But I still want to store the character string so that next time the admin wants to view or modify the settings he sees the same thing he entered before, what I called deprecated_text_1 and deprecated_text too. That part is already working.

Now since my modification needs an array of integers to do its job, and since I don't want to explode the character string every time it's used (wasteful of CPU time) I want to do that just once as it's entered, and store the numeric arrays of integers as values that the admin does not need to see. Same information different form.

So I strip the spaces, explode on comma, get rid of any null elements (I've seen null elements when I've done this before) and then I store the arrays as separate variables.

What I don't understand is how to tell SMF to save my two arrays. SMF handles the character strings okay, but I have to tell SMF to include my two arrays and save them too.

Yeah, I know, I talk funny. :)

[SiNaN]

I don't know and just asking; how can you store it as an array in db?

If you just don't want to explode it again and again we can add a simple hack in Load.php to explode it automatically when he gets it.
Former SMF Core Developer | My Mods | SimplePortal

Deprecated

I'm very, very thin on DB use. I've written only one piece of software that used a DB and I had about 5 calls to it. Use of DBs is probably my weakest area.

I had assumed you could store an array in a DB, and I'm sure you can but I guess not as one value. After all, smf_members is an array.

So I'll fall back and just store the CSV list, and then I'll explode it into an array at time of use. I'm probably being overly picky at trying to optimize for speed.

[SiNaN]

If you want, you can create your own table as smf_members but then you will need an sql query to get the values.

Even if we explode it in Load.php -where it's loaded- it will cause it be done on everypage; even in the ones that you don't use it.

In one function you just need to explode it once; so that's not something hard I guess. So I suggest sticking with the usual.
Former SMF Core Developer | My Mods | SimplePortal

Deprecated

The smf_members was just an example. I'm not doing anything with that.

I'll stick to the usual. :)

Deprecated

I'm pleased to say, my modification is done, it works, in fact it works ****ing perfectly!!! :D

Thank you all for the help and a special thanks to Karl and Sinan!

I need to run some tests and try to break the mod and use it on my production site for a couple days just to make sure it causes no problems, and then I'll upload it to the mod site mothership. :)

This is really gonna be kewl and it's my biggest mod to date!

Advertisement: