Rajout de la gestion des formulaires dans \Custom
[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 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     protected function addElement($name, $type='text', $label=null, $attributes=array(), $options=array())
36     {
37         if ($type) {
38             $attributes['type' = $type
39         }
40         if ($label) {
41             $options['label'] = $label;
42         }
43         $params = array('name' => $name);
44         if ($attributes) {
45             $params['attributes'] = $attributes;
46         }
47         if ($options) {
48             $params['options'] = $options;
49         }
50         $this->add($params);
51     }
52
53
54
55
56     public function setTranslator(Translator $translator = null, $textDomain = null)
57     {
58         $this->_translator = $translator;
59         $this->_textDomain = $textDomain;
60     }
61
62     public function getTranslator()
63     {
64         return $this->_translator;
65     }
66
67     public function hasTranslator()
68     {
69         return $this->_translator !== null;
70     }
71
72     public function setTranslatorEnabled($enabled = true)
73     {
74         $this->_translator_enabled = $enabed;
75     }
76
77     public function isTranslatorEnabled()
78     {
79         return $this->_translator_enabled;
80     }
81
82     public function setTranslatorTextDomain($textDomain = 'default')
83     {
84         $this->_textDomain = $textDomain;
85     }
86
87     public function getTranslatorTextDomain()
88     {
89         return $this->_textDomain;
90     }
91
92
93     protected function setMethod($method='post')
94     {
95         $this->setAttribute('method', $method);
96     }
97
98 }