LdapConnectViewHelper.php 1.44 KB
Newer Older
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
<?php

namespace UnicaenAuth\View\Helper;

use Zend\Form\Form;
use Zend\View\Helper\AbstractHelper;
use Zend\View\Renderer\PhpRenderer;
use Zend\View\Resolver\TemplatePathStack;

/**
 * Aide de vue dessinant le formulaire d'authentification LDAP,
 * si l'authentification LDAP est activée.
 *
 * @method PhpRenderer getView()
 * @author Unicaen
 */
class LdapConnectViewHelper extends AbstractHelper
{
    /**
     * @var bool
     */
    protected $enabled = true;

    /**
     * @var Form
     */
    protected $form;

    /**
     * @param bool $enabled
     * @return $this
     */
    public function setEnabled($enabled = true)
    {
        $this->enabled = $enabled;

        return $this;
    }

    /**
     * @param Form $form
     * @return $this
     */
    public function __invoke(Form $form)
    {
        $this->form = $form;

        $this->getView()->resolver()->attach(
            new TemplatePathStack(['script_paths' => [__DIR__ . "/partial"]])
        );

        return $this;
    }

    /**
     * @return string
     */
    public function __toString()
    {
        if (! $this->enabled) {
            return '';
        }

        try {
            return $this->getView()->render("ldap-connect", [
                'enabled' => $this->enabled,
                'form' => $this->form,
            ]);
        } catch (\Exception $e) {
            return '<p>' . $e->getMessage() . '</p><p>' . $e->getTraceAsString() . '</p>';
        }
    }
}