/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Applications();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Applications'])) {
         $model->attributes = $_POST['Applications'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
 public function submitApplication()
 {
     //save the main app and primary applicant
     $appFields = Input::all();
     $application = new Applications();
     $application->move_in_date = $appFields['move_in_date'];
     $application->reason_for_moving = $appFields['reason_for_moving'];
     $application->status = 0;
     $application->save();
     $lastID = $application->id;
     $occupants = [];
     foreach ($appFields['occupant'] as $afKey => $afVal) {
         foreach ($afVal as $k => $v) {
             $occupants[$k][$afKey] = $v;
         }
     }
     $occ = new Occupants();
     $occ->application_id = $lastID;
     $occ->first_name = $appFields['first_name'];
     $occ->last_name = $appFields['last_name'];
     $occ->date_of_birth = $appFields['date_of_birth'];
     $occ->email_address = $appFields['email'];
     $occ->primary_applicant = 1;
     $occ->save();
     //save additional occupants
     foreach ($occupants as $occupant) {
         $occ = new Occupants();
         $occ->application_id = $lastID;
         $occ->first_name = $occupant['firstname'];
         $occ->last_name = $occupant['lastname'];
         $occ->date_of_birth = $occupant['date_of_birth'];
         $occ->email_address = $occupant['email'];
         $occ->primary_applicant = 0;
         $occ->save();
     }
     echo "<pre>";
     print_r($occupants);
     print_r($appFields);
     exit('test');
 }
 public function actionCreate()
 {
     $data = $_POST;
     if (!empty($data)) {
         $data['project_id'] = trim($data['project_id']);
         $data['name'] = trim($data['name']);
         $data['description'] = trim($data['description']);
         $data['accessibility'] = trim($data['accessibility']);
         $data['repository_url'] = trim($data['repository_url']);
         $data['description'] = trim($data['description']);
         $data['instructions'] = trim($data['instructions']);
         $data['production_date'] = trim($data['production_date']);
         $data['termination_date'] = trim($data['termination_date']);
         $data['uses_mobile_patterns'] = $data['uses_mobile_patterns'] == 'true' ? 1 : 0;
         //FORM VALIDATION HERE
         $errors = array();
         //project id is required
         if (strlen($data['project_id']) == 0) {
             array_push($errors, 'PROJECT_ERROR: Project ID is required');
         } else {
             if (!Projects::model()->exists('project_id=:project_id', array(':project_id' => $data['project_id']))) {
                 array_push($errors, 'PROJECT_ERROR: Project ID does not exist');
             }
         }
         //name is required
         if (strlen($data['name']) == 0) {
             array_push($errors, 'NAME_ERROR: Name is required');
         }
         //type is required
         if (strlen($data['type_name']) == 0) {
             array_push($errors, 'TYPE_ERROR: Type is required');
             //invalid type
         } else {
             $app_type = ApplicationTypes::model()->find('name=:name', array(':name' => $data['type_name']));
             if ($app_type == null) {
                 array_push($errors, 'TYPE_ERROR: Type is not in the list');
             } else {
                 $data['type_id'] = $app_type->type_id;
             }
         }
         //accessibility is required
         if (strlen($data['accessibility']) == 0) {
             array_push($errors, 'ACCESSIBILITY_ERROR: Accessibility is required');
             //accessibility should either be PUBLIC or PRIVATE only
         } else {
             if ($data['accessibility'] != 'PUBLIC' && $data['accessibility'] != 'PRIVATE') {
                 array_push($errors, 'ACCESSIBILITY_ERROR: Accessibility option selected is invalid');
             }
         }
         //data is good
         if (count($errors) == 0) {
             $application = new Applications();
             $application->project_id = (int) $data['project_id'];
             $application->type_id = $data['type_id'];
             $application->name = $data['name'];
             $application->description = $data['description'];
             $application->accessibility = $data['accessibility'];
             $application->repository_url = $data['repository_url'];
             $application->uses_mobile_patterns = $data['uses_mobile_patterns'];
             $application->instructions = $data['instructions'];
             $application->rd_point_person = $data['rd_point_person'];
             $application->production_date = $data['production_date'];
             $application->termination_date = $data['termination_date'];
             $application->date_created = date("Y-m-d H:i:s");
             $application->date_updated = '0000-00-00 00:00:00';
             $application->created_by = Yii::app()->user->name;
             $application->save();
             // add to server if necessary
             if (isset($data['server_id'])) {
                 $app_server = new ApplicationServers();
                 $app_server->application_id = $application->application_id;
                 $app_server->server_id = $data['server_id'];
                 $app_server->application_path = $data['application_path'];
                 $app_server->application_log = $data['application_log'];
                 $app_server->date_created = date("Y-m-d H:i:s");
                 $app_server->date_updated = '0000-00-00 00:00:00';
                 $app_server->created_by = Yii::app()->user->name;
                 $app_server->save();
             }
             echo CJSON::encode(array('type' => 'success', 'data' => ''));
         } else {
             echo CJSON::encode(array('type' => 'error', 'data' => implode(',', $errors)));
         }
     } else {
         echo CJSON::encode(array('type' => 'error', 'data' => 'CSRF_ERROR: CSRF Token did not match'));
     }
 }