From 63aa65e3aa4cff8665fb4a5c369a02791f53510b Mon Sep 17 00:00:00 2001
From: efalcy <efalcy@afi-sa.fr>
Date: Thu, 1 Jun 2023 10:10:10 +0000
Subject: [PATCH] dev#178946: sort Cafeyn by novelty date

---
 FEATURES/178946                               | 10 ++
 VERSIONS_WIP/178946                           |  1 +
 library/Class/Album.php                       | 12 +++
 library/Class/Indexation/PseudoNotice.php     |  3 +-
 .../WebService/BibNumerique/Abstract.php      | 11 ++-
 .../BibNumerique/RessourceNumerique.php       | 21 +++-
 library/Trait/TimeSource.php                  |  3 +-
 library/digital_resources/ArteVod/Service.php | 19 +---
 library/digital_resources/Cafeyn/Config.php   |  1 +
 library/digital_resources/Cafeyn/Service.php  |  5 +
 .../Cafeyn/Service/Album.php                  |  2 +
 .../Cafeyn/tests/CafeynTest.php               | 95 ++++++++++++++++++-
 12 files changed, 160 insertions(+), 23 deletions(-)
 create mode 100644 FEATURES/178946
 create mode 100644 VERSIONS_WIP/178946

diff --git a/FEATURES/178946 b/FEATURES/178946
new file mode 100644
index 00000000000..d19d73c21db
--- /dev/null
+++ b/FEATURES/178946
@@ -0,0 +1,10 @@
+        '178946' =>
+            ['Label' => $this->_('Cafeyn : résultat de recherche : tri par date de nouveauté possible'),
+             'Desc' =>  $this->_('Il est possible de trier les ressources numériques Cafeyn par date : la date de sortie du magazine corresponds à la date de nouveauté'),
+             'Image' => '',
+             'Video' => '',
+             'Category' => 'Ressources Numériques',
+             'Right' => function($feature_description, $user) {return true;},
+             'Wiki' => '',
+             'Test' => '',
+             'Date' => '2023-05-24'],
\ No newline at end of file
diff --git a/VERSIONS_WIP/178946 b/VERSIONS_WIP/178946
new file mode 100644
index 00000000000..095be696770
--- /dev/null
+++ b/VERSIONS_WIP/178946
@@ -0,0 +1 @@
+ - fonctionnalité #178946 : Ressource Numérique Cafeyn : Résultat de recherche : possibilité de tri par date de nouveauté.
\ No newline at end of file
diff --git a/library/Class/Album.php b/library/Class/Album.php
index 2ae4cd97733..68ed5169c91 100644
--- a/library/Class/Album.php
+++ b/library/Class/Album.php
@@ -69,6 +69,7 @@ class Class_Album extends Storm_Model_Abstract {
   const VIDEO_URL_TYPE = 'video';
   const DURATION_FIELD = '215$a';
   const MISC_FIELD = '215$c';
+  const RELEASE_DATE_FIELD = '210$d';
   const FORMAT_FIELD = '215$d';
   const URI_TYPE_POSTER = 'poster';
   const URI_TYPE_TRAILER = 'trailer';
@@ -1208,6 +1209,17 @@ class Class_Album extends Storm_Model_Abstract {
   }
 
 
+  public function setReleaseDate(string $datetime) : self {
+    $this->addNote(self::RELEASE_DATE_FIELD,  $datetime);
+    return $this;
+  }
+
+
+  public function getReleaseDate() : string {
+    return $this->getNote(self::RELEASE_DATE_FIELD) ?? '';
+  }
+
+
   public function getMisc() {
     return $this->getNote(self::MISC_FIELD);
   }
diff --git a/library/Class/Indexation/PseudoNotice.php b/library/Class/Indexation/PseudoNotice.php
index 24282ab17e6..4163e1eae44 100644
--- a/library/Class/Indexation/PseudoNotice.php
+++ b/library/Class/Indexation/PseudoNotice.php
@@ -492,7 +492,8 @@ class Class_Indexation_PseudoNotice_Album extends Class_Indexation_PseudoNotice{
     $url_vignette = $this->_model->getThumbnailUrl();
     $this->_notice->setUrlVignette($url_vignette);
     $this->_notice->setUrlImage($url_vignette);
-
+    if ($release_date = $this->_model->getReleaseDate())
+      $this->_notice->setDateCreation($this->tryFormatDate($release_date, 'Y-m-d H:i:s'));
     parent::_index();
   }
 
diff --git a/library/Class/WebService/BibNumerique/Abstract.php b/library/Class/WebService/BibNumerique/Abstract.php
index 1d3a90f3a15..11908887e85 100644
--- a/library/Class/WebService/BibNumerique/Abstract.php
+++ b/library/Class/WebService/BibNumerique/Abstract.php
@@ -89,16 +89,21 @@ abstract class Class_WebService_BibNumerique_Abstract extends Class_WebService_A
     $harvestedIds = [];
     foreach ($ressources as $ressource) {
       $harvestedIds[] = $ressource->getId();
-      if ($ressource->isAlreadyHarvested())
+      if ($this->_skipAlreadyHarvested() && $ressource->isAlreadyHarvested())
         continue;
 
       $this->loadRessource($ressource);
-      $ressource->import();
+      $ressource->import(!$this->_skipAlreadyHarvested());
     }
     return $harvestedIds;
   }
 
 
