/**
  * Parses supported {placeholders} in redirect URL before redirecting
  *
  * @param SproutForms_EntryModel $entry
  */
 public function doSmartRedirect(SproutForms_EntryModel $entry)
 {
     $vars = array_merge(array('id' => 0, 'siteUrl' => craft()->getSiteUrl()), $entry->getContent()->getAttributes(), $entry->getAttributes());
     $this->redirectToPostedUrl($vars);
 }
 /**
  * @param SproutForms_EntryModel $entry
  *
  * @throws \Exception
  * @return bool
  */
 public function saveEntry(SproutForms_EntryModel &$entry)
 {
     $isNewEntry = !$entry->id;
     if ($entry->id) {
         $entryRecord = SproutForms_EntryRecord::model()->findById($entry->id);
         if (!$entryRecord) {
             throw new Exception(Craft::t('No entry exists with id “{id}”', array('id' => $entry->id)));
         }
     } else {
         $entryRecord = new SproutForms_EntryRecord();
     }
     $entryRecord->formId = $entry->formId;
     $entryRecord->ipAddress = $entry->ipAddress;
     $entryRecord->userAgent = $entry->userAgent;
     $entryRecord->validate();
     $entry->addErrors($entryRecord->getErrors());
     Craft::import('plugins.sproutforms.events.SproutForms_OnBeforeSaveEntryEvent');
     $event = new SproutForms_OnBeforeSaveEntryEvent($this, array('entry' => $entry, 'isNewEntry' => $isNewEntry));
     craft()->sproutForms->onBeforeSaveEntry($event);
     if (!$entry->hasErrors()) {
         $form = sproutForms()->forms->getFormById($entry->formId);
         $entry->getContent()->title = craft()->templates->renderObjectTemplate($form->titleFormat, $entry);
         $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
         try {
             if ($event->isValid) {
                 $oldFieldContext = craft()->content->fieldContext;
                 $oldContentTable = craft()->content->contentTable;
                 craft()->content->fieldContext = $entry->getFieldContext();
                 craft()->content->contentTable = $entry->getContentTable();
                 SproutFormsPlugin::log('Transaction: Event is Valid');
                 $success = craft()->elements->saveElement($entry);
                 SproutFormsPlugin::log('Element Saved: ' . $success);
                 if ($success) {
                     // Now that we have an element ID, save it on the other stuff
                     if ($isNewEntry) {
                         $entryRecord->id = $entry->id;
                     }
                     // Save our Entry Settings
                     $entryRecord->save(false);
                     if ($transaction !== null) {
                         $transaction->commit();
                         SproutFormsPlugin::log('Transaction committed');
                     }
                     // Reset our field context and content table to what they were previously
                     craft()->content->fieldContext = $oldFieldContext;
                     craft()->content->contentTable = $oldContentTable;
                     Craft::import('plugins.sproutforms.events.SproutForms_OnSaveEntryEvent');
                     $event = new SproutForms_OnSaveEntryEvent($this, array('entry' => $entry, 'isNewEntry' => $isNewEntry, 'event' => 'saveEntry', 'entity' => $entry));
                     craft()->sproutForms->onSaveEntry($event);
                     return true;
                 }
                 craft()->content->fieldContext = $oldFieldContext;
                 craft()->content->contentTable = $oldContentTable;
             } else {
                 SproutFormsPlugin::log('OnBeforeSaveEntryEvent is not valid', LogLevel::Error);
                 if ($event->fakeIt) {
                     sproutForms()->entries->fakeIt = true;
                 }
             }
         } catch (\Exception $e) {
             SproutFormsPlugin::log('Failed to save element');
             throw $e;
         }
     } else {
         SproutFormsPlugin::log('Service returns false');
         return false;
     }
 }