Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Jade Elizabeth on September 13, 2015, 01:20:05 AM

Title: Change when posts are counted mod
Post by: Jade Elizabeth on September 13, 2015, 01:20:05 AM
So on my forum I have a little bit of code in post.php to stop posters from a certain group from having their posts counted in certain boards. I'd like to make this into a mod. I want to have an interface with a list of boards you can check off and then I want to have a field for the membergroup id in question....and then I want to have it do what the code originally did. OR Perhaps just adding options to the board/membergroup interface, though the former would be much much easier and nicer for people who are just starting with this.

So I could store this stuff in the DB and have it load the variables as an array (I'd like to store it like this : "1,2,3,4,5") and use essentially this code but with variables so it's not hard coded...but I'm not sure if that is the best way to handle it. Can someone please help me out?

This is the code at the moment:
$posterOptions = array(
'id' => $user_info['id'],
'name' => $_POST['guestname'],
'email' => $_POST['email'],
'update_post_count' => (!$user_info['is_guest'] && !isset($_REQUEST['msg']) && $board_info['posts_count'] && (!in_array('41', $user_info['groups'])  || (in_array('41', $user_info['groups']) && in_array($board_info['id'], array(3, 83, 14, 73, 15, 74, 56, 28, 87, 84, 86, 79, 91))))),
Title: Re: Change when posts are counted mod
Post by: Jade Elizabeth on November 23, 2015, 10:42:30 PM
Friendly bump :)
Title: Re: Change when posts are counted mod
Post by: bobodragomirqqq on November 27, 2015, 04:20:14 AM
I also had the same problem ... hope to find a solution soon
Looking forward from you

Title: Re: Change when posts are counted mod
Post by: All Colours Sam on November 27, 2015, 11:04:12 AM
Do you want the whole interface code or do you just want some instructions and pointers to do it yourself?

Are you familiar with adding SMF $modSettings values and how $config_vars is used?
Title: Re: Change when posts are counted mod
Post by: Jade Elizabeth on November 27, 2015, 11:36:01 PM
Thanks Suki!

I would like to learn how to do it myself :D. I am not very familiar with #modSettings and have zero experience with $config_vars. I've used the context array but not 100% sure how I did it at the moment, it was a little while back.
Title: Re: Change when posts are counted mod
Post by: All Colours Sam on November 29, 2015, 04:20:19 PM
The simplest and straight forward place to add a custom admin setting is the general settings page which already provides a generic config_vars var:

Sources/ManageSettings.php:

$config_vars = array(
// Mod authors, add any settings UNDER this line. Include a comma at the end of the line and don't remove this statement!!
);



Each "setting" gets defined by an array, SMF supports the most common form fields, text, textarea, select, multiple select, checkbox, etc.

To show a list of selectable groups, you need to first get them in a custom function, something like this:


function getGroups()
{
global $smcFunc;
$request = $smcFunc['db_query']('', '
SELECT id_group, group_name
FROM {db_prefix}membergroups
WHERE id_group > {int:admin}',
array(
'admin' => 1,
)
);
$return = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$return[$row['id_group']] = $row['group_name'];
$smcFunc['db_free_result']($request);
return $return;
}



That function will return an array of all groups minus the admin one (it doens't check for custom admin groups)

and to show them as a multi select field you will do something like this:


// Are there any selectable groups?
$groups = getGroups();
if (!empty($groups))
$config_vars[] = array('select', 'myMod_selectGroups',
$groups,
'subtext' => $txt['my admin subtext for this particular setting'],
'multiple' => true,
);


As you can see, the code checks if the function returned something and only add the setting if it did, the setting its a new entry in the $config_vars array, its easier to add it this way instead of messing directly with the array.


Heres a quick explanation:

'select'  <--- the field type, in this case we want a select field
'myMod_selectGroups',  <---- your $modSettings name, this has to be unique and has to represent what the actual var do, this is the name of the setting and its how you are going to call it, in the example my modsetting will be called $modSettings['myMod_selectGroups']
$groups,  <--- its the groups array, you just need to pass it directly, SMF handles it internally.
'subtext' <--- the tiny text that appears below an admin setting to help explain what the setting do, you can leave it empty but its encoruage to set a $txt for it to help whoever is going to use that setting to understand what it actually does.
'multiple'  <--- a special param that only "select" fields have, it tells SMF that this field will be a multiple choice one.


A list of boards will be pretty much the same, just create another function to get a list of boards and return it.


So, in the end your ManageSettings.php file shoulsd look like this:



$config_vars = array(
// Mod authors, add any settings UNDER this line. Include a comma at the end of the line and don't remove this statement!!
);

// Are there any selectable groups?
$groups = getGroups();
if (!empty($groups))
$config_vars[] = array('select', 'myMod_selectGroups',
$groups,
'subtext' => $txt['my admin subtext for this particular setting'],
'multiple' => true,
);


The getGroups() function can be placed at the end on the file before the ?> tag

Thats the first step to add your settings, onces you have that then we can start on how to apply your $modSettings vars.
Title: Re: Change when posts are counted mod
Post by: Jade Elizabeth on November 30, 2015, 04:05:31 AM
Okay I have all of that. So the $config_vars comment meant after the ); and not before it? I would have put it after the comment so I'm glad you showed me how it should look! :D
Title: Re: Change when posts are counted mod
Post by: All Colours Sam on November 30, 2015, 08:55:52 AM
Nope!, the config var comment means "below itself"  as in adding a new key => value inside that array:

$config_vars = array(
// Mod authors, add any settings UNDER this line. Include a comma at the end of the line and don't remove this statement!!
array('select', 'myMod_selectGroups',
$groups,
'subtext' => $txt['my admin subtext for this particular setting'],
'multiple' => true,
),
);


Either way its just fine, you can add your key inside the array or add the same key via $config_vars[]

I used $config_vars[] because the code is doing a conditional, the "if (!empty($groups))" and while doable, its a mess to add that conditional inside an array.

So this:

$config_vars = array(
// Mod authors, add any settings UNDER this line. Include a comma at the end of the line and don't remove this statement!!
);

// Are there any selectable groups?
$groups = getGroups();
if (!empty($groups))
$config_vars[] = array('select', 'myMod_selectGroups',
$groups,
'subtext' => $txt['my admin subtext for this particular setting'],
'multiple' => true,
);



Its the same as this:

$config_vars = array(
// Mod authors, add any settings UNDER this line. Include a comma at the end of the line and don't remove this statement!!
!empty($groups) ? array('select', 'myMod_selectGroups',
$groups,
'subtext' => $txt['my admin subtext for this particular setting'],
'multiple' => true,
) : array(),
);



But the first one is preferable as its cleaner and less obtrusive
Title: Re: Change when posts are counted mod
Post by: Jade Elizabeth on November 30, 2015, 09:58:18 PM
Oh I see! I just facepalmed for missing that part. Thanks for explaining that bit! :D
Title: Re: Change when posts are counted mod
Post by: All Colours Sam on December 02, 2015, 10:07:21 AM
The rest its pretty straight forward, your $modSetting['myMod_selectGroups'] is now an array of groups IDs, you just have to check the current user against that array:

if (!empty($modSetting['myMod_selectGroups']))
{
// your logic here
}