+  protected function _skipAlreadyHarvested() : bool{
+    return true;
+  }
+
+
   public function loadRessource($film) {}
 
 
@@ -164,4 +169,4 @@ abstract class Class_WebService_BibNumerique_Abstract extends Class_WebService_A
   public function getName() {
     return '';
   }
-}
\ No newline at end of file
+}
diff --git a/library/Class/WebService/BibNumerique/RessourceNumerique.php b/library/Class/WebService/BibNumerique/RessourceNumerique.php
index 8d57ad0a19c..c5648987b51 100644
--- a/library/Class/WebService/BibNumerique/RessourceNumerique.php
+++ b/library/Class/WebService/BibNumerique/RessourceNumerique.php
@@ -50,8 +50,8 @@ class Class_WebService_BibNumerique_RessourceNumerique {
     $_zones = [],
     $_bibliotheques='',
     $_authors = [],
-    $_is_deleted = false;
-
+    $_is_deleted = false,
+    $_release_date = '';
 
   static public function getLogger() {
     if (!static::$_logger)
@@ -271,11 +271,24 @@ class Class_WebService_BibNumerique_RessourceNumerique {
     return $this->_zones[$codif];
   }
 
+
   public function setBibliotheques($bibs) {
     $this->_bibliotheques=$bibs;
     return $this;
   }
 
+
+  public function getReleaseDate() : string{
+    return $this->_release_date;
+  }
+
+
+  public function setReleaseDate(string $datetime) : self {
+    $this->_release_date = $datetime;
+    return $this;
+  }
+
+
   public function getBibliotheques() {
     return $this->_bibliotheques;
   }
@@ -396,6 +409,9 @@ class Class_WebService_BibNumerique_RessourceNumerique {
     if($duration = $this->getDuration())
       $album->setDuration($duration);
 
+    if ($release_date = $this->getReleaseDate())
+      $album->setReleaseDate($release_date);
+
     if($tags = $this->getTags())
       $album->setTags($tags);
 
@@ -519,6 +535,7 @@ class Class_WebService_BibNumerique_RessourceNumerique {
     return $this->_ressources;
   }
 
+
   public function isDeleted() {
     return $this->_is_deleted;
   }
diff --git a/library/Trait/TimeSource.php b/library/Trait/TimeSource.php
index 347c694afad..a675547667f 100644
--- a/library/Trait/TimeSource.php
+++ b/library/Trait/TimeSource.php
@@ -79,7 +79,8 @@ trait Trait_TimeSource {
 
   public static function tryFormatDate( string $date_str, string $return_format= 'Y-m-d'): string {
     $date_str = str_replace( '.0Z', '', $date_str);
-    $formats=['Y','Y-m-d', 'Y/m/d', 'Ymd', 'YmdHis', 'd-m-Y', 'd/m/Y'];
+
+    $formats=['Y','Y-m-d', 'Y/m/d', 'Ymd', 'YmdHis','Y-m-d*H:i:s', 'd-m-Y', 'd/m/Y'];
 
     foreach ($formats as $format) {
       if ($date = DateTime::createFromFormat($format, $date_str))
diff --git a/library/digital_resources/ArteVod/Service.php b/library/digital_resources/ArteVod/Service.php
index e9f1e8703cd..17800deb13e 100644
--- a/library/digital_resources/ArteVod/Service.php
+++ b/library/digital_resources/ArteVod/Service.php
@@ -68,22 +68,13 @@ class ArteVod_Service extends Class_DigitalResource_Service {
   }
 
 
-  protected function _importRessources($ressources) {
-    if(!$ressources)
-      return [];
-    $harvestedIds = [];
-
-
-    foreach ($ressources as $ressource) {
-      $harvestedIds[] = $ressource->getId();
-      if ($ressource->isAlreadyHarvested() && !$this->_config->getAdminVar( 'BATCH_UPDATE_NOTICE'))
-        continue;
+  protected function _skipAlreadyHarvested() : bool{
+    return !$this->_config->getAdminVar( 'BATCH_UPDATE_NOTICE');
+  }
 
-      $this->loadRessource($ressource);
-      $update=true;
 
-      $ressource->import($update);
-    }
+  protected function _importRessources($ressources) {
+    $harvestedIds=parent::_importRessources($ressources);
     Class_AdminVar::set('ArteVod_BATCH_UPDATE_NOTICE',0);
     return $harvestedIds;
   }
diff --git a/library/digital_resources/Cafeyn/Config.php b/library/digital_resources/Cafeyn/Config.php
index 1cb7fe0b138..b612abc3063 100644
--- a/library/digital_resources/Cafeyn/Config.php
+++ b/library/digital_resources/Cafeyn/Config.php
@@ -47,6 +47,7 @@ class Cafeyn_Config extends Class_DigitalResource_Config {
                             'API_ID' => Class_AdminVar_Meta::newDefault($this->_('Identifiant API fourni par Cafeyn'))->bePrivate(),
                             'API_PASSWORD' => Class_AdminVar_Meta::newDefault($this->_('Mot de passe API fourni par Cafeyn'))->bePrivate(),
                             'HARVEST_URL' => Class_AdminVar_Meta::newDefault($this->_('URL de moissonage de la ressource Cafeyn'))->bePrivate(),
+                            'BATCH_UPDATE_NOTICE' => Class_AdminVar_Meta::newOnOff($this->_('Activer la mise à jour systématique des notices lors du moissonnage'),['value' => 1])->bePrivate(),
                             'SSO_URL' => Class_AdminVar_Meta::newDefault($this->_('URL SSO de la plateforme Cafeyn'),
                                                                          ['value' => 'https://www.cafeyn.co/fr/externallogin'])->bePrivate(),
             ]
diff --git a/library/digital_resources/Cafeyn/Service.php b/library/digital_resources/Cafeyn/Service.php
index b24cd4dcec2..fe8ebac1111 100644
--- a/library/digital_resources/Cafeyn/Service.php
+++ b/library/digital_resources/Cafeyn/Service.php
@@ -73,6 +73,11 @@ class Cafeyn_Service extends Class_DigitalResource_Service {
   }
 
 
+  protected function _skipAlreadyHarvested() : bool{
+    return !$this->_config->getAdminVar( 'BATCH_UPDATE_NOTICE');
+  }
+
+
   public function getCatalogUrl($page_number) {
     return $this->_config->getHarvestUrl($page_number);
   }
diff --git a/library/digital_resources/Cafeyn/Service/Album.php b/library/digital_resources/Cafeyn/Service/Album.php
index 06297bcd672..711414efd7f 100644
--- a/library/digital_resources/Cafeyn/Service/Album.php
+++ b/library/digital_resources/Cafeyn/Service/Album.php
@@ -25,6 +25,8 @@ class Cafeyn_Service_Album extends Class_WebService_BibNumerique_RessourceNumeri
   public function __construct($json) {
 
     $year = explode('-', $json->releaseDate ?? '')[0];
+    if (isset($json->releaseDate) && ($date = $json->releaseDate))
+      $this->setReleaseDate($date);
 
     $this->setId(sprintf( '%s:%s',
                           $json->publicationId ?? '',
diff --git a/library/digital_resources/Cafeyn/tests/CafeynTest.php b/library/digital_resources/Cafeyn/tests/CafeynTest.php
index 4f2b4943c89..b6046fdad21 100644
--- a/library/digital_resources/Cafeyn/tests/CafeynTest.php
+++ b/library/digital_resources/Cafeyn/tests/CafeynTest.php
@@ -177,9 +177,16 @@ abstract class CafeynServiceTestCase extends AbstractControllerTestCase {
     $_album,
     $_service;
 
+  public function addFixtures() {
+    Class_AdminVar::set('Cafeyn_BATCH_UPDATE_NOTICE', '1');
+    return $this;
+  }
+
+
   public function setUp() {
     parent::setUp();
 
+    $this->addFixtures();
     CafeynAdminVars::activate();
 
     $json = file_get_contents(__DIR__. '/cafeyn.json');
@@ -200,6 +207,7 @@ abstract class CafeynServiceTestCase extends AbstractControllerTestCase {
 
     $this->_service = new Cafeyn_Service($config);
     Cafeyn_Service::setDefaultHttpClient($http_client);
+
     $this->_service->harvest();
     Class_Album::clearCache();
     $this->_album = Class_Album::find(1);
@@ -212,7 +220,7 @@ abstract class CafeynServiceTestCase extends AbstractControllerTestCase {
 class CafeynHarvest extends CafeynServiceTestCase {
   public function getAlbumAttribs() {
     return [
-            ['getTitre','Le quotidien du sport n°7 :  3 avril 2023'],
+            ['getTitre', 'Le quotidien du sport n°7 :  3 avril 2023'],
             ['getIdOrigine', '21896447:7'],
             ['getCategoryLabel', 'Cafeyn'],
             ['getUrlOrigine', 'https://cafeyn.co/'],
@@ -225,7 +233,8 @@ class CafeynHarvest extends CafeynServiceTestCase {
             ['getDescription',
              '« Le quotidien du sport » vous permettra de découvrir ou de retrouver tous les héros et les champions du sport.'
             ],
-            ['getDateMaj', '2017-02-06 10:00:00']
+            ['getDateMaj', '2017-02-06 10:00:00'],
+            ['getReleaseDate', '2023-04-03T00:00:00']
     ];
   }
 
@@ -299,6 +308,88 @@ class CafeynDashboardActivatedTest extends CafeynActivatedTestCase {
     $this->assertXPathContentContains('//h4', 'URL de moissonnage');
     $this->assertXPathContentContains('//pre', 'https://harvest-cafeyn/partner/publication/66/flow', $this->_response->getBody());
   }
+}
+
+
+
+
+class CafeynHarvestDisableUpdateTest extends CafeynServiceTestCase {
+  public function setUp() {
+    parent::setUp();
+    Class_Album::find(20)->index();
+  }
+
+
+  public function addFixtures() {
+    Class_AdminVar::set('Cafeyn_BATCH_UPDATE_NOTICE', '0');
+    $album = $this->fixture('Class_Album',
+                            ['id' => 20,
+                             'visible' => 1,
+                             'status' => 3,
+                             'titre' => '10 moutons',
+                             'id_origine'  => '21896447:7',
+                             'url_origine' => 'https://cafeyn.co/',
+                             'type_doc_id' => 'Cafeyn'
+                            ]);
+    return $this;
+  }
+
+
+  /** @test */
+  public function titleShouldNotBeUpdated() {
+    $this->assertEquals( '10 moutons',
+                         Class_Album::find(20)->getTitre());
+  }
+
+
+  /** @test */
+  public function noticeShouldNotHavePublicationDate() {
+    $this->assertEquals( '',
+                         Class_Album::find(20)->getNotice()->getDateCreation());
+  }
+}
+
+
+
+
+class CafeynHarvestUpdateTest extends CafeynServiceTestCase {
+  public function setUp() {
+    parent::setUp();
+    Class_Album::find(20)->index();
+  }
 
 
+  public function addFixtures() {
+    $album = $this->fixture('Class_Album',
+                            ['id' => 20,
+                             'titre' => '10 moutons',
+                             'visible' => 1,
+                             'status' => 3,
+                             'id_origine'  => '21896447:7',
+                             'url_origine' => 'https://cafeyn.co/',
+                             'type_doc_id' => 'Cafeyn'
+                            ]);
+    return $this;
+  }
+
+
+  /** @test */
+  public function titleShouldBeUpdated() {
+    $this->assertEquals( 'Le quotidien du sport n°7 :  3 avril 2023',
+                         Class_Album::find(20)->getTitre());
+  }
+
+
+  /** @test */
+  public function publicationDateShouldBeUpdated() {
+    $this->assertEquals( '2023-04-03T00:00:00',
+                         Class_Album::find(20)->getReleaseDate());
+  }
+
+
+  /** @test */
+  public function noticeShouldHavePublicationDate() {
+    $this->assertEquals( '2023-04-03 00:00:00',
+                         Class_Album::find(20)->getNotice()->getDateCreation());
+  }
 }
-- 
GitLab