noalyss-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Noalyss-commit] [noalyss] 04/15: New : 0001890: Avertissement si la doc


From: Dany De Bontridder
Subject: [Noalyss-commit] [noalyss] 04/15: New : 0001890: Avertissement si la document à télécharger est trop gros, on ne peut soumettre le fichier
Date: Fri, 5 Feb 2021 11:38:29 -0500 (EST)

sparkyx pushed a commit to branch master
in repository noalyss.

commit a2a31c8b3fc7e03af446c1679373aa8aa685b0f8
Author: sparkyx <danydb@noalyss.eu>
AuthorDate: Fri Jan 29 10:50:34 2021 +0100

    New : 0001890: Avertissement si la document à télécharger est trop gros,
    on ne peut soumettre le fichier
---
 html/js/scripts.js                          | 45 +++++++++++++++++++++++
 include/action.common.inc.php               |  3 +-
 include/ajax/ajax_ledger.php                | 56 ++++++++++++++---------------
 include/class/acc_ledger_fin.class.php      |  2 +-
 include/class/acc_ledger_purchase.class.php |  1 +
 include/class/acc_ledger_sold.class.php     |  1 +
 include/class/fiche.class.php               |  2 +-
 include/class/follow_up.class.php           |  1 +
 include/config.inc.example                  |  2 ++
 include/constant.php                        |  7 ++--
 include/lib/ac_common.php                   |  2 ++
 include/lib/ifile.class.php                 | 46 +++++++++++++++++++++++-
 include/lib/message_javascript.php          |  2 ++
 include/operation_ods_confirm.inc.php       |  1 +
 include/template/detail-action.php          |  9 +++++
 scenario/HtmlInput.test.php                 | 34 ++++++++++++++++++
 16 files changed, 180 insertions(+), 34 deletions(-)

diff --git a/html/js/scripts.js b/html/js/scripts.js
index d068eaf..04cc00b 100644
--- a/html/js/scripts.js
+++ b/html/js/scripts.js
@@ -3955,3 +3955,48 @@ var operation_tag = function (p_div)
             });
     };
 };
