From 11f6e75c28c0912a66c0af239420c428367dc69c Mon Sep 17 00:00:00 2001
From: Adam Lyall <magicmyth@magicmyth.com>
Date: Mon, 19 Nov 2012 14:16:45 +0000
Subject: [PATCH 1/3] Issue #1844564 - Missing table name braces

Resolves issues with installations that use table prefixes.
---
 opening_hours.module |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/opening_hours.module b/opening_hours.module
index 4637924..db610ed 100644
--- a/opening_hours.module
+++ b/opening_hours.module
@@ -161,7 +161,7 @@ function opening_hours_cron() {
 
     // Propagate instances that are still repeating.
     $propagate_query = db_query("
-      SELECT * FROM opening_hours
+      SELECT * FROM {opening_hours}
       WHERE repeat_end_date > '%s'
       AND repeat_rule IS NOT NULL AND repeat_rule != ''
     ", array(':date' => date('Y-m-d', $_SERVER['REQUEST_TIME'])));
@@ -275,7 +275,7 @@ function opening_hours_present_on_node($nid, $reset = FALSE) {
  * Load opening hours instance by id.
  */
 function opening_hours_instance_load($instance_id) {
-  $query = db_query("SELECT * FROM opening_hours WHERE instance_id = :id LIMIT 1", array(
+  $query = db_query("SELECT * FROM {opening_hours} WHERE instance_id = :id LIMIT 1", array(
     ':id' => $instance_id,
   ));
 
-- 
1.7.10.4


From bad6f214d94b2697e8d5e4e1af6ab2db439b45af Mon Sep 17 00:00:00 2001
From: Adam Lyall <magicmyth@magicmyth.com>
Date: Mon, 19 Nov 2012 14:32:51 +0000
Subject: [PATCH 2/3] Update database queries to Drupal 7 API

Some update paths (such as future instance updates) would fail from bad
database API calls. It seems bits of Drupal 6's API were still in use.
---
 includes/opening_hours.pages.inc |    2 +-
 opening_hours.module             |   14 +++++++-------
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/includes/opening_hours.pages.inc b/includes/opening_hours.pages.inc
index 91b6649..18f6d4c 100644
--- a/includes/opening_hours.pages.inc
+++ b/includes/opening_hours.pages.inc
@@ -170,7 +170,7 @@ function _opening_hours_instance_update($instance, $original) {
         db_query("
           DELETE FROM {opening_hours}
           WHERE original_instance_id = :id
-          AND date > '%s'
+          AND date > :end_date
         ", array(
           ':id' => $instance->instance_id,
           ':end_date' => $instance->repeat_end_date,
diff --git a/opening_hours.module b/opening_hours.module
index db610ed..038e91a 100644
--- a/opening_hours.module
+++ b/opening_hours.module
@@ -156,13 +156,13 @@ function opening_hours_cron() {
       DELETE FROM {opening_hours}
       WHERE original_instance_id IS NOT NULL
       AND customised = 0
-      AND date < '%s'
+      AND date < :date
     ", array(':date' => date('Y-m-d', $_SERVER['REQUEST_TIME'] - 7 * 86400)));
 
     // Propagate instances that are still repeating.
     $propagate_query = db_query("
       SELECT * FROM {opening_hours}
-      WHERE repeat_end_date > '%s'
+      WHERE repeat_end_date > :date
       AND repeat_rule IS NOT NULL AND repeat_rule != ''
     ", array(':date' => date('Y-m-d', $_SERVER['REQUEST_TIME'])));
 
@@ -428,14 +428,14 @@ function opening_hours_repeat_instance_propagate(&$instance) {
  */
 function opening_hours_repeat_stop_propagation($instance_id) {
   // Get the date of the last non-deleted instance.
-  $max_date = db_result(db_query('
-    SELECT MAX(date) FROM {opening_hours} WHERE original_instance_id = %d
-  ', array(':id' => $instance_id)));
+  $max_date = db_query('
+    SELECT MAX(date) FROM {opening_hours} WHERE original_instance_id = :id
+  ', array(':id' => $instance_id))->fetchField();
 
   db_query("
     UPDATE {opening_hours}
-    SET repeat_end_date = '%s'
-    WHERE instance_id = %d
+    SET repeat_end_date = :date
+    WHERE instance_id = :id
   ", array(
     ':date' => $max_date,
     ':id' => $instance_id,
-- 
1.7.10.4


From 8c16a6135c9c562f98c86a0343a6a8f6f95a6976 Mon Sep 17 00:00:00 2001
From: Adam Lyall <magicmyth@magicmyth.com>
Date: Mon, 19 Nov 2012 14:39:06 +0000
Subject: [PATCH 3/3] Request sometimes sets repeat_end_date to string 'null'

Not sure why this is but instead of being passed as literal null or not
at all, sometimes the PUT JSON data is having repeat_end_date set to a
string with the value 'null' instead of a PHP type NULL. The reason for
this should be tracked down but this patch works around the issue by
checking for the string 'null' as well as empty() test in order to unset
the field when needed. Without this a PDO exception will occur because
drupal_write_record will attempt to set a date column field to the string
'null'.
---
 includes/opening_hours.pages.inc |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/includes/opening_hours.pages.inc b/includes/opening_hours.pages.inc
index 18f6d4c..9eddecc 100644
--- a/includes/opening_hours.pages.inc
+++ b/includes/opening_hours.pages.inc
@@ -220,7 +220,7 @@ function _opening_hours_instance_update($instance, $original) {
   // drupal_write_record will try to write an empty string to the repeat
   // end date if it has an empty-ish value, which will fail, so be sure
   // to unset it first in this case.
-  if (empty($instance->repeat_end_date)) {
+  if (empty($instance->repeat_end_date) || strtolower($instance->repeat_end_date) == 'null') {
     unset($instance->repeat_end_date);
   }
 
-- 
1.7.10.4

