From 8891c672666daa255a0957be61c19c22e3d3311f Mon Sep 17 00:00:00 2001
From: Damien Burel <damien.burel@maarch.org>
Date: Wed, 4 May 2016 10:26:48 +0000
Subject: [PATCH] FEAT #3854 Improve Full Text

---
 .../trunk/class/class_lists_Abstract.php      |  38 +++++++
 .../indexing_searching/search_adv_result.php  | 104 ++++++++++++++----
 maarch_entreprise/trunk/js/functions.js       |  37 ++++---
 maarch_entreprise/trunk/lang/en.php           |   2 +
 maarch_entreprise/trunk/lang/fr.php           |   2 +
 maarch_entreprise/trunk/loadRepList.php       |  24 +++-
 .../template/documents_list_search_adv.html   |   2 +-
 .../trunk/xml/IVS/validation_rules.xml        |   1 +
 .../trunk/xml/config.xml.default              |  48 ++++++++
 9 files changed, 223 insertions(+), 35 deletions(-)

diff --git a/maarch_entreprise/trunk/class/class_lists_Abstract.php b/maarch_entreprise/trunk/class/class_lists_Abstract.php
index 749554f7aa5..fea762c6814 100644
--- a/maarch_entreprise/trunk/class/class_lists_Abstract.php
+++ b/maarch_entreprise/trunk/class/class_lists_Abstract.php
@@ -1243,6 +1243,41 @@ abstract class lists_Abstract extends Database
             }
         }
     }
