News:

Wondering if this will always be free?  See why free is better.

Main Menu

Creating a mod from scratch?

Started by asprin, September 15, 2019, 10:24:54 AM

Previous topic - Next topic

asprin

Hi everyone,

New to SMF here. But not new to the world of community forums. I've an idea about building a modification/add-on on SMF. But the thing is I couldn't find any help/documentation about building a modification from scratch. Is there an example thread which serves as a tutorial for this topic?

For the record, I'm very much experienced in

  • PHP
  • MySql
  • HTML
  • CSS
  • JS/jQuery
  • MVC pattern

So I believe I meet the per-requisites for building a modification. The only question is how?

To give a brief overview of what I'm currently searching for:


  • How to create a custom form?
  • How to display data entered in that form in a new page?
  • How to form a link between these forms and the board in the forum? Ex: A Fantasy Cricket board which will require creation of new events. I can use the forum's board feature to create a board/category for it. But when creating a new event, how would I link the event as a new thread in that board?

I hope the above is making sense.
[/list]

Kindred

Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

asprin

Thanks Kindred. I had already gone through those and while they are definitely helpful, I didn't find an example so to speak. Those look more like guidelines/best practices.

Is there a tutorial (either here in the community or external) where the author shows how a mod is actually created?

Also, from the links above I see a concept called as SSI. From what I understand, this file helps in passing data to external pages (non-SMF). So does that mean SMF has no ability to create pages that are internal to the framework?

Arantor

Sure it does.

The problem with writing a mod is that for the most part you're changing the core code so there's not really a lot to show you except for how to write the same mod over and over.

In your case, start in index.php and add a new action to the list to make the new URL for your form, you add the name of the action, the file to load for that action and the function to call when you get there.

Then create a new file for your new page in Sources, put a function in it. This is where your handling will go.

To display the form, make a file in Themes/default that has a template.php suffix, inside which is going to be a function called template_something, which will contain your form. Then in your Sources function, set $context['sub_template'] to the "something" in your template_something. This renders it.

Displaying on a new page, same process.

Not sure I understand what you mean in your last point about linking a board to something.

asprin

Quote from: Arantor on September 16, 2019, 02:40:56 AM
In your case, start in index.php and add a new action to the list to make the new URL for your form, you add the name of the action, the file to load for that action and the function to call when you get there.

Then create a new file for your new page in Sources, put a function in it. This is where your handling will go.

Sounds like a start. Thanks. I'll see how far I can go with that. Btw, are you referring to the "$actionArray" variable in the index.php file?

Quote from: Arantor on September 16, 2019, 02:40:56 AM
Not sure I understand what you mean in your last point about linking a board to something.
My apologies. I think I'll be able to explain it better once I've something started and ready to show.

Arantor

Yes, I was referring to $actionArray.

asprin

Made some progress. This is exciting.

Attaching screenshot.

Suki

Are you hosting your code in any software version control website such as GitHub or bitbucket? if so we can take a look at it and offer more refined help.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

asprin

Quote from: Suki on September 20, 2019, 12:52:03 PM
Are you hosting your code in any software version control website such as GitHub or bitbucket? if so we can take a look at it and offer more refined help.

No, I haven't set one up yet. But I like the idea of having guidance. Will see what I can do about that.

asprin

So done with the frontend part for now.

Coming to processing the form, what you folks suggest would be the ideal approach?

(a) Native form submission via
<form action="'.$scripturl.'?action=postevent" method="post" autocomplete="off">'

