public function execute(CommandContext $context)
 {
     PHPWS_Core::initModClass('hms', 'StudentFactory.php');
     PHPWS_Core::initModClass('hms', 'HousingApplicationFactory.php');
     PHPWS_Core::initModClass('hms', 'exception/InvalidTermException.php');
     $term = $context->get('term');
     $username = UserStatus::getUsername();
     $student = StudentFactory::getStudentByUsername($username, $term);
     $sem = Term::getTermSem($term);
     // Check for an existing application and delete it
     $app_result = HousingApplication::checkForApplication($username, $term);
     // If there's an existing housing application, handle deleting it
     if ($app_result !== FALSE) {
         switch ($sem) {
             case TERM_SPRING:
                 $application = new SpringApplication($app_result['id']);
                 break;
             case TERM_SUMMER1:
             case TERM_SUMMER2:
                 $application = new SummerApplication($app_result['id']);
                 break;
             case TERM_FALL:
                 $application = new FallApplication($app_result['id']);
                 break;
             default:
                 throw new InvalidTermException('Invalid term specified.');
         }
         // Save the old created on dates for re-use on new application
         $oldCreatedOn = $application->getCreatedOn();
         $oldCreatedBy = $application->getCreatedBy();
         $application->delete();
     }
     switch ($sem) {
         case TERM_FALL:
             $appType = 'fall';
             break;
         case TERM_SPRING:
             $appType = 'spring';
             break;
         case TERM_SUMMER1:
         case TERM_SUMMER2:
             $appType = 'summer';
             break;
         default:
             throw new Exception('Unknown application type');
     }
     $application = HousingApplicationFactory::getApplicationFromSession($_SESSION['application_data'], $term, $student, $appType);
     // If old created dates exist, use them as the 'created on' dates
     if (isset($oldCreatedOn)) {
         $application->setCreatedOn($oldCreatedOn);
         $application->setCreatedBy($oldCreatedBy);
     }
     $application->setCancelled(0);
     // Hard code a summer meal option for all summer applications.
     // Application for other terms use whatever the student selected
     if ($sem == TERM_SUMMER1 || $sem == TERM_SUMMER2) {
         $application->setMealPlan(BANNER_MEAL_5WEEK);
     }
     $result = $application->save();
     if ($result == TRUE) {
         // Log the fact that the application was submitted
         PHPWS_Core::initModClass('hms', 'HMS_Activity_Log.php');
         HMS_Activity_Log::log_activity($username, ACTIVITY_SUBMITTED_APPLICATION, $username);
         try {
             // report the application to banner;
             $application->reportToBanner();
         } catch (Exception $e) {
             // ignore any errors reporting this to banner, they'll be logged and admins notified
             // we've saved the student's application locally, so it's ok if this doesn't work
         }
         // Send the email confirmation
         PHPWS_Core::initModClass('hms', 'HMS_Email.php');
         HMS_Email::send_hms_application_confirmation($student, $application->getTerm());
     }
     $friendly_term = Term::toString($application->getTerm());
     NQ::simple('hms', hms\NotificationView::SUCCESS, "Your application for {$friendly_term} was successfully processed!  You will receive an email confirmation in the next 24 hours.");
     PHPWS_Core::initModClass('hms', 'applicationFeature/RlcApplication.php');
     PHPWS_Core::initModClass('hms', 'HMS_RLC_Application.php');
     $rlcReg = new RLCApplicationRegistration();
     if (ApplicationFeature::isEnabledForStudent($rlcReg, $term, $student) && HMS_RLC_Application::checkForApplication($student->getUsername(), $term) == FALSE && $application->rlc_interest == 1) {
         $rlcCmd = CommandFactory::getCommand('ShowRlcApplicationPage1View');
         $rlcCmd->setTerm($term);
         $rlcCmd->redirect();
     } else {
         $successCmd = CommandFactory::getCommand('ShowStudentMenu');
         $successCmd->redirect();
     }
 }
 public function show()
 {
     PHPWS_Core::initCoreClass('Form.php');
     $form = new PHPWS_Form();
     $submitCmd = CommandFactory::getCommand('HousingApplicationFormSubmit');
     $submitCmd->setTerm($this->term);
     $submitCmd->initForm($form);
     $tpl = array();
     /****************
      * Display Info *
      ****************/
     $tpl['STUDENT_NAME'] = $this->student->getFullName();
     $tpl['GENDER'] = $this->student->getPrintableGender();
     $tpl['ENTRY_TERM'] = Term::toString($this->student->getApplicationTerm());
     $tpl['CLASSIFICATION_FOR_TERM_LBL'] = HMS_Util::formatClass($this->student->getClass());
     $tpl['STUDENT_STATUS_LBL'] = HMS_Util::formatType($this->student->getType());
     $tpl['TERM'] = Term::toString($this->term);
     /**************
      * Cell Phone *
      **************/
     $form->addText('number');
     $form->setSize('number', 10);
     $form->setMaxSize('number', 10);
     $form->addCssClass('number', 'form-control');
     if (!is_null($this->existingApplication)) {
         $form->setValue('number', substr($this->existingApplication->getCellPhone(), 0));
     }
     $form->addCheck('do_not_call', 1);
     if (!is_null($this->existingApplication) && is_null($this->existingApplication->getCellPhone())) {
         $form->setMatch('do_not_call', 1);
     }
     // This is just getting worse and worse.
     // TODO: this, correctly.
     $sem = Term::getTermSem($this->term);
     if ($sem == TERM_SPRING || $sem == TERM_FALL) {
         /*************
          * Lifestyle *
          *************/
         // TODO: get rid of the magic numbers!!!
         $form->addDropBox('lifestyle_option', array('1' => _('Single Gender Building'), '2' => _('Co-Ed Building')));
         if (!is_null($this->existingApplication)) {
             $form->setMatch('lifestyle_option', $this->existingApplication->getLifestyleOption());
         } else {
             $form->setMatch('lifestyle_option', '1');
         }
         $form->addCssClass('lifestyle_option', 'form-control');
         /************
          * Bed time *
          ************/
         // TODO: magic numbers
         $form->addDropBox('preferred_bedtime', array('1' => _('Early'), '2' => _('Late')));
         $form->setClass('preferred_bedtime', 'form-control');
         if (!is_null($this->existingApplication)) {
             $form->setMatch('preferred_bedtime', $this->existingApplication->getPreferredBedtime());
         } else {
             $form->setMatch('preferred_bedtime', '1');
         }
         /******************
          * Room condition *
          ******************/
         //TODO: magic numbers
         $form->addDropBox('room_condition', array('1' => _('Neat'), '2' => _('Cluttered')));
         if (!is_null($this->existingApplication)) {
             $form->setMatch('room_condition', $this->existingApplication->getRoomCondition());
         } else {
             $form->setMatch('room_condition', '1');
         }
         $form->addCssClass('room_condition', 'form-control');
     } else {
         if ($sem == TERM_SUMMER1 || $sem == TERM_SUMMER2) {
             /* Private room option for Summer terms */
             $form->addDropBox('room_type', array(ROOM_TYPE_DOUBLE => 'Two person', ROOM_TYPE_PRIVATE => 'Private (if available)'));
             $form->setClass('room_type', 'form-control');
             if (!is_null($this->existingApplication)) {
                 $form->setMatch('room_type', $this->existingApplication->getRoomType());
             } else {
                 $form->setMatch('room_type', '0');
             }
         }
     }
     /*********************
      * Smoking Preference *
      *********************/
     $form->addDropBox('smoking_preference', array('1' => _('No'), '2' => _('Yes')));
     if (!is_null($this->existingApplication)) {
         $form->setMatch('smoking_preference', $this->existingApplication->getSmokingPreference());
     } else {
         $form->setMatch('smoking_preference', '1');
     }
     $form->addCssClass('smoking_preference', 'form-control');
     /***************
      * Meal Option *
      ***************/
     if ($sem == TERM_FALL || $sem == TERM_SPRING) {
         if ($this->student->getType() == TYPE_FRESHMEN) {
             $mealOptions = array(BANNER_MEAL_STD => 'Standard', BANNER_MEAL_HIGH => 'High', BANNER_MEAL_SUPER => 'Super');
         } else {
             $mealOptions = array(BANNER_MEAL_LOW => _('Low'), BANNER_MEAL_STD => _('Standard'), BANNER_MEAL_HIGH => _('High'), BANNER_MEAL_SUPER => _('Super'));
         }
     } else {
         if ($sem == TERM_SUMMER1 || $sem == TERM_SUMMER2) {
             $mealOptions = array(BANNER_MEAL_5WEEK => 'Summer 5-Week Plan');
         }
     }
     $form->addDropBox('meal_option', $mealOptions);
     $form->setClass('meal_option', 'form-control');
     $form->setMatch('meal_option', BANNER_MEAL_STD);
     $form->addCssClass('meal_option', 'form-control');
     if (!is_null($this->existingApplication)) {
         $form->setMatch('meal_option', $this->existingApplication->getMealPlan());
     } else {
         $form->setMatch('meal_option', BANNER_MEAL_STD);
     }
     /*********************
      * Emergency Contact *
      *********************/
     $form->addText('emergency_contact_name');
     $form->addCssClass('emergency_contact_name', 'form-control');
     $form->addText('emergency_contact_relationship');
     $form->addCssClass('emergency_contact_relationship', 'form-control');
     $form->addText('emergency_contact_phone');
     $form->addCssClass('emergency_contact_phone', 'form-control');
     $form->addText('emergency_contact_email');
     $form->addCssClass('emergency_contact_email', 'form-control');
     $form->addTextArea('emergency_medical_condition');
     $form->addCssClass('emergency_medical_condition', 'form-control');
     $form->setRows('emergency_medical_condition', 4);
     if (!is_null($this->existingApplication)) {
         $form->setValue('emergency_contact_name', $this->existingApplication->getEmergencyContactName());
         $form->setValue('emergency_contact_relationship', $this->existingApplication->getEmergencyContactRelationship());
         $form->setValue('emergency_contact_phone', $this->existingApplication->getEmergencyContactPhone());
         $form->setValue('emergency_contact_email', $this->existingApplication->getEmergencyContactEmail());
         $form->setValue('emergency_medical_condition', $this->existingApplication->getEmergencyMedicalCondition());
     }
     /**
      * Missing Person
      */
     $form->addText('missing_person_name');
     $form->addCssClass('missing_person_name', 'form-control');
     $form->addText('missing_person_relationship');
     $form->addCssClass('missing_person_relationship', 'form-control');
     $form->addText('missing_person_phone');
     $form->addCssClass('missing_person_phone', 'form-control');
     $form->addText('missing_person_email');
     $form->addCssClass('missing_person_email', 'form-control');
     if (!is_null($this->existingApplication)) {
         $form->setValue('missing_person_name', $this->existingApplication->getMissingPersonName());
         $form->setValue('missing_person_relationship', $this->existingApplication->getMissingPersonRelationship());
         $form->setValue('missing_person_phone', $this->existingApplication->getMissingPersonPhone());
         $form->setValue('missing_person_email', $this->existingApplication->getMissingPersonEmail());
     }
     /**
      * Special needs
      */
     $tpl['SPECIAL_NEEDS_TEXT'] = '';
     // setting this template variable to anything causes the special needs text to be displayed
     $form->addCheck('special_need', array('special_need'));
     $form->setLabel('special_need', array('Yes, I require special needs housing.'));
     if (isset($this->existingApplication)) {
         if (!is_null($this->existingApplication->physical_disability) && $this->existingApplication->physical_disability != "0" || !is_null($this->existingApplication->psych_disability) && $this->existingApplication->psych_disability != "0" || !is_null($this->existingApplication->medical_need) && $this->existingApplication->medical_need != "0" || !is_null($this->existingApplication->gender_need) && $this->existingApplication->gender_need != "0") {
             $form->setMatch('special_need', 'special_need');
         }
     }
     if (isset($_REQUEST['special_needs'])) {
         $form->addHidden('special_needs', $_REQUEST['special_needs']);
     }
     /*******
      * RLC *
      *******/
     PHPWS_Core::initModClass('hms', 'applicationFeature/RlcApplication.php');
     $rlcReg = new RLCApplicationRegistration();
     if (HMS_RLC_Application::checkForApplication($this->student->getUsername(), $this->term) == TRUE) {
         // Student has an RLC application on file already
         $tpl['RLC_SUBMITTED'] = '';
         $form->addHidden('rlc_interest', 0);
     } else {
         if (ApplicationFeature::isEnabledForStudent($rlcReg, $this->term, $this->student)) {
             // Feature is enabled, but student hasn't submitted one yet
             $form->addRadio('rlc_interest', array(0, 1));
             $form->setLabel('rlc_interest', array(_("No"), _("Yes")));
             if (!is_null($this->existingApplication) && !is_null($this->existingApplication->getRLCInterest())) {
                 $form->setMatch('rlc_interest', 'rlc_interest');
             } else {
                 $form->setMatch('rlc_interest', '0');
             }
         } else {
             // Feature is not enabled
             $form->addHidden('rlc_interest', 0);
         }
     }
     $tpl['CONTINUE_BTN'] = '';
     $form->mergeTemplate($tpl);
     $tpl = $form->getTemplate();
     Layout::addPageTitle("Housing Application Form");
     return PHPWS_Template::process($tpl, 'hms', 'student/student_application.tpl');
 }