From 4fb4a59e9b07d4df2b04b063c0ee434353944d67 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Tue, 20 Mar 2018 13:34:37 -0400
Subject: [PATCH] Turn off no-JS hierarchy support by default. (#1145)

---
 config/vufind/config.ini                      |  6 ++
 .../src/VuFind/View/Helper/Root/Config.php    | 10 +++
 .../templates/Recommend/SideFacets.phtml      | 58 ++++++++++-----
 .../templates/RecordTab/hierarchytree.phtml   | 10 ++-
 themes/bootstrap3/templates/search/home.phtml | 71 ++++++++++---------
 5 files changed, 102 insertions(+), 53 deletions(-)

diff --git a/config/vufind/config.ini b/config/vufind/config.ini
index d40b7d62aa6..edb7b7ce5ff 100644
--- a/config/vufind/config.ini
+++ b/config/vufind/config.ini
@@ -134,6 +134,12 @@ bookbagTogglesInSearch = true
 showBulkOptions = false
 ; Should users be allowed to save searches in their accounts?
 allowSavedSearches = true
+; Some VuFind features can be made compatible with non-Javascript browsers at
+; a performance cost. By default, this compatibility is disabled, but it can
+; be turned on here. Note that even with this setting turned on, some features
+; still require Javascript; this simply improves compatibility for certain
+; features (such as display of hierarchies).
+nonJavascriptSupportEnabled = false
 ; Generator value to display in an HTML header <meta> tag:
 generator = "VuFind 4.1.2"
 
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Config.php b/module/VuFind/src/VuFind/View/Helper/Root/Config.php
index be59ab30500..22f66e53546 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Config.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Config.php
@@ -68,4 +68,14 @@ class Config extends \Zend\View\Helper\AbstractHelper
     {
         return $this->configLoader->get($config);
     }
+
+    /**
+     * Is non-Javascript support enabled?
+     *
+     * @return bool
+     */
+    public function nonJavascriptSupportEnabled()
+    {
+        return $this->get('config')->Site->nonJavascriptSupportEnabled ?? false;
+    }
 }
diff --git a/themes/bootstrap3/templates/Recommend/SideFacets.phtml b/themes/bootstrap3/templates/Recommend/SideFacets.phtml
index b76c3107c1c..1813fa75f8e 100644
--- a/themes/bootstrap3/templates/Recommend/SideFacets.phtml
+++ b/themes/bootstrap3/templates/Recommend/SideFacets.phtml
@@ -45,27 +45,49 @@
         <?=$this->transEsc($cluster['label'])?>
       </button>
       <div id="side-collapse-<?=$this->escapeHtmlAttr($title) ?>" class="collapse<?php if(!in_array($title, $collapsedFacets)): ?> in<?php endif ?>">
-        <?php if (isset($rangeFacets[$title])): ?>
-          <?=$this->context($this)->renderInContext('Recommend/SideFacets/range-slider.phtml', ['title' => $title, 'facet' => $rangeFacets[$title]]); ?>
+        <?php if (isset($rangeFacets[$title])): // special display for ranges ?>
+          <?=$this->context($this)->renderInContext(
+            'Recommend/SideFacets/range-slider.phtml',
+            ['title' => $title, 'facet' => $rangeFacets[$title]]
+          ); ?>
         <?php else: ?>
-          <?php $contextVars = [
-            'allowExclude' => $this->recommend->excludeAllowed($title),
-            'title' => $title,
-            'sortOptions' => $hierarchicalFacetSortOptions[$title] ?? '',
-            'collapsedFacets' => $collapsedFacets
-          ]; ?>
-          <?php if (in_array($title, $hierarchicalFacets)): ?>
-            <?=$this->context($this)->renderInContext('Recommend/SideFacets/hierarchical-facet.phtml', $contextVars); ?>
+          <?php
+            // Set some variables so we can figure out which sub-template(s) to
+            // render. If we have a hierarchical facet, we will need to render a
+            // jsTree Javascript-based template. If we also support non-JS browsers,
+            // we will also need to render the basic default template. If we
+            // do NOT support non-JS browsers, we should instead display an error
+            // message.
+            $isHierarchical = in_array($title, $hierarchicalFacets);
+            $noJsSupport = $this->config()->nonJavascriptSupportEnabled();
+          ?>
+          <?php if ($isHierarchical): // do we need hierarchical JS display? ?>
+            <?=$this->context($this)->renderInContext(
+              'Recommend/SideFacets/hierarchical-facet.phtml',
+              [
+                'allowExclude' => $this->recommend->excludeAllowed($title),
+                'title' => $title,
+                'sortOptions' => $hierarchicalFacetSortOptions[$title] ?? '',
+                'collapsedFacets' => $collapsedFacets
+              ]
+            ); ?>
             <noscript>
