public function __construct(SchoolServiceInterface $schoolService) { $id = new Input('id'); $id->setRequired(false)->getValidatorChain()->attach(new Validator\Digits()); $registryNo = new Input('registry_no'); $registryNo->setRequired(true)->getValidatorChain()->attach(new Validator\NotEmpty())->attach(new Validator\Callback(['callback' => function ($value, $context = []) use($schoolService) { $id = isset($context['id']) ? $context['id'] : false; if (false === $id) { throw new Exception('No id in context'); } try { $school = $schoolService->findSchoolByRegistryNo($value); if (!$school || isset($school['id']) && !$school['id']) { return true; } return $school['id'] == $id; } catch (Exception $ex) { return false; } }, 'messageTemplate' => 'Το σχολείο με κωδικό %value% υπάρχει ήδη'])); $name = new Input('name'); $name->setRequired(true)->getFilterChain()->attach(new Filter\StripTags())->attach(new Filter\StringTrim()); $name->getValidatorChain()->attach(new Validator\NotEmpty()); $streetAddress = new Input('street_address'); $streetAddress->setRequired(false)->getFilterChain()->attach(new Filter\StripTags())->attach(new Filter\StringTrim()); $postalCode = new Input('postal_code'); $postalCode->setRequired(false)->getFilterChain()->attach(new Filter\Digits()); $phoneNumber = new Input('phone_number'); $phoneNumber->setRequired(false)->getFilterChain()->attach(new Filter\Digits()); $faxNumber = new Input('fax_number'); $faxNumber->setRequired(false)->getFilterChain()->attach(new Filter\Digits()); $email = new Input('email'); $email->setRequired(false)->getValidatorChain()->attach(new Validator\EmailAddress(['useDomainCheck' => false])); $municipality = new Input('municipality'); $municipality->setRequired(false)->getFilterChain()->attach(new Filter\StripTags())->attach(new Filter\StringTrim()); $schoolTypeId = new Input('schooltype_id'); $schoolTypeId->setRequired(true)->getFilterChain()->attach(new Filter\ToInt()); $prefectureId = new Input('prefecture_id'); $prefectureId->setRequired(true)->getFilterChain()->attach(new Filter\ToInt()); $educationlevelId = new Input('educationlevel_id'); $educationlevelId->setRequired(true)->getFilterChain()->attach(new Filter\ToInt()); $eduadminId = new Input('eduadmin_id'); $eduadminId->setRequired(false)->getFilterChain()->attach(new Filter\ToInt()); $creator = new Input('creator'); $creator->setRequired(true)->getValidatorChain()->attach(new Validator\EmailAddress(['useDomainCheck' => false])); $this->inputFilter = new InputFilter(); $this->inputFilter->add($id)->add($registryNo)->add($name)->add($streetAddress)->add($postalCode)->add($phoneNumber)->add($faxNumber)->add($email)->add($municipality)->add($schoolTypeId)->add($prefectureId)->add($educationlevelId)->add($eduadminId)->add($creator); }
public function __invoke($school_id) { $school = $this->schoolService->getSchool($school_id); try { $equipment = $this->inventoryService->getUnitEquipment($school['registry_no']); } catch (Exception $e) { $this->logger->error(sprintf('Problem retrieving assets from inventory for school %s', $school_id)); $this->logger->debug('Exception', [$e->getMessage(), $e->getTraceAsString()]); return false; } $labTypes = $this->getLabTypes(); $assetTypes = $this->getAssetTypes(); try { $locations = $this->getLocations($school_id, $equipment, $labTypes, $assetTypes); R::storeAll($locations); $this->logger->info(sprintf('Add assets from inventory for school %s', $school_id)); return $this->labService->getLabsBySchoolId($school_id); } catch (Exception $e) { $this->logger->error(sprintf('Problem inserting assets for school %s in database', $school_id)); $this->logger->debug('Exception', [$e->getMessage(), $e->getTraceAsString()]); return false; } }
public function __invoke(Request $req, Response $res, callable $next) { $res = $next($req, $res); $identity = $this->authService->getIdentity(); if (!$identity) { return $res; } $registryNo = $this->findUnitRegitryNo($identity); if (null === $registryNo) { $this->logger->error(sprintf('Unit for user %s not found in LDAP', $identity->mail), $identity->toArray()); return $this->logoutAndRediret($res, sprintf('Το σχολείο με email %s δεν βρέθηκε στο Κατάλογο του ΠΣΔ. <a href="%s" title="SSO logout">SSO Logout</a>', $identity->mail, $this->ssoLogoutUrl)); } $school = R::findOne('school', 'registry_no = ?', [$registryNo]); try { if (!$school) { $unit = call_user_func($this->fetchUnit, $registryNo); if (null === $unit) { $mmId = $this->findUnitMmId($identity); $unit = call_user_func($this->fetchUnitFromMMById, $mmId); } if (null === $unit) { $this->logger->error(sprintf('Unit with %s for user %s not found in MM', $identity->mail, $registryNo)); $this->logger->debug('Trace', ['registryNo' => $registryNo, 'mmId' => $mmId, 'identity' => $identity->toArray()]); return $this->logoutAndRediret($res, sprintf('Το σχολείο με κωδικό %s δεν βρέθηκε στο Μητρώο Μονάδων του ΠΣΔ. <a href="%s" title="SSO logout">SSO Logout</a>', $registryNo, $this->ssoLogoutUrl)); } $data = ['id' => '', 'registry_no' => $registryNo, 'name' => $unit['name'], 'street_address' => $unit['street_address'], 'postal_code' => $unit['postal_code'], 'phone_number' => $unit['phone_number'], 'fax_number' => $unit['fax_number'], 'email' => $identity->mail, 'municipality' => $unit['municipality'], 'schooltype_id' => $unit['unit_type_id'], 'prefecture_id' => $unit['prefecture_id'], 'educationlevel_id' => $unit['education_level_id'], 'eduadmin_id' => $unit['edu_admin_id'], 'creator' => $identity->mail]; $filtered = call_user_func($this->schoolInputFilter, $data); if (!$filtered['is_valid']) { $this->logger->error('Invalid data', $filtered); throw new Exception('Invalid data'); } $school = $this->schoolService->createSchool($filtered['values']); $this->logger->info(sprintf('School %s imported from MM to database', $registryNo), $filtered['values']); } $user = R::load('user', $identity->id); $user->school_id = $school['id']; R::store($user); $this->logger->info(sprintf('Set school %s to user %s', $registryNo, $identity->mail)); } catch (Exception $e) { $this->logger->error(sprintf('Problem inserting school %s form MM in database', $registryNo)); $this->logger->debug('Exception', [$e->getMessage(), $e->getTraceAsString()]); return $this->logoutAndRediret($res, sprintf('A problem occured fetching school data. <a href="%s" title="SSO logout">SSO Logout</a>', $this->ssoLogoutUrl)); } return $res; }