From f26a07d99e0d3a5cb4869c72b0b1a8f5a902de4f Mon Sep 17 00:00:00 2001
From: "Bradley M. Froehle" <brad.froehle@gmail.com>
Date: Thu, 5 Jan 2012 11:11:42 -0800
Subject: [PATCH] Issue #1394666 by bfroehle: Better handling for determining
 CAS library path.

---
 cas.admin.inc |   73 +++++++++++++++++++++++++++++++++++++-------------------
 cas.install   |    1 +
 cas.module    |    2 +-
 3 files changed, 50 insertions(+), 26 deletions(-)

diff --git a/cas.admin.inc b/cas.admin.inc
index 4c209a2..2cbeaf6 100644
--- a/cas.admin.inc
+++ b/cas.admin.inc
@@ -9,33 +9,50 @@
  * Provides settings pages.
  */
 function cas_admin_settings() {
+  $phpcas_url = 'https://wiki.jasig.org/display/CASC/phpCAS';
 
   $form['library'] = array(
     '#type' => 'fieldset',
     '#title' => t('Library (phpCAS)'),
     '#collapsible' => TRUE,
   );
-  if (module_exists('libraries')) {
-    // If Libraries API is enabled, print an information item.
-    $form['library']['cas_library_dir'] = array(
-      '#type' => 'item',
-      '#title' => t('Library directory'),
-      '#value' => t('Using <a href="@url">Libraries API</a>.', array('@url' => 'http://drupal.org/project/libraries')),
-      '#description' => t('Please ensure phpCAS is installed in a location compatible with Libraries API. For example, install phpCAS so that <em>sites/all/libraries/CAS/CAS.php</em> exists. See README.txt for more information.'),
-      '#after_build' => array('cas_library_version_check'),
-    );
-  }
-  else {
-    // If Libraries API is not installed, display path settings.
-    $form['library']['cas_library_dir'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Library directory'),
-      '#default_value' => variable_get('cas_library_dir', 'CAS'),
-      '#description' => t('Specify the path to the directory the CAS.php file resides in. Leave blank to load cas from your phpinclude path.'),
-      '#after_build' => array('cas_library_version_check'),
-    );
-  }
 
+  $form['library']['cas_library_use_libraries'] = array(
+    '#type' => 'radios',
+    '#title' => t('phpCAS Library'),
+    '#default_value' => module_exists('libraries') ? variable_get('cas_library_use_libraries', 1) : 0,
+    '#disabled' => !module_exists('libraries'),
+    '#options' => array(
+      1 => t('Locate using Libraries API.'),
+      0 => t('Provide an absolute or relative path.'),
+    ),
+  );
+
+  $form['library']['cas_library_libraries'] = array(
+    '#type' => 'item',
+    '#name' => 'cas_library_libraries', // Necessary for cas_library_version_check.
+    '#title' => T('phpCAS Library Path'),
+    '#description' => t('Please ensure <a href="@url">phpCAS</a> is installed in a location compatible with Libraries API. For example, install phpCAS so that <em>sites/all/libraries/CAS/CAS.php</em> exists. See README.txt for more information.', array('@url' => $phpcas_url)),
+    '#after_build' => array('cas_library_version_check'),
+    '#states' => array(
+      'invisible' => array(
+        'input[name="cas_library_use_libraries"]' => array('value' => 0),
+      ),
+    ),
+  );
+
+  $form['library']['cas_library_dir'] = array(
+    '#type' => 'textfield',
+    '#title' => t('phpCAS Library Path'),
+    '#default_value' => variable_get('cas_library_dir', 'CAS'),
+    '#description' => t('Specify the path where <a href="@url">phpCAS</a> is installed. The CAS.php file should reside in this directory. Leave blank to load CAS.php from your PHP include_path.', array('@url' => $phpcas_url)),
+    '#after_build' => array('cas_library_version_check'),
+    '#states' => array(
+      'invisible' => array(
+        'input[name="cas_library_use_libraries"]' => array('value' => 1),
+      ),
+    ),
+  );
 
   $form['server'] = array(
     '#type' => 'fieldset',
@@ -341,21 +358,27 @@ function cas_admin_settings() {
  * class and extracting the version.
  *
  * @param $element
- *   The form element containing the "library" fieldset.
+ *   The form element containing a "phpCAS library" selection.
  * @param $form_state
  *   An array containing the form's state information.
  *
  * @return
- *   The modified form element containing the "library" fieldset.
+ *   The modified form element.
  */
 function cas_library_version_check($element, &$form_state) {
-  $path = module_exists('libraries') ? NULL : $element['#value'];
+  if ($form_state['values']['cas_library_use_libraries'] != ($element['#name'] == 'cas_library_libraries')) {
+    // This will only be seen if the user toggles the Libraries API / path entry radio button.
+    $element['#description'] .= '<div class="warning messages">' . t('Please resubmit the form to test if phpCAS can be loaded.') . '</div>';
+    return $element;
+  }
+
   // Suppress errors if phpCAS cannot be loaded.
+  $path = ($element['#name'] == 'cas_library_dir') ? $element['#value'] : NULL;
   if ($version = @cas_phpcas_load($path)) {
-    $element['#suffix'] = '<div class="ok messages">' . t('phpCAS version %version successfully loaded.', array('%version' => $version)) . '</div>';
+    $element['#description'] .= '<div class="ok messages">' . t('phpCAS version %version successfully loaded.', array('%version' => $version)) . '</div>';
   }
   else {
-    $element['#suffix'] = '<div class="error messages">' . t('The phpCAS library was not found or could not be loaded.') . '</div>';
+    $element['#description'] .= '<div class="error messages">' . t('The phpCAS library was not found or could not be loaded.') . '</div>';
   }
   return $element;
 }
diff --git a/cas.install b/cas.install
index 8c481be..7d9c35b 100644
--- a/cas.install
+++ b/cas.install
@@ -88,6 +88,7 @@ function cas_uninstall() {
   variable_del('cas_hide_email');
   variable_del('cas_hide_password');
   variable_del('cas_library_dir');
+  variable_del('cas_library_use_libraries');
   variable_del('cas_login_drupal_invite');
   variable_del('cas_login_form');
   variable_del('cas_login_invite');
diff --git a/cas.module b/cas.module
index 3b84d97..aff1395 100644
--- a/cas.module
+++ b/cas.module
@@ -196,7 +196,7 @@ function cas_login_check($force_authentication = TRUE) {
  */
 function cas_phpcas_load($path = NULL) {
   if (!isset($path)) {
-    if (module_exists('libraries')) {
+    if (module_exists('libraries') && variable_get('cas_library_use_libraries', 1)) {
       $path = libraries_get_path('CAS');
     }
     else {
-- 
1.7.7.4

