/**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer $id the ID of the model to be loaded
  * @return Invitation the loaded model
  * @throws CHttpException
  */
 public function loadModel($id)
 {
     $model = Invitation::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new User();
     $model->username = "";
     $model->password = "";
     $infoModel = new UserInfo();
     $infoModel->user_id = $model->id;
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     $error = '';
     $form = 'user-Register-form';
     //If a new User has been successfully created e.g. the user has created an account from the register.php page
     if (isset($_POST['User']) && isset($_POST['UserInfo'])) {
         /*if ($this->actionVerifyRegistration() != "") {
               $this->render('create', array('model'=>$model));
           }*/
         echo "<script>console.log('New User Registered');</script>";
         // auto-fill biography information
         $model->attributes = $_POST['User'];
         $model->pic_url = '/coplat/images/profileimages/default_pic.jpg';
         $model->biography = "Tell us something about yourself...";
         $model->activation_chain = $this->genRandomString(10);
         $model->activated = 1;
         //$emailCheck=User::model()->find(array(
         //  'select'=>'email',
         //  'condition'=>'email=:email',
         //  'params'=>array(':email'=>$model->email))
         //);
         //$unameCheck=User::model()->find(array(
         //  'select'=>'username',
         //  'condition'=>'username=:username',
         //  'params'=>array(':username'=>$model->username))
         //);
         //if($emailCheck === null or $unameCheck === null){
         //	$error = "username or email taken";
         //	$this->render('create',array(
         //			'model'=>$model,'infoModel'=> $infoModel, 'error' => $error,
         //	));
         //	return;
         //}
         // hash entered password
         $pw = $model->password;
         $hasher = new PasswordHash(8, false);
         $model->password = $hasher->HashPassword($model->password);
         $error1 = $this->verifyRegistration();
         if ($error1 == null) {
             //application management
             $redirectMentorApp = false;
             //
             //$id = $_GET['id'];
             if (isset($_GET['id']) && $_GET['id'] > 0) {
                 $id = $_GET['id'];
                 $invitationInfo = Invitation::model()->findByPk($id);
                 if (isset($invitationInfo)) {
                     $model->isMentee = $invitationInfo->mentee;
                     if ($invitationInfo->mentor) {
                         $redirectMentorApp = true;
                     }
                 }
             }
             $model->save(false);
             if (isset($_POST['UserInfo'])) {
                 // get entered personal info
                 $infoModel->attributes = $_POST['UserInfo'];
                 $infoModel->user_id = $model->id;
                 $infoModel->save(false);
             }
             //newUserLogin($model, $pw);
             $login = new LoginForm();
             $login->username = $model->username;
             $login->password = $pw;
             $login->login();
             if ($model->isProMentor) {
                 $proMentor = new ProjectMentor();
                 $proMentor->user_id = $model->id;
                 $proMentor->max_hours = 0;
                 $proMentor->max_projects = 0;
                 $proMentor->save(false);
             }
             if ($model->isDomMentor) {
                 $domMentor = new DomainMentor();
                 $domMentor->user_id = $model->id;
                 $domMentor->max_tickets = 0;
                 $domMentor->save();
             }
             if ($model->isPerMentor) {
                 $perMentor = new PersonalMentor();
                 $perMentor->user_id = $model->id;
                 $perMentor->max_hours = 0;
                 $perMentor->max_mentees = 0;
                 $perMentor->save();
             }
             if ($redirectMentorApp) {
                 $this->redirect("/coplat/index.php/application/portal");
             } else {
                 $this->redirect("/coplat/index.php/");
             }
             // Confirmation email for registration
             //$userfullName = $model->fname.' '.$model->lname;
             //$adminName = User::getCurrentUser();
             //User::sendConfirmationEmail($userfullName, $model->email,$model->username,$pw,$adminName->fname.' '.$adminName->lname);
         }
     }
     if (isset($_POST['Roles'])) {
         $proMentor = ProjectMentor::model()->getProMentor($_COOKIE['UserID']);
         $perMentor = PersonalMentor::model()->getPerMentor($_COOKIE['UserID']);
         $domMentor = DomainMentor::model()->getDomMentor($_COOKIE['UserID']);
         //$model->save(false);
         $user = User::model()->findByPk($_COOKIE['UserID']);
         if ($user->isProMentor == 1) {
             //$proMentor = new ProjectMentor;
             $proMentor->user_id = $user->id;
             $proMentor->max_hours = $_POST['pjmhours'];
             $all = Project::model()->findAll();
             $proMentor->save();
             $count = 0;
             foreach ($all as $each) {
                 if (isset($_POST[$each->id . 'pjm'])) {
                     $p = Project::model()->findByPk($each->id);
                     $p->project_mentor_user_id = $_COOKIE['UserID'];
                     $p->save(false);
                     $count++;
                 }
             }
             $proMentor->max_projects = $count;
             $proMentor->save();
         }
         if ($user->isDomMentor == 1) {
             //UserDomain::model()->deleteAll("user_id = ".$user->id);
             $domMentor->max_tickets = $_POST['dmmaxtickets'];
             $domMentor->save();
             $all = Domain::model()->findAll();
             foreach ($all as $each) {
                 if (isset($_POST[$each->id])) {
                     $user_domain = new UserDomain();
                     $user_domain->user_id = $domMentor->user_id;
                     $user_domain->domain_id = $each->id;
                     $user_domain->active = 1;
                     $user_domain->save(false);
                     $allsubs = Subdomain::model()->findAllBySql("select * from subdomain where domain_id = {$each->id}");
                     if ($allsubs != null) {
                         foreach ($allsubs as $onesub) {
                             $temp = $onesub->id . 'ddmsub';
                             if (isset($_POST[$temp])) {
                                 $user_domain = new UserDomain();
                                 $user_domain->user_id = $domMentor->user_id;
                                 $user_domain->domain_id = $each->id;
                                 $user_domain->active = 1;
                                 $rate = $each->id . '-' . $onesub->id . 'dmrate';
                                 $tier = $each->id . '-' . $onesub->id . 'dmtier';
                                 $user_domain->rate = $_POST[$rate];
                                 $user_domain->tier_team = $_POST[$tier];
                                 $user_domain->subdomain_id = $onesub->id;
                                 $user_domain->save(false);
                             }
                         }
                     } else {
                         $user_domain = new UserDomain();
                         $user_domain->user_id = $domMentor->user_id;
                         $user_domain->domain_id = $each->id;
                         $user_domain->active = 1;
                         $user_domain->save(false);
                     }
                 }
             }
         }
         if ($user->isPerMentor) {
             //$perMentor = new PersonalMentor();
             $perMentor->user_id = $user->id;
             $perMentor->max_hours = $_POST['pmhours'];
             $all = Mentee::model()->findAll();
             $perMentor->save();
             $count = 0;
             foreach ($all as $each) {
                 if (isset($_POST[$each->user_id . 'pm'])) {
                     $p = Mentee::model()->findByPk($each->user_id);
                     $p->personal_mentor_user_id = $_COOKIE['UserID'];
                     $p->save(false);
                     $count++;
                 }
             }
             $perMentor->max_mentees = $count;
             $perMentor->save();
         }
         /* This is set up for a user being added by an admin
         			 * 
                     $hasher = new PasswordHash(8, false);
                     $pw = $this->genRandomString(8);
                     $user->password = $hasher->HashPassword($pw);
                     $user->save(false);
                     $userfullName = $user->fname.' '.$user->lname;
                     $adminName = User::getCurrentUser();
                     User::sendConfirmationEmail($userfullName, $user->email,$user->username,$pw,$adminName->fname.' '.$adminName->lname);
         			*/
     }
     //$error = '';
     $this->render('create', array('model' => $model, 'infoModel' => $infoModel, 'error' => $error));
     return;
     //$this->render('add',array('model'=>$model, 'error' => $error));
 }