Overview

Packages

  • Auth
  • Config
  • Controller
  • Date
  • Db
  • Feed
    • Abstract
    • Writers
  • File
    • Decorator
  • Form
    • Element
  • Image
  • Log
    • Writer
  • Net
    • Exception
  • None
  • PHP
  • PHPMailer
  • Session
  • Util
  • Validate
    • Validator
  • Zend
    • Registry

Classes

  • Form_Error
  • Form_Field
  • Form_Form
  • Form_Input
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Pry Framework
  4:  *
  5:  * LICENSE
  6:  *
  7:  * This source file is subject to the new BSD license that is bundled
  8:  * with this package in the file LICENSE.txt.
  9:  * 
 10:  * @version $Revision: 276 $
 11:  */
 12: 
 13: /**
 14:  * Class représentant un formulaire.
 15:  * 
 16:  * <code>
 17:  * $form = new Form_Form('monForm');
 18:  * $form->setPostedValue($_POST);
 19:  * $form->action('form.php')
 20:  *      ->setAttributes('onsubmit','');
 21:  * 
 22:  * $form->add('Text','nom')
 23:  *      ->label('test')
 24:  *      ->value('Mon nom')
 25:  *      ->addClass('cssclass')
 26:  *      ->minLength(3);
 27:  * $form->add('Submit','envoi')
 28:  *      ->id('envoiBtn')
 29:  *      ->value('Envoyer');
 30:  * 
 31:  * if($form->isValid($_POST))
 32:  * {
 33:  *      echo 'OK';
 34:  * }
 35:  * else
 36:  *      echo $form;
 37:  * </code>
 38:  * 
 39:  * @category Pry
 40:  * @package Form
 41:  * @version 1.1.0 
 42:  * @author Olivier ROGER <oroger.fr>
 43:  *       
 44:  */
 45: class Form_Form
 46: {
 47:     /**
 48:      * Liste des champs du formulaire
 49:      *
 50:      * @var array
 51:      * @access private
 52:      */
 53:     private $champs;
 54:     
 55:     /**
 56:      * Liste des champs submit
 57:      *
 58:      * @var array
 59:      * @access private
 60:      */
 61:     private $champs_submit;
 62:     
 63:     /**
 64:      * Liste des champs hidden
 65:      *
 66:      * @var array
 67:      */
 68:     private $champs_hidden;
 69:     
 70:     /**
 71:      * Contenu du formulaire après $_POST
 72:      *
 73:      * @var array
 74:      * @access private
 75:      */
 76:     private $posted_value;
 77:     
 78:     /**
 79:      * Liste des attributs du formulaire
 80:      *
 81:      * @var array
 82:      * @access private
 83:      */
 84:     protected $attrs;
 85:     
 86:     /**
 87:      * identifiant du formulaire
 88:      *
 89:      * @var string
 90:      * @access protected
 91:      */
 92:     protected $uniqid;
 93:     
 94:     /**
 95:      * Instance de formulaire
 96:      *
 97:      * @var array
 98:      * @access protected
 99:      */
100:     protected static $instances = array();
101:     
102:     /**
103:      * Liste des infos à afficher
104:      *
105:      * @var array
106:      * @access public
107:      */
108:     public $listTooltips;
109:     
110:     public $javascript;
111:     
112:     /**
113:      * Constructeur
114:      *
115:      * @param string $uniqid Id unique identifiant le formulaire
116:      * @param string $method
117:      * @access public
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:      * Valide le formulaire
149:      *
150:      * @param array $post
151:      * @param boolean $noSubmit true si aucun bouton submit.
152:      * Permet de valider le formulaire avec un envoi javascript
153:      * @access public
154:      * @return boolean
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,'[]')) // Cas des selection multiple
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:      * Ajoute un élément de formulaire
178:      *
179:      * @param string $type
180:      * @param string $nom
181:      * @access public
182:      * @return Form_Input
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:      * Attribue une action
209:      *
210:      * @param string $action
211:      * @access public
212:      * @return Form_Form
213:      */
214:     public function action($action)
215:     {
216:         $this->attrs['action'] = $action;
217:         return $this;
218:     }
219:     
220:     /**
221:      * Setter d'attribut
222:      *
223:      * @param string $nom
224:      * @param string $valeur
225:      * @access public
226:      * @return Form_Form
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:      * Enregistre les valeur poster pour réutilisation
237:      *
238:      * @param array $data
239:      * @access public
240:      * @return Form_Form
241:      */
242:     public function setPostedValue($data)
243:     {
244:         if(is_array($data))
245:         {
246:             foreach($data as $name=>$valeur)
247:             {
248:                 //var_dump($valeur);
249:                 $this->posted_value[$name] = $valeur;
250:             }
251:         }
252:         return $this;
253:     }
254:     
255:     /**
256:      * Récupère la valeur postée pour un élément
257:      *
258:      * @param string $name
259:      * @access public
260:      * @return string
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:      * Attribue une méthode au formulaire
271:      *
272:      * @param string $method
273:      * @access public
274:      * @return Form_Form
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:      * Attribue un enctype au formulaire
290:      *
291:      * @param string $txt
292:      * @access public
293:      * @return Form_Form
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:      * Vérifie si le formulaire à bien été soumis
306:      * 
307:      * @access private
308:      * @param boolean $noSubmit true si aucun bouton submit
309:      * @return boolean
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:      * Linéarise les attributs du formulaire
330:      * @access private
331:      * @return string
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:      * Retourne une chaine des différents élément du formulaire
344:      * @access private
345:      * @return string
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:      * Ecriture du formulaire et ajout des appel JS
358:      * @access private
359:      * @return string
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:  ?>
Pry Framework API documentation generated by ApiGen 2.6.1