<?php
// +-------------------------------------------------+
// | 2002-2011 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: dynamics_fields_file_box.class.php,v 1.1.2.2 2025/10/17 06:50:42 dgoron Exp $

if (stristr($_SERVER['REQUEST_URI'], ".class.php")) die("no access");

class dynamics_fields_file_box extends dynamics_fields {
    
    protected static $checks = [];
    
    public static function val($field) {
        global $default_tmp_storage_engine;
        
        if (!isset($field ['OPTIONS'][0]['METHOD'][0]['value']) || $field ['OPTIONS'][0]['METHOD'][0]['value']=="") {
            $field ['OPTIONS'][0]['METHOD'][0]['value']=1;
        }
        if (($field ['OPTIONS'][0]['METHOD'][0]['value']==2)&&($field ['OPTIONS'][0]['DATA_TYPE'][0]['value']=="")) {
            $field ['OPTIONS'][0]['DATA_TYPE'][0]['value']=1;
        }
        $val=array();
        
        $field_name=$field['NAME'];
        global ${$field_name};
        $field_name_=${$field_name};
        
        if (($fp=@fopen("temp/".$field_name_[0],"r"))) {
            while (!feof($fp)) {
                $val_=@fgets($fp);
                $val_=rtrim($val_);
                $val[]=$val_;
            }
            fclose($fp);
            //unlink($_FILES[$field["NAME"]]["tmp_name"]);
            if ($field['OPTIONS'][0]['METHOD'][0]['value']==1) {
                $val = addslashes_array($val);
                $ret=implode("', '",$val);
                if ($ret!="") {
                    $ret="'".$ret."'";
                }
                return $ret;
            } else {
                if ($field['OPTIONS'][0]['DATA_TYPE'][0]['value']=="1") {
                    $data_type="varchar(255)";
                } else {
                    $data_type="integer";
                }
                $requete="create temporary table ".$field['OPTIONS'][0]['TEMP_TABLE_NAME'][0]['value']." (val $data_type, INDEX (val)) ENGINE={$default_tmp_storage_engine} ";
                pmb_mysql_query($requete);
                foreach ($val as $value) {
                    $requete="insert into ".$field['OPTIONS'][0]['TEMP_TABLE_NAME'][0]['value']." values('".addslashes($value)."')";
                    pmb_mysql_query($requete);
                }
                return $field['OPTIONS'][0]['TEMP_TABLE_NAME'][0]['value'];
            }
        }
    }
    
    public static function aff($field,&$check_scripts) {
        global $msg;
        
        //pré-remplissage
        $param = $field['NAME'];
        global ${$param};
        
        $form_name = static::get_form_name();
        
        $ret="<input type=\"file\" name=\"".$field["NAME"]."\">";
        if ($field['MANDATORY']=="yes") {
            $check_scripts.="if (document.".$form_name.".".$field['NAME'].".value==\"\") return cancel_submit(\"".sprintf($msg["parperso_field_is_needed"],$field['ALIAS'][0]['value'])."\");\n";
        }
        return $ret;
    }
    
    public static function chk($field,&$check_message) {
        global $msg;
        global $_FILES;
        
        //La statique permet d'eviter le double passage dans chk entrainant une erreur de copie
        if (isset(static::$checks[$field['NAME']])) {
            $check_message = static::$checks[$field['NAME']]['check_message'];
            return static::$checks[$field['NAME']]['check_status'];
        }
        //Supression des vieux fichiers !
        $dir=opendir("temp");
        $files=array();
        while (false !== ($file=readdir($dir))) {
            $files[]=$file;
        }
        for ($i=0; $i<count($files); $i++) {
            $file=$files[$i];
            $date=filemtime("temp/".$file);
            if (((time()-$date)>=24*60*60)&&(substr($file,0,13)=="proc_actions_")) {
                unlink("temp/".$file);
            }
        }
        
        if (!empty($_FILES[$field['NAME']]["error"])) {
            $check_message=$msg['field_file_download'];
            $check_status = 0;
        } else {
            if (!empty($_FILES[$field['NAME']]["tmp_name"])) {
                if (move_uploaded_file($_FILES[$field['NAME']]["tmp_name"],"temp/proc_actions_".basename($_FILES[$field['NAME']]["tmp_name"]))) {
                    $field_name=$field['NAME'];
                    global ${$field_name};
                    $field_name_[0]="proc_actions_".basename($_FILES[$field['NAME']]["tmp_name"]);
                    ${$field_name}=$field_name_;
                    $check_status = 1;
                } else {
                    $check_message=$msg['field_file_copy'];
                    $check_status = 0;
                }
            } else {
                $field_name=$field['NAME'];
                global ${$field_name};
                $field_name_=${$field_name};
                if (file_exists("temp/".basename($field_name_[0]))) {
                    $check_status = 1;
                } else {
                    $check_message=$msg['field_file_not_exist'];
                    $check_status = 0;
                }
            }
        }
        static::$checks[$field['NAME']] = [
            'check_message' => $check_message,
            'check_status' => $check_status
        ];
        return $check_status;
    }
    
}