From 2f6255073f86b918d87f45d628dd613eff14a95b Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Wed, 24 Apr 2019 13:26:07 +0100
Subject: [PATCH] FIX #10387 TIME 1 notification errors

---
 .../notifications/batch/load_basket_event_stack.php  |  2 ++
 modules/notifications/diffusion_types/dest_user.php  |  4 ++--
 .../templates/class/templates_controler_Abstract.php |  6 +++---
 modules/templates/datasources/mlb_notes_content.php  | 12 +++++-------
 sql/develop.sql                                      |  2 +-
 sql/structure.sql                                    |  2 +-
 6 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/modules/notifications/batch/load_basket_event_stack.php b/modules/notifications/batch/load_basket_event_stack.php
index f913e15fb76..4b89851c4fb 100755
--- a/modules/notifications/batch/load_basket_event_stack.php
+++ b/modules/notifications/batch/load_basket_event_stack.php
@@ -207,6 +207,8 @@ $mailerParams = $xmlconfig->MAILER;
 
 // INCLUDES
 try {
+    Bt_myInclude('vendor/autoload.php');
+
     Bt_myInclude(
         'core' . DIRECTORY_SEPARATOR . 'class'
         . DIRECTORY_SEPARATOR . 'class_functions.php'
diff --git a/modules/notifications/diffusion_types/dest_user.php b/modules/notifications/diffusion_types/dest_user.php
index 16552725b6f..2dbda285f7a 100755
--- a/modules/notifications/diffusion_types/dest_user.php
+++ b/modules/notifications/diffusion_types/dest_user.php
@@ -22,7 +22,7 @@ switch ($request) {
         $arrayPDO = array(':recordid' => $event->record_id);
         switch ($event->table_name) {
             case 'notes':
-                $from .= ' JOIN notes ON notes.coll_id = li.coll_id AND notes.identifier = li.res_id';
+                $from .= ' JOIN notes ON notes.identifier = li.res_id';
                 $from .= ' JOIN res_letterbox lb ON lb.res_id = notes.identifier';
                 $where .= ' AND notes.id = :recordid AND li.item_id != notes.user_id'
                     .' AND ('
@@ -86,7 +86,7 @@ switch ($request) {
         $arrayPDO = array(':recordid' => $event->record_id);
         switch ($event->table_name) {
             case 'notes':
-                $from .= ' JOIN notes ON notes.coll_id = li.coll_id AND notes.identifier = li.res_id';
+                $from .= ' JOIN notes ON notes.identifier = li.res_id';
                 $from .= ' JOIN res_letterbox lb ON lb.res_id = notes.identifier';
                 $where .= ' AND notes.id = :recordid AND li.item_id != notes.user_id';
                 if ($notification->diffusion_properties != '') {
diff --git a/modules/templates/class/templates_controler_Abstract.php b/modules/templates/class/templates_controler_Abstract.php
index bf27752ec6c..d9a31561ac4 100755
--- a/modules/templates/class/templates_controler_Abstract.php
+++ b/modules/templates/class/templates_controler_Abstract.php
@@ -861,7 +861,7 @@ abstract class templates_controler_Abstract extends ObjectControler implements O
             }
             
             // Current basket
-            if (count($_SESSION['current_basket']) > 0) {
+            if (!empty($_SESSION['current_basket']) && count($_SESSION['current_basket']) > 0) {
                 foreach ($_SESSION['current_basket'] as $name => $value) {
                     if (!is_array($value)) {
                         $datasources['basket'][0][$name] = $value;
@@ -870,13 +870,13 @@ abstract class templates_controler_Abstract extends ObjectControler implements O
             }
             
             // User
-            if (count($_SESSION['user']) > 0) {
+            if (!empty($_SESSION['user']) && count($_SESSION['user']) > 0) {
                 foreach ($_SESSION['user'] as $name => $value) {
                     if (!is_array($value)) {
                         $datasources['user'][0][strtolower($name)] = $value;
                     }
                 }
-                if (count($_SESSION['user']['entities']) > 0) {
+                if (!empty($_SESSION['user']['entities']) && count($_SESSION['user']['entities']) > 0) {
                     foreach ($_SESSION['user']['entities'] as $entity) {
                         if ($entity['ENTITY_ID'] === $_SESSION['user']['primaryentity']['id']) {
                             $datasources['user'][0]['entity'] = $_SESSION['user']['entities'][0]['ENTITY_LABEL'];
diff --git a/modules/templates/datasources/mlb_notes_content.php b/modules/templates/datasources/mlb_notes_content.php
index f102a39d714..afcdaa7163d 100755
--- a/modules/templates/datasources/mlb_notes_content.php
+++ b/modules/templates/datasources/mlb_notes_content.php
@@ -27,9 +27,8 @@ foreach($events as $event) {
             . "FROM " . $res_view . " mlb "
             . "JOIN notes on notes.identifier = mlb.res_id "
             . "JOIN users on users.user_id = notes.user_id "
-            . "WHERE notes.coll_id = ? "
-            . "AND notes.id = ? ";
-        $arrayPDO = array($coll_id, $event->record_id);
+            . "WHERE notes.id = ? ";
+        $arrayPDO = array($event->record_id);
         break;
     
     case "res_letterbox" :
@@ -38,14 +37,13 @@ foreach($events as $event) {
             . "notes.*, "
             . "users.* " 
             . "FROM listinstance li JOIN " . $res_view . " mlb ON mlb.res_id = li.res_id "
-            . "JOIN notes on li.coll_id=notes.coll_id AND notes.identifier = li.res_id "
+            . "JOIN notes on notes.identifier = li.res_id "
             . "JOIN users on users.user_id = notes.user_id "
-            . "WHERE li.coll_id = ? "
-            . "AND li.item_id = ? "
+            . "WHERE li.item_id = ? "
             . "AND li.item_mode = 'dest' "
             . "AND li.item_type = 'user_id' "
             . "AND li.res_id = ? ";
-        $arrayPDO = array($coll_id, $recipient->user_id, $event->record_id);
+        $arrayPDO = array($recipient->user_id, $event->record_id);
         break;
     }
     
diff --git a/sql/develop.sql b/sql/develop.sql
index 56bd3ad594a..8de0a8a0c35 100755
--- a/sql/develop.sql
+++ b/sql/develop.sql
@@ -15,7 +15,7 @@ ALTER TABLE res_letterbox DROP COLUMN IF EXISTS external_signatory_book_id;
 ALTER TABLE res_letterbox ADD COLUMN external_signatory_book_id integer;
 
 ALTER TABLE users DROP COLUMN IF EXISTS external_id;
-ALTER TABLE users ADD COLUMN external_id json DEFAULT '{}';
+ALTER TABLE users ADD COLUMN external_id jsonb DEFAULT '{}';
 
 /* Redirected Baskets */
 DO $$ BEGIN
diff --git a/sql/structure.sql b/sql/structure.sql
index 44f3a169b69..3c7fc3e63c2 100755
--- a/sql/structure.sql
+++ b/sql/structure.sql
@@ -226,7 +226,7 @@ CREATE TABLE users
   cookie_date timestamp without time zone,
   failed_authentication INTEGER DEFAULT 0,
   locked_until TIMESTAMP without time zone,
-  external_id json DEFAULT '{}',
+  external_id jsonb DEFAULT '{}',
   CONSTRAINT users_pkey PRIMARY KEY (user_id),
   CONSTRAINT users_id_key UNIQUE (id)
 )
-- 
GitLab