/** * Member add or edit * CODE: member_create */ public function executeUpdate(sfWebRequest $request) { $new_pilot = false; #Security if (!$this->getUser()->hasCredential(array('Administrator', 'Staff', 'Member'), false)) { $this->getUser()->setFlash("warning", 'You don\'t have permission to access this url ' . $request->getReferer()); $this->redirect('dashboard/index'); } //FROM PERSON VIEW if ($request->getParameter('id')) { $member = MemberPeer::retrieveByPK($request->getParameter('id')); $this->forward404Unless($member); $this->title = 'Edit member'; $success = 'Member information has been successfully changed!'; $this->person = $member->getPerson(); } else { $this->person = PersonPeer::retrieveByPK($request->getParameter('person_id')); $this->forward404Unless($this->person); $member = new Member(); $this->title = 'Add new member'; $success = 'Member information has been successfully created!'; } $this->form = new MemberForm($member); $this->back = $request->getReferer(); $this->member = $member; if (strstr($request->getReferer(), 'person/view')) { $this->f_back = 1; } elseif (strstr($request->getReferer(), '/member/view/id')) { $this->f_back = 3; } elseif (strstr($request->getReferer(), '/member')) { $this->f_back = 2; } if ($request->isMethod('post')) { $this->referer = $request->getReferer(); $this->form->bind($request->getParameter('mem')); if ($this->form->isValid() && $this->form->getValue('member_class_id') != 0 && $this->form->getValue('flight_status')) { if ($request->getParameter('person_id')) { $member->setPersonId($request->getParameter('person_id')); } $person = PersonPeer::retrieveByPK($request->getParameter('person_id')); $member->setDateOfBirth($this->form->getValue('date_of_birth')); $member->setWeight($this->form->getValue('weight')); $member->setLanguages($this->form->getValue('languages')); if ($this->form->getValue('wing_id') == 0) { $member->setWingId(null); } else { $member->setWingId($this->form->getValue('wing_id')); } if ($this->form->getValue('secondary_wing_id') == 0) { $member->setSecondaryWingId(null); } else { $member->setSecondaryWingId($this->form->getValue('secondary_wing_id')); } // setting external_id $c = new Criteria(); $c->add(MemberPeer::EXTERNAL_ID, NULL, Criteria::ISNOTNULL); $c->addDescendingOrderByColumn(MemberPeer::ID); $external_member = MemberPeer::doSelectOne($c); $external_id = $external_member->getExternalId(); $currentExternalId = $external_id + 1; $member->setExternalId($currentExternalId); $member->setJoinDate($this->form->getValue('join_date')); $member->setMemberClassId($this->form->getValue('member_class_id')); $member->setSpouseName($this->form->getValue('spouse_name')); $member->setCoordinatorNotes($this->form->getValue('coordinator_notes')); $member->setFlightStatus($this->form->getValue('flight_status')); if ($this->form->getValue('co_pilot') == null) { $member->setCoPilot(0); } else { $member->setCoPilot($this->form->getValue('co_pilot')); } $new_member = $member->isNew(); if ($new_member) { $content = $this->getUser()->getName() . ' added new Member: ' . $person->getFirstName(); ActivityPeer::log($content); } $member->save(); // $wing_jobs = $request->getParameter('wing_job1[]'); if ($request->getParameter('wing_job1')) { $old_mem_wing_jobs = MemberWingJobPeer::getWingJob($member->getId()); foreach ($old_mem_wing_jobs as $old_mem_wing_job) { $old_mem_wing_job->delete(); } foreach ($wing_jobs as $wing_job) { $member_wing_job = new MemberWingJob(); $member_wing_job->setMemberId($member->getId()); $member_wing_job->setWingJobId($wing_job); $member_wing_job->save(); } } if (strtolower($member->getFlightStatus()) == 'command pilot') { // if command-pilot then make pilot $pilot = new Pilot(); $pilot->setMemberId($member->getId()); $pilot->setLicenseType('Default'); $pilot->setIfr(0); $pilot->setMultiEngine(0); $pilot->save(); $new_pilot = true; } else { $new_pilot = false; } $this->getUser()->setFlash('success', $success); $last = $request->getParameter('back'); $back_url = 'member/index'; if (strstr($last, 'member/edit')) { $back_url = 'member/index'; } elseif (strstr($last, 'person/view')) { if (isset($this->person)) { $back_url = '@person_view?id=' . $this->person->getId(); } } elseif (strstr($last, 'member/view')) { $back_url = '@member_view?id=' . $member->getId(); } //with error validation back url if (strstr($request->getReferer(), 'last/1')) { $back_url = '@person_view?id=' . $this->person->getId(); } elseif (strstr($request->getReferer(), 'last/2')) { $back_url = 'member/index'; } elseif (strstr($request->getReferer(), 'last/3')) { $back_url = '@member_view?id=' . $member->getId(); } // role for new member if ($new_member) { $role = '@Member'; $member_role = RolePeer::getByTitle($role); if ($member_role) { $person_role = new PersonRole(); $person_role->setPersonId($this->person->getId()); $person_role->setRoleId($member_role->getId()); $person_role->save(); } else { $url = $this->generateUrl('default_index', array('module' => 'role'), true); $this->getUser()->setFlash('warning', '"' . $role . '" role not found! Please navigate ' . $url . ' and fix.'); } } // role for new pilot if ($new_pilot) { $role = '@Pilot'; $pilot_role = RolePeer::getByTitle($role); $is_true = PersonRolePeer::getIsTrue($this->person->getId(), $pilot_role->getId()); if (!$is_true) { $person_role = new PersonRole(); $person_role->setPersonId($this->person->getId()); $person_role->setRoleId($pilot_role->getId()); $person_role->save(); } else { $url = $this->generateUrl('default_index', array('module' => 'role'), true); //$this->getUser()->setFlash('warning', '"'.$role.'" role not found! Please navigate '.$url.' and fix'); } } $this->redirect($back_url); } else { $this->getUser()->setFlash('warning', 'Please confirm flight status and choose the member class!'); } } else { # Set referer URL $this->referer = $request->getReferer() ? $request->getReferer() : 'member/index'; } $this->member = $member; }