From 7ffe4649bbc36cc28c8f66b51f1b364c1d99f7a1 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Wed, 11 Apr 2018 17:11:31 +0200
Subject: [PATCH] FEAT BAN

---
 addresses_ban/index_ban.php                   | 90 -------------------
 ban/indexBan.php                              | 85 ++++++++++++++++++
 ban/src/.gitkeep                              |  0
 src/app/user/models/UserModelAbstract.php     | 11 +--
 .../controllers/AutoCompleteController.php    | 15 ++--
 5 files changed, 100 insertions(+), 101 deletions(-)
 delete mode 100755 addresses_ban/index_ban.php
 create mode 100644 ban/indexBan.php
 create mode 100644 ban/src/.gitkeep

diff --git a/addresses_ban/index_ban.php b/addresses_ban/index_ban.php
deleted file mode 100755
index 0dc31dd0395..00000000000
--- a/addresses_ban/index_ban.php
+++ /dev/null
@@ -1,90 +0,0 @@
-<?php
-
-require '../vendor/autoload.php';
-$indexFileDirectory = __DIR__ . '/indexes/';
-$banDirectory       = __DIR__ . '/BAN/';
-
-$filesBan = scandir($banDirectory);
-if (!is_dir($indexFileDirectory)) {
-    $index = Zend_Search_Lucene::create($indexFileDirectory);
-} else {
-    if (isDirEmpty($indexFileDirectory)) {
-        $index = Zend_Search_Lucene::create($indexFileDirectory);
-    } else {
-        $index = Zend_Search_Lucene::open($indexFileDirectory);
-    }
-}
-$index->setFormatVersion(Zend_Search_Lucene::FORMAT_2_3); // we set the lucene format to 2.3
-Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive());
-$index->setMaxBufferedDocs(1000);
-
-foreach ($filesBan as $fileBan) {
-    if (!in_array($fileBan, ['.', '..']) && ($handle = fopen($banDirectory . $fileBan, "r")) !== false) {
-        $row = 1;
-        while (($data = fgetcsv($handle, 0, ";")) !== false) {
-            if ($row == 1) {
-                $row++;
-                continue;
-            }
-            if (!empty($data[9])) {
-                $doc = new Zend_Search_Lucene_Document();
-
-                $doc->addField(Zend_Search_Lucene_Field::UnIndexed('id', \SrcCore\models\TextFormatModel::normalize(['string' => $data[0]])));
-                if (!empty($data[1])) {
-                    $doc->addField(Zend_Search_Lucene_Field::Text('streetName', \SrcCore\models\TextFormatModel::normalize(['string' => $data[1]])));
-                }
-                $doc->addField(Zend_Search_Lucene_Field::UnIndexed('streetNumber', $data[3] . ' ' . $data[4]));
-                $doc->addField(Zend_Search_Lucene_Field::Text('postalCode', $data[6]));
-                $doc->addField(Zend_Search_Lucene_Field::Text('afnorName', $data[9]));
-                $doc->addField(Zend_Search_Lucene_Field::Text('city', \SrcCore\models\TextFormatModel::normalize(['string' => $data[10]])));
-
-                $index->addDocument($doc);
-
-                if ($row == 1000) {
-                    break;
-                }
-                if (fmod($row, 100) == 0) {
-                    echo "$row\n";
-                }
-                $row++;
-            }
-        }
-        fclose($handle);
-    }
-}
-
-$index->commit();
-
-
-
-// set_include_path(__DIR__ . '/../apps/maarch_entreprise/tools/' . PATH_SEPARATOR . get_include_path());
-// $_ENV['maarch_tools_path'] = __DIR__ . '/../apps/maarch_entreprise/tools/';
-// Zend_Search_Lucene_Analysis_Analyzer::setDefault(
-//     new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive() // we need utf8 for accents
-// );
-// Zend_Search_Lucene_Search_QueryParser::setDefaultOperator(Zend_Search_Lucene_Search_QueryParser::B_AND);
-// Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8');
-// $index = Zend_Search_Lucene::open($indexFileDirectory);
-// $hits = $index->find('gambetta~');
-// foreach ($hits as $hit) {
-//     var_dump($hit->Id . ': ' . $hit->streetNumber . ' ' . $hit->streetName . ' ' . $hit->postalCode . ' ' . $hit->city . ' ' . $hit->afnorName);
-// }
-
-/**
-* Check if a folder is empty
-* @param  $dir string path of the directory to chek
-* @return boolean true if the directory exists
-*/
-function isDirEmpty($dir)
-{
-    $dir = opendir($dir);
-    $isEmpty = true;
-    while (($entry = readdir($dir)) !== false) {
-        if ($entry !== '.' && $entry !== '..') {
-            $isEmpty = false;
-            break;
-        }
-    }
-    closedir($dir);
-    return $isEmpty;
-}
diff --git a/ban/indexBan.php b/ban/indexBan.php
new file mode 100644
index 00000000000..73834efdd03
--- /dev/null
+++ b/ban/indexBan.php
@@ -0,0 +1,85 @@
+<?php
+
+require '../vendor/autoload.php';
+$indexFileDirectory = __DIR__ . '/indexes/';
+$banDirectory       = __DIR__ . '/src/';
+
+$filesBan = scandir($banDirectory);
+if (!is_dir($indexFileDirectory)) {
+    $index = Zend_Search_Lucene::create($indexFileDirectory);
+} else {
+    if (isDirEmpty($indexFileDirectory)) {
+        $index = Zend_Search_Lucene::create($indexFileDirectory);
+    } else {
+        $index = Zend_Search_Lucene::open($indexFileDirectory);
+    }
+}
+$index->setFormatVersion(Zend_Search_Lucene::FORMAT_2_3); // we set the lucene format to 2.3
+Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive());
+$index->setMaxBufferedDocs(1000);
+
+$row = 1;
+foreach ($filesBan as $fileBan) {
+    if (!in_array($fileBan, ['.', '..']) && ($handle = fopen($banDirectory . $fileBan, "r")) !== false) {
+        echo "$fileBan\n";
+        $duplicateAddresses = [];
+        $currentCity = '';
+        $i = 1;
+        while (($data = fgetcsv($handle, 0, ";")) !== false) {
+            if ($i == 1) {
+                $i++;
+                continue;
+            }
+
+            if (!empty($data[9])) {
+                if ($currentCity != $data[6]) {
+                    $duplicateAddresses = [];
+                }
+                $currentCity = $data[6];
+
+                if (empty($duplicateAddresses[$data[3] . $data[4] . $data[9] . $data[6]])) {
+                    $doc = new Zend_Search_Lucene_Document();
+
+                    $doc->addField(Zend_Search_Lucene_Field::UnIndexed('banId', \SrcCore\models\TextFormatModel::normalize(['string' => $data[0]])));
+                    if (!empty($data[1])) {
+                        $doc->addField(Zend_Search_Lucene_Field::Text('streetName', \SrcCore\models\TextFormatModel::normalize(['string' => $data[1]])));
+                    }
+                    $streetNumber = empty($data[4]) ? $data[3] : ($data[3] . ' ' . $data[4]);
+                    $doc->addField(Zend_Search_Lucene_Field::Text('streetNumber', $streetNumber));
+                    $doc->addField(Zend_Search_Lucene_Field::Text('postalCode', $data[6]));
+                    $doc->addField(Zend_Search_Lucene_Field::Text('afnorName', $data[9]));
+                    $doc->addField(Zend_Search_Lucene_Field::Text('city', \SrcCore\models\TextFormatModel::normalize(['string' => $data[10]])));
+
+                    $index->addDocument($doc);
+                    $duplicateAddresses[$data[3] . $data[4] . $data[9] . $data[6]] = true;
+                }
+            }
+            if (fmod($row, 100) == 0) {
+                echo "$row\n";
+            }
+            $row++;
+        }
+        fclose($handle);
+    }
+}
+
+$index->commit();
+
+/**
+* Check if a folder is empty
+* @param  $dir string path of the directory to chek
+* @return boolean true if the directory exists
+*/
+function isDirEmpty($dir)
+{
+    $dir = opendir($dir);
+    $isEmpty = true;
+    while (($entry = readdir($dir)) !== false) {
+        if ($entry !== '.' && $entry !== '..') {
+            $isEmpty = false;
+            break;
+        }
+    }
+    closedir($dir);
+    return $isEmpty;
+}
diff --git a/ban/src/.gitkeep b/ban/src/.gitkeep
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/src/app/user/models/UserModelAbstract.php b/src/app/user/models/UserModelAbstract.php
index b0dde544e00..423315d77c6 100644
--- a/src/app/user/models/UserModelAbstract.php
+++ b/src/app/user/models/UserModelAbstract.php
@@ -58,11 +58,11 @@ class UserModelAbstract
         return $aUser[0];
     }
 