+            <?php if (!$noJsSupport): ?>
+              <span class="facet"><?=$this->transEsc('Please enable JavaScript.')?></span>
+            <?php endif; ?>
           <?php endif; ?>
-          <?php $contextVars = [
-            'options' => $options,
-            'allowExclude' => $this->recommend->excludeAllowed($title),
-            'facets_before_more' => $this->recommend->getShowMoreSetting($title),
-            'showMoreInLightbox' => $this->recommend->getShowInLightboxSetting($title)
-          ]; ?>
-          <?=$this->context($this)->renderInContext('Recommend/SideFacets/cluster-list.phtml', array_merge($contextVars, ['title' => $title, 'cluster' => $cluster])); ?>
-          <?php if (in_array($title, $hierarchicalFacets)): ?>
+          <?php if (!$isHierarchical || $noJsSupport): // do we need regular display? ?>
+            <?=$this->context($this)->renderInContext(
+              'Recommend/SideFacets/cluster-list.phtml',
+              [
+                'options' => $options, 'title' => $title, 'cluster' => $cluster,
+                'allowExclude' => $this->recommend->excludeAllowed($title),
+                'facets_before_more' => $this->recommend->getShowMoreSetting($title),
+                'showMoreInLightbox' => $this->recommend->getShowInLightboxSetting($title)
+              ]
+            ); ?>
+          <?php endif; ?>
+          <?php if ($isHierarchical): // close tag opened in matching if above ?>
             </noscript>
           <?php endif; ?>
         <?php endif; ?>
diff --git a/themes/bootstrap3/templates/RecordTab/hierarchytree.phtml b/themes/bootstrap3/templates/RecordTab/hierarchytree.phtml
index 0ca8b8f7049..630d4be321e 100644
--- a/themes/bootstrap3/templates/RecordTab/hierarchytree.phtml
+++ b/themes/bootstrap3/templates/RecordTab/hierarchytree.phtml
@@ -53,9 +53,13 @@
       <input type="hidden" value="<?=isset($this->treeContext) ? $this->treeContext : 'Record'?>" class="hiddenContext" />
       <?php if ($this->layout()->getTemplate() != 'layout/lightbox'): ?>
         <noscript>
-          <ul class="fa-ul">
-            <?=$this->tab->renderTree($this->url('home'))?>
-          </ul>
+          <?php if ($this->config()->nonJavascriptSupportEnabled()): ?>
+            <ul class="fa-ul">
+              <?=$this->tab->renderTree($this->url('home'))?>
+            </ul>
+          <?php else: ?>
+            <?=$this->transEsc('Please enable JavaScript.')?>
+          <?php endif; ?>
         </noscript>
       <?php endif; ?>
     </div>
diff --git a/themes/bootstrap3/templates/search/home.phtml b/themes/bootstrap3/templates/search/home.phtml
index ba6f5263677..c9fde55bb56 100644
--- a/themes/bootstrap3/templates/search/home.phtml
+++ b/themes/bootstrap3/templates/search/home.phtml
@@ -15,6 +15,7 @@
   $basicSearch = $options->getSearchAction();
   $advSearch = $options->getAdvancedSearchAction();
   $columnSize = 10;
+  $noJsSupport = $this->config()->nonJavascriptSupportEnabled();
 
   $this->layout()->breadcrumbs = false;
 ?>
@@ -68,43 +69,49 @@ JS;
           </div>
         </div>
         <noscript>
