From 176108ed145a3cd10b26e1bf514f0547eee19baa Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Wed, 31 Aug 2016 15:16:49 +0200
Subject: [PATCH] hotline #47256 keep connection in front

---
 VERSIONS_HOTLINE/47256                        |  1 +
 .../modules/admin/views/scripts/head.phtml    |  7 ++--
 .../modules/opac/views/scripts/head.phtml     |  1 +
 library/ZendAfi/View/Helper/Heartbeat.php     | 34 +++++++++++++++++++
 .../opac/controllers/IndexControllerTest.php  | 25 ++++++++++++++
 5 files changed, 63 insertions(+), 5 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/47256
 create mode 100644 library/ZendAfi/View/Helper/Heartbeat.php

diff --git a/VERSIONS_HOTLINE/47256 b/VERSIONS_HOTLINE/47256
new file mode 100644
index 00000000000..d70278c4fa5
--- /dev/null
+++ b/VERSIONS_HOTLINE/47256
@@ -0,0 +1 @@
+ - ticket #47256 : Ajout du heartbeat en front office lorsque l'on est connecté avec un compte ayant les droits d'accès au back office
\ No newline at end of file
diff --git a/application/modules/admin/views/scripts/head.phtml b/application/modules/admin/views/scripts/head.phtml
index 55a75604cbc..77c641132b3 100644
--- a/application/modules/admin/views/scripts/head.phtml
+++ b/application/modules/admin/views/scripts/head.phtml
@@ -33,10 +33,7 @@
 
     echo $this->headScript();
     echo $this->headLink();
+
+    echo $this->heartbeat();
     ?>
-    <script>
-     (function heartbeat() {
-       $.get('<?php echo BASE_URL ?>/admin/index/heartbeat', function() {setTimeout(heartbeat, 60000);});
-     })();
-    </script>
   </head>
diff --git a/application/modules/opac/views/scripts/head.phtml b/application/modules/opac/views/scripts/head.phtml
index ba9c7ded0b7..d4414778402 100644
--- a/application/modules/opac/views/scripts/head.phtml
+++ b/application/modules/opac/views/scripts/head.phtml
@@ -112,5 +112,6 @@
 
   $head_scripts->renderJavaScripts();
   $script_loader->renderJavaScripts();
+  echo $this->heartbeat();
 ?>
 </head>
diff --git a/library/ZendAfi/View/Helper/Heartbeat.php b/library/ZendAfi/View/Helper/Heartbeat.php
new file mode 100644
index 00000000000..0f32da62999
--- /dev/null
+++ b/library/ZendAfi/View/Helper/Heartbeat.php
@@ -0,0 +1,34 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, 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 ZendAfi_View_Helper_Heartbeat extends ZendAfi_View_Helper_BaseHelper {
+  public function heartbeat() {
+    return Class_Users::isCurrentUserCanAccesBackend()
+      ? $this->_tag('script',
+                    sprintf('(function heartbeat() {$.get("%s", function() {setTimeout(heartbeat, 60000);}); })();',
+                            $this->view->absoluteUrl(['module' => 'admin',
+                                                      'controller' => 'index',
+                                                      'action' => 'heartbeat'], null, true)))
+      : '';
+  }
+}
+?>
\ No newline at end of file
diff --git a/tests/application/modules/opac/controllers/IndexControllerTest.php b/tests/application/modules/opac/controllers/IndexControllerTest.php
index 29429ca5597..bf16032b97c 100644
--- a/tests/application/modules/opac/controllers/IndexControllerTest.php
+++ b/tests/application/modules/opac/controllers/IndexControllerTest.php
@@ -465,3 +465,28 @@ class IndexControllerInspectorGadgetTest extends AbstractControllerTestCase {
     $this->assertEquals([], $calls);
   }
 }
+
+
+
+class IndexControllerHeartBeatTest extends Admin_AbstractControllerTestCase {
+
+  /** @test */
+  public function indexAsAdminShouldContainsHeartbeatScript() {
+    $this->dispatch('/opac/index/index', true);
+    $this->assertXPathContentContains('//script', '/admin/index/heartbeat');
+  }
+
+
+  /** @test */
+  public function indexAdAbonneShouldNotContainsHeartbeatScript() {
+    ZendAfi_Auth::getInstance()->logUser($this->fixture('Class_Users',
+                                                        ['id' => 2,
+                                                         'login' => 'abonne',
+                                                         'password' => 'year',
+                                                         'id_site' => 1,
+                                                         'idabon' => 789465,
+                                                         'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB]));
+    $this->dispatch('/opac/index/index', true);
+    $this->assertNotXPathContentContains('//script', '/admin/index/heartbeat');
+  }
+}
-- 
GitLab