/**
  * @throws Exception
  * @throws HttpException
  */
 public function actionSaveRecipientList()
 {
     $this->requirePostRequest();
     $id = craft()->request->getPost('id');
     if ($id && is_numeric($id)) {
         $model = sproutEmailDefaultMailer()->getRecipientListById($id);
         if (!$model) {
             throw new Exception(Craft::t('Recipient list with id ({id}) was not found.', array('id' => $id)));
         }
     } else {
         $model = new SproutEmail_DefaultMailerRecipientListModel();
     }
     $name = craft()->request->getPost('name', $model->name);
     $model->setAttribute('name', $name);
     $model->setAttribute('handle', sproutEmail()->createHandle($name));
     if ($model->validate() && sproutEmailDefaultMailer()->saveRecipientList($model)) {
         craft()->userSession->setNotice(Craft::t('Recipient list saved successfully.'));
         if (craft()->request->isAjaxRequest()) {
             $this->returnJson(array('success' => 'true', 'list' => array('id' => $model->id)));
         }
         $this->redirectToPostedUrl($model);
     }
     craft()->userSession->setError(Craft::t('Unable to save recipient list.'));
     if (craft()->request->isAjaxRequest()) {
         $this->returnErrorJson(Craft::t('Unable to save recipient list.'));
     }
     craft()->urlManager->setRouteVariables(array('recipientList' => $model));
 }
 public function saveRecipientList(SproutEmail_DefaultMailerRecipientListModel &$model)
 {
     if (isset($model->id) && is_numeric($model->id)) {
         $record = SproutEmail_DefaultMailerRecipientListRecord::model()->findById($model->id);
         if ($record) {
             $record->setAttributes($model->getAttributes(), false);
         }
     } else {
         $record = new SproutEmail_DefaultMailerRecipientListRecord();
         $record->name = $model->name;
         $record->handle = $model->handle;
         $record->dynamic = (int) $model->dynamic;
     }
     if ($record->validate()) {
         try {
             $record->save(false);
             $model->id = $record->id;
             return true;
         } catch (\Exception $e) {
             $model->addError('save', $e->getMessage());
         }
     } else {
         $model->addErrors($record->getErrors());
     }
     return false;
 }