コード例 #1
0
ファイル: SchoolAsset.php プロジェクト: eellak/gredu_labs
 public function __construct(LabServiceInterface $labService, AssetServiceInterface $assetsService)
 {
     $id = new Input('id');
     $id->setRequired(false)->getValidatorChain()->attach(new Validator\Digits());
     $itemCategoryId = new Input('itemcategory_id');
     $itemCategoryId->setRequired(true)->getValidatorChain()->attach(new Validator\NotEmpty())->attach(new Validator\Callback(['callback' => function ($value) use($assetsService) {
         try {
             $itemCategory = $assetsService->getItemCategoryById($value);
             return isset($itemCategory['id']) && $itemCategory['id'] == $value;
         } catch (Exception $ex) {
             return false;
         }
     }, 'message' => 'Ο τύπος δεν βρέθηκε']));
     $labId = new Input('lab_id');
     $labId->setRequired(true)->getFilterChain()->attach(new Filter\ToInt());
     $labId->getValidatorChain()->attach(new Validator\NotEmpty())->attach(new Validator\Callback(['callback' => function ($value) use($labService) {
         try {
             $lab = $labService->getLabById($value);
             return isset($lab['id']) && $lab['id'] == $value;
         } catch (Exception $ex) {
             return false;
         }
     }, 'message' => 'Το εργαστήριο δεν βρέθηκε']));
     $qty = new Input('qty');
     $qty->setRequired(true)->getFilterChain()->attach(new Filter\Digits())->attach(new Filter\ToInt());
     $qty->getValidatorChain()->attach(new Validator\NotEmpty())->attach(new Validator\GreaterThan(['min' => 0]));
     $acquisitionYear = new Input('acquisition_year');
     $acquisitionYear->setRequired(false)->getFilterChain()->attach(new Filter\Digits());
     $acquisitionYear->getValidatorChain()->attach(new Validator\Date(['format' => 'Y']))->attach(new Validator\LessThan(['max' => date('Y'), 'inclusive' => true]));
     $comments = new Input('comments');
     $comments->setRequired(false)->getFilterChain()->attach(new Filter\StripTags())->attach(new Filter\StringTrim());
     $this->inputFilter = new InputFilter();
     $this->inputFilter->add($id)->add($labId)->add($itemCategoryId)->add($qty)->add($acquisitionYear)->add($comments);
 }
コード例 #2
0
 public function __construct(LabServiceInterface $labService, AssetServiceInterface $assetsService)
 {
     $labId = new Input('lab_id');
     $labId->setRequired(true)->getFilterChain()->attach(new Filter\ToInt());
     $labId->getValidatorChain()->attach(new Validator\NotEmpty());
     $itemCategoryId = new Input('itemcategory_id');
     $itemCategoryId->setRequired(true)->getFilterChain()->attach(new Filter\ToInt());
     $itemCategoryId->getValidatorChain()->attach(new Validator\NotEmpty())->attach(new Validator\Callback(['callback' => function ($value) use($assetsService) {
         try {
             $type = $assetsService->getItemCategoryById($value);
             return $type && $type['id'] == $value;
         } catch (Exception $ex) {
             return false;
         }
     }, 'message' => 'Ο τύπος εξοπλισμού δεν είναι έγκυρος']));
     $qty = new Input('qty');
     $qty->setRequired(true)->getFilterChain()->attach(new Filter\ToInt());
     $qty->getValidatorChain()->attach(new Validator\NotEmpty())->attach(new Validator\GreaterThan(['min' => 0]));
     $qtyacquired = new Input('qtyacquired');
     $qtyacquired->setRequired(true)->getFilterChain()->attach(new Filter\ToInt());
     $qtyacquired->getValidatorChain()->attach(new Validator\NotEmpty())->attach(new Validator\GreaterThan(['min' => 0, 'inclusive' => true]));
     $reasons = new Input('reasons');
     $reasons->setRequired(true)->getFilterChain()->attach(new Filter\StripTags())->attach(new Filter\StringTrim());
     $reasons->getValidatorChain()->attach(new Validator\NotEmpty());
     $this->add($labId)->add($itemCategoryId)->add($qty)->add($qtyacquired)->add($reasons);
 }
コード例 #3
0
 private function getAssetTypes()
 {
     return array_reduce($this->assetService->getAllItemCategories(), function ($map, $type) {
         $map[trim($type['name'])] = $type['id'];
         return $map;
     }, []);
 }
コード例 #4
0
 public function __invoke(Request $req, Response $res)
 {
     $school = $req->getAttribute('school');
     if ($req->isPost()) {
         $this->appFormInputFilter->setData(array_merge($req->getParams(), ['school_id' => $school->id, 'submitted_by' => $this->authService->getIdentity()->mail]));
         $isValid = $this->appFormInputFilter->isValid();
         if ($isValid) {
             $data = $this->appFormInputFilter->getValues();
             $appForm = $this->appFormService->submit($data);
             $_SESSION['applicationForm']['appForm'] = $appForm;
             $res = $res->withRedirect($this->successUrl);
             return $res;
         }
         $this->view['form'] = ['is_valid' => $isValid, 'values' => $this->appFormInputFilter->getValues(), 'raw_values' => $this->appFormInputFilter->getRawValues(), 'messages' => $this->appFormInputFilter->getMessages()];
     }
     $loadForm = (bool) $req->getParam('load', false);
     $this->view['choose'] = !$loadForm && !$req->isPost();
     if (!$req->isPost() && $loadForm) {
         if (null !== ($appForm = $this->appFormService->findSchoolApplicationForm($school->id))) {
             $this->view['form'] = ['values' => $appForm];
         }
     }
     $labs = $this->labService->getLabsBySchoolId($school->id);
     $res = $this->view->render($res, 'application_form/form.twig', ['lab_choices' => array_map(function ($lab) {
         return ['value' => $lab['id'], 'label' => $lab['name']];
     }, $labs), 'type_choices' => array_map(function ($category) {
         return ['value' => $category['id'], 'label' => $category['name']];
     }, $this->assetsService->getAllItemCategories())]);
     return $res;
 }
