applications/vanilla/models/class.draftmodel.php
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');
      $MaxCommentLength = Gdn::Config('Vanilla.Comment.MaxLength');
      if (is_numeric($MaxCommentLength) && $MaxCommentLength > 0) {
         $this->Validation->SetSchemaProperty('Body', 'Length', $MaxCommentLength);
         $this->Validation->ApplyRule('Body', 'Length');
      }
      
      // Get the DraftID from the form so we know if we are inserting or updating.
      $DraftID = ArrayValue('DraftID', $FormPostValues, '');
      $Insert = $DraftID == '' ? TRUE : FALSE;
      
      // Remove the discussionid from the form value collection if it's empty
      if (array_key_exists('DiscussionID', $FormPostValues) && $FormPostValues['DiscussionID'] == '')
         unset($FormPostValues['DiscussionID']);
      
      if ($Insert) {
         // If no categoryid is defined, grab the first available.
         if (ArrayValue('CategoryID', $FormPostValues) === FALSE)
            $FormPostValues['CategoryID'] = $this->SQL->Get('Category', '', '', 1)->FirstRow()->CategoryID;
            
      }
      // Add the update fields because this table's default sort is by DateUpdated (see $this->Get()).
      $this->AddInsertFields($FormPostValues);
      $this->AddUpdateFields($FormPostValues);
      
      // Remove checkboxes from the fields if they were unchecked
      if (ArrayValue('Announce', $FormPostValues, '') === FALSE)
         unset($FormPostValues['Announce']);

      if (ArrayValue('Closed', $FormPostValues, '') === FALSE)
         unset($FormPostValues['Closed']);

      if (ArrayValue('Sink', $FormPostValues, '') === FALSE)
         unset($FormPostValues['Sink']);
         
      // Validate the form posted values
      if ($this->Validate($FormPostValues, $Insert)) {
         $Fields = $this->Validation->SchemaValidationFields(); // All fields on the form that relate to the schema
         $DraftID = intval(ArrayValue('DraftID', $Fields, 0));
         
         // If the post is new and it validates, make sure the user isn't spamming
         if ($DraftID > 0) {
            // Update the draft
            $Fields = RemoveKeyFromArray($Fields, 'DraftID'); // Remove the primary key from the fields for saving
            $this->SQL->Put($this->Name, $Fields, array($this->PrimaryKey => $DraftID));
         } else {
            // Insert the draft
            unset($Fields['DraftID']);
            $DraftID = $this->SQL->Insert($this->Name, $Fields);
            $this->UpdateUser($Session->UserID);
         }
      }
      
      return $DraftID;
   }
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');
      $MaxCommentLength = Gdn::Config('Vanilla.Comment.MaxLength');
      if (is_numeric($MaxCommentLength) && $MaxCommentLength > 0) {
         $this->Validation->SetSchemaProperty('Body', 'Length', $MaxCommentLength);
         $this->Validation->ApplyRule('Body', 'Length');
      }
      
      // Get the DraftID from the form so we know if we are inserting or updating.
      $DraftID = ArrayValue('DraftID', $FormPostValues, '');
      $Insert = $DraftID == '' ? TRUE : FALSE;
      
      // Remove the discussionid from the form value collection if it's empty
      if (array_key_exists('DiscussionID', $FormPostValues) && $FormPostValues['DiscussionID'] == '')
         unset($FormPostValues['DiscussionID']);
      
      if ($Insert) {
         // If no categoryid is defined, grab the first available.
         if (ArrayValue('CategoryID', $FormPostValues) === FALSE)
            $FormPostValues['CategoryID'] = $this->SQL->Get('Category', '', '', 1)->FirstRow()->CategoryID;
            
      }
      // Add the update fields because this table's default sort is by DateUpdated (see $this->Get()).
      $this->AddInsertFields($FormPostValues);
      $this->AddUpdateFields($FormPostValues);
      
      // Remove checkboxes from the fields if they were unchecked
      if (ArrayValue('Announce', $FormPostValues, '') === FALSE)
         unset($FormPostValues['Announce']);

      if (ArrayValue('Closed', $FormPostValues, '') === FALSE)
         unset($FormPostValues['Closed']);

      if (ArrayValue('Sink', $FormPostValues, '') === FALSE)
         unset($FormPostValues['Sink']);
         
      // Validate the form posted values
      if ($this->Validate($FormPostValues, $Insert)) {
         $Fields = $this->Validation->SchemaValidationFields(); // All fields on the form that relate to the schema
         $DraftID = intval(ArrayValue('DraftID', $Fields, 0));
         
         // If the post is new and it validates, make sure the user isn't spamming
         if ($DraftID > 0) {
            // Update the draft
            $Fields = RemoveKeyFromArray($Fields, 'DraftID'); // Remove the primary key from the fields for saving
            $this->SQL->Put($this->Name, $Fields, array($this->PrimaryKey => $DraftID));
         } else {
            // Insert the draft
            unset($Fields['DraftID']);
            $DraftID = $this->SQL->Insert($this->Name, $Fields);
            $this->UpdateUser($Session->UserID);
         }
      }
      
      return $DraftID;
   }
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' ) ; $ MaxCommentLength = Gdn :: Config ( 'Vanilla.Comment.MaxLength' ) ; if ( is_numeric ( $ MaxCommentLength ) && $ MaxCommentLength > 0 ) { $ this -> Validation -> SetSchemaProperty ( 'Body' , 'Length' , $ MaxCommentLength ) ; $ this -> Validation -> ApplyRule ( 'Body' , 'Length' ) ; } // Get the DraftID from the form so we know if we are inserting or updating. $ DraftID = ArrayValue ( 'DraftID' , $ FormPostValues , '' ) ; $ Insert = $ DraftID == '' ? TRUE : FALSE ; // Remove the discussionid from the form value collection if it's empty if ( array_key_exists ( 'DiscussionID' , $ FormPostValues ) && $ FormPostValues [ 'DiscussionID' ] == '' ) unset ( $ FormPostValues [ 'DiscussionID' ] ) ; if ( $ Insert ) { // If no categoryid is defined, grab the first available. if ( ArrayValue ( 'CategoryID' , $ FormPostValues ) === FALSE ) $ FormPostValues [ 'CategoryID' ] = $ this -> SQL -> Get ( 'Category' , '' , '' , 1 ) -> FirstRow ( ) -> CategoryID ; } // Add the update fields because this table's default sort is by DateUpdated (see $this->Get()). $ this -> AddInsertFields ( $ FormPostValues ) ; $ this -> AddUpdateFields ( $ FormPostValues ) ; // Remove checkboxes from the fields if they were unchecked if ( ArrayValue ( 'Announce' , $ FormPostValues , '' ) === FALSE ) unset ( $ FormPostValues [ 'Announce' ] ) ; if ( ArrayValue ( 'Closed' , $ FormPostValues , '' ) === FALSE ) unset ( $ FormPostValues [ 'Closed' ] ) ; if ( ArrayValue ( 'Sink' , $ FormPostValues , '' ) === FALSE ) unset ( $ FormPostValues [ 'Sink' ] ) ; // Validate the form posted values if ( $ this -> Validate ( $ FormPostValues , $ Insert ) ) { $ Fields = $ this -> Validation -> SchemaValidationFields ( ) ; // All fields on the form that relate to the schema $ DraftID = intval ( ArrayValue ( 'DraftID' , $ Fields , 0 ) ) ; // If the post is new and it validates, make sure the user isn't spamming if ( $ DraftID > 0 ) { // Update the draft $ Fields = RemoveKeyFromArray ( $ Fields , 'DraftID' ) ; // Remove the primary key from the fields for saving $ this -> SQL -> Put ( $ this -> Name , $ Fields , array ( $ this -> PrimaryKey => $ DraftID ) ) ; } else { // Insert the draft unset ( $ Fields [ 'DraftID' ] ) ; $ DraftID = $ this -> SQL -> Insert ( $ this -> Name , $ Fields ) ; $ this -> UpdateUser ( $ Session -> UserID ) ; } } return $ DraftID ; }
Insert or update a draft from form values.

@since 2.0.0
@access public

@param array $FormPostValues Form values sent from form model.
@return int Unique ID of draft.
Insert or update a draft from form values .