From 79b65318101294458e6d3a85c03377f0b9d1e9f0 Mon Sep 17 00:00:00 2001
From: Hugh Barnes <hughbris@1129256.no-reply.drupal.org>
Date: Tue, 18 Oct 2011 14:51:23 +1300
Subject: [PATCH] First brush allowing caching to be disabled, requires some work with conditional field form validation

---
 flickrapi.module |   38 ++++++++++++++++++++++++++++++--------
 1 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/flickrapi.module b/flickrapi.module
index cf040c6..a1dce58 100644
--- a/flickrapi.module
+++ b/flickrapi.module
@@ -33,20 +33,35 @@ function flickrapi_admin_settings() {
     '#default_value' => variable_get('flickrapi_api_secret', ''),
     '#description' => t("API key's secret from Flickr."),
   );
+
+  $form['flickrapi_cache_enabled'] = array(
+    '#type' => 'checkbox',
+    '#default_value' => variable_get('flickrapi_cache_enabled',TRUE), //FIXME: should this use $form_state to deal with submissions following errors?
+    '#title' => t('Allow Flickr API to store results on your server'),
+    '#description' => t('This can improve page loading speeds for your users if you are calling the same Flickr requests frequently. You may want to disable it for testing, to save on your own bandwidth costs, or if you have concerns about keeping images on your own server. Note that some Flickr methods enforce restrictions on the frequency with which they may be called, !documented.', array(
+      '!documented' => l(t('as per the documentation'), 'http://www.flickr.com/services/api/'),
+      )
+    ),
+  );
+  $form['flickrapi_cache_settings'] = array(
+    '#type' => 'fieldset',
+    '#title' => 'Flickr API cache settings',
+    '#collapsible' => TRUE,
+    '#collapsed' => !(variable_get('flickrapi_cache_enabled', TRUE)),
+    );
   $times = array(900, 1800, 2700, 3600, 7200, 10800, 14400, 18000, 21600, 43200, 86400);
   $ageoptions = drupal_map_assoc($times, 'format_interval');
-  $form['flickrapi_cache_duration'] = array(
+  $form['flickrapi_cache_settings']['flickrapi_cache_duration'] = array(
     '#type' => 'select',
     '#title' => t('Update interval'),
     '#options' => $ageoptions,
     '#default_value' => variable_get('flickrapi_cache_duration', 3600),
     '#description' => t("The refresh interval indicating how often you want to check cached Flickr API calls are up to date."),
   );
-
-  $form['flickrcachepath'] = array(
+  $form['flickrapi_cache_settings']['flickrcachepath'] = array( //FIXME: this var name isn't consistent with the others
     '#title' => t('Flickr Cache Path'),
-    '#required' => TRUE,
-    '#description' => t('Location on server file system where results of Flickr API calls can be cached.'),
+    '#required' => variable_get('flickrapi_cache_enabled', FALSE),
+    '#description' => t('Location on server file system where results of Flickr API calls can be cached. This is required only when caching is enabled and must be a valid path.'),
     '#type' => 'textfield',
     '#default_value' => variable_get('flickrcachepath', '/tmp'),
   );
@@ -58,6 +73,7 @@ function flickrapi_admin_settings() {
 function flickrapi_admin_settings_validate($form, &$form_state) {
   $key = trim($form_state['values']['flickrapi_api_key']);
   $sec = trim($form_state['values']['flickrapi_api_secret']);
+  $cache_required = $form_state['values']['flickrapi_cache_enabled']; //TODO: set up conditional logic
   $cache_dir = $form_state['values']['flickrcachepath'];
 	
   if ($key && (preg_match('/^[A-Fa-f\d]{32}$/', $key) != 1)) {
@@ -67,8 +83,9 @@ function flickrapi_admin_settings_validate($form, &$form_state) {
     form_set_error('flickrapi_api_secret', t('This does not appear to be a Flickr API secret.'));
   }
 
-  if(!is_dir($cache_dir)) {
-    form_set_error('flickrcachepath', t('Folder does not exist.'));
+  //FIXME: this doesn't adequately handle where the cache location field is set mandatory but the cache has been disabled before submission, need to switch off or ignore #required somehow at some point
+  if ($cache_required && !is_dir($cache_dir)) {
+    form_set_error('flickrcachepath', t('Cache folder does not exist.')); //TODO: check it's writeable?
   }
 }
 
@@ -88,7 +105,12 @@ function flickrapi_phpFlickr() {
     }
   }
   $flickr = new phpFlickr($api_key);
-  $flickr->enableCache("fs", variable_get('flickrcachepath', '/tmp'));
+
+  $cache_enabled = variable_get('flickrapi_cache_enabled', FALSE);
+  if($cache_enabled) {
+    $flickr->enableCache("fs", variable_get('flickrcachepath', '/tmp'));
+  }
+
   return $flickr;
 }
 
-- 
1.7.2.5

