tangra logo
   
[ class tree: tangra_lib ] [ index: tangra_lib ] [ all elements ]
 

Source for file form_field_file.class.php

Documentation is available at form_field_file.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. //  $Id$
  4. //
  5.  
  6. /**
  7.  * Contains class Form_Field_File
  8.  *
  9.  * @package tangra_lib
  10.  * @subpackage form
  11.  */
  12.  
  13.  
  14. /**
  15.  *
  16.  */
  17. require_once(TANGRA_MAIN_DIR.'form/fields/form_field.class.php');
  18.  
  19.  
  20. /**
  21.  * Represents HTML field type=file
  22.  *
  23.  * Please note that this field will be represented by 2 form fields:
  24.  * - type="file" - for the file itself
  25.  * - type="hidden" - for the hidden field that holds Maximum file size
  26.  *
  27.  * @package tangra_lib
  28.  * @subpackage form
  29.  */
  30. class Form_Field_File extends Form_Field {
  31.     /**
  32.      * Maximal file size
  33.      *
  34.      * @var integer 
  35.      * @internal
  36.      */
  37.     private $max_file_size;
  38.  
  39.  
  40.     /**
  41.      * Constructor
  42.      *
  43.      * @param string $name Name of the field
  44.      * @param boolean $required Is field required. Default is false
  45.      * @param integer $max_file_size Maximal allowed file size. Please note that php.ini settings "upload_max_filesize" and "file_uploads" are also in effect.
  46.      */
  47.     function __construct($name$required false$max_file_size{
  48.         parent::__construct($namefalse$required);
  49.         tangra_if_not_int_throw_e($max_file_size);
  50.         $this->max_file_size = $max_file_size;
  51.  
  52.         $this->add_potential_error('size_exceeded');
  53.         $this->add_potential_error('partial_upload');
  54.     }
  55.  
  56.  
  57.     /**
  58.      * Returns masximum allowed file size
  59.      *
  60.      * @return integer 
  61.      */
  62.     public function get_max_file_size({
  63.         return $this->max_file_size;
  64.     }
  65.  
  66.  
  67.     /**
  68.      * Captures submit
  69.      *
  70.      * @param string $form_name 
  71.      * @param array $submit_array 
  72.      * @internal
  73.      */
  74.     public function capture_submit($form_name&$submit_array{
  75.         if (array_key_exists($form_name.'_'.$this->get_name()$submit_array['_FILES'])) {
  76.             $tmp_arr $submit_array['_FILES'];
  77.             $this->set_html_value($tmp_arr[$form_name.'_'.$this->get_name()]);
  78.         else {
  79.             $this->set_html_value(NULL);
  80.         }
  81.     }
  82.  
  83.  
  84.     /**
  85.      * Performs basic check for validity
  86.      *
  87.      * @return boolean 
  88.      * @internal
  89.      */
  90.     public function basic_check({
  91.         $has_errors false;
  92.  
  93.         $html_value $this->get_html_value();
  94.  
  95.         if ($this->get_required(&& (($html_value['error'== 4|| ($html_value == NULL))) {
  96.             $has_errors true;
  97.             $this->set_error('mandatory_missing');
  98.         else {
  99.             if ($html_value['error'== || $html_value['error'== || $html_value['size'$this->get_max_file_size()) {
  100.                 $has_errors true;
  101.                 $this->set_error('size_exceeded');
  102.             else {
  103.                 if ($html_value['error'== 3{
  104.                     $has_errors true;
  105.                     $this->set_error('partial_upload');
  106.                 }
  107.             }
  108.         }
  109.  
  110.         return $has_errors;
  111.     }
  112.  
  113.  
  114.     /**
  115.      * Returns array with field properties
  116.      *
  117.      * @return array 
  118.      * @internal
  119.      */
  120.     public function get_properties_array({
  121.         $ret parent::get_properties_array();
  122.  
  123.         $ret['mfs'$this->get_max_file_size();
  124.  
  125.         return $ret;
  126.     }
  127. }