示例#1
0
 /**
  * Inserts or updates the discussion via form values.
  * 
  * Events: BeforeSaveDiscussion, AfterSaveDiscussion.
  *
  * @since 2.0.0
  * @access public
  * 
  * @param array $FormPostValues Data sent from the form model.
  * @return int $DiscussionID Unique ID of the discussion.
  */
 public function Save($FormPostValues)
 {
     $Session = Gdn::Session();
     // Define the primary key in this model's table.
     $this->DefineSchema();
     // Add & apply any extra validation rules:
     $this->Validation->ApplyRule('Body', 'Required');
     $this->Validation->AddRule('MeAction', 'function:ValidateMeAction');
     $this->Validation->ApplyRule('Body', 'MeAction');
     $MaxCommentLength = Gdn::Config('Vanilla.Comment.MaxLength');
     if (is_numeric($MaxCommentLength) && $MaxCommentLength > 0) {
         $this->Validation->SetSchemaProperty('Body', 'Length', $MaxCommentLength);
         $this->Validation->ApplyRule('Body', 'Length');
     }
     // Validate category permissions.
     $CategoryID = GetValue('CategoryID', $FormPostValues);
     if ($CategoryID > 0) {
         $Category = CategoryModel::Categories($CategoryID);
         if ($Category && !$Session->CheckPermission('Vanilla.Discussions.Add', TRUE, 'Category', GetValue('PermissionCategoryID', $Category))) {
             $this->Validation->AddValidationResult('CategoryID', 'You do not have permission to post in this category');
         }
     }
     // Get the DiscussionID from the form so we know if we are inserting or updating.
     $DiscussionID = ArrayValue('DiscussionID', $FormPostValues, '');
     // See if there is a source ID.
     if (GetValue('SourceID', $FormPostValues)) {
         $DiscussionID = $this->SQL->GetWhere('Discussion', ArrayTranslate($FormPostValues, array('Source', 'SourceID')))->Value('DiscussionID');
         if ($DiscussionID) {
             $FormPostValues['DiscussionID'] = $DiscussionID;
         }
     } elseif (GetValue('ForeignID', $FormPostValues)) {
         $DiscussionID = $this->SQL->GetWhere('Discussion', array('ForeignID' => $FormPostValues['ForeignID']))->Value('DiscussionID');
         if ($DiscussionID) {
             $FormPostValues['DiscussionID'] = $DiscussionID;
         }
     }
     $Insert = $DiscussionID == '' ? TRUE : FALSE;
     $this->EventArguments['Insert'] = $Insert;
     if ($Insert) {
         unset($FormPostValues['DiscussionID']);
         // If no categoryid is defined, grab the first available.
         if (!GetValue('CategoryID', $FormPostValues) && !C('Vanilla.Categories.Use')) {
             $FormPostValues['CategoryID'] = GetValue('CategoryID', CategoryModel::DefaultCategory(), -1);
         }
         $this->AddInsertFields($FormPostValues);
         // The UpdateUserID used to be required. Just add it if it still is.
         if (!$this->Schema->GetProperty('UpdateUserID', 'AllowNull', TRUE)) {
             $FormPostValues['UpdateUserID'] = $FormPostValues['InsertUserID'];
         }
         // $FormPostValues['LastCommentUserID'] = $Session->UserID;
         $FormPostValues['DateLastComment'] = $FormPostValues['DateInserted'];
     } else {
         // Add the update fields.
         $this->AddUpdateFields($FormPostValues);
     }
     // Set checkbox values to zero if they were unchecked
     if (ArrayValue('Announce', $FormPostValues, '') === FALSE) {
         $FormPostValues['Announce'] = 0;
     }
     if (ArrayValue('Closed', $FormPostValues, '') === FALSE) {
         $FormPostValues['Closed'] = 0;
     }
     if (ArrayValue('Sink', $FormPostValues, '') === FALSE) {
         $FormPostValues['Sink'] = 0;
     }
     //	Prep and fire event
     $this->EventArguments['FormPostValues'] =& $FormPostValues;
     $this->EventArguments['DiscussionID'] = $DiscussionID;
     $this->FireEvent('BeforeSaveDiscussion');
     // Validate the form posted values
     $this->Validate($FormPostValues, $Insert);
     $ValidationResults = $this->ValidationResults();
     // If the body is not required, remove it's validation errors.
     $BodyRequired = C('Vanilla.DiscussionBody.Required', TRUE);
     if (!$BodyRequired && array_key_exists('Body', $ValidationResults)) {
         unset($ValidationResults['Body']);
     }
     if (count($ValidationResults) == 0) {
         // If the post is new and it validates, make sure the user isn't spamming
         if (!$Insert || !$this->CheckForSpam('Discussion')) {
             // Get all fields on the form that relate to the schema
             $Fields = $this->Validation->SchemaValidationFields();
             // Get DiscussionID if one was sent
             $DiscussionID = intval(ArrayValue('DiscussionID', $Fields, 0));
             // Remove the primary key from the fields for saving
             $Fields = RemoveKeyFromArray($Fields, 'DiscussionID');
             $StoredCategoryID = FALSE;
             if ($DiscussionID > 0) {
                 // Updating
                 $Stored = $this->GetID($DiscussionID, DATASET_TYPE_ARRAY);
                 // Clear the cache if necessary.
                 if (GetValue('Announce', $Stored) != GetValue('Announce', $Fields)) {
                     $CacheKeys = array('Announcements');
                     $this->SQL->Cache($CacheKeys);
                 }
                 self::SerializeRow($Fields);
                 $this->SQL->Put($this->Name, $Fields, array($this->PrimaryKey => $DiscussionID));
                 SetValue('DiscussionID', $Fields, $DiscussionID);
                 LogModel::LogChange('Edit', 'Discussion', (array) $Fields, $Stored);
                 if (GetValue('CategoryID', $Stored) != GetValue('CategoryID', $Fields)) {
                     $StoredCategoryID = GetValue('CategoryID', $Stored);
                 }
             } else {
                 // Inserting.
                 if (!GetValue('Format', $Fields) || C('Garden.ForceInputFormatter')) {
                     $Fields['Format'] = C('Garden.InputFormatter', '');
                 }
                 if (C('Vanilla.QueueNotifications')) {
                     $Fields['Notified'] = ActivityModel::SENT_PENDING;
                 }
                 // Check for spam.
                 $Spam = SpamModel::IsSpam('Discussion', $Fields);
                 if ($Spam) {
                     return SPAM;
                 }
                 // Check for approval
                 $ApprovalRequired = CheckRestriction('Vanilla.Approval.Require');
                 if ($ApprovalRequired && !GetValue('Verified', Gdn::Session()->User)) {
                     LogModel::Insert('Pending', 'Discussion', $Fields);
                     return UNAPPROVED;
                 }
                 // Create discussion
                 $DiscussionID = $this->SQL->Insert($this->Name, $Fields);
                 $Fields['DiscussionID'] = $DiscussionID;
                 // Update the cache.
                 if ($DiscussionID && Gdn::Cache()->ActiveEnabled()) {
                     $CategoryCache = array('LastDiscussionID' => $DiscussionID, 'LastCommentID' => NULL, 'LastTitle' => Gdn_Format::Text($Fields['Name']), 'LastUserID' => $Fields['InsertUserID'], 'LastDateInserted' => $Fields['DateInserted'], 'LastUrl' => DiscussionUrl($Fields));
                     CategoryModel::SetCache($Fields['CategoryID'], $CategoryCache);
                     // Clear the cache if necessary.
                     if (GetValue('Announce', $Fields)) {
                         Gdn::Cache()->Remove('Announcements');
                     }
                 }
                 // Update the user's discussion count.
                 $this->UpdateUserDiscussionCount(Gdn::Session()->UserID);
                 // Assign the new DiscussionID to the comment before saving.
                 $FormPostValues['IsNewDiscussion'] = TRUE;
                 $FormPostValues['DiscussionID'] = $DiscussionID;
                 // Do data prep.
                 $DiscussionName = ArrayValue('Name', $Fields, '');
                 $Story = ArrayValue('Body', $Fields, '');
                 $NotifiedUsers = array();
                 $UserModel = Gdn::UserModel();
                 $ActivityModel = new ActivityModel();
                 if (GetValue('Type', $FormPostValues)) {
                     $Code = 'HeadlineFormat.Discussion.' . $FormPostValues['Type'];
                 } else {
                     $Code = 'HeadlineFormat.Discussion';
                 }
                 $HeadlineFormat = T($Code, '{ActivityUserID,user} started a new discussion: <a href="{Url,html}">{Data.Name,text}</a>');
                 $Category = CategoryModel::Categories(GetValue('CategoryID', $Fields));
                 $Activity = array('ActivityType' => 'Discussion', 'ActivityUserID' => $Fields['InsertUserID'], 'HeadlineFormat' => $HeadlineFormat, 'RecordType' => 'Discussion', 'RecordID' => $DiscussionID, 'Route' => DiscussionUrl($Fields), 'Data' => array('Name' => $DiscussionName, 'Category' => GetValue('Name', $Category)));
                 // Allow simple fulltext notifications
                 if (C('Vanilla.Activity.ShowDiscussionBody', FALSE)) {
                     $Activity['Story'] = $Story;
                 }
                 // Notify all of the users that were mentioned in the discussion.
                 $Usernames = array_merge(GetMentions($DiscussionName), GetMentions($Story));
                 $Usernames = array_unique($Usernames);
                 // Use our generic Activity for events, not mentions
                 $this->EventArguments['Activity'] = $Activity;
                 // Notifications for mentions
                 foreach ($Usernames as $Username) {
                     $User = $UserModel->GetByUsername($Username);
                     if (!$User) {
                         continue;
                     }
                     // Check user can still see the discussion.
                     if (!$UserModel->GetCategoryViewPermission($User->UserID, GetValue('CategoryID', $Fields))) {
                         continue;
                     }
                     $Activity['HeadlineFormat'] = T('HeadlineFormat.Mention', '{ActivityUserID,user} mentioned you in <a href="{Url,html}">{Data.Name,text}</a>');
                     $Activity['NotifyUserID'] = GetValue('UserID', $User);
                     $ActivityModel->Queue($Activity, 'Mention');
                 }
                 // Notify everyone that has advanced notifications.
                 if (!C('Vanilla.QueueNotifications')) {
                     try {
                         $Fields['DiscussionID'] = $DiscussionID;
                         $this->NotifyNewDiscussion($Fields, $ActivityModel, $Activity);
                     } catch (Exception $Ex) {
                         throw $Ex;
                     }
                 }
                 // Throw an event for users to add their own events.
                 $this->EventArguments['Discussion'] = $Fields;
                 $this->EventArguments['NotifiedUsers'] = $NotifiedUsers;
                 $this->EventArguments['MentionedUsers'] = $Usernames;
                 $this->EventArguments['ActivityModel'] = $ActivityModel;
                 $this->FireEvent('BeforeNotification');
                 // Send all notifications.
                 $ActivityModel->SaveQueue();
             }
             // Get CategoryID of this discussion
             $Discussion = $this->GetID($DiscussionID, DATASET_TYPE_ARRAY);
             $CategoryID = GetValue('CategoryID', $Discussion, FALSE);
             // Update discussion counter for affected categories
             $this->UpdateDiscussionCount($CategoryID, $Insert ? $Discussion : FALSE);
             if ($StoredCategoryID) {
                 $this->UpdateDiscussionCount($StoredCategoryID);
             }
             // Fire an event that the discussion was saved.
             $this->EventArguments['FormPostValues'] = $FormPostValues;
             $this->EventArguments['Fields'] = $Fields;
             $this->EventArguments['DiscussionID'] = $DiscussionID;
             $this->FireEvent('AfterSaveDiscussion');
         }
     }
     return $DiscussionID;
 }
