From b1a707d788c4dd862ced86a4dc375a8eecc06cdd Mon Sep 17 00:00:00 2001
From: pbarroca <pbarroca@afi-sa.fr>
Date: Fri, 8 Dec 2017 17:12:20 +0100
Subject: [PATCH] dev #17352 : sandboxing

---
 FEATURES/17352                                | 10 ++
 VERSIONS_WIP/17352                            |  1 +
 .../php/classes/classe_notice_integration.php | 21 +++-
 .../Cosmogramme/Integration/PhaseNotice.php   |  8 +-
 .../Cosmogramme/Integration/RecordHistory.php | 99 +++++++++++++++++++
 5 files changed, 137 insertions(+), 2 deletions(-)
 create mode 100644 FEATURES/17352
 create mode 100644 VERSIONS_WIP/17352
 create mode 100644 library/Class/Cosmogramme/Integration/RecordHistory.php

diff --git a/FEATURES/17352 b/FEATURES/17352
new file mode 100644
index 00000000000..a21e13fa6bd
--- /dev/null
+++ b/FEATURES/17352
@@ -0,0 +1,10 @@
+        '17352' =>
+            ['Label' => $this->_('Cosmogramme: garder l'unimarc natif d'une notice'),
+             'Desc' => '',
+             'Image' => '',
+             'Video' => '',
+             'Category' => '',
+             'Right' => function($feature_description, $user) {return true;},
+             'Wiki' => '',
+             'Test' => '',
+             'Date' => '2017-12-08'],
\ No newline at end of file
diff --git a/VERSIONS_WIP/17352 b/VERSIONS_WIP/17352
new file mode 100644
index 00000000000..d42f28617b9
--- /dev/null
+++ b/VERSIONS_WIP/17352
@@ -0,0 +1 @@
+ - ticket #17352 : Cosmogramme: garder l'unimarc natif d'une notice
\ No newline at end of file
diff --git a/cosmogramme/php/classes/classe_notice_integration.php b/cosmogramme/php/classes/classe_notice_integration.php
index 632a62597bb..cf81c9cbaa6 100644
--- a/cosmogramme/php/classes/classe_notice_integration.php
+++ b/cosmogramme/php/classes/classe_notice_integration.php
@@ -75,6 +75,9 @@ class notice_integration {
 
   protected $_codif_provider;
 
+  protected $_record_history;
+
+
   public function __construct() {
     $this->indexation = indexation::getInstance();
     $this->filtrer_fulltext = Class_CosmoVar::get("filtrer_fulltext");
@@ -156,7 +159,7 @@ class notice_integration {
 
     unset($this->notice);
 
-    if(!$this->analyseur->ouvrirNotice($data,$this->id_profil,$this->sigb,$this->type_doc_force)) {
+    if(!$this->analyseur->ouvrirNotice($data, $this->id_profil, $this->sigb, $this->type_doc_force)) {
       $this->erreur=$this->analyseur->getLastError();
       return 0;
     }
@@ -485,6 +488,10 @@ class notice_integration {
 
     $notice = Class_Notice::newInstance($this->noticeToDBEnreg());
     $notice->save();
+
+    if ($this->_record_history)
+      $this->_record_history->setRecord($notice);
+
     $this->statut = static::RECORD_INSERT;
     return $notice->getId();
   }
@@ -625,6 +632,9 @@ class notice_integration {
     $this->notice["qualite"] = $qualite;
     $this->notice["id_origine"] = $id_origine;
 
+    if ($this->_record_history)
+      $this->_record_history->setRecord($notice);
+
     if ($this->statut == static::RECORD_UPDATE)
       return $id_notice;
 
@@ -739,6 +749,9 @@ class notice_integration {
     foreach($exemplaires as $exemplaire)
       $exemplaire->save();
 
+    if ($this->_record_history)
+      $this->_record_history->setItems($exemplaires);
+
     if($record = Class_Notice::find($id_notice))
       $record->setDateMaj(dateDuJour(2))->save();
 
@@ -1384,6 +1397,12 @@ class notice_integration {
     $this->_codif_provider = $provider;
     return $this;
   }
+
+
+  public function setRecordHistory($history) {
+    $this->_record_history = $history;
+    return $this;
+  }
 }
 
 
diff --git a/library/Class/Cosmogramme/Integration/PhaseNotice.php b/library/Class/Cosmogramme/Integration/PhaseNotice.php
index 98e0668c8e1..8d282aef1b3 100644
--- a/library/Class/Cosmogramme/Integration/PhaseNotice.php
+++ b/library/Class/Cosmogramme/Integration/PhaseNotice.php
@@ -47,7 +47,13 @@ class Class_Cosmogramme_Integration_PhaseNotice
                                       $integration->getProfil(),
                                       $integration->getTypeDoc());
     $integrator->setServiceRunner(static::$_service_runner);
+
+    $history = new Class_Cosmogramme_Integration_RecordHistory($data, $integration);
+    $integrator->setRecordHistory($history);
+
     $integrator->traiteNotice($data);
+    $history->save();
+
     $this->_incrementCount($integrator->getStatut());
   }
 
@@ -127,4 +133,4 @@ class Class_Cosmogramme_Integration_PhaseNotice
   public static function setServiceRunner($runner) {
     static::$_service_runner = $runner;
   }
-}
\ No newline at end of file
+}
diff --git a/library/Class/Cosmogramme/Integration/RecordHistory.php b/library/Class/Cosmogramme/Integration/RecordHistory.php
new file mode 100644
index 00000000000..197a989f953
--- /dev/null
+++ b/library/Class/Cosmogramme/Integration/RecordHistory.php
@@ -0,0 +1,99 @@
+<?php
+/**
+ * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+class Class_Cosmogramme_Integration_RecordHistory {
+  use Trait_StaticFileSystem, Trait_TimeSource;
+
+  const BASE_PATH = 'records';
+
+  protected
+    $_data,
+    $_integration,
+    $_folder_manager,
+    $_record_id,
+    $_item_ids = [];
+
+
+  public function __construct($data, $integration) {
+    $this->_data = $data;
+    $this->_integration = $integration;
+  }
+
+
+  public function setRecord($record) {
+    $this->_record_id = $record->getId();
+  }
+
+
+  public function setItems($items) {
+    $this->_item_ids = array_map(function($item) { return $item->getId(); },
+                                 $items);
+  }
+
+
+  public function save() {
+    if (!$this->_record_id)
+      return $this;
+
+    foreach($this->_item_ids as $item_id)
+      $this->_saveForItem($item_id);
+
+    return $this;
+  }
+
+
+  protected function _saveForItem($id) {
+    if (!$id)
+      return $this;
+
+    if (!$path = $this->_getPathOf($id))
+      return $this;
+
+    $this->getFileSystem()
+         ->file_put_contents($path, $this->_data);
+
+    return $this;
+  }
+
+
+  protected function _getPathOf($id) {
+    if (!$this->getFolderManager()
+        ->ensure($folder = $this->_getFolderPath()))
+      return;
+
+    return $folder . '/'. $id;
+  }
+
+
+  protected function _getFolderPath() {
+    return PATH_TEMP . implode('/', [static::BASE_PATH,
+                                     $this->_integration->getIdBib()]);
+  }
+
+
+  public function getFolderManager() {
+    if (null === $this->_folder_manager)
+      $this->_folder_manager = Class_Folder_Manager::newInstanceLimitedTo(PATH_TEMP);
+
+    return $this->_folder_manager;
+  }
+}
-- 
GitLab