/**
  * Save an application
  *
  * @param Applications_ApplicationModel $application
  * @return bool
  * @throws Exception
  * @throws \CDbException
  * @throws \Exception
  */
 public function save(Applications_ApplicationModel $application)
 {
     $isNewApplication = !$application->id;
     // Application data
     if (!$isNewApplication) {
         $applicationRecord = Applications_ApplicationRecord::model()->findById($application->id);
         if (!$applicationRecord) {
             throw new Exception(Craft::t('No application exists with the ID “{id}”', array('id' => $application->id)));
         }
     } else {
         $applicationRecord = new Applications_ApplicationRecord();
     }
     $applicationRecord->formId = $application->formId;
     $applicationRecord->firstName = $application->firstName;
     $applicationRecord->lastName = $application->lastName;
     $applicationRecord->email = $application->email;
     $applicationRecord->phone = $application->phone;
     $applicationRecord->validate();
     $application->addErrors($applicationRecord->getErrors());
     if (!$application->hasErrors()) {
         $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
         try {
             // Fire an 'onBeforeSaveApplication' event
             $this->onBeforeSave(new Event($this, array('application' => $application, 'isNewApplication' => $isNewApplication)));
             if (craft()->elements->saveElement($application)) {
                 // Now that we have an element ID, save it on the other stuff
                 if ($isNewApplication) {
                     $applicationRecord->id = $application->id;
                 }
                 $applicationRecord->save(false);
                 // Fire an 'onSaveEvent' event
                 $this->onSave(new Event($this, array('application' => $application, 'isNewApplication' => $isNewApplication)));
                 if ($transaction !== null) {
                     $transaction->commit();
                 }
                 return true;
             }
         } catch (\Exception $e) {
             if ($transaction !== null) {
                 $transaction->rollback();
             }
             throw $e;
         }
     }
     return false;
 }
 /**
  * Populates an element model based on a query result.
  *
  * @param array $row
  * @return array
  */
 public function populateElementModel($row)
 {
     return Applications_ApplicationModel::populateModel($row);
 }
 /**
  * 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));
     }
 }