/** * @param SproutEmail_CampaignModel $campaign * @param string $tab * * @throws \Exception * @return int CampaignRecordId */ public function saveCampaign(SproutEmail_CampaignModel $campaign, $tab = 'info') { $oldCampaign = null; if (is_numeric($campaign->id)) { $campaignRecord = SproutEmail_CampaignRecord::model()->findById($campaign->id); $oldCampaign = SproutEmail_CampaignModel::populateModel($campaignRecord); } else { $campaignRecord = new SproutEmail_CampaignRecord(); } $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null; // @todo - Refactor. We no longer use tabs in the CP section. switch ($tab) { case 'fields': $fieldLayout = $campaign->getFieldLayout(); craft()->fields->saveLayout($fieldLayout); // Delete our previous record if ($campaign->id && $oldCampaign && $oldCampaign->fieldLayoutId) { craft()->fields->deleteLayoutById($oldCampaign->fieldLayoutId); } // Assign our new layout id info to our // form model and records $campaign->fieldLayoutId = $fieldLayout->id; $campaignRecord->fieldLayoutId = $fieldLayout->id; $campaignRecord = $this->saveCampaignInfo($campaign); if ($campaignRecord->hasErrors()) { if ($transaction) { $transaction->rollBack(); } return $campaign; } break; // save the campaign default: if ($campaign->type == 'notification') { $campaign->type = Campaign::Notification; } else { $campaign->type = Campaign::Email; } try { // Save the Campaign $campaignRecord = $this->saveCampaignInfo($campaign); // Rollback if saving fails if ($campaignRecord->hasErrors()) { $transaction->rollBack(); return $campaign; } // If we have a Notification, also Save the Entry if ($campaign->type == Campaign::Notification) { if (isset($oldCampaign->id)) { $criteria = craft()->elements->getCriteria('SproutEmail_Entry'); $criteria->campaignId = $oldCampaign->id; $entry = $criteria->first(); } if (isset($entry)) { // if we have a blast already, update it $entry->campaignId = $campaignRecord->id; $entry->subjectLine = $entry->subjectLine != '' ? $entry->subjectLine : $campaign->name; $entry->getContent()->title = $campaign->name; } else { // If we don't have a blast yet, create a new entry $entry = new SproutEmail_EntryModel(); $entry->campaignId = $campaignRecord->id; $entry->subjectLine = $campaign->name; $entry->getContent()->title = $campaign->name; } if (sproutEmail()->entries->saveEntry($entry, $campaign)) { // @todo - redirect and such } else { SproutEmailPlugin::log(json_encode($entry->getErrors())); } } } catch (\Exception $e) { SproutEmailPlugin::log(json_encode($e)); throw new Exception(Craft::t('Error: Campaign could not be saved.')); } break; } if ($transaction) { $transaction->commit(); } return SproutEmail_CampaignModel::populateModel($campaignRecord); }
/** * Populates a SproutEmail_EntryModel with post data * * @param SproutEmail_EntryModel $entry * * @return \Craft\SproutEmail_EntryModel */ protected function populateEntryModel(SproutEmail_EntryModel $entry) { $entry->campaignId = $this->campaign->id; $entry->slug = craft()->request->getPost('slug', $entry->slug); $entry->enabled = (bool) craft()->request->getPost('enabled', $entry->enabled); $entry->fromName = craft()->request->getPost('sproutEmail.fromName'); $entry->fromEmail = craft()->request->getPost('sproutEmail.fromEmail'); $entry->replyTo = craft()->request->getPost('sproutEmail.replyTo'); $entry->subjectLine = craft()->request->getRequiredPost('subjectLine'); $entry->getContent()->title = $entry->subjectLine; if (empty($entry->slug)) { $entry->slug = ElementHelper::createSlug($entry->subjectLine); } $fieldsLocation = craft()->request->getParam('fieldsLocation', 'fields'); $entry->setContentFromPost($fieldsLocation); $entry->setContentPostLocation($fieldsLocation); return $entry; }