d09c246cc30950fcb6f478315aecd4412adce640
[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         $this->_translator = $translator;
68         $this->_textDomain = $textDomain;
69     }
70
71     public function getTranslator()
72     {
73         return $this->_translator;
74     }
75
76     public function hasTranslator()
77     {
78         return $this->_translator !== null;
79     }
80
81     public function setTranslatorEnabled($enabled = true)
82     {
83         $this->_translator_enabled = $enabed;
84     }
85
86     public function isTranslatorEnabled()
87     {
88         return $this->_translator_enabled;
89     }
90
91     public function setTranslatorTextDomain($textDomain = 'default')
92     {
93         $this->_textDomain = $textDomain;
94     }
95
96     public function getTranslatorTextDomain()
97     {
98         return $this->_textDomain;
99     }
100
101
102     protected function setMethod($method='post')
103     {
104         $this->setAttribute('method', $method);
105     }
106
107 }