Form   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 18
lcom 1
cbo 5
dl 0
loc 130
ccs 0
cts 96
cp 0
rs 10
c 2
b 1
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getKeyFields() 0 4 1
A getFormService() 0 4 1
A getForm() 0 4 1
A __invoke() 0 8 1
A renderform() 0 17 1
B renderFormMessages() 0 26 6
A prepareOriginalFields() 0 17 3
A camel() 0 5 1
A dash() 0 5 1
A setFormService() 0 5 1
A setForm() 0 5 1
1
<?php
2
namespace SpeckCatalog\View\Helper;
3
4
use Zend\View\Helper\HelperInterface;
5
use Zend\View\Model\ViewModel;
6
use Zend\View\Helper\AbstractHelper;
7
use SpeckCatalog\Service\FormServiceAwareInterface;
8
use SpeckCatalog\Model\AbstractModel;
9
10
class Form extends AbstractHelper implements FormServiceAwareInterface
11
{
12
    protected $formService;
13
14
    protected $partialDir = "/speck-catalog/catalog-manager/partial/form/";
15
16
    protected $form;
17
18
    protected $name;
19
20
    protected $model;
21
22
    public function __invoke(AbstractModel $model = null, $name = null)
23
    {
24
        $this->model = $model;
25
        $this->name = $name;
26
        $this->form = $this->getFormService()->getForm($name, $model);
27
28
        return $this;
29
    }
30
31
    public function renderform()
32
    {
33
        $this->form
34
            ->edit()
35
            ->prepare()
36
            ->setAttribute('id', $this->name)
37
            ->setAttribute('class', 'live-form');
38
39
        $view = new ViewModel(array(
40
            $this->camel($this->name) => $this->model,
41
            'form' => $this->form,
42
            'formErrors' => $this->renderFormMessages($this->form, false),
43
            'originalFields' => $this->prepareOriginalFields($this->form),
44
        ));
45
        $view->setTemplate($this->partialDir . $this->dash($this->name));
46
        return $this->getView()->render($view);
47
    }
48
49
    public function renderFormMessages($form, $showValid = true)
50
    {
51
        $html = '';
52
53
        if (!$form->isValid()) {
54
            $html .= '<div class="alert alert-error">';
55
            $html .= '<ul>';
56
            foreach ($form->getElements() as $element) {
57
                if (count($element->getMessages()) > 0) {
58
                    $html .= '<li>' . $element->getLabel() . ' - '
59
                        . implode(', ', $element->getMessages()) . '</li>';
60
                }
61
            }
62
            $html .= '</ul></div>';
63
        } elseif ($showValid) {
64
            $html .= '<div class="alert alert-success">';
65
            if ($form instanceof \SpeckCatalog\Form\Product) {
66
                $html .= 'All Data is valid!  <a href="#" class="save-product">Save Now</a>';
67
            } else {
68
                $html .= 'All Data is valid!  <a href="#" class="save-now">Save Now</a>';
69
            }
70
            $html .= '</div>';
71
        }
72
73
        return $html;
74
    }
75
76
    public function getKeyFields()
77
    {
78
        return $this->getFormService()->getKeyFields($this->name, $this->model);
79
    }
80
81
    public function prepareOriginalFields($form)
82
    {
83
        $html = '';
84
        if (!$form->isNew()) {
85
            foreach ($form->getOriginalFields() as $field) {
86
                $element = $form->get('original_' . $field);
87
                $html .= '<input type="hidden" name = "' . $element->getName()
88
                    . '" value="' . $element->getValue() . '" />';
89
            }
90
        } else {
91
            throw new \Exception(
92
                'form is not prepared for edit, cannot prepare'
93
                . ' original fields until edit method is called on the form'
94
            );
95
        }
96
        return $html;
97
    }
98
99
    protected function camel($name)
100
    {
101
        $camel = new \Zend\Filter\Word\UnderscoreToCamelCase;
102
        return lcfirst($camel->__invoke($name));
103
    }
104
105
    protected function dash($name)
106
    {
107
        $dash = new \Zend\Filter\Word\UnderscoreToDash;
108
        return $dash->__invoke($name);
109
    }
110
111
    public function getFormService()
112
    {
113
        return $this->formService;
114
    }
115
116
    public function setFormService($formService)
117
    {
118
        $this->formService = $formService;
119
        return $this;
120
    }
121
122
    /**
123
     * @return form
124
     */
125
    public function getForm()
126
    {
127
        return $this->form;
128
    }
129
130
    /**
131
     * @param $form
132
     * @return self
133
     */
134
    public function setForm($form)
135
    {
136
        $this->form = $form;
137
        return $this;
138
    }
139
}
140