public function lcformAction() { $id = $this->getEvent()->getRouteMatch()->getParam('id'); $subid = $this->getEvent()->getRouteMatch()->getParam('subparam'); $lecturermodule = ""; if ($subid) { $lecturermodule = $this->em->getRepository("\\Application\\Entity\\Lecturermodule")->find($subid); } $deptentity = $this->em->getRepository("\\Application\\Entity\\Staff")->findOneBy(array("fkUserid" => $this->userid)); //Get selected module information $module = $this->em->getRepository("\\Application\\Entity\\Classmodule")->find($id); //Get form $form = new \Application\Form\Lecturermodule($this->em, $deptentity->getFkDeptid()->getPkDeptid()); $form->bind($this->request->getPost()); if ($this->request->getPost('save')) { $form->setData($this->request->getPost()); if ($form->isValid()) { $formData = $form->getData(); //If request other departments option is not selected if ($formData['Lecturermodule']['fkStaffid'] != "-1") { if ($subid) { //Get existing record information $entity = $lecturermodule; } else { //Set new entity $entity = new \Application\Entity\Lecturermodule(); } //Delete serviced module first $this->exams->deletefromdb("\\Application\\Entity\\Servicedmodule", array("fkClassmoduleid" => $module->getPkClassmoduleid())); //Get selected staff entity $staffentity = $this->em->getRepository('\\Application\\Entity\\Staff')->find($formData['Lecturermodule']['fkStaffid']); $entity->setFkClassmoduleid($module); $entity->setFkStaffid($staffentity); $this->exams->saveLecturerModule($entity); $this->redirect()->toRoute("examination", array("action" => "lecturermodule")); } else { //Define custom validation $validator = new \Zend\Validator\Callback(function ($formvalue) { //Checking if department has been selected on not return empty($formvalue['fkReqdeptid']) ? false : true; }); $validator->setMessage("Select department"); if ($validator->isValid($formData)) { //Delete any lecturer assigned to the module $this->exams->deletefromdb("\\Application\\Entity\\Lecturermodule", array("fkClassmoduleid" => $module->getPkClassmoduleid())); //Get servicing department entity $servicingdeptentity = $this->em->getRepository('\\Application\\Entity\\Department')->find($formData['fkReqdeptid']); //If the module is already assigned update $currentmodule = $this->em->getRepository('\\Application\\Entity\\Servicedmodule')->findOneBy(array("fkClassmoduleid" => $module->getPkClassmoduleid())); if (count($currentmodule) > 0) { $entity = $currentmodule; } else { //Save serviced entity $entity = new \Application\Entity\Servicedmodule(); } $entity->setFkClassmoduleid($module); $entity->setReqdept($deptentity->getFkDeptid()); $entity->setServicingdept($servicingdeptentity); $entity->setFlag("REQUESTED"); if ($this->exams->saveServicedModule($entity)) { //If allocatre, send an email to hod } $this->redirect()->toRoute("examination", array("action" => "lecturermodule")); } else { $messages = $validator->getMessages(); $form->get('fkReqdeptid')->setMessages(array($messages['callbackValue'])); } } } } return new ViewModel(array("module" => $module, "form" => $form, "details" => $lecturermodule)); }