/**
  * @throws \Exception
  * @throws Exception
  * @throws HttpException
  */
 public function actionSaveRecipient()
 {
     $this->requirePostRequest();
     $id = craft()->request->getPost('id');
     if ($id && is_numeric($id)) {
         $model = sproutEmailDefaultMailer()->getRecipientById($id);
         if (!$model) {
             throw new Exception(Craft::t('Recipient with id ({id}) was not found.', array('id' => $id)));
         }
     } else {
         $model = new SproutEmail_DefaultMailerRecipientModel();
     }
     $model->setAttributes(craft()->request->getPost('recipient'));
     if ($model->validate() && sproutEmailDefaultMailer()->saveRecipient($model)) {
         craft()->userSession->setNotice(Craft::t('Recipient saved successfully.'));
         $this->redirectToPostedUrl($model);
         craft()->end();
     }
     craft()->userSession->setError(Craft::t('Unable to save recipient.'));
     craft()->urlManager->setRouteVariables(array('recipient' => $model));
 }
 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;
 }
 /**
  * @param $recipientListHandle
  *
  * @return SproutEmail_DefaultMailerRecipientModel[]
  */
 public function getRecipients($recipientListHandle)
 {
     if ($list = $this->getService()->getRecipientListByHandle($recipientListHandle)) {
         return SproutEmail_DefaultMailerRecipientModel::populateModels($list->recipients);
     }
 }
 public function populateElementModel($row)
 {
     return SproutEmail_DefaultMailerRecipientModel::populateModel($row);
 }