1: <?php
2:
3: /**
4: * Pry Framework
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file LICENSE.txt.
10: *
11: */
12:
13: namespace Pry\Form\Element;
14:
15: /**
16: * Element Autocompleter. Permet de définir un champs texte avec une auto complétion.
17: * @category Pry
18: * @package Form
19: * @subpackage Form_Element
20: * @version 1.5.1
21: * @author Olivier ROGER <oroger.fr>
22: *
23: *
24: */
25: class AutoCompleter extends Text
26: {
27:
28: /**
29: * Class css du div d'autocompletion
30: *
31: * @var string
32: * @access private
33: */
34: private $autoCompleteCss;
35:
36: /**
37: * Url du fichier fournissant les données
38: *
39: * @var string
40: * @access private
41: */
42: private $url;
43:
44: /**
45: * Nom du paramèter envoyé en post
46: *
47: * @var string
48: * @access private
49: */
50: private $paramName;
51:
52: /**
53: * Nombre de caractère mini avant déclenchement de l'autocompletion
54: *
55: * @var int
56: * @access private
57: */
58: private $minChar;
59:
60: /**
61: * Nombre de résultat maximum retourné
62: *
63: * @var int
64: * @access private
65: */
66: private $max;
67:
68: /**
69: * Fréquence de rafraichissement de l'input en ms
70: *
71: * @var float
72: * @access private
73: */
74: private $frequency;
75:
76: /**
77: * Paramètres additionnels
78: *
79: * @var array
80: */
81: private $params;
82:
83: /**
84: * Constructeur.
85: *
86: * @param string $nom
87: * @param Form_Form $form
88: */
89: public function __construct($nom, $form)
90: {
91: parent::__construct($nom, $form);
92: $this->autoCompleteCss = 'autocomplete';
93: $this->minChar = 2;
94: $this->max = 25;
95: $this->frequency = 400;
96: $this->params = '{';
97: }
98:
99: /**
100: * Défini la class css pour le div autocompletion
101: *
102: * @param string $css
103: * @return Form_Element_AutoCompleter
104: */
105: public function setAutoCompleteClass($css)
106: {
107: $this->autoCompleteCss = $css;
108: return $this;
109: }
110:
111: /**
112: * Défini l'adresse du fichier
113: *
114: * @param string $url
115: * @return Form_Element_AutoCompleter
116: */
117: public function setUrl($url)
118: {
119: $this->url = $url;
120: return $this;
121: }
122:
123: /**
124: * Défini le nom du paramètre envoyé
125: *
126: * @param string $name
127: * @return Form_Element_AutoCompleter
128: */
129: public function setParamName($name)
130: {
131: $this->paramName = $name;
132: return $this;
133: }
134:
135: /**
136: * Défini le nombre de caractère nécessaire
137: *
138: * @param int $char
139: * @return Form_Element_AutoCompleter
140: */
141: public function minCharNeeded($char)
142: {
143: $this->minChar = $char;
144: return $this;
145: }
146:
147: /**
148: * Taux de rafraichissement de l'input en sec
149: *
150: * @param float $frq
151: * @return Form_Element_AutoCompleter
152: */
153: public function refreshFrequency($frq)
154: {
155: $this->frequency = $frq;
156: return $this;
157: }
158:
159: public function setMaxResult($max)
160: {
161: $this->max = intval($max);
162: }
163:
164: /**
165: * Ajoute des paramètres
166: *
167: * @param array $tab
168: * @return Form_Element_AutoCompleter
169: */
170: public function addParams($tab)
171: {
172: foreach ($tab as $key => $value) {
173: $this->params .=$key . ':' . $value . ',';
174: }
175: $this->params = substr($this->params, 0, strlen($this->params) - 1);
176: return $this;
177: }
178:
179: /**
180: * Ecrit l'objet
181: *
182: * @return string
183: */
184: public function __toString()
185: {
186: $this->params.='}';
187: $field.=parent::__toString();
188: //$field.="\t".'</p><div id="'.$this->attrs['id'].'_autocomplete" class="'.$this->autoCompleteCss.'"></div><p>'."\n";
189: $this->form->javascript.='$("#' . $this->attrs['id'] . '").autocomplete("{source:' . $this->url . '",delay:' . $this->frequency . ',minLength:' . $this->minChar . '});';
190: return $field;
191: }
192:
193: }
194:
195: ?>