TeacherForm.php 3.28 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
<?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\TeacherForm\Action;

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

class TeacherForm
{
    /**
     * @var Twig
     */
    protected $view;
    /**
     *
     * @var TeacherFormServiceInterface
     */
    protected $TeacherFormService;
    /**
     *
     * @var InputFilterInterface
     */
    protected $TeacherFormInputFilter;
    /**
     *
     * @var AuthenticationServiceInterface
     */
    protected $authService;
    /**
     *
     * @var type SLIM application container
     */
    protected $container;
    /**
     *
     * @var string
     */
    protected $successUrl;

    public function __construct(
    Twig $view, TeacherFormServiceInterface $TeacherFormService, InputFilterInterface $TeacherFormInputFilter, $successUrl, $container

    ) {
        $this->view                     = $view;
        $this->TeacherFormService       = $TeacherFormService;
        $this->TeacherFormInputFilter   = $TeacherFormInputFilter;
        $this->successUrl               = $successUrl;
        $this->container                = $container;
    }

    public function __invoke(Request $req, Response $res)
    {
        if ($req->isPost()) {
            $reqParams = array_map(function($string) use ($link) { return htmlspecialchars($string); }, $req->getParams());
            $this->TeacherFormInputFilter->setData($reqParams);
            $isValid = $this->TeacherFormInputFilter->isValid();

            if ($isValid) {
                $data                             = $this->TeacherFormInputFilter->getValues();
                $TeacherForm                      = $this->TeacherFormService->submit($data, $reqParams);
                $_SESSION['teacherForm']['tForm'] = $TeacherForm;
                $res                              = $res->withRedirect($this->successUrl);

                return $res;
            } else {

                $this->view['form'] = [
                'is_valid'   => $isValid,
                'values'     => $this->TeacherFormInputFilter->getValues(),
                'raw_values' => $this->TeacherFormInputFilter->getRawValues(),
                'messages'   => $this->TeacherFormInputFilter->getMessages(),
                'branches'   => array_map(function ($branch) {
                 return ['value' => $branch['id'], 'label' => $branch['name']];
                }, $this->TeacherFormService->getBranches()),
                ];
                $res = $this->view->render($res, 'teacher_form/form.twig', []);
              }
        } else {
            $this->view['form'] = [
           'branches'  => array_map(function ($branch) {
               return ['value' => $branch['id'], 'label' => $branch['name']];
           }, $this->TeacherFormService->getBranches()),
           ];
            $res = $this->view->render($res, 'teacher_form/form.twig', []);
        }
        return $res;
    }
}