/**
  * Saves an application.
  */
 public function actionSave()
 {
     // require post request
     $this->requirePostRequest();
     // set the applicationId from POST
     $applicationId = craft()->request->getPost('applicationId');
     // if applicationId, assume we are editing and get the application by id
     if ($applicationId) {
         // grab the application by the id
         $application = craft()->applications->getApplicationById($applicationId);
         // if application id does not exist
         if (!$application) {
             // throw new exception that the application does not exist
             throw new Exception(Craft::t('No application exists with the ID “{id}”', array('id' => $applicationId)));
         }
     } else {
         // setup a new application
         $application = new Applications_ApplicationModel();
     }
     // set the application attributes from POST
     $application->formId = craft()->request->getPost('formId', $application->formId);
     $application->firstName = craft()->request->getPost('firstName');
     $application->lastName = craft()->request->getPost('lastName');
     $application->email = craft()->request->getPost('email');
     $application->phone = craft()->request->getPost('phone');
     // set the content from POST
     $application->setContentFromPost('fields');
     // if we could save the application
     if (craft()->applications->save($application)) {
         // notify the user that the application saved
         craft()->userSession->setNotice(Craft::t('Application saved.'));
         $this->redirectToPostedUrl($application);
     } else {
         // notify the user the application did NOT save
         craft()->userSession->setError(Craft::t('Couldn’t save application.'));
         // send the user back to the edit template
         craft()->urlManager->setRouteVariables(array('application' => $application));
     }
 }