/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Customer();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Customer'])) {
         $model->attributes = $_POST['Customer'];
         if ($model->save()) {
             // Comprobamos si existe la lista de envio de todos los clientes
             if (count(Mailist::model()->findByAttributes(array('Name' => 'Todos los clientes', 'UserID' => Yii::app()->user->ID)))) {
                 // Si existe la lista añadimos el nuevo cliente
                 $ListID = Mailist::model()->findByAttributes(array('Name' => 'Todos los clientes'))->ID;
                 $modelCustomerList = new CustomerList();
                 $modelCustomerList->UserID = Yii::app()->user->ID;
                 $modelCustomerList->ListID = $ListID;
                 $modelCustomerList->CustomerID = $model->ID;
                 $modelCustomerList->save();
             } else {
                 // Si no existe la lista la creamos y añadimos el cliente nuevo
                 $modelMailist = new Mailist();
                 $modelMailist->Name = "Todos los clientes";
                 $modelMailist->UserID = Yii::app()->user->ID;
                 if ($modelMailist->save()) {
                     $modelCustomerList = new CustomerList();
                     $modelCustomerList->UserID = Yii::app()->user->ID;
                     $modelCustomerList->ListID = $modelMailist->ID;
                     $modelCustomerList->CustomerID = $model->ID;
                     $modelCustomerList->save();
                 }
             }
             $this->redirect(array('admin'));
         }
     }
     $this->render('create', array('model' => $model));
 }
 public function actionAddCustomer()
 {
     $modelList = Mailist::model()->findByPK($_POST["listID"]);
     if (CustomerList::model()->findByAttributes(array("UserID" => Yii::app()->user->ID, "CustomerID" => $_POST["newEmail"], "ListID" => $_POST["listID"]))) {
         echo "<p class='jqueryError'>Este cliente ya est&aacute; en la lista. Prueba con otro por favor</p>";
         $this->renderPartial('_emailist', array('model' => $modelList));
     } else {
         $model = new CustomerList();
         $model->UserID = Yii::app()->user->ID;
         $model->CustomerID = $_POST["newEmail"];
         $model->ListID = $_POST["listID"];
         if ($model->save()) {
             echo "<p class='jqueryOk'>Cliente a&ntilde;adido a la lista.</p>";
             $this->renderPartial('_emailist', array('model' => $modelList));
         } else {
             echo "<p class='jqueryError'>Error a&ntilde;adiendo el cliente. Intentalo de nuevo por favor.</p>";
             $this->renderPartial('_emailist', array('model' => $modelList));
         }
     }
 }