1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: kschr |
5
|
|
|
* Date: 3/20/2017 |
6
|
|
|
* Time: 4:54 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Magium\Configuration\View; |
10
|
|
|
|
11
|
|
|
use Zend\Form\Element; |
12
|
|
|
use Zend\Form\ElementInterface; |
13
|
|
|
use Zend\Form\View\Helper\FormElement; |
14
|
|
|
use Zend\Form\View\Helper\FormInput; |
15
|
|
|
use Zend\Form\View\Helper\FormSelect; |
16
|
|
|
use Zend\View\Helper\AbstractHelper; |
17
|
|
|
use Zend\View\Renderer\RendererInterface; |
18
|
|
|
|
19
|
|
|
class MagiumRenderer extends AbstractHelper |
20
|
|
|
{ |
21
|
1 |
|
public function __invoke($section, $group, $id, $options) |
22
|
|
|
{ |
23
|
1 |
|
$type = 'text'; |
24
|
1 |
|
if (isset($options['type'])) { |
25
|
1 |
|
$type = $options['type']; |
26
|
|
|
} |
27
|
1 |
|
$value = $options['value']; |
28
|
|
|
|
29
|
1 |
|
$viewClass = 'Zend\Form\View\Helper\Form' . ucfirst(strtolower($type)); |
30
|
1 |
|
$formClass = 'Zend\Form\Element\\' . ucfirst(strtolower($type)); |
31
|
1 |
|
$reflectionClass = new \ReflectionClass($viewClass); |
32
|
1 |
|
if (!$reflectionClass->isSubclassOf(AbstractHelper::class)) { |
33
|
|
|
throw new InvalidViewConfigurationException('Invalid setting input type'); |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
$instance = $reflectionClass->newInstance(); |
37
|
1 |
|
$formInstance = new \ReflectionClass($formClass); |
38
|
1 |
|
$this->configureFormView($instance); |
39
|
1 |
|
$name = sprintf('%s_%s_%s', $section, $group, $id); |
40
|
1 |
|
$formElement = $formInstance->newInstance($name, $options); |
41
|
|
|
/* @var $formElement Element */ |
42
|
1 |
|
if ($formElement instanceof Element\Select) { |
43
|
|
|
$formElement->setOptions(['options' => $options['source']]); |
44
|
|
|
} |
45
|
1 |
|
$formElement->setAttribute('onchange', 'magiumRegisterChange(event)'); |
46
|
1 |
|
$formElement->setAttribute('class', 'form-control'); |
47
|
1 |
|
$formElement->setAttribute('data-path', $options['path']); |
48
|
1 |
|
$formElement->setValue($value); |
49
|
1 |
|
$output = $instance->render($formElement); |
50
|
1 |
|
return $output; |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
protected function configureFormView(AbstractHelper $instance) |
54
|
|
|
{ |
55
|
1 |
|
$view = $this->getView(); |
56
|
1 |
|
if (!$view instanceof RendererInterface) { |
57
|
|
|
throw new InvalidViewConfigurationException('View is not the correct instance type'); |
58
|
|
|
} |
59
|
1 |
|
$instance->setView($view); |
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
|