+
+    protected function _tmplt_showActionAdvResultFA($parameter, $resultTheLine) {
+        //var_dump($parameter);exit;
+        $my_explode= explode ("|", $parameter);
+
+        if (!$my_explode[1]) {
+            return _WRONG_PARAM_FOR_LOAD_VALUE;
+        } else {
+            if (count($my_explode) >= 4 ) {
+                //Init
+                $actionIsDisabled = false;
+
+                //Check if action is disabled
+                if (isset($my_explode[4]) && !empty($my_explode[4])) {
+                    $actionIsDisabled = $this->_checkDisabledRules($my_explode[4], $resultTheLine);
+                }
+                //If disabled, return blank
+                if ($actionIsDisabled) {
+                    return '&nbsp;';
+                } else {
+                    //return action icon
+                    $color = '';
+                    if (!empty($_SESSION['fullTextAttachments']['letterbox']) &&
+                        in_array($resultTheLine[1]['res_id'], $_SESSION['fullTextAttachments']['letterbox'])) {
+                        $color = 'style="color: #009dc5"';
+                    }
+                    return '<a href="javascript://" onClick="'.$my_explode[3]
+                    .'" title="'.$my_explode[1].'"><i class="fa fa-'
+                    . $my_explode[2] . ' fa-2x" '. $color .' title="' . $my_explode[1] . '"></i></a>';
+                }
+            } else {
+                return _WRONG_PARAM_FOR_LOAD_VALUE;
+            }
+        }
+    }
     
     protected function _tmplt_clickOnLine($resultTheLine, $listKey, $lineIsDisabled) {
         
@@ -1582,6 +1617,9 @@ abstract class lists_Abstract extends Database
         ##showActionFA## : show action Font Awesome
         } elseif (preg_match("/^showActionFA\|/", $parameter)) {
             $var = $this->_tmplt_showActionFA($parameter, $resultTheLine);
+        ##showActionAdvResultFA## : show action Font Awesome
+        } elseif (preg_match("/^showActionAdvResultFA\|/", $parameter)) {
+            $var = $this->_tmplt_showActionAdvResultFA($parameter, $resultTheLine);
         ##clickOnLine## : Action on click under the line
         } elseif (preg_match("/^clickOnLine$/", $parameter)) {
             $var = $this->_tmplt_clickOnLine($resultTheLine, $listKey, $lineIsDisabled);
diff --git a/maarch_entreprise/trunk/indexing_searching/search_adv_result.php b/maarch_entreprise/trunk/indexing_searching/search_adv_result.php
index 9b894b47275..bc3eec0dc38 100644
--- a/maarch_entreprise/trunk/indexing_searching/search_adv_result.php
+++ b/maarch_entreprise/trunk/indexing_searching/search_adv_result.php
@@ -52,6 +52,7 @@ $indexes = $type->get_all_indexes($coll_id);
 $_SESSION['error_search'] = '';
 $_SESSION['searching']['comp_query'] = '';
 $_SESSION['save_list']['fromDetail'] = "false";
+$_SESSION['fullTextAttachments'] = [];
 
 
 // define the row of the start
@@ -324,8 +325,8 @@ if (count($_REQUEST['meta']) > 0) {
                     $_SESSION['error_search'] = _FULLTEXT_ERROR;
                 } else {
                     // FULLTEXT
-                    $fulltext_request = $_REQUEST['fulltext'];
-                    $json_txt .= " 'fulltext' : ['" 
+                    $fulltext_request = $func->normalize($_REQUEST['fulltext']);
+                    $json_txt .= " 'fulltext' : ['"
                         . addslashes(trim($_REQUEST['fulltext'])) . "'],";
                     set_include_path('apps' . DIRECTORY_SEPARATOR 
                         . $_SESSION['config']['app_id'] 
@@ -341,27 +342,90 @@ if (count($_REQUEST['meta']) > 0) {
                     
                     $_SESSION['search']['plain_text'] = $_REQUEST['fulltext'];
 
-                    $path_to_lucene_index = $_SESSION['collections'][0]['path_to_lucene_index'];
-                    if (is_dir($path_to_lucene_index))
-                    {
-                        if (!$func->isDirEmpty($path_to_lucene_index)) {
-                            $index = Zend_Search_Lucene::open($path_to_lucene_index);
-                            $hits = $index->find(urldecode($fulltext_request));
-                            $Liste_Ids = "0";
-                            $cptIds = 0;
-                            foreach ($hits as $hit) {
-                                if ($cptIds < 500) {
-                                    $Liste_Ids .= ", '". $hit->Id ."'";
-                                } else {
-                                    break;
+                    foreach ($_SESSION['collections'] as $key => $tmpCollection) {
+                        $path_to_lucene_index = $tmpCollection['path_to_lucene_index'];
+
+                        if (is_dir($path_to_lucene_index))
+                        {
+                            if (!$func->isDirEmpty($path_to_lucene_index)) {
+                                $index = Zend_Search_Lucene::open($path_to_lucene_index);
+                                $hits = $index->find(urldecode($fulltext_request));
+                                $Liste_Ids = "0";
+                                $cptIds = 0;
+                                foreach ($hits as $hit) {
+                                    if ($cptIds < 500) {
+                                        $Liste_Ids .= ", '". $hit->Id ."'";
+                                    } else {
+                                        break;
+                                    }
+                                    $cptIds ++;
                                 }
-                                $cptIds ++;
+
+                                if ($tmpCollection['table'] == 'res_attachments') {
+                                    $tmpArray = preg_split("/[,' ]/", $Liste_Ids);
+                                    array_splice($tmpArray, 0, 1);
+                                    $_SESSION['fullTextAttachments']['attachments'] = $tmpArray;
+                                    $db = new Database();
+                                    $stmt = $db->query("SELECT DISTINCT res_id_master FROM res_attachments WHERE res_id IN ($Liste_Ids)");
+                                    $idMasterDatas = [];
+                                    while ($tmp = $stmt->fetchObject())
+                                        $idMasterDatas[] = $tmp;
+
+                                    $Liste_Ids = '0';
+                                    foreach ($idMasterDatas as $tmpIdMaster) {
+                                        $Liste_Ids .= ", '{$tmpIdMaster->res_id_master}'";
+                                        $_SESSION['fullTextAttachments']['letterbox'][] = $tmpIdMaster->res_id_master;
+                                    }
+                                } elseif ($tmpCollection['table'] == 'res_version_attachments') {
+                                    $tmpArray = preg_split("/[,' ]/", $Liste_Ids);
+                                    array_splice($tmpArray, 0, 1);
+                                    $_SESSION['fullTextAttachments']['versionAttachments'] = $tmpArray;
+                                    $db = new Database();
+                                    $stmt = $db->query("SELECT DISTINCT res_id_master FROM res_version_attachments WHERE res_id IN ($Liste_Ids)");
+                                    $idMasterDatas = [];
+                                    while ($tmp = $stmt->fetchObject())
+                                        $idMasterDatas[] = $tmp;
+
+                                    $Liste_Ids = '0';
+                                    foreach ($idMasterDatas as $tmpIdMaster) {
+                                        $Liste_Ids .= ", '{$tmpIdMaster->res_id_master}'";
+                                        $_SESSION['fullTextAttachments']['letterbox'][] = $tmpIdMaster->res_id_master;
+                                    }
+                                }
+
+                                if ($key == 0)
+                                    $where_request .= ' (';
+
+                                $where_request .= " res_id IN ($Liste_Ids) ";
+
+                                if (empty($_SESSION['collections'][$key + 1]))
+                                    $where_request .= ') and ';
+                                else
+                                    $where_request .= ' or ';
+                            } else {
+                                if ($key == 0)
+                                    $where_request .= ' (';
+
+                                $where_request .= " 1=-1 ";
+
+                                if (empty($_SESSION['collections'][$key + 1]))
+                                    $where_request .= ') and ';
+                                else
+                                    $where_request .= ' or ';
                             }
-                            $where_request .= " res_id IN ($Liste_Ids) and ";
+                        } else {
+                            if ($key == 0)
+                                $where_request .= ' (';
+
+                            $where_request .= " 1=-1 ";
+
+                            if (empty($_SESSION['collections'][$key + 1]))
+                                $where_request .= ') and ';
+                            else
+                                $where_request .= ' or ';
                         }
-                    } else {
-                        $where_request .= " 1=-1 and ";
-                    }                    
+                    }
+
                 }
             }
             // TAGS
diff --git a/maarch_entreprise/trunk/js/functions.js b/maarch_entreprise/trunk/js/functions.js
index 9e212a60ce3..d06d66c38c7 100644
--- a/maarch_entreprise/trunk/js/functions.js
+++ b/maarch_entreprise/trunk/js/functions.js
@@ -2518,26 +2518,37 @@ function showVille(url,value){
 
         }
     })
-    };
+    }
 
 }
 
 
