public function saveRecipient(SproutEmail_DefaultMailerRecipientModel &$model) { $isNewElement = !$model->id; if (!$isNewElement) { $record = SproutEmail_DefaultMailerRecipientRecord::model()->findById($model->id); if (!$record) { throw new Exception(Craft::t('No recipient exists with the id “{id}”', array('id' => $model->id))); } } else { $record = new SproutEmail_DefaultMailerRecipientRecord(); } $record->setAttributes($model->getAttributes(), false); $record->validate(); $model->addErrors($record->getErrors()); if (!$model->hasErrors()) { $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null; try { if (craft()->elements->saveElement($model, true)) { $saved = false; if ($isNewElement) { $record->id = $model->id; } if ($record->save(false)) { $model->id = $record->id; $saved = $this->saveRecipientListRecipientRelations($record->id, $model->recipientLists); } if ($transaction !== null && $saved) { $transaction->commit(); } return $saved; } $model->addErrors($record->getErrors()); } catch (\Exception $e) { if ($transaction !== null) { $transaction->rollback(); } throw $e; } } return false; }