function testSubscription() { $thread = $this->objFromFixture("ForumThread", "Thread1"); $thread2 = $this->objFromFixture("ForumThread", "Thread2"); $member = $this->objFromFixture("Member", "test1"); $member2 = $this->objFromFixture("Member", "test2"); $this->assertTrue(ForumThread_Subscription::already_subscribed($thread->ID, $member->ID)); $this->assertTrue(ForumThread_Subscription::already_subscribed($thread->ID, $member2->ID)); $this->assertFalse(ForumThread_Subscription::already_subscribed($thread2->ID, $member->ID)); $this->assertFalse(ForumThread_Subscription::already_subscribed($thread2->ID, $member2->ID)); }
/** * Check to see if the user has subscribed to this thread * * @return bool */ function getHasSubscribed() { $member = Member::currentUser(); return $member ? ForumThread_Subscription::already_subscribed($this->ID, $member->ID) : false; }
/** * Post a message to the forum. This method is called whenever you want to make a * new post or edit an existing post on the forum * * @param Array - Data * @param Form - Submitted Form */ function doPostMessageForm($data, $form) { $member = Member::currentUser(); $content = isset($data['Content']) ? $this->filterLanguage($data["Content"]) : ""; $title = isset($data['Title']) ? $this->filterLanguage($data["Title"]) : false; // If a thread id is passed append the post to the thread. Otherwise create // a new thread $thread = false; if (isset($data['ThreadID'])) { $thread = DataObject::get_by_id('ForumThread', $data['ThreadID']); } // If this is a simple edit the post then handle it here. Look up the correct post, // make sure we have edit rights to it then update the post $post = false; if (isset($data['ID'])) { $post = DataObject::get_by_id('Post', $data['ID']); if ($post && $post->isFirstPost()) { if ($title) { $thread->Title = $title; } } } // Check permissions $messageSet = array('default' => _t('Forum.LOGINTOPOST', 'You\'ll need to login before you can post to that forum. Please do so below.'), 'alreadyLoggedIn' => _t('Forum.NOPOSTPERMISSION', 'I\'m sorry, but you do not have permission post to this forum.'), 'logInAgain' => _t('Forum.LOGINTOPOSTAGAIN', 'You have been logged out of the forums. If you would like to log in again to post, enter a username and password below.')); // Creating new thread if (!$thread && !$this->canPost()) { Security::permissionFailure($this, $messageSet); return false; } // Replying to existing thread if ($thread && !$post && !$thread->canPost()) { Security::permissionFailure($this, $messageSet); return false; } // Editing existing post if ($thread && $post && !$post->canEdit()) { Security::permissionFailure($this, $messageSet); return false; } if (!$thread) { $thread = new ForumThread(); $thread->ForumID = $this->ID; if ($title) { $thread->Title = $title; } $starting_thread = true; } // from now on the user has the correct permissions. save the current thread settings $thread->write(); if (!$post || !$post->canEdit()) { $post = new Post(); $post->AuthorID = $member ? $member->ID : 0; $post->ThreadID = $thread->ID; } $post->ForumID = $thread->ForumID; $post->Content = $content; $post->write(); // Upload and Save all files attached to the field // Attachment will always be blank, If they had an image it will be at least in Attachment-0 if (!empty($data['Attachment'])) { $id = 0; // // @todo this only supports ajax uploads. Needs to change the key (to simply Attachment). // while (isset($data['Attachment-' . $id])) { $image = $data['Attachment-' . $id]; if ($image) { // check to see if a file of same exists $title = Convert::raw2sql($image['name']); $file = DataObject::get_one("Post_Attachment", "\"Title\" = '{$title}' AND \"PostID\" = '{$post->ID}'"); if (!$file) { $file = new Post_Attachment(); $file->PostID = $post->ID; $file->OwnerID = Member::currentUserID(); $upload = new Upload(); $upload->loadIntoFile($image, $file); $file->write(); } } $id++; } } // Add a topic subscription entry if required if (isset($data['TopicSubscription'])) { if (!ForumThread_Subscription::already_subscribed($thread->ID)) { // Create a new topic subscription for this member $obj = new ForumThread_Subscription(); $obj->ThreadID = $thread->ID; $obj->MemberID = Member::currentUserID(); $obj->write(); } } else { // See if the member wanted to remove themselves if (ForumThread_Subscription::already_subscribed($post->TopicID)) { DB::query("DELETE FROM \"ForumThread_Subscription\" WHERE \"ThreadID\" = '{$post->ThreadID}' AND \"MemberID\" = '{$member->ID}'"); } } // Send any notifications that need to be sent ForumThread_Subscription::notify($post); // Send any notifications to moderators of the forum if (Forum::$notify_moderators) { if (isset($starting_thread) && $starting_thread) { $this->notifyModerators($post, $thread, true); } else { $this->notifyModerators($post, $thread); } } return $this->redirect($post->Link()); }
/** * Post a message to the forum. This method is called whenever you want to make a * new post or edit an existing post on the forum * * @param Array - Data * @param Form - Submitted Form */ function doPostMessageForm($data, $form) { $member = Member::currentUser(); $content = isset($data['Content']) ? $this->filterLanguage($data["Content"]) : ""; $title = isset($data['Title']) ? $this->filterLanguage($data["Title"]) : false; // If a thread id is passed append the post to the thread. Otherwise create // a new thread $thread = false; if (isset($data['ThreadID'])) { $thread = DataObject::get_by_id('ForumThread', $data['ThreadID']); } // If this is a simple edit the post then handle it here. Look up the correct post, // make sure we have edit rights to it then update the post $post = false; if (isset($data['ID'])) { $post = DataObject::get_by_id('Post', $data['ID']); if ($post && $post->isFirstPost()) { if ($title) { $thread->Title = $title; } } } // Check permissions $messageSet = array('default' => _t('Forum.LOGINTOPOST', 'You\'ll need to login before you can post to that forum. Please do so below.'), 'alreadyLoggedIn' => _t('Forum.NOPOSTPERMISSION', 'I\'m sorry, but you do not have permission post to this forum.'), 'logInAgain' => _t('Forum.LOGINTOPOSTAGAIN', 'You have been logged out of the forums. If you would like to log in again to post, enter a username and password below.')); // Creating new thread if (!$thread && !$this->canPost()) { Security::permissionFailure($this, $messageSet); return false; } // Replying to existing thread if ($thread && !$post && !$thread->canPost()) { Security::permissionFailure($this, $messageSet); return false; } // Editing existing post if ($thread && $post && !$post->canEdit()) { Security::permissionFailure($this, $messageSet); return false; } if (!$thread) { $thread = new ForumThread(); $thread->ForumID = $this->ID; if ($title) { $thread->Title = $title; } $starting_thread = true; } // Upload and Save all files attached to the field // Attachment will always be blank, If they had an image it will be at least in Attachment-0 //$attachments = new DataObjectSet(); $attachments = new ArrayList(); if (!empty($data['Attachment-0']) && !empty($data['Attachment-0']['tmp_name'])) { $id = 0; // // @todo this only supports ajax uploads. Needs to change the key (to simply Attachment). // while (isset($data['Attachment-' . $id])) { $image = $data['Attachment-' . $id]; if ($image && !empty($image['tmp_name'])) { $file = Post_Attachment::create(); $file->OwnerID = Member::currentUserID(); $folder = Config::inst()->get('ForumHolder', 'attachments_folder'); try { $upload = Upload::create()->loadIntoFile($image, $file, $folder); $file->write(); $attachments->push($file); } catch (ValidationException $e) { $message = _t('Forum.UPLOADVALIDATIONFAIL', 'Unallowed file uploaded. Please only upload files of the following: '); $message .= implode(', ', Config::inst()->get('File', 'allowed_extensions')); $form->addErrorMessage('Attachment', $message, 'bad'); Session::set("FormInfo.Form_PostMessageForm.data", $data); return $this->redirectBack(); } } $id++; } } // from now on the user has the correct permissions. save the current thread settings $thread->write(); if (!$post || !$post->canEdit()) { $post = new Post(); $post->AuthorID = $member ? $member->ID : 0; $post->ThreadID = $thread->ID; } $post->ForumID = $thread->ForumID; $post->Content = $content; $post->write(); if ($attachments) { foreach ($attachments as $attachment) { $attachment->PostID = $post->ID; $attachment->write(); } } // Add a topic subscription entry if required $isSubscribed = ForumThread_Subscription::already_subscribed($thread->ID); if (isset($data['TopicSubscription'])) { if (!$isSubscribed) { // Create a new topic subscription for this member $obj = new ForumThread_Subscription(); $obj->ThreadID = $thread->ID; $obj->MemberID = Member::currentUserID(); $obj->write(); } } elseif ($isSubscribed) { // See if the member wanted to remove themselves DB::prepared_query('DELETE FROM "ForumThread_Subscription" WHERE "ThreadID" = ? AND "MemberID" = ?', array($post->ThreadID, $member->ID)); } // Send any notifications that need to be sent ForumThread_Subscription::notify($post); // Send any notifications to moderators of the forum if (Forum::$notify_moderators) { if (isset($starting_thread) && $starting_thread) { $this->notifyModerators($post, $thread, true); } else { $this->notifyModerators($post, $thread); } } return $this->redirect($post->Link()); }