-function loadRepList(id)
+function loadRepList(id, isFullText)
 {
-    new Effect.toggle('repList_'+id, 'appear' , {delay:0.2});
+    if ($('repList_' + id).style.display != 'none') {
+        new Effect.toggle('repList_'+id, 'appear' , {delay:0.2});
+    } else {
+        new Effect.toggle('repList_'+id, 'appear' , {delay:0.2});
 
-    var path_manage_script = 'index.php?page=loadRepList&display=true';
+        var path_manage_script = 'index.php?page=loadRepList&display=true';
+
+        if (typeof isFullText == 'undefined')
+            isFullText = '';
+
+        new Ajax.Request(path_manage_script,
+        {
+            method:'post',
+            parameters: {
+                res_id_master : id,
+                isFullText : isFullText
+            },
+            onSuccess: function(answer){
+                eval("response = "+answer.responseText);
+                $('divRepList_'+id).innerHTML = response.toShow;
+            }
+        });
+    }
 
-    new Ajax.Request(path_manage_script,
-    {
-        method:'post',
-        parameters: { res_id_master : id},
-        onSuccess: function(answer){
-            eval("response = "+answer.responseText);
-            $('divRepList_'+id).innerHTML = response.toShow;
-        }
-    });
 }
 
 function checkBeforeOpenBlank (url, value)
diff --git a/maarch_entreprise/trunk/lang/en.php b/maarch_entreprise/trunk/lang/en.php
index 63b12ceedb7..27b72241661 100644
--- a/maarch_entreprise/trunk/lang/en.php
+++ b/maarch_entreprise/trunk/lang/en.php
@@ -577,6 +577,8 @@ if (!defined('_LAST_QUERY')) define('_LAST_QUERY', 'Latest query');
 if (!defined('_LATE')) define('_LATE', 'Late');
 if (!defined('_LATE_PROCESS')) define('_LATE_PROCESS', 'Late');
 if (!defined('_LETTERBOX')) define('_LETTERBOX', 'Main collection');
+if (!defined("_ATTACHMENTS_COLL")) define("_ATTACHMENTS_COLL", "Attachments collection");
+if (!defined("_ATTACHMENTS_VERS_COLL")) define("_ATTACHMENTS_VERS_COLL", "Version attachments collection");
 if (!defined('_LETTER_INFO')) define('_LETTER_INFO','Information on document');
 if (!defined('_LIMIT_DATE_PROCESS')) define('_LIMIT_DATE_PROCESS','Processing deadline');
 if (!defined('_LINK')) define('_LINK', 'Link');
diff --git a/maarch_entreprise/trunk/lang/fr.php b/maarch_entreprise/trunk/lang/fr.php
index ec9c26f478e..b8fd190eee4 100644
--- a/maarch_entreprise/trunk/lang/fr.php
+++ b/maarch_entreprise/trunk/lang/fr.php
@@ -855,6 +855,8 @@ if (!defined("_DOWNLOADED_FILE")) define("_DOWNLOADED_FILE", "Fichier chargé");
 if (!defined("_WRONG_FILE_TYPE")) define("_WRONG_FILE_TYPE", "Ce type de fichier n'est pas permis");
 
 if (!defined("_LETTERBOX")) define("_LETTERBOX", "Collection des courriers");
+if (!defined("_ATTACHMENTS_COLL")) define("_ATTACHMENTS_COLL", "Collection des attachements");
+if (!defined("_ATTACHMENTS_VERS_COLL")) define("_ATTACHMENTS_VERS_COLL", "Collection des version d'attachements");
 if (!defined("_APA_COLL")) define("_APA_COLL", "Collection de l'archivage physique");
 if (!defined("_REDIRECT_TO_ACTION")) define("_REDIRECT_TO_ACTION", "Rediriger vers une action");
 if (!defined("_DOCUMENTS_LIST")) define("_DOCUMENTS_LIST", "Liste");
diff --git a/maarch_entreprise/trunk/loadRepList.php b/maarch_entreprise/trunk/loadRepList.php
index 59f58c84b8d..8e6fdc6b4e1 100644
--- a/maarch_entreprise/trunk/loadRepList.php
+++ b/maarch_entreprise/trunk/loadRepList.php
@@ -73,7 +73,29 @@ if (isset($_REQUEST['res_id_master'])) {
                 $stmt = $db->query($query, $arrayPDO);
 
                 while ($return_db = $stmt->fetchObject()) {
-                    $return .= '<tr style="border: 1px solid;" style="background-color: #FFF;">';
+                    if (!empty($_REQUEST['isFullText']))
+                    {
+                        if ($return_db->format != 'pdf') {
+                            $t = str_replace('.' . $return_db->format, '.pdf', $return_db->filename);
+                            $stmtFullText = $db->query('SELECT res_id FROM res_view_attachments WHERE filename = ? and attachment_type = ? ORDER BY relation desc',
+                                                        [str_replace('.' . $return_db->format, '.pdf', $return_db->filename), 'converted_pdf']);
+                            $lineFullText = $stmtFullText->fetchObject();
+                            if ($lineFullText && $lineFullText->res_id != 0)
+                                $resIdConverted = $lineFullText->res_id;
+                        }
+
+                        if ((!empty($_SESSION['fullTextAttachments']['attachments']) && in_array($return_db->res_id, $_SESSION['fullTextAttachments']['attachments'])) ||
+                            (!empty($_SESSION['fullTextAttachments']['versionAttachments']) && in_array($return_db->res_id_version, $_SESSION['fullTextAttachments']['versionAttachments']))) {
+                            $return .= '<tr style="border: 1px solid;color: #009dc5;font-weight: bold" style="background-color: #FFF;">';
+                        } else if (!empty($resIdConverted) && in_array($resIdConverted, $_SESSION['fullTextAttachments']['attachments'])) {
+                            $return .= '<tr style="border: 1px solid;color: #009dc5;font-weight: bold" style="background-color: #FFF;">';
+                        } else {
+                            $return .= '<tr style="border: 1px solid;" style="background-color: #FFF;">';
+
+                        }
+                    } else {
+                        $return .= '<tr style="border: 1px solid;" style="background-color: #FFF;">';
+                    }
                         $return .= '<td>';
                             $return .= '&nbsp;&nbsp;';
                             $return .= functions::xssafe($return_db->identifier);
diff --git a/maarch_entreprise/trunk/template/documents_list_search_adv.html b/maarch_entreprise/trunk/template/documents_list_search_adv.html
index d1d13dad7f4..b7333d82075 100644
--- a/maarch_entreprise/trunk/template/documents_list_search_adv.html
+++ b/maarch_entreprise/trunk/template/documents_list_search_adv.html
@@ -151,7 +151,7 @@ Mods
                                         <td style="font-size:10px;text-align:left"  width ="80px" >##loadValue|type_label##</td>
                                         <td style="font-size:10px;" width ="50px">##func_bool_see_multi_contacts##</td>
                                         <td style="font-size:10px;" width ="30px">##func_bool_see_notes##</td>
-                                        <td style="font-size:10px;" width ="30px">##showActionFA|#defineLang|_ATTACHMENTS#|paperclip|loadRepList('#loadValue|res_id#')|#loadValue|count_attachment# == 0##</td>
+                                        <td style="font-size:10px;" width ="30px">##showActionAdvResultFA|#defineLang|_ATTACHMENTS#|paperclip|loadRepList('#loadValue|res_id#', 'FT')|#loadValue|count_attachment# == 0##</td>
                                         <td style="font-size:10px;" width ="30px">##showActionFA|#defineLang|_WF#|gear|loadDiffList('#loadValue|res_id#')##</td>
                                         <td style="font-size:10px;" width ="30px" >##showIconDocument##</td>
                                         <td style="font-size:10px;" width ="30px" >##showIconDetails##</td>
diff --git a/maarch_entreprise/trunk/xml/IVS/validation_rules.xml b/maarch_entreprise/trunk/xml/IVS/validation_rules.xml
index c59aca797d9..2862ce70cd9 100755
--- a/maarch_entreprise/trunk/xml/IVS/validation_rules.xml
+++ b/maarch_entreprise/trunk/xml/IVS/validation_rules.xml
@@ -718,6 +718,7 @@
 
     <validationRule name="res_id_master" extends="standardForm" mode="error">  
       <parameter name="res_id_master" type="integer" />
+      <parameter name="isFullText" type="string" />
     </validationRule>
 
     <validationRule name="identifier_integer" extends="standardForm" mode="error">  
diff --git a/maarch_entreprise/trunk/xml/config.xml.default b/maarch_entreprise/trunk/xml/config.xml.default
index d3e382ece61..d1da8bcec19 100644
--- a/maarch_entreprise/trunk/xml/config.xml.default
+++ b/maarch_entreprise/trunk/xml/config.xml.default
@@ -75,6 +75,54 @@
             <default_category>incoming</default_category>
         </categories>
     </COLLECTION>
+    <COLLECTION>
+        <id>attachments_coll</id>
+        <label>_ATTACHMENTS_COLL</label>
+        <table>res_attachments</table>
+        <version_table>res_version_attachments</version_table>
+        <view>res_view_attachments</view>
+        <adr>adr_attachments</adr>
+        <index_file>index_attachments.xml</index_file>
+        <script_add>index_attachments.php</script_add>
+        <script_search>search_attachments.php</script_search>
+        <script_search_result>list_results_attachments.php</script_search_result>
+        <script_details>details_attachments.php</script_details>
+        <extensions>
+            <table/>
+        </extensions>
+        <path_to_lucene_index>C:\Maarch\docservers\indexes\attachments_coll\</path_to_lucene_index>
+        <categories>
+            <category>
+                <id>incoming</id>
+                <label>_INCOMING</label>
+            </category>
+            <default_category>incoming</default_category>
+        </categories>
+    </COLLECTION>
+    <COLLECTION>
+        <id>version_attachments_coll</id>
+        <label>_ATTACHMENTS_VERS_COLL</label>
+        <table>res_version_attachments</table>
+        <version_table>res_version_attachments</version_table>
+        <view>res_view_attachments</view>
+        <adr>adr_attachments</adr>
+        <index_file>index_attachments.xml</index_file>
+        <script_add>index_attachments.php</script_add>
+        <script_search>search_attachments.php</script_search>
+        <script_search_result>list_results_attachments.php</script_search_result>
+        <script_details>details_attachments.php</script_details>
+        <extensions>
+            <table/>
+        </extensions>
+        <path_to_lucene_index>C:\Maarch\docservers\indexes\version_attachments_coll\</path_to_lucene_index>
+        <categories>
+            <category>
+                <id>incoming</id>
+                <label>_INCOMING</label>
+            </category>
+            <default_category>incoming</default_category>
+        </categories>
+    </COLLECTION>
     <HISTORY>
         <usersdel>true</usersdel>
         <usersban>true</usersban>
-- 
GitLab