-    public static function create(array $aArgs = [])
+    public static function create(array $aArgs)
     {
         ValidatorModel::notEmpty($aArgs, ['user']);
         ValidatorModel::notEmpty($aArgs['user'], ['userId', 'firstname', 'lastname']);
-        ValidatorModel::stringType($aArgs['user'], ['userId', 'firstname', 'lastname', 'mail', 'initials', 'thumbprint', 'phone', 'changePassword']);
+        ValidatorModel::stringType($aArgs['user'], ['userId', 'firstname', 'lastname', 'mail', 'initials', 'thumbprint', 'phone', 'changePassword', 'loginmode']);
 
         DatabaseModel::insert([
             'table'         => 'users',
@@ -77,7 +77,7 @@ class UserModelAbstract
                 'enabled'           => 'Y',
                 'status'            => 'OK',
                 'change_password'   => empty($aArgs['user']['changePassword']) ? 'Y' : $aArgs['user']['changePassword'],
-                'loginmode'         => 'standard',
+                'loginmode'         => empty($aArgs['user']['loginmode']) ? 'standard' : $aArgs['user']['loginmode'],
                 'password'          => SecurityModel::getPasswordHash('maarch')
             ]
         ]);
@@ -90,7 +90,7 @@ class UserModelAbstract
         ValidatorModel::notEmpty($aArgs, ['id', 'user']);
         ValidatorModel::notEmpty($aArgs['user'], ['firstname', 'lastname']);
         ValidatorModel::intVal($aArgs, ['id']);
-        ValidatorModel::stringType($aArgs['user'], ['firstname', 'lastname', 'mail', 'initials', 'thumbprint', 'phone', 'enabled']);
+        ValidatorModel::stringType($aArgs['user'], ['firstname', 'lastname', 'mail', 'initials', 'thumbprint', 'phone', 'enabled', 'loginmode']);
 
         DatabaseModel::update([
             'table'     => 'users',
@@ -101,7 +101,8 @@ class UserModelAbstract
                 'phone'         => $aArgs['user']['phone'],
                 'initials'      => $aArgs['user']['initials'],
                 'enabled'       => $aArgs['user']['enabled'],
-                'thumbprint'    => $aArgs['user']['thumbprint']
+                'thumbprint'    => $aArgs['user']['thumbprint'],
+                'loginmode'     => empty($aArgs['user']['loginmode']) ? 'standard' : $aArgs['user']['loginmode'],
             ],
             'where'     => ['id = ?'],
             'data'      => [$aArgs['id']]
diff --git a/src/core/controllers/AutoCompleteController.php b/src/core/controllers/AutoCompleteController.php
index 33aad37da54..ee55311e5fc 100644
--- a/src/core/controllers/AutoCompleteController.php
+++ b/src/core/controllers/AutoCompleteController.php
@@ -131,18 +131,21 @@ class AutoCompleteController
         \Zend_Search_Lucene_Search_QueryParser::setDefaultOperator(\Zend_Search_Lucene_Search_QueryParser::B_AND);
         \Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8');
 
-        $index = \Zend_Search_Lucene::open('addresses_ban/indexes');
+        $index = \Zend_Search_Lucene::open('ban/indexes');
+        \Zend_Search_Lucene::setResultSetLimit(10);
         $hits = $index->find($data['address']);
+
         $addresses = [];
         foreach($hits as $key => $hit){
             $addresses[] = [
-                $hit->id,
-                $hit->afnorName,
-                $hit->postalCode,
-                $hit->streetNumber
+                'banId'         => $hit->banId,
+                'number'        => $hit->streetNumber,
+                'afnorName'     => $hit->afnorName,
+                'postalCode'    => $hit->postalCode,
+                'city'          => $hit->city
             ];
         }
 
-        return $response->withJson(['count' => count($hits), 'addresses' => $addresses]);
+        return $response->withJson(['addresses' => $addresses]);
     }
 }
-- 
GitLab