function run($request) { set_time_limit(0); // check to see if this has been run before. If it has then we will have already // have removed the parentID field $checkForMigration = DB::query("SHOW COLUMNS FROM \"Post\"")->column(); if (!in_array('ParentID', $checkForMigration)) { echo "Script has already ran. You can only run the migration script once.\n"; return false; } // go through all the posts with a parent ID = 0 and create the new thread objects $oldThreads = DB::query("SELECT * FROM \"Post\" WHERE \"ParentID\" = '0'"); if ($oldThreads) { $toCreateTotal = $oldThreads->numRecords(); echo "Creating " . $toCreateTotal . " new Forum Threads \n"; $holder = DataObject::get_one("ForumHolder"); if (!$holder) { return user_error('No Forum Holder Found', E_USER_ERROR); } $failbackForum = new Forum(); $failbackForum->Title = "Unimported Threads"; $failbackForum->ParentID = $holder->ID; $failbackForum->write(); $needsFailback = false; $totalThreadsSuccessfulCount = 0; $totalThreadsErroredCount = 0; while ($oldThread = $oldThreads->nextRecord()) { $hasError = false; $thread = new ForumThread(); if (isset($oldThread['Title'])) { $thread->Title = $oldThread['Title']; } else { $hasError = true; $thread->Title = "Question"; } $thread->NumViews = isset($oldThread['NumViews']) ? $oldThread['NumViews'] : 0; $thread->IsSticky = isset($oldThread['IsSticky']) ? $oldThread['IsSticky'] : false; $thread->IsReadOnly = isset($oldThread['IsReadOnly']) ? $oldThread['IsReadOnly'] : false; $thread->IsGlobalSticky = isset($oldThread['IsGlobalSticky']) ? $oldThread['IsGlobalSticky'] : false; if (isset($oldThread['ForumID'])) { $thread->ForumID = $oldThread['ForumID']; } else { $hasError = true; $needsFailback = true; $thread->ForumID = $failbackForum->ID; } $thread->write(); echo "Converted Thread: {$thread->ID} - {$thread->Title}. \n"; // update all the children DB::query("\n\t\t\t\t\tUPDATE \"Post\" \n\t\t\t\t\tSET \"ThreadID\" = '{$thread->ID}', \"ForumID\" = '{$thread->ForumID}'\n\t\t\t\t\tWHERE \"TopicID\" = '" . $oldThread['ID'] . "'\n\t\t\t\t"); if (!$hasError) { $totalThreadsSuccessfulCount++; } else { $totalThreadsErroredCount++; } } } echo "Converted {$totalThreadsSuccessfulCount} threads. Could not import {$totalThreadsErroredCount} threads.<br />"; if (!$needsFailback) { $failbackForum->delete(); } else { echo "Incorrectly imported threads are available to self moderate at <a href='" . $failbackForum->Link() . "'>here</a><br />"; } // transfer subscriptions // was a rename table but mysql had locking issues. $subscriptions = DB::query("SELECT * FROM \"Post_Subscription\""); $subCount = 0; if ($subscriptions) { while ($sub = $subscriptions->nextRecord()) { // don't import really odd data if (isset($sub['TopicID']) && isset($sub['MemberID'])) { $subCount++; $threadSub = new ForumThread_Subscription(); $threadSub->ThreadID = $sub['TopicID']; $threadSub->MemberID = $sub['MemberID']; $threadSub->write(); } } } echo "Transferred {$subCount} Thread Subscriptions<br />"; // Update the permissions on the forums. The Posters, Viewers have changed from a int field // to an actual modelled relationship $forums = DataObject::get('Forum'); if ($forums) { foreach ($forums as $forum) { $forum->ForumPostersGroupID = DB::query("SELECT \"ForumPostersGroup\" FROM \"Forum\" WHERE \"ID\" = '{$forum->ID}'")->value(); if ($viewingGroup = DB::query("SELECT \"ForumViewersGroup\" FROM \"Forum\" WHERE \"ID\" = '{$forum->ID}'")->value()) { $forum->ViewerGroups()->add(DataObject::get_by_id('Group', $viewingGroup)); } $forum->write(); } } // cleanup task. Delete old columns which are hanging round DB::dontRequireField('Post', 'ParentID'); DB::dontRequireField('Post', 'TopicID'); DB::dontRequireField('Post', 'Title'); DB::dontRequireField('Post', 'NumViews'); DB::dontRequireField('Post', 'IsSticky'); DB::dontRequireField('Post', 'IsReadOnly'); DB::dontRequireField('Post', 'IsGlobalSticky'); DB::dontRequireTable('Post_Subscription'); DB::dontRequireField('Forum', 'ForumViewersGroup'); DB::dontRequireField('Forum', 'ForumViewersGroupID'); DB::dontRequireField("Forum", 'ForumPostersGroup'); echo "Renamed old data columns in Post and removed Post_Subscription table <br />"; $this->attachLastPostIDs(); echo "Set ForumThread last post and update LastEdited to the most recent post <br />"; echo "Finished<br />"; }
/** * 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()); }