Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

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