Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Check for attachments presence

Vanilla 2.2.1

Task: detect whether a discussion has attachments (Advanced Editor enabled) and prevent saving/editing discussion if no attachment(s) found.

Could you please direct me to how does one check programmatically the presence of attachments?

Thanks.

Tagged:

Comments

  • RiverRiver MVP
    edited July 2016

    @Stayen said:
    Vanilla 2.2.1

    Task: detect whether a discussion has attachments (Advanced Editor enabled) and prevent saving/editing discussion if no attachment(s) found.

    Could you please direct me to how does one check programmatically the presence of attachments?

    Thanks.

    Your task can be interpreted in multiple ways.

    • what is an attachment?
    • prevent saving/ editing if no attachment found.
    • by discussion - do you mean comment or discussion or post or entire discussion, or at least one attachment in the entire threaded discussion.

    I'll assume it is purely created with the advanced editor and no other plugin.
    I'll assume prevent saving/ editing if no attachment found in the specific post.
    I'll assume you mean creating new discussions require attachments, and any discussions created with attachments cannot be saved if you edit them and try to remove an attachment.
    I'll assume that if you edit a discussion or comment that previously did not have an attachment then you don't have the restriction to require an attachment until you add an attachment (since this would require more work).

    The simplest way that I found is checking form values.

    use this event in a plugin for the initial Discussion post

    public function DiscussionModel_BeforeSaveDiscussion_Handler($sender) {
       $PostVals = ($sender->EventArguments['FormPostValues']);
    
      // editing ASSUMPTION THE EDITED DISCUSSION HAD AN ATTACHMENT PRIOR TO EDITING
      if (!val("MediaIDs",$PostVals) && val("RemoveMediaIDs",$PostVals) &&  val("UpdateUserID",$PostVals)) {
        $sender->Validation->addValidationResult('Attachment', 'An edited discussion must include an  Attachment');  
        }
    // new discussion
    if(!val("MediaIDs",$PostVals) &&  !val("UpdateUserID",$PostVals)) {
        $sender->Validation->addValidationResult('Attachment', 'A new discussion must include an  Attachment');
        }
    }
    

    use this event in a plugin if you want to do the same for comments.

    public function CommentModel_BeforeSaveComment_Handler($sender) {
       $PostVals = ($sender->EventArguments['FormPostValues']);
    
    // editing ASSUMPTION THE EDITED COMMENT HAD AN ATTACHMENT PRIOR TO EDITING
     if (!val("MediaIDs",$PostVals) && val("RemoveMediaIDs",$PostVals) &&  val("UpdateUserID",$PostVals)) {
        $sender->Validation->addValidationResult('Attachment', 'An edited comment must include an  Attachment');  
        }
    // new comment
    if(!val("MediaIDs",$PostVals) &&  !val("UpdateUserID",$PostVals)) {
        $sender->Validation->addValidationResult('Attachment', 'A new comment must include an  Attachment');
        }
    
    }
    

    Pragmatism is all I have to offer. Avoiding the sidelines and providing centerline pro-tips.

  • @River, thanks for the detailed response with code samples, that's exactly what I was looking for, to start with.

Sign In or Register to comment.