コード例 #1
0
ファイル: actions.class.php プロジェクト: taryono/school
 public function executeEnquiryProcess()
 {
     /* Initialize */
     $this->getEnquirer();
     $valid = true;
     $error_message = array();
     $now = date('Y-m-d H:i:s');
     /* Filter User */
     if (!$this->filterUser(true)) {
         $this->jsonwrapper->print_json(array('success' => false, 'code' => 'security', 'message' => 'You must login to access this url.'));
     }
     /* Get All Parameter */
     $param = $this->getAllRequestParameter();
     /* enquiry_name */
     if (empty($param['enquirer_name'])) {
         $error_message[] = array('field' => 'enquirer_name', 'message' => 'Please enter your enquirer name');
         $valid = false;
     }
     /* enquiry_type */
     if (empty($param['enquiry_type'])) {
         $error_message[] = array('field' => 'enquiry_type', 'message' => 'Please enter enquiry type');
         $valid = false;
     } else {
         /* Check enquiry type exist or not */
         switch ($param['enquiry_type']) {
             case TestApplicant::ENQ_PHONE:
             case TestApplicant::ENQ_FAX:
             case TestApplicant::ENQ_EMAIL:
             case TestApplicant::ENQ_DIRECT:
                 break;
             default:
                 $error_message[] = array('field' => 'enquiry_type', 'message' => 'Enquiry type does not exist');
                 $valid = false;
                 break;
         }
     }
     /* enquiry_getinformation */
     if (empty($param['getinformation'])) {
         $error_message[] = array('field' => 'getinformation', 'message' => 'Please enter this field');
         $valid = false;
     } else {
         switch ($param['getinformation']) {
             case TestApplicant::REG_INFO_FAM:
             case TestApplicant::REG_INFO_NEWSP:
             case TestApplicant::REG_INFO_SCH:
             case TestApplicant::REG_INFO_BAN:
             case TestApplicant::REG_INFO_MISC:
                 break;
             default:
                 $error_message[] = array('field' => 'getinformation', 'message' => 'Information type does not exist');
                 $valid = false;
                 break;
         }
     }
     /* enquiry comment */
     if (empty($param['comment'])) {
         $error_message[] = array('field' => 'comment', 'message' => 'Please enter a comment');
         $valid = false;
     }
     /* --- Applicant --- */
     /* applicant name */
     if (empty($param['applicant_name'])) {
         $error_message[] = array('field' => 'applicant_name', 'message' => 'Please enter your Applicant Fullname');
         $valid = false;
     }
     /* Relationship */
     if (empty($param['relationship'])) {
         $error_message[] = array('field' => 'relationship', 'message' => 'Please enter your relation with applicant');
         $valid = false;
     }
     /* Address */
     if (empty($param['address'])) {
         $error_message[] = array('field' => 'address', 'message' => 'Please enter applicant address');
         $valid = false;
     }
     /* POB */
     if (empty($param['pob'])) {
         $error_message[] = array('field' => 'pob', 'message' => 'Please enter applicant place of birth');
         $valid = false;
     }
     /* DOB */
     if (empty($param['dob'])) {
         $error_message[] = array('field' => 'dob', 'message' => 'Please enter applicant date of birth');
         $valid = false;
     }
     /* present school */
     if (empty($param['present_school'])) {
         $error_message[] = array('field' => 'present_school', 'message' => 'Please enter Applicant Present School');
         $valid = false;
     }
     /* present level */
     if (empty($param['present_level'])) {
         $error_message[] = array('field' => 'present_level', 'message' => 'Please enter Applicant Present Level');
         $valid = false;
     }
     /* school_interested */
     if (empty($param['school_interested'])) {
         $error_message[] = array('field' => 'school_interested', 'message' => 'Please enter Applicant School Interested');
         $valid = false;
     } else {
         /* Check School Interested Exist or Not */
         $department = DepartmentPeer::retrieveByPK($param['school_interested']);
         if (!$department) {
             $error_message[] = array('field' => 'school_interested', 'message' => 'Shool Interested you entered is not valid');
             $valid = false;
         } else {
             if ($department->getParent() == 0) {
                 $error_message[] = array('field' => 'school_interested', 'message' => 'Shool Interested you entered is not valid');
                 $valid = false;
             } else {
                 /* level_interested */
                 if (empty($param['level_interested'])) {
                     $error_message[] = array('field' => 'level_interested', 'message' => 'Please enter Applicant Level Interested');
                     $valid = false;
                 } else {
                     /* Check class exist or not */
                     $classCriteria = new Criteria();
                     $classCriteria->add(VClassGroupPeer::DEPARTMENT_ID, $department->getId(), Criteria::EQUAL);
                     $classCriteria->add(VCLassGroupPeer::ID, $param['level_interested'], Criteria::EQUAL);
                     $classInterested = VCLassGroupPeer::doSelectOne($classCriteria);
                     if (!$classInterested) {
                         $error_message[] = array('field' => 'level_interested', 'message' => 'Level Interested you entered is not valid');
                         $valid = false;
                     }
                 }
             }
         }
     }
     /* Check all parameter valid or not */
     if (!$valid) {
         $this->jsonwrapper->print_json(array('success' => false, 'code' => 'notvalid', 'error' => $error_message));
     }
     /* Start Transaction */
     $connection = Propel::getConnection('propel');
     $connection->begin();
     /* TABLE TEST_APPLICANT_ENQUIRER */
     $this->user->setFullname($param['enquirer_name']);
     $this->user->setEnquiryType($param['enquiry_type']);
     $this->user->setComments($param['comment']);
     /* Try Save */
     try {
         $this->user->save();
     } catch (Exception $e) {
         $connection->rollback();
         $this->jsonwrapper->print_json(array('success' => false, 'code' => 'fail', 'message' => 'Error while save the data, please try again later.', 'message2' => 'Error save enquirer - process1'));
     }
     /* TABLE TEST_APPLICANT */
     /* Get this applicant, if empty then create a new one */
     $applicant = $this->pmbTools->getApplicant($this->user);
     if (!$applicant) {
         $applicant = new TestApplicant();
         $newApplicant = true;
     } else {
         $newApplicant = false;
     }
     /* If New Applicant */
     /* This field cannot be changed */
     if ($newApplicant) {
         $applicantCode = $this->pmbTools->generateApplicantCode(StudentDetail::BUY);
         $applicant->setStatus(StudentDetail::BUY);
         $applicant->setCode($this->pmbTools->generateApplicantCode(StudentDetail::BUY));
         $applicant->setEnrolled(0);
         $applicant->setApplicantType(TestApplicant::STATUS_NEW);
     }
     /* Field can be changed */
     $applicant->setName($param['applicant_name']);
     $applicant->setTestApplicantEnquirerId($this->user->getId());
     $applicant->setEnquirerRelation($param['relationship']);
     $applicant->setRegInfo($param['getinformation']);
     $applicant->setYear(date('Y', strtotime($now)));
     $applicant->setDepartment1($department->getId());
     $applicant->setClassGroup1($classInterested->getId());
     /* Try save applicant */
     try {
         $applicant->save();
     } catch (Exception $e) {
         $connection->rollback();
         $this->jsonwrapper->print_json(array('success' => false, 'code' => 'fail', 'message' => 'Error while save the data, please try again later.', 'message2' => 'Error save applicant - process2'));
     }
     /* TABLE APPLICANT_DETAIL */
     if ($applicant->getTestApplicantDetail()) {
         $applicantDetail = $applicant->getTestApplicantDetail();
         $newApplicantDetail = false;
     } else {
         $applicantDetail = new TestApplicantDetail();
         $newApplicantDetail = true;
     }
     /* Fill Applicant Detail */
     $applicantDetail->setAddress($param['address']);
     $applicantDetail->setPob($param['pob']);
     $applicantDetail->setDob(date('Y-m-d', strtotime($param['dob'])));
     $applicantDetail->setSchoolOfOrigin($param['present_school']);
     $applicantDetail->setClassOfOrigin($param['present_level']);
     /* Try save applicant_detail */
     try {
         $applicantDetail->save();
     } catch (Exception $e) {
         $connection->rollback();
         $this->jsonwrapper->print_json(array('success' => false, 'code' => 'fail', 'message' => 'Error while save the data, please try again later.', 'message2' => 'Error save applicant_detail - process3'));
     }
     if ($newApplicantDetail) {
         /* Create relation between applicant and applicant_detail */
         $applicant->setTestApplicantDetailId($applicantDetail->getId());
         try {
             $applicant->save();
         } catch (Exception $e) {
             $connection->rollback();
             $this->jsonwrapper->print_json(array('success' => false, 'code' => 'fail', 'message' => 'Error while save the data, please try again later.', 'message2' => 'Error create relation between applicant and applicant_detail - process3.1'));
         }
     }
     $connection->commit();
     $this->jsonwrapper->print_json(array('success' => true, 'code' => 'success', 'message' => 'Data successfully saved', 'href' => $this->pmbTools->pmbUrl('account/index')));
 }