(b) Via Ajax (prefer to user jQuery here but don't mind using vanilla javascript either)

Any suggestions on form tokens for security purposes?

Arantor

SMF generally does native forms mostly because it tries to avoid relying on JS for things.

That said, it's your mod, you can build it how you like.

As far as security goes, the minimum accepted level is to pass the session values through a hidden input (using $context['session_var'] as the key and $context['session_id'] as the value), with a checkSession call when checking the submission.

You can upgrade this to a one time token (on top) in 2.1 by using createToken to make a token, putting it in the form in much the same way (I forget exactly how off hand but most forms will give you an example) and then using validateToken at submission.

asprin

Quote from: Arantor on September 22, 2019, 04:57:02 AM
As far as security goes, the minimum accepted level is to pass the session values through a hidden input (using $context['session_var'] as the key and $context['session_id'] as the value), with a checkSession call when checking the submission.

Awesome. Granted that I still have to go through most of the manuals related to SMF, but loving these bits of information. I think I'll leave out tokens for now and follow the above approach.

Arantor

Honestly, the greatest manual to SMF development is SMF's own code. Whenever I wrote mods, I studied the code to see what it did and to try to replicate the behaviours I wanted.

asprin

More updates. Have decided to transform my idea into a vBookie/Sportsbook mod. This would involve


  • Event Creation (done)
  • Outcome Creation (done)
  • Place Wager (in progress)
  • Event Settlement

Loving the way it's been coming out.

Aleksi "Lex" Kilpinen

Quote from: asprin on September 16, 2019, 01:10:14 AM
Thanks Kindred. I had already gone through those and while they are definitely helpful, I didn't find an example so to speak. Those look more like guidelines/best practices.

Is there a tutorial (either here in the community or external) where the author shows how a mod is actually created?
A little late to the party, but the modsite ( https://custom.simplemachines.org/mods/ ) actually shows you all edits done for any mod you can find, and you can download the packages, extract them, and study what is not done through direct edits. That's pretty much how I have learned what I have about how SMF code functions.
Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

asprin

Quote from: Aleksi "Lex" Kilpinen on September 24, 2019, 01:47:26 PM
A little late to the party, but the modsite ( https://custom.simplemachines.org/mods/ ) actually shows you all edits done for any mod you can find, and you can download the packages, extract them, and study what is not done through direct edits. That's pretty much how I have learned what I have about how SMF code functions.

Thanks for the tip!

A little more progress made today, although work is keeping me busy of late.

Finished with editing and deleting an outcome. Next move is a major one where a user will be able to place a wager/bet on an outcome.


asprin

So, I'm about to start work on letting a user place a wager on an outcome. But before I do that, I need to know how much money/cash/vcash does the member have in his/her account.

From the mods section, I found two mods that deal with this virtual currency thing:


  • SMF Shop
  • ST Shop
By going through the code of these mods, I found that SMF Shop uses a column named "money" and ST Shop uses a column named "shopMoney" to store the user's balance.

So I was wondering if there was a way to determine if any of these columns is present in the "members" table and if not, I go ahead and create a new one to store the balance. Is this something that can be achieved via $smcFunc ? (the checking of those two columns)

So basically something like the below:
if(<members table has either "money" or "shopMoney" column>) // need help with this condition
{
      // fetch the current balance from that column (I can handle this part)
}
else
{
     // create a new column in the members table (I can handle this part)
}

Arantor

Honestly, I wouldn't try to guess based on presence of column (because it's possible a user had at one time installed one, uninstalled it leaving the data in the database, and installed the other), but simply ask the user.

What you could do is call db_extend('Packages'); to load the extra functions available there, then use $smcFunc['db_list_columns']() to get the column names. You wouldn't want to do this on the fly because it's not the cheapest query ever but you certainly could do this in the installer and admin areas.

I'd probably add a new column anyway, just for your use, then let the user choose whether to use yours or another if it happens to be around.

asprin

Quote from: Arantor on October 08, 2019, 10:05:44 AM
Honestly, I wouldn't try to guess based on presence of column (because it's possible a user had at one time installed one, uninstalled it leaving the data in the database, and installed the other), but simply ask the user.
That's an excellent point. I had installed ST Shop a few weeks back and then uninstalled it. And now when I go and look in the members table, the ST Shop columns are still present. I simply assumed that the modder would take care of removing the columns as well upon uninstallation, but I guess I was wrong.

Quote from: Arantor on October 08, 2019, 10:05:44 AM
What you could do is call db_extend('Packages'); to load the extra functions available there, then use $smcFunc['db_list_columns']() to get the column names. You wouldn't want to do this on the fly because it's not the cheapest query ever but you certainly could do this in the installer and admin areas.
What does this do exactly? From the naming convention, it does look like it will list out the columns of a table, but wouldn't this again return the columns of an un-installed mod too? i.e. the same problem as the above point?


Quote from: Arantor on October 08, 2019, 10:05:44 AM
I'd probably add a new column anyway, just for your use, then let the user choose whether to use yours or another if it happens to be around.
This sounds like a safe bet. Let the user decide from the Admin section which column he intends to use.

Thanks for the valuable inputs.

Illori

depending on how the mod package is created, it may offer to the users to remove the left over data in the database. otherwise it most likely would stay in the database forever.

Advertisement: