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_Element_AutoCompleter
  • Form_Element_Checkbox
  • Form_Element_Colorpicker
  • Form_Element_Date
  • Form_Element_DatePicker
  • Form_Element_Email
  • Form_Element_File
  • Form_Element_Hidden
  • Form_Element_Html
  • Form_Element_Ip
  • Form_Element_Mac
  • Form_Element_Multi
  • Form_Element_NumericStepper
  • Form_Element_Password
  • Form_Element_Radio
  • Form_Element_Select
  • Form_Element_Slider
  • Form_Element_Submit
  • Form_Element_Text
  • Form_Element_Textarea
  • 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:  * Element File. Selection de fichier
 15:  * 
 16:  * @category Pry
 17:  * @package Form
 18:  * @subpackage Form_Element
 19:  * @version 1.1.2 
 20:  * @author Olivier ROGER <oroger.fr>    
 21:  *
 22:  */
 23: class Form_Element_File extends Form_Input
 24: {
 25:     /**
 26:      * Taille en octet maximal pour le fichier envoyer
 27:      *
 28:      * @var int
 29:      * @access private
 30:      */
 31:     private $maxFileSize;
 32:     
 33:     /**
 34:      * Tableau des extensions autorisées
 35:      *
 36:      * @var array
 37:      * @access private
 38:      */
 39:     private $extension;
 40:     
 41:     /**
 42:      * Nom de l'élément pour multiple
 43:      *
 44:      * @var string
 45:      */
 46:     private $name;
 47:     
 48:     /**
 49:      * Nombre d'input à afficher en cas de multiple
 50:      *
 51:      * @var int
 52:      * @since 1.1.0
 53:      */
 54:     private $nbInput;
 55:     
 56:     /**
 57:      * Upload multiple ?
 58:      *
 59:      * @var boolean
 60:      * @since 1.1.0
 61:      */
 62:     private $multiple;
 63:     
 64:     /**
 65:      * Constructeur. Par defaut extension jpg,png,gif et taille de 2Mo
 66:      *
 67:      * @param string $nom
 68:      * @param Form_Form $form
 69:      * @access public
 70:      */
 71:     public function __construct($nom,$form)
 72:     {
 73:         parent::__construct($nom,$form);
 74:         $this->name             = $nom;
 75:         $this->nbInput          = 1;
 76:         $this->multiple         = false;
 77:         $this->attrs['type']    = 'file';
 78:         $this->form->enctype('multipart/form-data');
 79:         $this->maxFileSize      = 2097152; //2Mo
 80:         $this->extension        = array('jpg','gif','png');
 81:     }
 82:     
 83:     /**
 84:      * Défini la taille maximal de fichier autorisé
 85:      *
 86:      * @param int $size
 87:      * @access public
 88:      * @return Form_Element_File
 89:      */
 90:     public function maxFileSize($size)
 91:     {
 92:         $this->maxFileSize = $size;
 93:         return $this;
 94:     }
 95:     
 96:     /**
 97:      * Supprime toutes les extensions préenregistrées
 98:      *
 99:      * @access public
100:      * @return Form_Element_File
101:      */
102:     public function flushAllowedFileType()
103:     {
104:         $this->extension = array();
105:         return $this;
106:     }
107:     
108:     /**
109:      * Défini le type file comme multiple
110:      *
111:      * @param int $nb Nombre d'input
112:      * @since 1.1.0
113:      * @return Form_Element_File
114:      */
115:     public function multiple($nb=1)
116:     {
117:         $this->nbInput       = intval($nb);
118:         $this->multiple      = true;
119:         $this->attrs['name'] = $this->attrs['name'].'[]';
120:         return $this;
121:     }
122:     
123:     /**
124:      * Ajoute une ou des extensions à accepter
125:      *
126:      * @param mixed $newExt
127:      * @access public
128:      * @return Form_Element_File
129:      */
130:     public function allowFileType($newExt)
131:     {
132:         if(is_array($newExt))
133:         {
134:             foreach($newExt as $value)
135:             {
136:                 if(!in_array($value,$this->extension))
137:                     $this->extension[] = $value;
138:             }
139:         }
140:         else
141:             if(!in_array($newExt,$this->extension))
142:                 $this->extension[] = $newExt;
143:         
144:         return $this;
145:     }
146:     
147:     /**
148:      * Validation
149:      *
150:      * @param string $value
151:      * @access public
152:      * @return boolean
153:      */
154:     public function isValid($value)
155:     {
156:         if(isset($_FILES[$this->name]))
157:         {
158:             if(!$this->multiple)
159:             {
160:                 if(parent::isValid($_FILES[$this->name]['name']))
161:                 {
162:                     if(!$this->required && $_FILES[$this->name]['tmp_name']=='')
163:                     {
164:                         return true;
165:                     }
166:                     //Taille
167:                     if($this->maxFileSize<$_FILES[$this->name]['size'])
168:                     {
169:                         $this->errorMsg = Form_Error::TOOBIG;
170:                         return false;
171:                     }
172:                     //Bien envoyé ?
173:                     if(!is_uploaded_file($_FILES[$this->name]['tmp_name']) || $_FILES[$this->name]['error'] !=0)
174:                     {
175:                         $this->errorMsg = Form_Error::UPLOAD;
176:                         return false;
177:                     }
178:                     //Extension
179:                     if(!empty($this->extension))
180:                     {
181:                         $extension = pathinfo($_FILES[$this->name]['name'],PATHINFO_EXTENSION);
182:                         if(!in_array($extension,$this->extension))
183:                         {
184:                             $this->errorMsg = Form_Error::EXT;
185:                             return false;
186:                         }
187:                     }
188:                     return true;
189:                 }
190:             }
191:             else
192:             {
193:                 $return = true;
194:                 for($i=0;$i<$this->nbInput;$i++)
195:                 {
196:                     if(parent::isValid($_FILES[$this->name]['name'][$i]))
197:                     {
198:                         if(!$this->required && $_FILES[$this->name]['tmp_name'][$i]=='')
199:                         {
200:                             $return = $return && true;
201:                             break;
202:                         }
203:                         else if($this->required && $_FILES[$this->name]['tmp_name'][$i]=='')
204:                         {
205:                             $return = $return && false;
206:                             $this->errorMsg = Form_Error::REQUIRED;
207:                         }
208:                         //Taille
209:                         if($this->maxFileSize<$_FILES[$this->name]['size'][$i])
210:                         {
211:                             $this->errorMsg = Form_Error::TOOBIG;
212:                             $return = $return && false;
213:                         }
214:                         //Bien envoyé ?
215:                         if(!is_uploaded_file($_FILES[$this->name]['tmp_name'][$i]) || $_FILES[$this->name]['error'][$i] !=0)
216:                         {
217:                             $this->errorMsg = Form_Error::UPLOAD;
218:                             $return = $return && false;
219:                         }
220:                         //Extension
221:                         if(!empty($this->extension))
222:                         {
223:                             $extension = pathinfo($_FILES[$this->name]['name'][$i],PATHINFO_EXTENSION);
224:                             if(!in_array($extension,$this->extension))
225:                             {
226:                                 $this->errorMsg = Form_Error::EXT;
227:                                 $return = $return && false;
228:                             }
229:                         }
230:                         $return =  $return && true;
231:                     }
232:                     else
233:                     {
234:                         $return = $return && false;
235:                     }
236:                 }
237:             return $return;
238:             }
239:         }
240:         return false;
241:     }
242:     
243:     /**
244:      * Ecriture de l'objet
245:      *
246:      * @access public
247:      * @return string
248:      */
249:     public function __toString()
250:     {
251:         $css = $this->cssClass();
252:         $label = '';
253:         //LABEL
254:         if(!empty($this->label))
255:         {
256:             $label = "\t".'<label for="'.$this->attrs['id'].'" class="'.$this->cssLabel.'">'.$this->label.'</label>'."\n";
257:             if($this->labelNewLine)
258:                 $label.="\t".'<br />'."\n";
259:         }
260:         //INPUT DE BASE
261:         $attributs = $this->attrsToString();
262:         $field = "\t".'<input '.$css.' '.$attributs.' />'."\n";
263:         //INFOS
264:         if(!empty($this->info))
265:             $field.="\t".'<img src="'.$this->imgInfo.'" id="'.$this->attrs['name'].'_tooltip" class="form_tooltip" title="'.$this->info.'" alt="" style="cursor:help;" />';
266:         //AUTRES INPUTS 
267:         if($this->multiple && $this->nbInput>1)
268:         {
269:             for($i=0;$i<($this->nbInput-1);$i++)
270:             {
271:                 $field.="\t".'<br /><input '.$css.' '.$attributs.' />'."\n";
272:             }
273:         }
274:         //MAX FILE SIZE
275:         $field.="\t".'<input type="hidden" name="MAX_FILE_SIZE" value="'.$this->maxFileSize.'" />'."\n";
276:         if($this->fieldNewLine)
277:             $field.="\t".'<br />'."\n";
278:         $error='';
279:         //ERROR
280:         if(!is_null($this->errorMsg))
281:         {
282:             $error='<span class="'.$this->errorClass.'">'.$this->errorMsg.'</span><br />';
283:         }
284:         return $label.$field.$error;
285:     }
286: }
287:  ?>
Pry Framework API documentation generated by ApiGen 2.6.1