PDA

View Full Version : How To Make Attachments Required ...?


fai99al99
01-23-2015, 04:30 AM
Hello,

I Want To Make Attachments Required For A Certain Forum
When Someone Add A New Thread , How To Do This?

Thanks for your time,

Dave
01-23-2015, 08:54 AM
Hook into newthread_post_start and then check if there are any attachments present.

nerbert
01-23-2015, 09:30 AM
I think you would need an event listener attached to the form that would listen for "submit" and fire a function that would use a regex to check for an attachment on the text submitted and if not preventDefault() and throw an alert or put text in the error div. I guess you would also have to have server checking that would redirect to an error message.

I may play around with it later.

nerbert
01-23-2015, 11:11 PM
Here we go. This adds an additional hidden input to enabled forums for server checking in case someone posts with JavaScript disabled. The server returns an error message if no attachment is uploaded.

With JavaScript enabled the page will throw an alert if no attachment is uploaded. I had hoped I could distinguish between a submission and preview but that was too complicated so the alert shows for both submit and preview.

Enable this and set forum(s) in "Message Posting Check for Attachment" in Options

Two phrases can be edited by searching phrases for "ca_"

Check it thoroughly and get back if anything goes wrong.

fai99al99
01-24-2015, 04:21 AM
Thank you nerbert for your time making this , I really appreciate It.

I have tested It out ,but It seems It does not work ...
It works fine when I click the Preview button only .. but when I click the submit button it does not show any alert that there is no Attachment


I have been searching and figured a another way to do this:

hook : newpost_process

if($type=='thread' AND in_array($foruminfo['forumid'], array(1,2,3,4,5)))
{

$attachs = $vbulletin->db->query_read("SELECT attachmentid
FROM " . TABLE_PREFIX . "attachment
WHERE posthash = '" . $vbulletin->db->escape_string($post['posthash']) . "'
AND userid = " . $vbulletin->userinfo['userid'] . "
ORDER BY attachmentid");
if($vbulletin->db->num_rows($attachs) <= 0)
$errors[] = $vbphrase['required_attachments'];
}

nerbert
01-24-2015, 04:36 AM
I'm getting the alert for submit and preview for both sets of buttons. I don't know why an event listener wouldn't fire for that. I was wondering about how to see if an attachment exists for a thread and the only other way to do it besides the new hidden input would be a query like you did. I wanted to avoid queries if possible. Did you find the checkattachmentjavascript template? That's where my JS is.

--------------- Added 1422082661 at 1422082661 ---------------

I see you're using a hook in newpost instead of newthread. Is this supposed to require an attachment for each post of the thread or just for the opening post of a new thread?

fai99al99
01-24-2015, 05:15 AM
I have re-install It , but still the same problem facing me.
yes, checkattachmentjavascript template Is there

nerbert
01-24-2015, 06:12 AM
No matter how you detect an attachment in the server code it would be nice to have an alert before it's submitted rather than getting an error message from the server and going back.

Have you tried editing in more alerts in the JavaScript to see if it's detecting the submit event , whether the function is called and how many "li" elements are in the list? When the page loads there's only one "li" in the list. I don't know what it does, all it contains is &nbsp;. After you load an attachment it should have 2 "li"s.

Try this:

<script type="text/javascript">
function Check_For_Attachment() {
this.form = document.forms.vbform;
this.attachlist = fetch_object("attachlist_list2");
this.checkInput = document.createElement("input");
this.checkInput.setAttribute("type", "hidden");
this.checkInput.setAttribute("name", "checkattachment");
this.checkInput.setAttribute("value", "");
this.form.appendChild(this.checkInput);
this.alertText = "{vb:rawphrase ca_alert_text}";
YAHOO.util.Event.on(this.form, "submit", function(e){
alert("submit detected");
checkAttachment.checkForm(e)
})

}

Check_For_Attachment.prototype.checkForm = function(e) {
var items = fetch_tags(this.attachlist, "li");
alert("list items " + items.length)
if(items.length < 2) {
alert(this.alertText)
YAHOO.util.Event.stopEvent(e);
} else {
this.checkInput.value = "true";
}
}

YAHOO.util.Event.onDOMReady(function(e) {
checkAttachment = new Check_For_Attachment();
});
</script>

fai99al99
01-24-2015, 07:54 AM
did not work ..
I tried to change hook location and editing the code ,but still no luck

to distinguish between a submission and preview buttons I used this:
$_REQUEST['sbutton']

nerbert
01-24-2015, 08:06 AM
I used $_REQUEST['preview'] but you don't get that until after it's submitted and is read in server code. It doesn't help with the JavaScript though. Well, I don't know what the problem is but as long as you can check at the server I guess you have what you need.

Too bad it didn't work for you.

fai99al99
02-10-2015, 12:41 PM
How Can I Make A specific Attachments Images Formats like "png,jpg...etc" Required! Using :


if($type=='thread' AND in_array($foruminfo['forumid'], array(1,2,3,4,5)))
{

$attachs = $vbulletin->db->query_read("SELECT attachmentid
FROM " . TABLE_PREFIX . "attachment
WHERE posthash = '" . $vbulletin->db->escape_string($post['posthash']) . "'
AND userid = " . $vbulletin->userinfo['userid'] . "
ORDER BY attachmentid");


if($vbulletin->db->num_rows($attachs) <= 0)
$errors[] = $vbphrase['required_attachments'];
}