From d38925002100cb31a4a5b89c2c17f4292e826969 Mon Sep 17 00:00:00 2001
From: Laurent <llaffont@afi-sa.fr>
Date: Thu, 2 Oct 2014 17:45:13 +0200
Subject: [PATCH] Cache annexes in cosmogramme

---
 .../php/classes/classe_codif_cache.php        | 53 +++++++++++++++++++
 cosmogramme/php/classes/classe_unimarc.php    |  6 ++-
 library/Class/CodifAnnexe.php                 |  7 ++-
 3 files changed, 63 insertions(+), 3 deletions(-)
 create mode 100644 cosmogramme/php/classes/classe_codif_cache.php

diff --git a/cosmogramme/php/classes/classe_codif_cache.php b/cosmogramme/php/classes/classe_codif_cache.php
new file mode 100644
index 00000000000..cbb2d00c608
--- /dev/null
+++ b/cosmogramme/php/classes/classe_codif_cache.php
@@ -0,0 +1,53 @@
+<?php
+/**
+ * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * AFI-OPAC 2.0 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).
+ *
+ * AFI-OPAC 2.0 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 AFI-OPAC 2.0; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+abstract class CodifCacheAbstract {
+	static $_instance;
+
+	protected $_cache = [];
+
+	public static function getInstance() {
+		if (!isset(static::$_instance))
+			static::$_instance = new self();
+		return static::$_instance;
+	}
+
+
+	public function find($code) {
+		if (!isset($this->_cache[$code]))
+			$this->_cache[$code] = $this->load($code);
+		return $this->_cache[$code];
+	}
+
+
+	abstract public function load($code);
+}
+
+
+
+class CodifAnnexeCache extends CodifCacheAbstract {
+	public function load($code) {
+		return Class_CodifAnnexe::findFirstBy(['code' => $code]);
+	}
+}
+
+?>
\ No newline at end of file
diff --git a/cosmogramme/php/classes/classe_unimarc.php b/cosmogramme/php/classes/classe_unimarc.php
index 7b461171a94..aebfb9d6163 100644
--- a/cosmogramme/php/classes/classe_unimarc.php
+++ b/cosmogramme/php/classes/classe_unimarc.php
@@ -25,6 +25,8 @@ require_once 'classe_dewey.php';
 require_once 'classe_pcdm4.php';
 require_once 'classe_indexation.php';
 require_once 'classe_isbn.php';
+require_once 'classe_codif_cache.php';
+
 
 class notice_unimarc extends iso2709_record {
 	private $id_profil;									// Id du profil de données pour le fichier chargé
@@ -445,8 +447,8 @@ class notice_unimarc extends iso2709_record {
 
 
 	protected function isAnnexeInvisible($id) {
-		return ($enreg = fetchEnreg('select * from codif_annexe where code=\'' . $id . '\''))
-						 && $enreg['invisible'] == 1;
+		$annexe = CodifAnnexeCache::getInstance()->find($id);
+		return $annexe && !$annexe->isVisible();
 	}
 
 
diff --git a/library/Class/CodifAnnexe.php b/library/Class/CodifAnnexe.php
index 3a2ea77e584..5a58684e3df 100644
--- a/library/Class/CodifAnnexe.php
+++ b/library/Class/CodifAnnexe.php
@@ -16,7 +16,7 @@
  *
  * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
  * along with AFI-OPAC 2.0; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
 class Class_CodifAnnexe extends Storm_Model_Abstract {
@@ -38,6 +38,11 @@ class Class_CodifAnnexe extends Storm_Model_Abstract {
 	public function getMailIntBib() {
 		return $this->getIntBib()->getMail();
 	}
+
+
+	public function isVisible() {
+		return (1 != $this->getInvisible());
+	}
 }
 
 ?>
-- 
GitLab