News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Adding A New Function?

Started by zoolman, February 21, 2011, 05:08:51 AM

Previous topic - Next topic

zoolman

I am working on a mod for my site which will allow a member to "approve" or "disprove" any post in a particular forum. This is per post not just per thread. There will be 2 buttons under each post and when a member clicks on either one, it will add a record to my own table. Also, it will list every member who has clicked either option under each post. This is for a very particular purpose but I can release it as a mod if anyone is interested.

I am struggling with the code for this though. I can read from the database and list those who have submitted their response. However, I am struggling with submitting the "votes". I have created the 2 buttons but cannot get a function to be called when they are pressed. I tried using a <form> with an action which was a function in my own php file but it is not being called.

How do I do this?

Arantor

Which version of SMF?

If it's SMF 2.0, what's wrong with post moderation which does basically that anyway?

zoolman

Quote from: Arantor on February 21, 2011, 05:10:25 AM
Which version of SMF?

If it's SMF 2.0, what's wrong with post moderation which does basically that anyway?

2.0 RC5. This isn't to approve the post in the sense that that post moderation works. This is different. It's kind of a like a poll vote but on a per post rather than per thread basis. With just two options which are the same each time. Just approve or disapprove. This is completely separate from post moderation though.

Arantor

Ah, more like a 'Like this post'/'Dislike this post' setup... http://www.simplemachines.org/community/index.php?topic=421381.0 might be of interest to you then.

zoolman

Quote from: Arantor on February 21, 2011, 05:25:14 AM
Ah, more like a 'Like this post'/'Dislike this post' setup... http://www.simplemachines.org/community/index.php?topic=421381.0 might be of interest to you then.

Not quite but similar I guess. I'll take a look to see if it helps me. Thanks.

zoolman

That is similar to what I want but I still need and want to write my own mod for exactly what I need. There is no code available in that thread you posted. Thanks for posting it but are there any code examples to help me. Thanks.

Arantor

There aren't any specific code examples to help you, and in any case your code is going to be modifying the core code directly anyway.

I still don't know exactly what you're trying to do (count of per post reputation, or tracking user votes for/against a given post), so can't give you great advice.

If it's just per post reputation as a count, simplest way would be to add a column to the messages table and store it in there, and modify Display.php both in Display() and prepareDisplayContext() to fetch the column (first one to get it from the DB, second to push it into the callback $output variable), and then modifying Display.template.php to display it in the post itself.

If it's user/post tracking, best thing would be a separate table, queried from relatively early in Display(), once the list of message ids is known, and stored in the global $context where you can pick it up later in Display.template.php. Depending on whether you need to know the names or not, you might decide to join to the members table to get the list of names (or a call to loadMemberData to get everyone's details)

As for actually casting the vote, simplest way would be a new action, for which you define an entry in index.php itself which handles the process of voting. I have no idea how you want that to look so can't suggest something to look at as an example (since I don't know if it needs any user interaction other than just voting). If it IS just voting and clicking on the link is enough to vote, I'd suggest looking at LockTopic.php for an example of a simple action that performs a change then returns back to the topic.

zoolman

Here is what I am doing. I've added a php file to Sources called approval.php. This has the code:
<?php
if (!defined('SMF'))
die('Hacking attempt...');

/* The job of this file is to handle submitting an approval

void approval_submit()
- handles submitting the approval response
- accessed from ?action=approvalsubmit
*/

function approval_submit()
{
// code removed
}
?>


I've removed the actual code in the function to keep it shorter as it is irrelevant. The problem is the function not being called at all not what it is doing.

I've added this to display_template.php
echo '<form action="', $scripturl, '?action=approvalsubmit" method="post">';
echo '<input type="radio" name="approvalresponse" value=" approve" /> Approve';
echo '<input type="radio" name="approvalresponse" value=" disapprove" /> Disapprove ';
echo '<textarea rows="1" cols="50" name="comment"></textarea><br>';
echo '<input type="submit" value="Submit" />';
echo '</form>';


I changed it slightly so it uses radio buttons instead and also the member can included a comment as to why they approved or disapproved the post.

I've added this to index.php
'approvalsubmit' => array('approval.php', 'approval_submit'),


The function is never called when I click the submit button. What am I missing?


Arantor

QuoteI've added this to display_template.php

Which will break it. The entire display template is already a form for inline moderation functions.

zoolman


Arantor

As I see it you have three choices:

1. Rip out the entire form that's set up in the display template which will break inline moderation, and may also break quick edit.
2. Add your form(s) after the main form and reposition with Javascript.
3. Handle the entire submission AJAXively whereupon you can dynamically add the form items you need without touching the overall form, grab their values, then send an asynchronous request to the server to handle the change.

zoolman

Ok, well none of those make any sense to me. I'm probably out of my depth so it sounds like it is time for me to give up. Thanks though.

zoolman

I don't really understand because I can find loads of mods which make changes to display.template and add a form to it (no javascript or ajax). So what is wrong with mine and not the others?

Arantor

Yes, and if you look through those mods' support topics, no doubt you'll find comments about it breaking quick moderation and/or quick edit.

The problem is you can't have a form inside a form.

zoolman

So why are there other forms within even the default display.template? For example the poll form? Surely this is a form within a form then if the whole of display.template is a form?

I'm just trying to understand.

Arantor

You misunderstand me.

The entire *post* display is a form. The poll form appears *above* it, so it's not a form within a form. You want to put a form per post - that's going to be within the quick moderation form.

The other main form in a topic - jump to - is also outside the quick moderation form, as is the quick reply area.

zoolman

Thanks. I get it now. Thanks for your patience. My questions must seem a bit dumb to those with more experience.

Arantor

Not at all :)

That's one of the things that people find hard with modding something like SMF - what should be a simple little tweak ends up being a lot of hassle because you end up breaking something else if you're not careful. (I wrote lots of mods for SMF, found this happening unfortunately too often :()

zoolman

Out of interest if I changed my idea so that it was on a "per topic" basis rather than "per post" basic would the mod be any easier. In theory I could then put my form above the moderation form (the same as the poll form). This wouldn't be ideal as I would prefer to have it under the first post in the topic.

That I see as the easiest solution. i.e. disabling polls in this forum (as they wouldn't be needed in this forum) and display my new form in it's place instead. Not ideal but I might be able to live with it.

Otherwise it looks like I add a button (not in a form) and clicking on that calls a javascript function which would update my database. I assume I would then need to use AJAX if I wanted that change to be immediately viewable on the page.

I don't know javascript or ajax though so this would be a much longer term solution.

Arantor

Oh, it'd be a lot easier per topic, since you could trivially add it to near the start of Display.template.php to manage the form.

QuoteOtherwise it looks like I add a button (not in a form) and clicking on that calls a javascript function which would update my database. I assume I would then need to use AJAX if I wanted that change to be immediately viewable on the page.

Yes. There are a lot of tutorials out there about doing this.


Thinking about it you could do it with regular links rather than a button, and making images to make them look like buttons if you wanted?

Advertisement: