1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44:
45: class Form_Form
46: {
47: 48: 49: 50: 51: 52:
53: private $champs;
54:
55: 56: 57: 58: 59: 60:
61: private $champs_submit;
62:
63: 64: 65: 66: 67:
68: private $champs_hidden;
69:
70: 71: 72: 73: 74: 75:
76: private $posted_value;
77:
78: 79: 80: 81: 82: 83:
84: protected $attrs;
85:
86: 87: 88: 89: 90: 91:
92: protected $uniqid;
93:
94: 95: 96: 97: 98: 99:
100: protected static $instances = array();
101:
102: 103: 104: 105: 106: 107:
108: public $listTooltips;
109:
110: public $javascript;
111:
112: 113: 114: 115: 116: 117: 118:
119: public function __construct($uniqid,$method = 'post')
120: {
121: $this->champs = array();
122: $this->champs_submit = array();
123: $this->champs_hidden = array();
124: $this->attrs = array();
125: $this->listTooltips = array();
126:
127: if($uniqid !== FALSE && in_array($uniqid,self::$instances))
128: throw new Exception('Une instance de ce formulaire semble déjà exister');
129: else
130: {
131: $this->uniqid = $uniqid;
132: self::$instances[] = $this->uniqid;
133: $this->method($method);
134: $this->add('Hidden','uniqid')
135: ->value($this->uniqid);
136: if(!$this->isSubmited(true))
137: {
138: Util_Token::genToken(30);
139: $token = Util_Token::getToken();
140: }
141: else
142: $token = $_POST['csrf_protect'];
143: $this->add('Hidden','csrf_protect')->value($token);
144: }
145: }
146:
147: 148: 149: 150: 151: 152: 153: 154: 155:
156: public function isValid($post,$noSubmit = false)
157: {
158: if($this->isSubmited($noSubmit) && Util_Token::checkToken())
159: {
160: $valid = true;
161: foreach($this->champs as $objet)
162: {
163: $nom = $objet->getName();
164: if(strstr($nom,'[]'))
165: $nom = substr($nom,0,strlen($nom)-2);
166: if(!isset($post[$nom]))
167: $post[$nom] = null;
168: $valid = $objet->isValid($post[$nom]) && $valid;
169: }
170: return $valid;
171: }
172: return false;
173: }
174:
175:
176: 177: 178: 179: 180: 181: 182: 183:
184: public function add($type,$nom)
185: {
186: if(!isset($this->champs[$nom]))
187: {
188: $classChamps = 'Form_Element_'.$type;
189: $oChamps = new $classChamps($nom,$this);
190: if($type !='Html')
191: {
192: if($type=='Submit')
193: $this->champs_submit[$nom] = $oChamps;
194: elseif($type == 'Hidden')
195: $this->champs_hidden[$nom] = $oChamps;
196:
197: $this->champs[$nom] = $oChamps;
198: }
199: else
200: $this->champs[$nom.uniqid()] = $oChamps;
201: return $oChamps;
202: }
203: else
204: throw new UnexpectedValueException('Un champs avec le nom '.$nom.' existe déjà dans '.$this->uniqid);
205: }
206:
207: 208: 209: 210: 211: 212: 213:
214: public function action($action)
215: {
216: $this->attrs['action'] = $action;
217: return $this;
218: }
219:
220: 221: 222: 223: 224: 225: 226: 227:
228: public function setAttributes($nom,$valeur)
229: {
230: if(!isset($this->attrs[$nom]))
231: $this->attrs[$nom] = $valeur;
232: return $this;
233: }
234:
235: 236: 237: 238: 239: 240: 241:
242: public function setPostedValue($data)
243: {
244: if(is_array($data))
245: {
246: foreach($data as $name=>$valeur)
247: {
248:
249: $this->posted_value[$name] = $valeur;
250: }
251: }
252: return $this;
253: }
254:
255: 256: 257: 258: 259: 260: 261:
262: public function getPostedvalue($name)
263: {
264: if(isset($this->posted_value[$name]))
265: return $this->posted_value[$name];
266: else
267: return '';
268: }
269: 270: 271: 272: 273: 274: 275:
276: public function method($method)
277: {
278: $method = strtolower($method);
279: if(in_array($method,array('post','get')))
280: {
281: $this->attrs['method'] = $method;
282: return $this;
283: }
284: else
285: throw new Exception('Merci d\'utiliser post ou get');
286: }
287:
288: 289: 290: 291: 292: 293: 294:
295: public function enctype($txt)
296: {
297: if (in_array($txt, array('multipart/form-data', 'application/x-www-form-urlencoded')))
298: $this->attrs['enctype'] = $txt;
299: else
300: throw new InvalidArgumentException('Enctype non supporté.');
301: return $this;
302: }
303:
304: 305: 306: 307: 308: 309: 310:
311: private function isSubmited($noSubmit)
312: {
313: $methode = ($this->attrs['method']=='post')?$_POST:$_GET;
314: if(!empty($methode['uniqid']) && $methode['uniqid'] == $this->uniqid)
315: {
316: if($noSubmit)
317: return true;
318:
319: foreach($this->champs_submit as $submit)
320: {
321: if(isset($methode[$submit->getName()]))
322: return true;
323: }
324: }
325: return false;
326: }
327:
328: 329: 330: 331: 332:
333: private function attrsToString()
334: {
335: $attributs = '';
336: foreach($this->attrs as $key=>$value)
337: $attributs .= $key.'="'.$value.'" ';
338:
339: return $attributs;
340: }
341:
342: 343: 344: 345: 346:
347: private function fieldsToString()
348: {
349: $champsTxt = '';
350: foreach($this->champs as $champ)
351: $champsTxt .= $champ->__toString()."\n";
352:
353: return $champsTxt;
354: }
355:
356: 357: 358: 359: 360:
361: public function __toString()
362: {
363: $form = '<form '.$this->attrsToString().' >'."\n";
364: $form.='<p>'."\n";
365: $form.= $this->fieldsToString();
366: $form.='</p>'."\n";
367: $form.='</form>'."\n";
368: $form.='<script type="text/javascript" >'."\n";
369: $form .= '$(document).ready(function(){';
370: if(!empty($this->javascript))
371: {
372: $form.= $this->javascript;
373: }
374: if(count($this->listTooltips)>0)
375: {
376: $form.= '$(\'.form_tooltip\').tipTip();';
377: }
378: $form.='});';
379: $form.='</script>'."\n";
380: return $form;
381: }
382: }
383: ?>