示例#2
0
文件: stub.php 项目: sitexa/vanilla
 * @copyright 2009-2016 Vanilla Forums Inc.
 * @license http://www.opensource.org/licenses/gpl-2.0.php GNU GPL v2
 * @since 2.0
 * @package Vanilla
 */
$SQL = Gdn::database()->sql();
// Only do this once, ever.
$Row = $SQL->get('Discussion', '', 'asc', 1)->firstRow(DATASET_TYPE_ARRAY);
if ($Row) {
    return;
}
$DiscussionModel = new DiscussionModel();
// Prep default content
$DiscussionTitle = "BAM! You&rsquo;ve got a sweet forum";
$DiscussionBody = "There&rsquo;s nothing sweeter than a fresh new forum, ready to welcome your community. A Vanilla Forum has all the bits and pieces you need to build an awesome discussion platform customized to your needs. Here&rsquo;s a few tips:\n<ul>\n   <li>Use the <a href=\"/dashboard/settings/gettingstarted\">Getting Started</a> list in the Dashboard to configure your site.</li>\n   <li>Don&rsquo;t use too many categories. We recommend 3-8. Keep it simple!</li>\n   <li>&ldquo;Announce&rdquo; a discussion (click the gear) to stick to the top of the list, and &ldquo;Close&rdquo; it to stop further comments.</li>\n   <li>Use &ldquo;Sink&rdquo; to take attention away from a discussion. New comments will no longer bring it back to the top of the list.</li>\n   <li>Bookmark a discussion (click the star) to get notifications for new comments. You can edit notification settings from your profile.</li>\n</ul>\nGo ahead and edit or delete this discussion, then spread the word to get this place cooking. Cheers!";
$CommentBody = "This is the first comment on your site and it&rsquo;s an important one.\n\nDon&rsquo;t see your must-have feature? We keep Vanilla nice and simple by default. Use <b>addons</b> to get the special sauce your community needs.\n\nNot sure which addons to enable? Our favorites are Button Bar and Tagging. They&rsquo;re almost always a great start.";
$WallBody = "Ping! An activity post is a public way to talk at someone. When you update your status here, it posts it on your activity feed.";
// Prep content meta data
$SystemUserID = Gdn::userModel()->GetSystemUserID();
$TargetUserID = Gdn::session()->UserID;
$Now = Gdn_Format::toDateTime();
$CategoryID = val('CategoryID', CategoryModel::DefaultCategory());
// Get wall post type ID
$WallCommentTypeID = $SQL->getWhere('ActivityType', array('Name' => 'WallPost'))->value('ActivityTypeID');
// Insert first discussion & comment
$DiscussionID = $SQL->Options('Ignore', true)->insert('Discussion', array('Name' => t('StubDiscussionTitle', $DiscussionTitle), 'Body' => t('StubDiscussionBody', $DiscussionBody), 'Format' => 'Html', 'CategoryID' => $CategoryID, 'ForeignID' => 'stub', 'InsertUserID' => $SystemUserID, 'DateInserted' => $Now, 'DateLastComment' => $Now, 'LastCommentUserID' => $SystemUserID, 'CountComments' => 1));
$CommentID = $SQL->insert('Comment', array('DiscussionID' => $DiscussionID, 'Body' => t('StubCommentBody', $CommentBody), 'Format' => 'Html', 'InsertUserID' => $SystemUserID, 'DateInserted' => $Now));
$SQL->update('Discussion')->set('LastCommentID', $CommentID)->where('DiscussionID', $DiscussionID)->put();
$DiscussionModel->UpdateDiscussionCount($CategoryID);
// Insert first wall post
$SQL->insert('Activity', array('Story' => t('StubWallBody', $WallBody), 'Format' => 'Html', 'HeadlineFormat' => '{RegardingUserID,you} &rarr; {ActivityUserID,you}', 'NotifyUserID' => -1, 'ActivityUserID' => $TargetUserID, 'RegardingUserID' => $SystemUserID, 'ActivityTypeID' => $WallCommentTypeID, 'InsertUserID' => $SystemUserID, 'DateInserted' => $Now, 'DateUpdated' => $Now));
示例#3
0
文件: stub.php 项目: bishopb/vanilla
 * Vanilla stub content for a new forum.
 *
 * Called by VanillaHooks::Setup() to insert stub content upon enabling app.
 * @package Vanilla
 */
// Only do this once, ever.
$DiscussionModel = new DiscussionModel();
if ($DiscussionModel->GetCount()) {
    return;
}
$SQL = Gdn::Database()->SQL();
$DiscussionModel = new DiscussionModel();
// Prep default content
$DiscussionTitle = "BAM! You&rsquo;ve got a sweet forum";
$DiscussionBody = "There&rsquo;s nothing sweeter than a fresh new forum, ready to welcome your community. A Vanilla Forum has all the bits and pieces you need to build an awesome discussion platform customized to your needs. Here&rsquo;s a few tips:\n<ul>\n   <li>Use the <a href=\"/dashboard/settings/gettingstarted\">Getting Started</a> list in the Dashboard to configure your site.</li>\n   <li>Don&rsquo;t use too many categories. We recommend 3-8. Keep it simple!</li>\n   <li>&ldquo;Announce&rdquo; a discussion (click the gear) to stick to the top of the list, and &ldquo;Close&rdquo; it to stop further comments.</li>\n   <li>Use &ldquo;Sink&rdquo; to take attention away from a discussion. New comments will no longer bring it back to the top of the list.</li>\n   <li>Bookmark a discussion (click the star) to get notifications for new comments. You can edit notification settings from your profile.</li>\n</ul>\nGo ahead and edit or delete this discussion, then spread the word to get this place cooking. Cheers!";
$CommentBody = "This is the first comment on your site and it&rsquo;s an important one. \n\nDon&rsquo;t see your must-have feature? We keep Vanilla nice and simple by default. Use <b>addons</b> to get the special sauce your community needs.\n\nNot sure which addons to enable? Our favorites are Button Bar and Tagging. They&rsquo;re almost always a great start.";
$WallBody = "Ping! An activity post is a public way to talk at someone. When you update your status here, it posts it on your activity feed.";
// Prep content meta data
$SystemUserID = Gdn::UserModel()->GetSystemUserID();
$TargetUserID = Gdn::Session()->UserID;
$Now = Gdn_Format::ToDateTime();
$CategoryID = GetValue('CategoryID', CategoryModel::DefaultCategory());
// Get wall post type ID
$WallCommentTypeID = $SQL->GetWhere('ActivityType', array('Name' => 'WallPost'))->Value('ActivityTypeID');
// Insert first discussion & comment
$DiscussionID = $SQL->Insert('Discussion', array('Name' => T('StubDiscussionTitle', $DiscussionTitle), 'Body' => T('StubDiscussionBody', $DiscussionBody), 'Format' => 'Html', 'CategoryID' => $CategoryID, 'ForeignID' => 'stub', 'InsertUserID' => $SystemUserID, 'DateInserted' => $Now, 'DateLastComment' => $Now, 'LastCommentUserID' => $SystemUserID, 'CountComments' => 1));
$CommentID = $SQL->Insert('Comment', array('DiscussionID' => $DiscussionID, 'Body' => T('StubCommentBody', $CommentBody), 'Format' => 'Html', 'InsertUserID' => $SystemUserID, 'DateInserted' => $Now));
$SQL->Update('Discussion')->Set('LastCommentID', $CommentID)->Where('DiscussionID', $DiscussionID)->Put();
$DiscussionModel->UpdateDiscussionCount($CategoryID);
// Insert first wall post
$SQL->Insert('Activity', array('Story' => T('StubWallBody', $WallBody), 'Format' => 'Html', 'HeadlineFormat' => '{RegardingUserID,you} &rarr; {ActivityUserID,you}', 'NotifyUserID' => -1, 'ActivityUserID' => $TargetUserID, 'RegardingUserID' => $SystemUserID, 'ActivityTypeID' => $WallCommentTypeID, 'InsertUserID' => $SystemUserID, 'DateInserted' => $Now, 'DateUpdated' => $Now));