+
+/**
+ * Check the sum of size of all the FILES to upload
+ * @param p_object the form DOM object,
+ * @param p_max_size MAX_FILE_SIZE constant (see config.inc.php or 
constant.php)
+ * @returns true if the sum of filesize is greater than the limit
+ */
+function check_file_size(p_object,p_max_size)
+{
+    var sum_file=0;
+    for(var i=0;i<p_object.elements.length;i++) {
+        var a=p_object.elements[i];
+        if ( p_object.elements[i].getAttribute('type')=="file" )
+        {
+            if( p_object.elements[i].files[0]){
+
+                sum_file+=p_object.elements[i].files[0].size;
+            }
+        }
+    }
+    if ( sum_file > p_max_size) {alert_box(content[78]);return false;}
+    return true;
+}
+
+/**
+ * Check that the receipt file is not too big
+ * @see ajax_ledger.php , ledger_detail_file
+ * @param int p_max_size maximum size
+ * @param p_info name of the waiting box
+ * @returns true if  file size is less than the maximum
+ */
+function check_receipt_size(p_max_size,p_info)
+{
+    document.getElementById(p_info).style.display="inline";
+    console.debug ("param  p_max_file_size"+p_max_size);
+    var f=document.getElementById("receipt_id");
+    if ( f && f.files[0] && f.files[0].size > parseFloat(p_max_size)) {
+        document.getElementById("receipt_info_id").innerHTML=content[78];
+        document.getElementById(p_info).style.display="none";
+        return false;
+    }
+    document.getElementById("receipt_info_id").innerHTML="";
+    document.getElementById("form_file").submit();
+    return true;
+}
\ No newline at end of file
diff --git a/include/action.common.inc.php b/include/action.common.inc.php
index 4e568e5..9c5a25e 100644
--- a/include/action.common.inc.php
+++ b/include/action.common.inc.php
@@ -225,7 +225,8 @@ if ($sub_action == 'detail')
        if ($g_user->can_write_action($ag_id)  == true)
        {
             
-            echo '<form  enctype="multipart/form-data"  id="action_common_frm" 
class="print" action="do.php"  method="post"    style="display:inline">';
+            printf( '<form  enctype="multipart/form-data"  
id="action_common_frm" class="print" action="do.php" 
+method="post"    style="display:inline" onsubmit="return 
check_file_size(this,%s)">',MAX_FILE_SIZE);
             echo $supl_hidden;
             echo HtmlInput::hidden('ac', $http->request('ac'));
             echo dossier::hidden();
diff --git a/include/ajax/ajax_ledger.php b/include/ajax/ajax_ledger.php
index 40cd7c4..d9ac3ec 100644
--- a/include/ajax/ajax_ledger.php
+++ b/include/ajax/ajax_ledger.php
@@ -224,8 +224,7 @@ case 'file':
     $obj=$op->get_quant();     /* return an obj. ACH / FIN or VEN or null if 
nothing is found*/
     
     $repo = new Database();
-    $theme = $repo->get_value("select the_filestyle from theme where 
the_name=$1", array($_SESSION[SESSION_KEY.'g_theme']));
-    html_min_page_start($theme);
+    html_min_page_start($_SESSION[SESSION_KEY.'g_theme']);
 
     // if there is a receipt document
     if ( $obj->det->jr_pj_name=='')
@@ -238,7 +237,9 @@ case 'file':
         }
         if ( $access=='W')
         {
-            echo '<FORM METHOD="POST" ENCTYPE="multipart/form-data" 
id="form_file">';
+            $check_receipt=sprintf("check_receipt_size('%s','file%s')",
+                MAX_FILE_SIZE,$div);
+            echo '<FORM METHOD="POST" ENCTYPE="multipart/form-data" 
id="form_file" >';
 
             $sp=new ISpan('file'.$div);
             
$sp->style="display:none;background-color:red;color:white;font-size:12px";
@@ -248,8 +249,10 @@ case 'file':
             echo dossier::hidden();
             echo HtmlInput::hidden('jr_id',$jr_id);
             echo HtmlInput::hidden('div',$div);
+            echo '<INPUT TYPE="FILE" id="receipt_id" name="pj" 
onchange="'.$check_receipt.'">';
+
+            echo '<p id="receipt_info_id" class="error"></p>';
 
-            echo '<INPUT TYPE="FILE" name="pj" 
onchange="getElementById(\'file'.$div.'\').style.display=\'inline\';submit(this);">';
             echo '</FORM>';
         }
         else
@@ -285,17 +288,20 @@ case 'file':
         {
             // Not possible to remove the file thanks a modal dialog box,
             // because of the frameset
-            $x=sprintf('<a class="smallbutton icon" 
style="margin-left:12;margin-right:12" 
href="ajax_misc.php?op=ledger&gDossier=%d&div=%s&jr_id=%s&act=rmf" 
onclick="return confirm(\'Effacer le document ?\')">'."x".'</a>',
-                       $gDossier,$div,$jr_id);
-            
+
+            $x=Icon_Action::trash(uniqid(),
+                sprintf("if (confirm(content[47])) 
{document.location.href='ajax_misc.php?op=ledger&gDossier=%d&div=%s&jr_id=%s&act=rmf'}",
+                    $gDossier,$div,$jr_id));
+
         }  
         $filename= $obj->det->jr_pj_name;
-        if ( strlen($obj->det->jr_pj_name) > 20 )
+        if ( strlen($obj->det->jr_pj_name) > 60 )
         {
-            $filename=mb_substr($obj->det->jr_pj_name,0,23);
+            $filename=mb_substr($obj->det->jr_pj_name,0,60);
         }
         echo HtmlInput::show_receipt_document($jr_id,h($filename));
         echo $x;
+        echo '<p id="receipt_info_id" class="error"></p>';
         echo '</div>';
         echo '</body></html>';
         exit();
@@ -314,29 +320,24 @@ case 'loadfile':
         // Show a link to the new file
         $op->get();
         $obj=$op->get_quant(); /* return an obj. ACH / FIN or VEN or null if 
nothing is found*/
-
-        echo "<html><head>";
-        $repo=new Database();
-        $theme=$repo->get_value("select the_filestyle from theme where 
the_name=$1",array($_SESSION[SESSION_KEY.'g_theme']));
-        echo    "   <LINK REL=\"stylesheet\" type=\"text/css\" href=\"$theme\" 
media=\"screen\">";
-        echo "</head>";
+        html_min_page_start($_SESSION[SESSION_KEY.'g_theme']);
                if ( ! isset($_REQUEST['ajax']) ) echo "<body 
class=\"op_detail_frame\">"; else echo "<body>";
-               echo "<h2>"._("Document")."</h2>";
         echo '<div class="op_detail_frame">';
-        
+        $x="";
         // check if the user can remove a document
         if ($g_user->check_action (RMRECEIPT) == 1) {
             // Not possible to remove the file thanks a modal dialog box,
             // because of the frameset
-            $x=sprintf('<a class="mtitle" class="notice" 
style="margin-left:12;margin-right:12px" 
href="ajax_misc.php?op=ledger&gDossier=%d&div=%s&jr_id=%s&act=rmf" 
onclick="return confirm(\'Effacer le document ?\')">'.SMALLX.'</a>',
-                   $gDossier,$div,$jr_id);
-            echo $x;
+            $x=Icon_Action::trash(uniqid(),
+                    sprintf("if (confirm(content[47])) 
{document.location.href='ajax_misc.php?op=ledger&gDossier=%d&div=%s&jr_id=%s&act=rmf'}",
+                    $gDossier,$div,$jr_id));
         }
         $filename= $obj->det->jr_pj_name;
         echo HtmlInput::show_receipt_document($jr_id,h($filename));
+        echo $x;
 
         echo '</div>';
-
+        echo '</body></html>';
     }
     exit();
 /////////////////////////////////////////////////////////////////////////////
@@ -345,16 +346,14 @@ case 'loadfile':
 case 'rmf':
     if (   $access == 'W' && $g_user->check_action (RMRECEIPT) == 1)
     {
-        echo "<html><head>";
         $repo=new Database();
-        $theme=$repo->get_value("select the_filestyle from theme where 
the_name=$1",array($_SESSION[SESSION_KEY.'g_theme']));
-        echo    "   <LINK REL=\"stylesheet\" type=\"text/css\" href=\"$theme\" 
media=\"screen\">";
-        echo "</head><body class=\"op_detail_frame\">";
-               echo "<h2>"._("Document")."</h2>";
+        html_min_page_start($_SESSION[SESSION_KEY.'g_theme']);
         echo '<div class="op_detail_frame">';
+        $check_receipt=sprintf("check_receipt_size('%s','file%s')",
+            MAX_FILE_SIZE,$div);
         echo '<FORM METHOD="POST" ENCTYPE="multipart/form-data" 
id="form_file">';
         $sp=new ISpan('file'.$div);
-        
$sp->style="display:none;width:155;height:15;background-color:red;color:white;font-size:10";
+        
$sp->style="display:none;width:155px;height:15px;background-color:red;color:white;font-size:10px";
         $sp->value=_("Chargement");
         echo $sp->input();
 
@@ -363,7 +362,8 @@ case 'rmf':
         echo HtmlInput::hidden('jr_id',$jr_id);
         echo HtmlInput::hidden('div',$div);
 
-        echo '<INPUT TYPE="FILE" name="pj" 
onchange="getElementById(\'file'.$div.'\').style.display=\'inline\';submit(this);">';
+        echo '<INPUT TYPE="FILE" id="receipt_id" name="pj" 
onchange="'.$check_receipt.'">';
+        echo '<p id="receipt_info_id" class="error"></p>';
         echo '</FORM>';
         $ret=$cn->exec_sql("select jr_pj from jrn where 
jr_id=$1",array($jr_id));
         if (Database::num_row($ret) != 0)
diff --git a/include/class/acc_ledger_fin.class.php 
b/include/class/acc_ledger_fin.class.php
index 50db843..de48fcb 100644
--- a/include/class/acc_ledger_fin.class.php
+++ b/include/class/acc_ledger_fin.class.php
@@ -648,7 +648,7 @@ class Acc_Ledger_Fin extends Acc_Ledger
         $r.='</table>';
                // check for upload piece
                $file = new IFile();
-
+        $file->setAlertOnSize(true);
                $r.="<br>"._("Ajoutez une pièce justificative")." ";
                $r.=$file->input("pj", "");
 
diff --git a/include/class/acc_ledger_purchase.class.php 
b/include/class/acc_ledger_purchase.class.php
index d624af0..a20209d 100644
--- a/include/class/acc_ledger_purchase.class.php
+++ b/include/class/acc_ledger_purchase.class.php
@@ -1752,6 +1752,7 @@ EOF;
         $r.='<p class="decale">';
         // check for upload piece
         $file=new IFile();
+        $file->setAlertOnSize(true);
         $file->table=0;
         $r.=_("Ajoutez une pièce justificative ");
         $r.=$file->input("pj","");
diff --git a/include/class/acc_ledger_sold.class.php 
b/include/class/acc_ledger_sold.class.php
index 27ec56a..3074f76 100644
--- a/include/class/acc_ledger_sold.class.php
+++ b/include/class/acc_ledger_sold.class.php
@@ -988,6 +988,7 @@ EOF;
         // check for upload piece
         $file = new IFile();
         $file->table = 0;
+        $file->setAlertOnSize(true);
         $r.='<p class="decale">';
         $r.=_("Ajoutez une pièce justificative ");
         $r.=$file->input("pj", "");
diff --git a/include/class/fiche.class.php b/include/class/fiche.class.php
index 12f7828..ba01e63 100644
--- a/include/class/fiche.class.php
+++ b/include/class/fiche.class.php
@@ -1899,7 +1899,7 @@ class Fiche
             $odd="";
              $odd  = ($i % 2 == 0 ) ? ' odd ': ' even ';
              $accounting=$tiers->strAttribut(ATTR_DEF_ACCOUNT);
-             if ( $p_action == 'bank' && $amount['debit'] <  $amount['credit'] 
 ){
+             if ( ! empty($accounting) && $p_action == 'bank' && 
$amount['debit'] <  $amount['credit']  ){
                  if ( strpos($accounting,$bank->p_value)===0 || 
strpos($accounting,$cash->p_value)===0 || strpos($accounting,$cc->p_value)===0){
                  //put in red if c>d
                  $odd.=" notice ";
diff --git a/include/class/follow_up.class.php 
b/include/class/follow_up.class.php
index 1be5941..e055706 100644
--- a/include/class/follow_up.class.php
+++ b/include/class/follow_up.class.php
@@ -401,6 +401,7 @@ class Follow_Up
         /* for new files */
         $upload=new IFile();
         $upload->name="file_upload[]";
+        $upload->setAlertOnSize(true);
         $upload->readOnly=$readonly;
         $upload->value="";
         $aAttachedFile=$this->db->get_array('select 
d_id,d_filename,d_description,d_mimetype,'.
diff --git a/include/config.inc.example b/include/config.inc.example
index e1be7d1..56bbdff 100644
--- a/include/config.inc.example
+++ b/include/config.inc.example
@@ -72,3 +72,5 @@ define ("dbname","");
 // $pdftk = /snap/bin/pdftk
 // Define a random session key if you work with different version of NOALYSS
 // define ('SESSION_KEY','abcde');
+// Max size is defined by default to 2MB, it could be also needed to modify 
PHP Ini file
+//    define ("MAX_FILE_SIZE",2097152);
\ No newline at end of file
diff --git a/include/constant.php b/include/constant.php
index 5e9302f..a76070a 100644
--- a/include/constant.php
+++ b/include/constant.php
@@ -228,8 +228,11 @@ define ("FICHE_TYPE_ACH_SER",3);
 define ("FICHE_TYPE_ACH_MAT",7);
 define ("FICHE_TYPE_PROJET",26);
 define ("FICHE_TYPE_MATERIAL",7);
-
-/** 
+// Max size is defined by default to 2MB,
+if ( ! defined("MAX_FILE_SIZE")) {
+    define ("MAX_FILE_SIZE",2097152);
+}
+/**
  * -- pour utiliser unoconv démarrer un server libreoffice 
  * commande
  * libreoffice --headless --accept="socket,host=127.0.0.1,port=2002;urp;" 
--nofirststartwizard 
diff --git a/include/lib/ac_common.php b/include/lib/ac_common.php
index e4a09c2..6f27cdf 100644
--- a/include/lib/ac_common.php
+++ b/include/lib/ac_common.php
@@ -421,9 +421,11 @@ function html_min_page_start($p_theme="", $p_script="", 
$p_script2="")
     <LINK REL=\"stylesheet\" type=\"text/css\" href=\"$style\" 
media=\"screen\">
     <link rel=\"stylesheet\" type=\"text/css\" href=\"style-print.css\" 
media=\"print\">" .
     $p_script2 . "
+    <script src=\"js/prototype.js\" type=\"text/javascript\"></script>
     <script src=\"js/scripts.js\" type=\"text/javascript\"></script>
     <script src=\"js/acc_ledger.js\" type=\"text/javascript\"></script>
     <script src=\"js/smoke.js\" type=\"text/javascript\"></script>";
+    include_once NOALYSS_INCLUDE.'/lib/message_javascript.php';
     echo '</HEAD>
     ';
 
diff --git a/include/lib/ifile.class.php b/include/lib/ifile.class.php
index f5e8a37..b952f49 100644
--- a/include/lib/ifile.class.php
+++ b/include/lib/ifile.class.php
@@ -25,13 +25,57 @@
 require_once NOALYSS_INCLUDE.'/lib/html_input.class.php';
 class IFile extends HtmlInput
 {
+    // if true , the size is tested and a box is displaid
+    private $alert_on_size;
+    function __construct($p_name = "", $p_value = "", $p_id = "")
+    {
+        parent::__construct($p_name, $p_value, $p_id);
+        $this->alert_on_size=false;
+    }
+
+    /**
+     * @return false
+     */
+    public function getAlertOnSize(): bool
+    {
+        return $this->alert_on_size;
+    }
+
+    /**
+     *  if true , the size is tested and a box is displaid
+     * @param false $alert_on_size
+     */
+    public function setAlertOnSize(bool $alert_on_size): void
+    {
+        $this->alert_on_size = $alert_on_size;
+    }
+
     /*!\brief show the html  input of the widget*/
     public function input($p_name=null,$p_value=null)
     {
         $this->name=($p_name==null)?$this->name:$p_name;
         $this->value=($p_value==null)?$this->value:$p_value;
         if ( $this->readOnly==true) return $this->display();
-        $r='<INPUT class="inp" TYPE="file" name="'.$this->name.'" 
VALUE="'.$this->value.'">';
+        if ($this->id=="") $this->id=uniqid("file_");
+        $r=sprintf('<INPUT class="inp" TYPE="file" name="%s" id="%s" 
value="%s">',
+            $this->name,
+            $this->id,
+            $this->value);
+        if ( $this->alert_on_size)
+        {
+            $max_size=MAX_FILE_SIZE;
+            $max_size_mb=round($max_size / 1024 /1024,2);
+            $too_large=h(_("Fichier trop grand(max = $max_size_mb MB)"));
+
+            $js_check_size=sprintf('
+            document.getElementById("%s").addEventListener("change",function 
() 
+            { 
+                var fFile=document.getElementById("%s");
+                if (fFile.files[0] && fFile.files[0].size>%s) { 
smoke.alert("%s");} 
+            });',$this->id,$this->id,$max_size,$too_large);
+            $r.=create_script($js_check_size);
+
+        }
         return $r;
 
     }
diff --git a/include/lib/message_javascript.php 
b/include/lib/message_javascript.php
index 74d816f..75dd82d 100644
--- a/include/lib/message_javascript.php
+++ b/include/lib/message_javascript.php
@@ -109,4 +109,6 @@ content[74]="<?php echo htmlspecialchars(_('TVA due ou 
récupérable quand l\'op
 content[75]="<?php echo htmlspecialchars(_('Journaux Achat ou vente en mode 
simple, TVA ou détaillé'),ENT_QUOTES)?>";
 content[76]="<?php echo htmlspecialchars(_('Il est conseillé d\'avoir un 
quickcode de moins de 9 car.'),ENT_QUOTES)?>";
 content[77]="<?php echo htmlspecialchars(_("Permet de chercher dans le suivi 
pour les contacts multiples"),ENT_QUOTES)?>";
+<?php $file_too_large=sprintf("Fichier trop grand , taille max = %s 
mb",(round(MAX_FILE_SIZE/1024/1024,2)));?>
+content[78]="<?php echo htmlspecialchars($file_too_large,ENT_QUOTES)?>";
 </script>
diff --git a/include/operation_ods_confirm.inc.php 
b/include/operation_ods_confirm.inc.php
index e29348e..666d705 100644
--- a/include/operation_ods_confirm.inc.php
+++ b/include/operation_ods_confirm.inc.php
@@ -71,6 +71,7 @@ echo $ledger->confirm($_POST,false);
     <div id="document_div_id" style="display:none;height:185px;height:10rem">
       <?php
       $file = new IFile();
+      $file->setAlertOnSize(true);
         $file->table = 0;
         echo '<p class="decale">';
         echo _("Ajoutez une pièce justificative ");
diff --git a/include/template/detail-action.php 
b/include/template/detail-action.php
index b9ba146..78453a0 100644
--- a/include/template/detail-action.php
+++ b/include/template/detail-action.php
@@ -484,6 +484,11 @@ try {
        docAdded=document.getElementById('add_file');
        new_element=document.createElement('li');
        new_element.innerHTML='<input class="inp" type="file" value="" 
name="file_upload[]"/><label>Description</label> <input type="input" 
class="input_text" name="input_desc[]" >';
+
+    new_element.innerHTML+='<span id="<?=uniqid("file")?>" 
onclick="document.getElementById(\'add_file\').removeChild(this.parentNode)" 
class="icon">&#xe80f;</span>';
+    
+    
+       
        docAdded.appendChild(new_element);
 }
 catch(exception) { alert('<?php echo j(_('Je ne peux pas ajouter de 
fichier'))?>'); alert(exception.message);}
@@ -498,6 +503,10 @@ catch(exception) { alert('<?php echo j(_('Je ne peux pas 
ajouter de fichier'))?>
         ?>
         <label><?php echo _('Description')?></label>
         <input type="input" class="input_text" name="input_desc[]" >
+          <?php
+            
$js="document.getElementById('add_file').removeChild(this.parentNode)";
+            echo Icon_Action::trash(uniqid(),$js);
+          ?>
       </li>
     </ol>
   <span   >
diff --git a/scenario/HtmlInput.test.php b/scenario/HtmlInput.test.php
index 794771e..7abbd12 100644
--- a/scenario/HtmlInput.test.php
+++ b/scenario/HtmlInput.test.php
@@ -125,6 +125,40 @@ $http=new HttpInput();
     ?>
     </ol>
 </form>
+<h2>
+    Fichier - IFile
+</h2>
+<form method="POST" enctype="application/x-www-form-urlencoded" 
onsubmit="check_size();return false;">
+
+<?php
+
+    $file=new IFILE("file_to_upload");
+    $file->id="file_to_upload";
+    echo "fichier ",$file->input();
+    echo HtmlInput::submit("file","Upload");
+?>
+</form>
+<script>
+    function check_size()
+    {
+        var aFile=document.getElementsByTagName("input");
+        console.debug("afile");
+        console.debug(aFile);
+
+        for (var i = 0;i < aFile.length;i++) {
+        if ( aFile[i].getAttribute("type")=="file" ) {
+                console.debug("file"+aFile[i].files[0].size);
+        }
+
+    }
+    }
+    
document.getElementById("file_to_upload").addEventListener("change",function() {
+
+        if ( this.files[0] ) {
+            console.debug("file"+this.files[0].size);
+        }
+    });
+</script>
 <div id="debug_box"></div>
 
     
\ No newline at end of file



reply via email to

[Prev in Thread] Current Thread [Next in Thread]