InvitationModel::save PHP Method

save() public method

public save ( array $FormPostValues, array | boolean $UserModel, array $Options = [] ) : boolean
$FormPostValues array
$UserModel array | boolean
$Options array
return boolean
    public function save($FormPostValues, $UserModel, $Options = array())
    {
        $Session = Gdn::session();
        $UserID = $Session->UserID;
        $SendEmail = val('SendEmail', $Options, true);
        $Resend = val('Resend', $Options, false);
        // Define the primary key in this model's table.
        $this->defineSchema();
        // Add & apply any extra validation rules:
        $this->Validation->applyRule('Email', 'Email');
        // Make sure required db fields are present.
        $this->AddInsertFields($FormPostValues);
        if (!isset($FormPostValues['DateExpires'])) {
            $Expires = strtotime(c('Garden.Registration.InviteExpiration'));
            if ($Expires > time()) {
                $FormPostValues['DateExpires'] = Gdn_Format::toDateTime($Expires);
            }
        }
        $FormPostValues['Code'] = $this->GetInvitationCode();
        // Validate the form posted values
        if ($this->validate($FormPostValues, true) === true) {
            $Fields = $this->Validation->ValidationFields();
            // All fields on the form that need to be validated
            $Email = val('Email', $Fields, '');
            // Make sure this user has a spare invitation to send.
            $InviteCount = $UserModel->GetInvitationCount($UserID);
            if ($InviteCount == 0) {
                $this->Validation->addValidationResult('Email', 'You do not have enough invitations left.');
                return false;
            }
            // Make sure that the email does not already belong to an account in the application.
            $TestData = $UserModel->getWhere(array('Email' => $Email));
            if ($TestData->numRows() > 0) {
                $this->Validation->addValidationResult('Email', 'The email you have entered is already related to an existing account.');
                return false;
            }
            // Make sure that the email does not already belong to an invitation in the application.
            $TestData = $this->getWhere(array('Email' => $Email));
            $DeleteID = false;
            if ($TestData->numRows() > 0) {
                if (!$Resend) {
                    $this->Validation->addValidationResult('Email', 'An invitation has already been sent to the email you entered.');
                    return false;
                } else {
                    // Mark the old invitation for deletion.
                    $DeleteID = val('InvitationID', $TestData->firstRow(DATASET_TYPE_ARRAY));
                }
            }
            // Define the fields to be inserted
            $Fields = $this->Validation->SchemaValidationFields();
            // Call the base model for saving
            $InvitationID = $this->insert($Fields);
            // Delete an old invitation.
            if ($InvitationID && $DeleteID) {
                $this->delete($DeleteID);
            }
            // Now that saving has succeeded, update the user's invitation settings
            if ($InviteCount > 0) {
                $UserModel->ReduceInviteCount($UserID);
            }
            // And send the invitation email
            if ($SendEmail) {
                try {
                    $this->send($InvitationID);
                } catch (Exception $ex) {
                    $this->Validation->addValidationResult('Email', sprintf(t('Although the invitation was created successfully, the email failed to send. The server reported the following error: %s'), strip_tags($ex->getMessage())));
                    return false;
                }
            }
            return true;
        }
        return false;
    }

Usage Example

 public function createInvitationLink()
 {
     if (Efiwebsetting::getData('checkOAuth') == 'yes') {
         IMBAuth::checkOAuth();
     }
     $json = array();
     $json['status_code'] = 1;
     $id_user = Generic::mustCheck($_GET['id_user'], "NO ID User found!");
     $id_order = Generic::mustCheck($_GET['id_order'], "NO ID Order found!");
     // check apakah id_order dgn id_user msh aktiv
     $objOrder = new MasterOrderModel();
     $arrOrder = $objOrder->getWhere("id_order='{$id_order}' AND id_user = '******' AND status_payment = '0'");
     Generic::checkCountWithMsg($arrOrder, "Order is not activate");
     $objInvitation = new InvitationModel();
     $objInvitation->inv_id_resto = $arrOrder[0]->id_restaurant;
     $objInvitation->inv_id_order = $id_order;
     $objInvitation->inv_id_table = $arrOrder[0]->id_table;
     $objInvitation->inv_from = $id_user;
     $id = $objInvitation->save();
     if ($id != "") {
         $link = _BPATH . "Invitation/acceptInvitation?id_invitation={$id}";
         $json['status_message'] = addslashes($link);
     } else {
         $json['status_code'] = 0;
         $json['status_message'] = "Link can't create!";
     }
     echo json_encode($json);
     die;
 }