From 963c26013735f8bdc27ad26a8a4de53545f4f432 Mon Sep 17 00:00:00 2001
From: Patrick Barroca <pbarroca@afi-sa.fr>
Date: Fri, 13 Apr 2018 11:11:06 +0200
Subject: [PATCH] dev #17352 : fix RT

---
 FEATURES/17352                                |  8 +++---
 VERSIONS_WIP/17352                            |  2 +-
 library/Class/AdminVar.php                    |  8 +++++-
 .../Cosmogramme/Integration/RawRecord.php     | 26 +++++++++++++++----
 .../RawRecordHistory/RawRecordVersionTest.php | 13 +++++++---
 5 files changed, 43 insertions(+), 14 deletions(-)

diff --git a/FEATURES/17352 b/FEATURES/17352
index a21e13fa6bd..e13be545ba6 100644
--- a/FEATURES/17352
+++ b/FEATURES/17352
@@ -1,10 +1,10 @@
         '17352' =>
-            ['Label' => $this->_('Cosmogramme: garder l'unimarc natif d'une notice'),
-             'Desc' => '',
+            ['Label' => $this->_('Visualisation du fichier envoyé par le SIGB pour chaque notice'),
+             'Desc' => $this->_('Bokeh conserve le dernier fichier reçu du SIGB pour chaque notice. Celui-ce est téléchargeable à partir d'Inspector Gadget. Nécessite une activation.'),
              'Image' => '',
              'Video' => '',
-             'Category' => '',
+             'Category' => 'Catalogue',
              'Right' => function($feature_description, $user) {return true;},
              'Wiki' => '',
              'Test' => '',
-             'Date' => '2017-12-08'],
\ No newline at end of file
+             'Date' => '2018-04-13'],
\ No newline at end of file
diff --git a/VERSIONS_WIP/17352 b/VERSIONS_WIP/17352
index d42f28617b9..8f017955532 100644
--- a/VERSIONS_WIP/17352
+++ b/VERSIONS_WIP/17352
@@ -1 +1 @@
- - ticket #17352 : Cosmogramme: garder l'unimarc natif d'une notice
\ No newline at end of file
+ - ticket #17352 : Cosmogramme: Bokeh conserve le dernier fichier reçu du SIGB pour chaque notice. Celui-ce est téléchargeable à partir d'Inspector Gadget. Nécessite une activation.
\ No newline at end of file
diff --git a/library/Class/AdminVar.php b/library/Class/AdminVar.php
index eba089f45b0..0a564dd66a9 100644
--- a/library/Class/AdminVar.php
+++ b/library/Class/AdminVar.php
@@ -396,7 +396,8 @@ class Class_AdminVarLoader extends Storm_Model_Loader {
   protected function _getCosmogrammeVars() {
     return [
             'DATE_LAST_FULL_INTEGRATION_USERS' => Class_AdminVar_Meta::newDefault($this->_('Date du dernier import total des abonnés (modifié par cosmogramme)')),
-            'INTEGRATIONS_TOO_LONG_ALERT_THRESHOLD' => Class_AdminVar_Meta::newDefault($this->_('Seuil d\'alerte en heures pour détecter que les traitements d\'intégration prennent trop de temps. Par défaut: 2', ['value' => 2]))
+            'INTEGRATIONS_TOO_LONG_ALERT_THRESHOLD' => Class_AdminVar_Meta::newDefault($this->_('Seuil d\'alerte en heures pour détecter que les traitements d\'intégration prennent trop de temps. Par défaut: 2', ['value' => 2])),
+            'KEEP_LAST_SIGB_RECORD' => Class_AdminVar_Meta::newOnOff($this->_('Conserve le dernier fichier transmis par le SIGB pour chaque notice. Celui-ci est téléchargeable à partir de l\'Inspecteur Gadget.'))->bePrivate(),
     ];
   }
 
@@ -957,6 +958,11 @@ class Class_AdminVarLoader extends Storm_Model_Loader {
     return Class_AdminVar::isBibNumEnabled()
       && Class_AdminVar::get('SITO_IN_ALBUMS');
   }
+
+
+  public function shouldKeepLastSigbRecord() {
+    return Class_AdminVar::isModuleEnabled('KEEP_LAST_SIGB_RECORD');
+  }
 }
 
 
diff --git a/library/Class/Cosmogramme/Integration/RawRecord.php b/library/Class/Cosmogramme/Integration/RawRecord.php
index 34255efab3a..adf999966e0 100644
--- a/library/Class/Cosmogramme/Integration/RawRecord.php
+++ b/library/Class/Cosmogramme/Integration/RawRecord.php
@@ -29,20 +29,35 @@ class Class_Cosmogramme_Integration_RawRecord {
     $_id,
     $_date;
 
+
   public static function hasVersionFor($item) {
-    return (new static('', $item->getIdOrigine(), $item->getIdIntBib()))
-      ->hasVersion();
+    return static::isActive()
+      ? static::_newFor($item)->hasVersion()
+      : false;
   }
 
 
   public static function findFor($item) {
-    $instance = new static('', $item->getIdOrigine(), $item->getIdIntBib());
+    if (!static::isActive())
+      return;
+
+    $instance = static::_newFor($item);
     return ($instance = $instance->loadLastVersion())
       ? $instance
       : null;
   }
 
 
+  public static function isActive() {
+    return Class_AdminVar::shouldKeepLastSigbRecord();
+  }
+
+
+  protected static function _newFor($item) {
+    return new static('', $item->getIdOrigine(), $item->getIdIntBib());
+  }
+
+
   public function __construct($data, $id, $library_id) {
     $this->_library_id = $library_id;
     $this->_data = $data;
@@ -85,12 +100,13 @@ class Class_Cosmogramme_Integration_RawRecord {
 
 
   protected function _isValid() {
-    return is_string($this->_data);
+    return static::isActive() && is_string($this->_data);
   }
 
 
   public function loadLastVersion() {
-    if (!$version = $this->getLastVersion())
+    if (!static::isActive()
+        || (!$version = $this->getLastVersion()))
       return;
 
     $this->_data = $version->getData()['data'];
diff --git a/tests/scenarios/RawRecordHistory/RawRecordVersionTest.php b/tests/scenarios/RawRecordHistory/RawRecordVersionTest.php
index 5475cc61236..08534617a10 100644
--- a/tests/scenarios/RawRecordHistory/RawRecordVersionTest.php
+++ b/tests/scenarios/RawRecordHistory/RawRecordVersionTest.php
@@ -27,17 +27,24 @@ class RawRecordUnimarcVersionTest extends ModelTestCase {
 
   public function setUp() {
     parent::setUp();
-    Class_Versions_RawRecord::defaultToFile();
 
     $marc = file_get_contents(__DIR__ . '/audiard_herosdiscret.uni');
-
     $this->_record = new Class_Cosmogramme_Integration_RawRecord($marc, '694479', 12);
+  }
+
+
+  /** @test */
+  public function whenNotActivatedShouldNotHaveCreatedOneVersion() {
+    Class_AdminVar::set('KEEP_LAST_SIGB_RECORD', 0);
     $this->_record->save();
+    $this->assertEquals(0, $this->_record->numberOfVersions());
   }
 
 
   /** @test */
-  public function shouldHaveCreatedOneVersion() {
+  public function whenActivatedShouldHaveCreatedOneVersion() {
+    Class_AdminVar::set('KEEP_LAST_SIGB_RECORD', 1);
+    $this->_record->save();
     $this->assertEquals(1, $this->_record->numberOfVersions());
   }
 }
-- 
GitLab