Correction sur le formulaire
[zf2.biz/galerie.git] / vendor / zf2biz / Custom / Form / AbstractForm.php
1 <?php
2
3 namespace Custom\Form;
4
5 use Zend\Form\Form;
6
7 use Zend\I18n\Translator\TranslatorAwareInterface;
8 use Zend\I18n\Translator\Translator;
9
10
11 abstract class AbstractForm extends Form implements TranslatorAwareInterface
12 {
13
14     private $_translator = null;
15     private $_textDomain = 'default';
16     private $_translator_enabled = false; 
17
18
19     abstract public function initialize();
20
21
22
23
24     public function translate($k)
25     {
26         if ($this->_translator && $this->_translator_enabled) {
27             return $this->_translator->translate($k, $this->_textDomain);
28         }
29         return $k;
30     }
31
32
33
34
35
36     protected function addElements(array $paramsArray)
37     {
38         foreach($paramsArray as $params) {
39             $this->add($params);
40         }
41     }
42
43
44     protected function addElement($name, $type='text', $label=null, array $attributes=array(), array $options=array())
45     {
46         if ($type) {
47             $attributes['type'] = $type;
48         }
49         if ($label) {
50             $options['label'] = $label;
51         }
52         $params = array('name' => $name);
53         if ($attributes) {
54             $params['attributes'] = $attributes;
55         }
56         if ($options) {
57             $params['options'] = $options;
58         }
59         $this->add($params);
60     }
61
62
63
64
65     public function setTranslator(Translator $translator = null, $textDomain = null)
66     {
67         if ($translator) {
68             $this->_translator = $translator;
69             $this->_translator_enabled = true;
70         }
71         if ($textDomain) {
72             $this->_textDomain = $textDomain;
73         }
74     }
75
76     public function getTranslator()
77     {
78         return $this->_translator;
79     }
80
81     public function hasTranslator()
82     {
83         return $this->_translator !== null;
84     }
85
86     public function setTranslatorEnabled($enabled = true)
87     {
88         $this->_translator_enabled = $enabled;
89     }
90
91     public function isTranslatorEnabled()
92     {
93         return $this->_translator_enabled;
94     }
95
96     public function setTranslatorTextDomain($textDomain = 'default')
97     {
98         $this->_textDomain = $textDomain;
99     }
100
101     public function getTranslatorTextDomain()
102     {
103         return $this->_textDomain;
104     }
105
106
107     protected function setMethod($method='post')
108     {
109         $this->setAttribute('method', $method);
110     }
111
112 }