From 8c5b864f57e8f31377f9bc7eceaf4bf4221c6586 Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Tue, 26 Jun 2018 12:27:33 +0000
Subject: [PATCH] FIX #7988 add controle in js backdate

---
 core/class/class_request.php               |  90 +++++--------
 core/class/class_resource.php              | 145 +++++++--------------
 modules/attachments/js/functions.js        |   1 -
 modules/content_management/js/functions.js |   3 +
 4 files changed, 83 insertions(+), 156 deletions(-)

diff --git a/core/class/class_request.php b/core/class/class_request.php
index 72f4d2908e3..7d956ffa5a3 100755
--- a/core/class/class_request.php
+++ b/core/class/class_request.php
@@ -59,8 +59,7 @@ class request extends dbquery
     public function PDOselect($select, $where, $parameters = null, $other, $database_type, $limit="default", $left_join=false, $first_join_table="", $second_join_table="", $join_key="", $add_security = true, $catch_error = false, $distinct_argument = false)
     {
         $db = new Database();
-        if($limit == 0 || $limit == "default")
-        {
+        if ($limit == 0 || $limit == "default") {
             $limit = $_SESSION['config']['databasesearchlimit'];
         }
       
@@ -69,18 +68,16 @@ class request extends dbquery
         $table = '';
         $table_string = '';
         $field_string = '';
-        foreach (array_keys($select) as $value)
-        {
+        foreach (array_keys($select) as $value) {
             $table = $value;
             $table_string .= $table.",";
-            foreach ($select[$value] as $subvalue)
-            {
+            foreach ($select[$value] as $subvalue) {
                 $field = $subvalue;
 
                 $field_string .= $table.".".$field.",";
 
                 //fix order by alt_identifier
-                if($field == 'alt_identifier'){
+                if ($field == 'alt_identifier') {
                     $field_string .= "order_alphanum(alt_identifier),";
                 }
             }
@@ -91,26 +88,20 @@ class request extends dbquery
         $field_string = substr($field_string, 0, -1);
 
         //Extracts data from the second argument : the where clause
-        if (trim($where) <> "")
-        {
+        if (trim($where) <> "") {
             $where_string = $where;
             //$where_string = " where ".$where;
-        }
-        else
-        {
+        } else {
             $where_string = "";
         }
-         $join = '';
-        if($left_join)
-        {
+        $join = '';
+        if ($left_join) {
             //Reste table string
             $table_string = "";
 
             //Add more table in join syntax
-            foreach (array_keys($select) as $value)
-            {
-                if ($value <> $first_join_table && $value <> $second_join_table)
-                {
+            foreach (array_keys($select) as $value) {
+                if ($value <> $first_join_table && $value <> $second_join_table) {
                     $table_string = $value.",";
                 }
             }
@@ -120,21 +111,14 @@ class request extends dbquery
             $join .= $second_join_table." on ".$second_join_table.".".$join_key." = ".$first_join_table.".".$join_key;
         }
 
-        if($add_security)
-        {
-            foreach(array_keys($_SESSION['user']['security']) as $coll)
-            {
-                if(isset($_SESSION['user']['security'][$coll]['DOC']['table']))
-                {
-                    if(preg_match('/'.$_SESSION['user']['security'][$coll]['DOC']['table'].'/',$table_string) || preg_match('/'.$_SESSION['user']['security'][$coll]['DOC']['view'].'/',$table_string) )
-                    {
-                        if(empty($where_string))
-                        {
+        if ($add_security) {
+            foreach (array_keys($_SESSION['user']['security']) as $coll) {
+                if (isset($_SESSION['user']['security'][$coll]['DOC']['table'])) {
+                    if (preg_match('/'.$_SESSION['user']['security'][$coll]['DOC']['table'].'/', $table_string) || preg_match('/'.$_SESSION['user']['security'][$coll]['DOC']['view'].'/', $table_string)) {
+                        if (empty($where_string)) {
                             $where_string = "( ".$_SESSION['user']['security'][$coll]['DOC']['where']." ) ";
                             //$where_string = " where ( ".$_SESSION['user']['security'][$coll]['DOC']['where']." ) ";
-                        }
-                        else
-                        {
+                        } else {
                             $where_string = ''.$where_string." and ( ".$_SESSION['user']['security'][$coll]['DOC']['where']." ) ";
                         }
                         break;
@@ -145,8 +129,7 @@ class request extends dbquery
         //Time to create the SQL Query
         $query = "";
         $dist = '';
-        if($distinct_argument == true)
-        {
+        if ($distinct_argument == true) {
             $dist = " distinct ";
         }
         
@@ -159,18 +142,14 @@ class request extends dbquery
 
         $res_query = $db->query($query, $parameters, $catch_error);
 
-        if($catch_error && !$res_query)
-        {
+        if ($catch_error && !$res_query) {
             return false;
         }
         $result=array();
-        while($line = $res_query->fetch(PDO::FETCH_ASSOC))
-        {
+        while ($line = $res_query->fetch(PDO::FETCH_ASSOC)) {
             $temp= array();
-            foreach (array_keys($line) as $resval)
-            {
-                if (!is_int($resval))
-                {
+            foreach (array_keys($line) as $resval) {
+                if (!is_int($resval)) {
                     array_push(
                         $temp,
                         array(
@@ -180,10 +159,9 @@ class request extends dbquery
                     );
                 }
             }
-            array_push($result,$temp);
+            array_push($result, $temp);
         }
-        if(count($result) == 0 && $catch_error)
-        {
+        if (count($result) == 0 && $catch_error) {
             return true;
         }
         return $result;
@@ -204,7 +182,7 @@ class request extends dbquery
         $value_string = "( ";
         $parameters = array();
         for ($i=0;$i<count($data);$i++) {
-            if(
+            if (
                 trim(strtoupper($data[$i]['value'])) == "SYSDATE"
                 || trim(strtoupper($data[$i]['value'])) == "CURRENT_TIMESTAMP"
             ) {
@@ -247,13 +225,13 @@ class request extends dbquery
         $parameters = array();
         for ($i=0; $i < count($data);$i++) {
             if ($data[$i]['type'] == "string" || $data[$i]['type'] == "date") {
-                if ($databasetype == "POSTGRESQL" && $data[$i]['type'] == "date" 
+                if ($databasetype == "POSTGRESQL" && $data[$i]['type'] == "date"
                     && ($data[$i]['value'] == '' || $data[$i]['value'] == ' ')) {
                     $update_string .= $data[$i]['column']."=NULL,";
                 } else {
                     if (trim(strtoupper($data[$i]['value'])) == "SYSDATE") {
                         $update_string .= $data[$i]['column']."=sysdate,";
-                    } elseif(trim(strtoupper($data[$i]['value'])) == "CURRENT_TIMESTAMP") {
+                    } elseif (trim(strtoupper($data[$i]['value'])) == "CURRENT_TIMESTAMP") {
                         $update_string .= $data[$i]['column']."=CURRENT_TIMESTAMP,";
                     } else {
                         $update_string .= $data[$i]['column']."=?,";
@@ -262,10 +240,10 @@ class request extends dbquery
                 }
             } else {
                 if ($data[$i]['value'] == 'NULL') {
-                    $update_string .= $data[$i]['column']."=NULL,";   
+                    $update_string .= $data[$i]['column']."=NULL,";
                 } else {
                     $update_string .= $data[$i]['column']."=?,";
-                    $parameters[] = $data[$i]['value']; 
+                    $parameters[] = $data[$i]['value'];
                 }
             }
         }
@@ -304,14 +282,12 @@ class request extends dbquery
     *************************************************************************/
     public function extract_date($date_field, $arg = '')
     {
-        switch ($_SESSION['config']['databasetype'])
-        {
+        switch ($_SESSION['config']['databasetype']) {
         case "SQLSERVER":
             return '';
         
         case "MYSQL":
-            switch($arg) 
-            {
+            switch ($arg) {
             case 'year'     : return ' date_format('.$date_field.', %Y)';
             case 'month'    : return ' date_format('.$date_field.', %m)';
             case 'day'      : return ' date_format('.$date_field.', %d)';
@@ -322,8 +298,7 @@ class request extends dbquery
             }
         
         case "POSTGRESQL":
-            switch($arg) 
-            {
+            switch ($arg) {
             case 'year'     : return " date_part( 'year', ".$date_field.")";
             case 'month'    : return " date_part( 'month', ".$date_field.")";
             case 'day'      : return " date_part( 'day', ".$date_field.")";
@@ -334,8 +309,7 @@ class request extends dbquery
             }
         
         case "ORACLE":
-            switch($arg) 
-            {
+            switch ($arg) {
             case 'year'     : return " to_char(".$date_field.", 'YYYY')";
             case 'month'    : return " to_char(".$date_field.", 'MM')";
             case 'day'      : return " to_char(".$date_field.", 'DD')";
diff --git a/core/class/class_resource.php b/core/class/class_resource.php
index 314cdfb7e27..35c9adce527 100755
--- a/core/class/class_resource.php
+++ b/core/class/class_resource.php
@@ -37,7 +37,7 @@
 * @ingroup core
 */
  class resource extends request
-{
+ {
 
     /**
     * Resource identifier
@@ -129,11 +129,11 @@
     * @param  $data  array Data array
     * @param  $databasetype string Type of the db (MYSQL, SQLSERVER, ...)
     */
-    function load_into_db($table_res, $path, $filename, $docserver_path, $docserver_id, $data, $databasetype, $calledByWs=false)
+    public function load_into_db($table_res, $path, $filename, $docserver_path, $docserver_id, $data, $databasetype, $calledByWs=false)
     {
         $filetmp = $docserver_path;
         $tmp = $path;
-        $tmp = str_replace('#',DIRECTORY_SEPARATOR,$tmp);
+        $tmp = str_replace('#', DIRECTORY_SEPARATOR, $tmp);
         $filetmp .= $tmp;
         $filetmp .= $filename;
         $db = new Database();
@@ -151,26 +151,20 @@
         array_push($data, array('column' => "path", 'value' => $path, 'type' => "string"));
         array_push($data, array('column' => "filename", 'value' => $filename, 'type' => "string"));
         array_push($data, array('column' => 'creation_date', 'value' => $db->current_datetime(), 'type' => "function"));
-        if(!$this->check_basic_fields($data))
-        {
+        if (!$this->check_basic_fields($data)) {
             $_SESSION['error'] = $this->error;
             functions::xecho($this->error);
             return false;
-        }
-        else
-        {
-            if(!$this->insert($table_res, $data, $_SESSION['config']['databasetype']))
-            {
+        } else {
+            if (!$this->insert($table_res, $data, $_SESSION['config']['databasetype'])) {
                 if (!$calledByWs) {
                     $this->error = _INDEXING_INSERT_ERROR."<br/>".$this->show();
                 }
                 return false;
-            }
-            else
-            {
+            } else {
                 $db2 = new Database();
                 $stmt = $db2->query(
-                    "select res_id from " . $table_res 
+                    "select res_id from " . $table_res
                         . " where docserver_id = ? and path = ? and filename= ?  order by res_id desc ",
                     array(
                         $docserver_id,
@@ -179,8 +173,7 @@
                     )
                 );
                 $res = $stmt->fetchObject();
-
-                 if($table_res == 'res_letterbox'){
+                if ($table_res == 'res_letterbox') {
                     require_once("core".DIRECTORY_SEPARATOR."class".DIRECTORY_SEPARATOR."class_history.php");
                     $hist = new history();
                     $hist->add(
@@ -226,77 +219,49 @@
         $find_fingerprint = false;
         $find_filesize = false;
         $find_status = false;
-        for($i=0; $i < count($data);$i++)
-        {
-            if($data[$i]['column'] == 'format')
-            {
+        for ($i=0; $i < count($data);$i++) {
+            if ($data[$i]['column'] == 'format') {
                 $find_format = true;
                 // must be tested in the file_index.php file (module = indexing_searching)
-            }
-            elseif($data[$i]['column'] == 'typist' )
-            {
+            } elseif ($data[$i]['column'] == 'typist') {
                 $find_typist = true;
-            }
-            elseif($data[$i]['column'] == 'creation_date')
-            {
+            } elseif ($data[$i]['column'] == 'creation_date') {
                 $find_creation_date = true;
-                if($data[$i]['value'] <> $db->current_datetime())
-                {
+                if ($data[$i]['value'] <> $db->current_datetime()) {
                     $error .= _CREATION_DATE_ERROR.'<br/>';
                 }
-            }
-            elseif($data[$i]['column'] == 'docserver_id')
-            {
+            } elseif ($data[$i]['column'] == 'docserver_id') {
                 $find_docserver_id =  true;
                 $db = new Database();
-                if(!$db->query("select docserver_id from ".$_SESSION['tablename']['docservers']." where docserver_id = ?", array($data[$i]['value'])))
-                {
+                if (!$db->query("select docserver_id from ".$_SESSION['tablename']['docservers']." where docserver_id = ?", array($data[$i]['value']))) {
                     $error .= _DOCSERVER_ID_ERROR.'<br/>';
                 }
-            }
-            elseif($data[$i]['column'] == 'path' )
-            {
+            } elseif ($data[$i]['column'] == 'path') {
                 $find_path = true;
-                if( empty($data[$i]['value']))
-                {
+                if (empty($data[$i]['value'])) {
                     $error .= _PATH_ERROR.'<br/>';
                 }
-            }
-            elseif($data[$i]['column'] == 'filename' )
-            {
+            } elseif ($data[$i]['column'] == 'filename') {
                 $find_filename = true;
                 //if(!preg_match("/^[0-9]+.([a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z]?|maarch)$/", $data[$i]['value']))
-                if(!preg_match("/^[\w-.]+.([a-zA-Z-0-9][a-zA-Z-0-9][a-zA-Z-0-9][a-zA-Z-0-9]?|maarch)$/", $data[$i]['value']))
-                {
+                if (!preg_match("/^[\w-.]+.([a-zA-Z-0-9][a-zA-Z-0-9][a-zA-Z-0-9][a-zA-Z-0-9]?|maarch)$/", $data[$i]['value'])) {
                     $error .= _FILENAME_ERROR . ' ' . $data[$i]['value'] . '<br/>';
                 }
-            }
-            elseif($data[$i]['column'] == "offset_doc")
-            {
+            } elseif ($data[$i]['column'] == "offset_doc") {
                 $find_offset = true;
-            }
-            elseif($data[$i]['column'] == 'logical_adr')
-            {
+            } elseif ($data[$i]['column'] == 'logical_adr') {
                 $find_logical_adr = true;
-            }
-            elseif($data[$i]['column'] == 'fingerprint'  )
-            {
+            } elseif ($data[$i]['column'] == 'fingerprint') {
                 $find_fingerprint  = true;
-                if(!preg_match("/^[0-9A-Fa-f]+$/", $data[$i]['value']))
-                {
+                if (!preg_match("/^[0-9A-Fa-f]+$/", $data[$i]['value'])) {
                     $error .= _FINGERPRINT_ERROR.'<br/>';
                 }
-            }
-            elseif($data[$i]['column'] == 'filesize'  )
-            {
+            } elseif ($data[$i]['column'] == 'filesize') {
                 $find_filesize = true;
-                if( $data[$i]['value'] <= 0)
-                {
+                if ($data[$i]['value'] <= 0) {
                     $error .= _FILESIZE_ERROR.'<br/>';
                 }
-            }
-            elseif($data[$i]['column'] == 'status' )
-            {
+            } elseif ($data[$i]['column'] == 'status') {
                 $find_status = true;
                 /*if( !preg_match("/^[A-Z][A-Z][A-Z][A-Z]*$/", $data[$i]['value']))
                 {
@@ -305,58 +270,44 @@
             }
         }
 
-        if($find_format == false)
-        {
+        if ($find_format == false) {
             $error .= _MISSING_FORMAT.'<br/>';
         }
-        if($find_typist == false)
-        {
+        if ($find_typist == false) {
             $error .= _MISSING_TYPIST.'<br/>';
         }
-        if($find_creation_date == false)
-        {
+        if ($find_creation_date == false) {
             $error .= _MISSING_CREATION_DATE.'<br/>';
         }
-        if($find_docserver_id == false)
-        {
+        if ($find_docserver_id == false) {
             $error .= _MISSING_DOCSERVER_ID.'<br/>';
         }
-        if($find_path == false)
-        {
+        if ($find_path == false) {
             $error .= _MISSING_PATH.'<br/>';
         }
-        if($find_filename == false)
-        {
+        if ($find_filename == false) {
             $error .= _MISSING_FILENAME.'<br/>';
         }
-        if($find_offset == false)
-        {
+        if ($find_offset == false) {
             $error .= _MISSING_OFFSET.'<br/>';
         }
-        if($find_logical_adr == false)
-        {
+        if ($find_logical_adr == false) {
             $error .= _MISSING_LOGICAL_ADR.'<br/>';
         }
-        if($find_fingerprint == false)
-        {
+        if ($find_fingerprint == false) {
             $error .= _MISSING_FINGERPRINT.'<br/>';
         }
-        if($find_filesize == false)
-        {
+        if ($find_filesize == false) {
             $error .= _MISSING_FILESIZE.'<br/>';
         }
-        if($find_status == false)
-        {
+        if ($find_status == false) {
             $error .= _MISSING_STATUS.'<br/>';
         }
 
         $this->error = $error;
-        if(!empty($error))
-        {
+        if (!empty($error)) {
             return false;
-        }
-        else
-        {
+        } else {
             return true;
         }
     }
@@ -369,22 +320,22 @@
     * @param $whereClause security clause
     * @return array of adr fields if is ok
     */
-    public function getResourceAdr($view, $resId, $whereClause, $adrTable) {
-
+    public function getResourceAdr($view, $resId, $whereClause, $adrTable)
+    {
         $control = array();
-        if(!isset($view) || empty($resId) || empty($whereClause)) {
+        if (!isset($view) || empty($resId) || empty($whereClause)) {
             $control = array("status" => "ko", "error" => _PB_WITH_ARGUMENTS);
             return $control;
         }
         $docserverAdr = array();
         $db = new Database();
-        $query = "select res_id, docserver_id, path, filename, format, fingerprint, offset_doc, is_multi_docservers from " . $view 
+        $query = "select res_id, docserver_id, path, filename, format, fingerprint, offset_doc, is_multi_docservers from " . $view
             . " where res_id = ? ". $whereClause;
         $stmt = $db->query($query, array($resId));
         if ($stmt->rowCount() > 0) {
             $line = $stmt->fetchObject();
             $format = $line->format;
-            if($line->is_multi_docservers == "Y") {
+            if ($line->is_multi_docservers == "Y") {
                 if (
                     $adrTable == 'adr_letterbox' ||
                     $adrTable == 'adr_attachments' ||
@@ -394,11 +345,11 @@
                     if ($adrTable == 'adr_x') {
                         $adrTable = 'adr_letterbox';
                     }
-                    $query = "select res_id, docserver_id, path, filename, offset_doc, fingerprint, adr_priority from " 
+                    $query = "select res_id, docserver_id, path, filename, offset_doc, fingerprint, adr_priority from "
                         . $adrTable . " where res_id = ? order by adr_priority";
                     $stmt = $db->query($query, array($resId));
                     if ($stmt->rowCount() > 0) {
-                        while($line = $stmt->fetchObject()) {
+                        while ($line = $stmt->fetchObject()) {
                             array_push($docserverAdr, array("docserver_id" => $line->docserver_id, "path" => $line->path, "filename" => $line->filename, "format" => $format, "fingerprint" => $line->fingerprint, "offset_doc" => $line->offset_doc, "adr_priority" => $line->adr_priority));
                         }
                     } else {
@@ -419,4 +370,4 @@
             return $control;
         }
     }
-}
+ }
diff --git a/modules/attachments/js/functions.js b/modules/attachments/js/functions.js
index 25199cc82de..5313cdfebf8 100755
--- a/modules/attachments/js/functions.js
+++ b/modules/attachments/js/functions.js
@@ -809,7 +809,6 @@ function showContactInfo(target,contactTarget,addressTarget) {
 }
 
 function checkEffectiveDateStatus(effectiveDateStatus) {
-    console.log($j('#'+effectiveDateStatus.id).val());
     if ($j('#'+effectiveDateStatus.id).val() == 'NO_RTURN') {
         $j('#'+effectiveDateStatus.id).parent().find('[name=back_date\\[\\]]').val('');
         $j('#'+effectiveDateStatus.id).parent().find('[name=back_date\\[\\]]').prop('disabled',true);
diff --git a/modules/content_management/js/functions.js b/modules/content_management/js/functions.js
index 3e948303a54..f274b495efc 100755
--- a/modules/content_management/js/functions.js
+++ b/modules/content_management/js/functions.js
@@ -177,6 +177,9 @@ function showAppletLauncher(target, resId, objectTable, objectType, mode) {
         var chrono = $j('#'+target.id).parent().parent().find('[name=chrono\\[\\]]').val();
         var title = cleanTitle($j('#'+target.id).parent().parent().find('[name=title\\[\\]]').val());
         var back_date = $j('#'+target.id).parent().parent().find('[name=back_date\\[\\]]').val();
+        if (typeof back_date === "undefined") {
+            back_date = '';
+        }
         var backDateStatus = $j('#'+target.id).parent().parent().find('[name=backDateStatus\\[\\]]').val();
         var path = 'index.php?display=true&module=content_management&page=applet_modal_launcher&uniqueId='+attachNum+'&objectType='+objectType+'&objectId='+templateOffice+'&attachType='+attachment_types+'&objectTable=' + objectTable + '&contactId='+contactidAttach+'&addressId='+addressidAttach+'&chronoAttachment='+chrono+'&titleAttachment='+title+'&backDateStatus='+backDateStatus+'&back_date='+back_date+'&resMaster=' + resId
     }
-- 
GitLab