+          <?php if (!$noJsSupport): ?>
+            <h2><?=$this->transEsc('home_browse') . ' ' . $this->transEsc($details['label'])?></h2>
+            <?=$this->transEsc('Please enable JavaScript.')?>
+          <?php endif; ?>
       <?php endif; ?>
-      <?php $sortedList = $this->sortFacetList($this->results, $field, $details['list'], $basicSearch); ?>
-      <div class="home-facet <?=$this->escapeHtmlAttr($field) ?>">
-        <h2><?=$this->transEsc('home_browse') . ' ' . $this->transEsc($details['label'])?></h2>
-        <div class="home-facet-container">
-          <ul class="home-facet-list">
-            <?php
-              // Special case: two columns for LC call numbers...
-              $maxListLength = $field == 'callnumber-first'
-                ? $columnSize * 2 : $columnSize;
-
-              // Special case: custom URLs for collections...
-              $moreUrl = $field == 'hierarchy_top_title'
-                ? $this->url('collections-home') : $this->url($advSearch);
+      <?php if (!$isHierarchy || $noJsSupport): // do we need regular display? ?>
+        <?php $sortedList = $this->sortFacetList($this->results, $field, $details['list'], $basicSearch); ?>
+        <div class="home-facet <?=$this->escapeHtmlAttr($field) ?>">
+          <h2><?=$this->transEsc('home_browse') . ' ' . $this->transEsc($details['label'])?></h2>
+          <div class="home-facet-container">
+            <ul class="home-facet-list">
+              <?php
+                // Special case: two columns for LC call numbers...
+                $maxListLength = $field == 'callnumber-first'
+                  ? $columnSize * 2 : $columnSize;
 
-              // Convenience variable:
-              $currentListLength = count($sortedList);
-            ?>
-            <?php $i = 0; foreach ($sortedList as $url => $value):
                 // Special case: custom URLs for collections...
-                if ($field == 'hierarchy_top_title') {
-                    $url = $this->url('collections-bytitle') . '?title=' . urlencode($value);
-                }
+                $moreUrl = $field == 'hierarchy_top_title'
+                  ? $this->url('collections-home') : $this->url($advSearch);
+
+                // Convenience variable:
+                $currentListLength = count($sortedList);
               ?>
-              <li><a href="<?=$url?>"><?=$this->escapeHtml(empty($value) ? '-' : $value)?></a></li>
-              <?php if (++$i >= $currentListLength) break; // end of list? bail out! ?>
-              <?php if ($i >= $maxListLength): // list too long? show more link! ?>
-                <li><a href="<?=$moreUrl?>"><strong><?=$this->transEsc("More options")?>...</strong></a></li>
-                <?php break; ?>
-              <?php elseif ($i % $columnSize === 0): // end of column? insert break! ?>
-                </ul><ul class="home-facet-list">
-              <?php endif; ?>
-            <?php endforeach; ?>
-          </ul>
+              <?php $i = 0; foreach ($sortedList as $url => $value):
+                  // Special case: custom URLs for collections...
+                  if ($field == 'hierarchy_top_title') {
+                      $url = $this->url('collections-bytitle') . '?title=' . urlencode($value);
+                  }
+                ?>
+                <li><a href="<?=$url?>"><?=$this->escapeHtml(empty($value) ? '-' : $value)?></a></li>
+                <?php if (++$i >= $currentListLength) break; // end of list? bail out! ?>
+                <?php if ($i >= $maxListLength): // list too long? show more link! ?>
+                  <li><a href="<?=$moreUrl?>"><strong><?=$this->transEsc("More options")?>...</strong></a></li>
+                  <?php break; ?>
+                <?php elseif ($i % $columnSize === 0): // end of column? insert break! ?>
+                  </ul><ul class="home-facet-list">
+                <?php endif; ?>
+              <?php endforeach; ?>
+            </ul>
+          </div>
         </div>
-      </div>
-      <?php if ($isHierarchy): ?>
+      <?php endif; ?>
+      <?php if ($isHierarchy): // close tag opened in matching if above ?>
         </noscript>
       <?php endif; ?>
     <?php endforeach; ?>
-- 
GitLab