UniversityForm.php 3.4 KB
Newer Older
Aspasia Katsi's avatar
Aspasia Katsi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
<?php
/**
 * gredu_labs.
 *
 * @link https://github.com/eellak/gredu_labs for the canonical source repository
 *
 * @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
 * @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
 */
namespace GrEduLabs\UniversityForm\Action;

use GrEduLabs\UniversityForm\Service\UniversityFormServiceInterface;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Views\Twig;
use Zend\Authentication\AuthenticationServiceInterface;
use Zend\InputFilter\InputFilterInterface;

class UniversityForm
{
    /**
     * @var Twig
     */
    protected $view;
    /**
     *
     * @var UniversityFormServiceInterface
     */
    protected $UniversityFormService;
    /**
     *
     * @var InputFilterInterface
     */
    protected $UniversityFormInputFilter;
    /**
     *
     * @var AuthenticationServiceInterface
     */
    protected $authService;
    /**
     *
     * @var type SLIM application container
     */
    protected $container;
    /**
     *
     * @var string
     */
    protected $successUrl;

    public function __construct(
Aspasia Katsi's avatar
Aspasia Katsi committed
52
    Twig $view, UniversityFormServiceInterface $UniversityFormService, InputFilterInterface $UniversityFormInputFilter2,InputFilterInterface $UniversityFormInputFilter1, $successUrl,$container
Aspasia Katsi's avatar
Aspasia Katsi committed
53 54 55 56
    )
     {
        $this->view                    = $view;
        $this->UniversityFormService   = $UniversityFormService;
Aspasia Katsi's avatar
Aspasia Katsi committed
57 58 59
        $this->UniversityFormInputFilter = $UniversityFormInputFilter1;
        $this->UniversityFormInputFilter2 = $UniversityFormInputFilter2;
        $this->UniversityFormInputFilter1 = $UniversityFormInputFilter1;
Aspasia Katsi's avatar
Aspasia Katsi committed
60 61 62 63 64 65 66 67 68 69 70
        $this->successUrl =             $successUrl;
        $this->container               = $container;
    }

    public function __invoke(Request $req, Response $res)
    {

       
        if ($req->isPost()) {
            $reqParams = $req->getParams();

Aspasia Katsi's avatar
Aspasia Katsi committed
71 72 73 74 75 76 77 78 79
            if (isset($reqParams['newselect'])&& $reqParams['newselect']=='ereunitika')
            {
                $this->UniversityFormInputFilter = $this->UniversityFormInputFilter2;
            }
            else    
            {
                $this->UniversityFormInputFilter = $this->UniversityFormInputFilter1;
            }

Aspasia Katsi's avatar
Aspasia Katsi committed
80 81
            $this->UniversityFormInputFilter->setData($reqParams);
            $isValid = $this->UniversityFormInputFilter->isValid();      
Aspasia Katsi's avatar
Aspasia Katsi committed
82

Aspasia Katsi's avatar
Aspasia Katsi committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
            if ($isValid) {
                
                $data =                            $this->UniversityFormInputFilter->getValues();
                $UniversityForm =                  $this->UniversityFormService->submit($data);
                $_SESSION['UnivForm']['uForm'] =   $UniversityForm;
                $res =                             $res->withRedirect($this->successUrl);
               return $res;
           } 
           else {
                
                $this->view['form'] = [

                'is_valid' => $isValid,
                'values' => $this->UniversityFormInputFilter->getValues(),
                'raw_values' => $this->UniversityFormInputFilter->getRawValues(),
                'messages' => $this->UniversityFormInputFilter->getMessages()
            ];
            $res = $this->view->render($res, 'university_form/form.twig', []);
                } 
Aspasia Katsi's avatar
Aspasia Katsi committed
102
          
Aspasia Katsi's avatar
Aspasia Katsi committed
103 104 105 106 107 108 109 110 111 112
    }
    else
    {
        $res = $this->view->render($res, 'university_form/form.twig', [
                ]);
       return $res;

    }
}
}