/**
  * Populate Element Model
  *
  */
 public function populateElementModel($row, $normalize = false)
 {
     $entry = FormBuilder2_EntryModel::populateModel($row);
     if ($normalize) {
         $entry = $entry->_normalizeDataForElementsTable();
     }
     return $entry;
 }
 /**
  * Process Submission Entry
  *
  */
 public function processSubmissionEntry(FormBuilder2_EntryModel $submission)
 {
     // Fire Before Save Event
     $this->onBeforeSave(new Event($this, array('entry' => $submission)));
     $form = craft()->formBuilder2_form->getFormById($submission->formId);
     $formFields = $form->fieldLayout->getFieldLayout()->getFields();
     $attributes = $form->getAttributes();
     $formSettings = $attributes['formSettings'];
     $submissionRecord = new FormBuilder2_EntryRecord();
     // File Uploads
     if ($submission->files) {
         $fileIds = [];
         foreach ($submission->files as $key => $value) {
             if ($value->size) {
                 $folder = $value->getFolder();
                 // Make sure folder excist
                 $source = $folder->getSource()['settings'];
                 IOHelper::ensureFolderExists($source['path'], $suppressErrors = true);
                 // Save/Store Files
                 $fileName = IOHelper::getFileName($value->filename, true);
                 $response = craft()->assets->insertFileByLocalPath($value->originalName, $fileName, $value->folderId, AssetConflictResolution::KeepBoth);
                 $fileIds[] = $response->getDataItem('fileId');
                 // Delete Temp Files
                 IOHelper::deleteFile($value->originalName, true);
                 if ($response->isError()) {
                     $response->setError(Craft::t('There was an error with file uploads.'));
                 }
             }
             $submissionRecord->files = $fileIds;
         }
     }
     // Build Entry Record
     $submissionRecord->formId = $submission->formId;
     $submissionRecord->title = $submission->title;
     $submissionRecord->submission = $submission->submission;
     $submissionRecord->validate();
     $submission->addErrors($submissionRecord->getErrors());
     // Save To Database
     if (!$submission->hasErrors()) {
         $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
         try {
             if (craft()->elements->saveElement($submission)) {
                 $submissionRecord->id = $submission->id;
                 $submissionRecord->save(false);
                 if ($transaction !== null) {
                     $transaction->commit();
                 }
                 return $submissionRecord->id;
             } else {
                 return false;
             }
         } catch (\Exception $e) {
             if ($transaction !== null) {
                 $transaction->rollback();
             }
             throw $e;
         }
         return true;
     } else {
         return false;
     }
 }
 /**
  * Process Submission Entry
  *
  */
 public function processSubmissionEntry(FormBuilder2_EntryModel $submission)
 {
     // Fire Before Save Event
     $this->onBeforeSave(new Event($this, array('entry' => $submission)));
     $form = craft()->formBuilder2_form->getFormById($submission->formId);
     $formFields = $form->fieldLayout->getFieldLayout()->getFields();
     $attributes = $form->getAttributes();
     $formSettings = $attributes['formSettings'];
     $submissionRecord = new FormBuilder2_EntryRecord();
     // Build Entry Record
     $submissionRecord->formId = $submission->formId;
     $submissionRecord->title = $submission->title;
     $submissionRecord->files = $submission->files;
     $submissionRecord->submission = $submission->submission;
     $submissionRecord->validate();
     $submission->addErrors($submissionRecord->getErrors());
     // Save To Database
     if (!$submission->hasErrors()) {
         $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
         try {
             if (craft()->elements->saveElement($submission)) {
                 $submissionRecord->id = $submission->id;
                 $submissionRecord->save(false);
                 if ($transaction !== null) {
                     $transaction->commit();
                 }
                 return $submissionRecord->id;
             } else {
                 return false;
             }
         } catch (\Exception $e) {
             if ($transaction !== null) {
                 $transaction->rollback();
             }
             throw $e;
         }
         return true;
     } else {
         return false;
     }
 }