|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace FullscreenInteractive\SilverStripe\Forum\Form; |
| 4 | + |
| 5 | +use SilverStripe\Forms\CheckboxField; |
| 6 | +use SilverStripe\Forms\FieldList; |
| 7 | +use SilverStripe\Forms\Form; |
| 8 | +use SilverStripe\Forms\HiddenField; |
| 9 | +use SilverStripe\Forms\TextareaField; |
| 10 | +use SilverStripe\Forms\TextField; |
| 11 | +use SilverStripe\Forms\FileField; |
| 12 | +use SilverStripe\Forms\FormAction; |
| 13 | +use SilverStripe\Forms\LiteralField; |
| 14 | +use FullscreenInteractive\SilverStripe\Forum\Model\Post; |
| 15 | +use SilverStripe\Forms\Validation\RequiredFieldsValidator; |
| 16 | +use SilverStripe\Security\SecurityToken; |
| 17 | + |
| 18 | +class PostMessageForm extends Form |
| 19 | +{ |
| 20 | + protected ?Post $post = null; |
| 21 | + |
| 22 | + public function __construct($controller, $name) |
| 23 | + { |
| 24 | + $fields = FieldList::create([ |
| 25 | + TextField::create("Title", _t('Forum.FORUMTHREADTITLE', 'Title')), |
| 26 | + TextareaField::create("Content", _t('Forum.FORUMREPLYCONTENT', 'Content')), |
| 27 | + HiddenField::create('ThreadID', 'ThreadID'), |
| 28 | + HiddenField::create('ID', 'ID'), |
| 29 | + CheckboxField::create( |
| 30 | + "TopicSubscription", |
| 31 | + _t('Forum.SUBSCRIBETOPIC', 'Subscribe to this topic (Receive email notifications when a new reply is added)') |
| 32 | + ) |
| 33 | + ]); |
| 34 | + |
| 35 | + if ($controller->data()->canAttach()) { |
| 36 | + $fields->insertAfter("Content", FileField::create("Attachment", _t('Forum.ATTACH', 'Attach file'))); |
| 37 | + } |
| 38 | + |
| 39 | + $actions = FieldList::create([ |
| 40 | + FormAction::create("doPostMessageForm", _t('Forum.REPLYFORMPOST', 'Post')) |
| 41 | + ]); |
| 42 | + |
| 43 | + $required = RequiredFieldsValidator::create(["Title", "Content"]); |
| 44 | + |
| 45 | + parent::__construct($controller, $name, $fields, $actions, $required); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + public function setPost(Post $post) |
| 50 | + { |
| 51 | + $this->loadDataFrom($post); |
| 52 | + |
| 53 | + $this->post = $post; |
| 54 | + |
| 55 | + if (!$post->isFirstPost() || $post->ThreadID) { |
| 56 | + $this->fields->makeFieldReadonly('Title'); |
| 57 | + } |
| 58 | + |
| 59 | + if ($post->Attachments()->exists()) { |
| 60 | + $attachments = sprintf( |
| 61 | + "<div id=\"CurrentAttachments\"><h4>%s</h4><ul>", |
| 62 | + _t('Forum.CURRENTATTACHMENTS', 'Current Attachments') |
| 63 | + ); |
| 64 | + |
| 65 | + // An instance of the security token |
| 66 | + $token = SecurityToken::inst(); |
| 67 | + |
| 68 | + foreach ($post->Attachments() as $attachment) { |
| 69 | + // Generate a link properly, since it requires a security token |
| 70 | + $attachmentLink = $this->controller->Link('deleteattachment', $attachment->ID); |
| 71 | + $attachmentLink = $token->addToUrl($attachmentLink); |
| 72 | + |
| 73 | + $attachments .= sprintf( |
| 74 | + "<li class='attachment-%d'>%s [<a href='%s' rel='%d' class='deleteAttachment'>%s</a>]</li>", |
| 75 | + $attachment->ID, |
| 76 | + $attachment->Name, |
| 77 | + $attachmentLink, |
| 78 | + $attachment->ID, |
| 79 | + _t('Forum.REMOVE', 'remove') |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + $attachments .= "</ul></div>"; |
| 84 | + |
| 85 | + $this->fields->push(LiteralField::create('CurrentAttachments', $attachments)); |
| 86 | + } |
| 87 | + |
| 88 | + return $this; |
| 89 | + } |
| 90 | +} |
0 commit comments