コード例 #5
0
ファイル: ListAssets.php プロジェクト: kanellov/gredu_labs
 public function __invoke(Request $req, Response $res, array $args = [])
 {
     $school = $req->getAttribute('school', false);
     if (!$school) {
         return $res->withStatus(403, 'No school');
     }
     $assets = $this->schoolAssetsService->getAssetsForSchool($school->id);
     $itemCategories = $this->assetsService->getAllItemCategories();
     $labs = $this->labService->getLabsBySchoolId($school->id);
     return $this->view->render($res, 'schools/assets.twig', ['school' => $school, 'assets' => $assets, 'item_categories' => array_map(function ($category) {
         return ['value' => $category['id'], 'label' => $category['name']];
     }, $itemCategories), 'labs' => array_map(function ($lab) {
         return ['value' => $lab['id'], 'label' => $lab['name']];
     }, array_filter($labs, function ($lab) {
         return $lab['is_new'] !== "1";
     }))]);
 }
コード例 #6
0
ファイル: ApplicationForm.php プロジェクト: eellak/gredu_labs
 public function __invoke(Request $req, Response $res)
 {
     $school = $req->getAttribute('school');
     if ($req->isPost()) {
         $reqParams = $req->getParams();
         array_splice($reqParams['items'], 0, 0);
         $this->appFormInputFilter->setData(array_merge($reqParams, ['school_id' => $school->id, 'submitted_by' => $this->authService->getIdentity()->mail]));
         $isValid = $this->appFormInputFilter->isValid();
         if ($isValid) {
             $data = $this->appFormInputFilter->getValues();
             $appForm = $this->appFormService->submit($data);
             $_SESSION['applicationForm']['appForm'] = $appForm;
             $res = $res->withRedirect($this->successUrl);
             return $res;
         }
         $this->view['form'] = ['is_valid' => $isValid, 'values' => $this->appFormInputFilter->getValues(), 'raw_values' => $this->appFormInputFilter->getRawValues(), 'messages' => $this->appFormInputFilter->getMessages()];
     }
     $loadForm = (bool) $req->getParam('load', false);
     $this->view['choose'] = !$loadForm && !$req->isPost();
     if (!$req->isPost() && $loadForm) {
         // take care of new options in applications and migrate existing ones
         if (null !== ($appForm = $this->appFormService->findSchoolApplicationForm($school->id))) {
             /**
              * Do mapping of old items to new only if items do exist (old form) 
              * and the map is available at the app settings.
              * TODO: Only one version migrations are supported. If the old items are
              * two or more versions older, they will not be handled.
              */
             // get the existing (db) application form version
             $items_version = $this->version;
             if (isset($appForm['items']) && \count($appForm['items']) > 0) {
                 $items_version = array_values($appForm['items'])[0]['version'];
             }
             if ($this->version != $items_version && isset($appForm['items']) && isset($this->container['settings']['application_form']['itemcategory']['map']) && $this->container['settings']['application_form']['itemcategory']['map']['fromversion'] == $items_version && $this->container['settings']['application_form']['itemcategory']['map']['toversion'] == $this->version && isset($this->container['settings']['application_form']['itemcategory']['map']['items'])) {
                 // if map exists for this version, use it
                 $items_map = $this->container['settings']['application_form']['itemcategory']['map']['items'];
                 $appForm['items'] = array_map(function ($item) use($items_map) {
                     $migrate_values = [];
                     if (isset($items_map[$item['itemcategory_id']]) && intval($items_map[$item['itemcategory_id']]) > 0) {
                         $migrate_values = ['itemcategory_prev' => $item['itemcategory_id'], 'itemcategory_id_prev' => $item['itemcategory_id'], 'itemcategory_id' => intval($items_map[$item['itemcategory_id']])];
                     } else {
                         $migrate_values = ['itemcategory_prev' => '', 'itemcategory_id_prev' => -1];
                     }
                     $migrate_values['prev_form_load'] = true;
                     return array_merge($item, $migrate_values);
                 }, $appForm['items']);
             } elseif ($this->version != $items_version && isset($appForm['items']) && isset($this->container['settings']['application_form']['itemcategory']['map']) && ($this->container['settings']['application_form']['itemcategory']['map']['fromversion'] != $items_version || $this->container['settings']['application_form']['itemcategory']['map']['toversion'] != $this->version)) {
                 // if map does not exist for this version, notify user
                 $items_map = $this->container['settings']['application_form']['itemcategory']['map']['items'];
                 $appForm['items'] = array_map(function ($item) use($items_map) {
                     $migrate_values = ['itemcategory_prev' => '', 'itemcategory_id_prev' => -2, 'prev_form_load' => true];
                     return array_merge($item, $migrate_values);
                 }, $appForm['items']);
             }
             $this->view['form'] = ['values' => $appForm];
         }
     }
     $labs = $this->labService->getLabsBySchoolId($school->id);
     $res = $this->view->render($res, 'application_form/form.twig', ['lab_choices' => array_map(function ($lab) {
         return ['value' => $lab['id'], 'label' => $lab['name']];
     }, $labs), 'type_choices' => array_map(function ($category) {
         return ['value' => $category['id'], 'label' => $category['name']];
     }, $this->assetsService->getAllItemCategories($this->version))]);
     return $res;
 }