From cc1517da90059095d2df520e47adbd3a85c4af80 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 6 Feb 2007 11:03:22 +0000 Subject: [PATCH] Closes #220, Add a more friendly way to enter dates in search window and accessDate field Addresss #352, Make sure data layer doesn't allow bad data via the API Access date field is now human-friendly. Also enforcing SQL date form for the field in the DB and discarding bad data passed via setField(). --- chrome/content/zotero/itemPane.js | 21 ++++++++++++++++++--- chrome/content/zotero/xpcom/data_access.js | 9 +++++++++ chrome/content/zotero/xpcom/schema.js | 7 +++++-- userdata.sql | 2 +- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/chrome/content/zotero/itemPane.js b/chrome/content/zotero/itemPane.js index b25fc9edf..a7b8b93fc 100644 --- a/chrome/content/zotero/itemPane.js +++ b/chrome/content/zotero/itemPane.js @@ -830,6 +830,11 @@ var ZoteroItemPane = new function() if (valueText){ var date = Zotero.Date.sqlToDate(valueText, true); valueText = date ? date.toLocaleString() : ''; + + // Don't show time for access date if none + if (fieldName == 'accessDate') { + valueText = valueText.replace('00:00:00 ', ''); + } } break; } @@ -1241,11 +1246,21 @@ var ZoteroItemPane = new function() // Fields else { - // Access date needs to be converted to UTC + // Access date needs to be parsed and converted to UTC if (fieldName=='accessDate' && value!='') { - var localDate = Zotero.Date.sqlToDate(value); - value = Zotero.Date.dateToSQL(localDate, true); + if (Zotero.Date.isSQLDate(value) || Zotero.Date.isSQLDateTime(value)) { + var localDate = Zotero.Date.sqlToDate(value); + value = Zotero.Date.dateToSQL(localDate, true); + } + else { + var d = Zotero.Date.strToDate(value); + value = null; + if (d.year && d.month != undefined && d.day) { + d = new Date(d.year, d.month, d.day); + value = Zotero.Date.dateToSQL(d, true); + } + } } if(saveChanges) diff --git a/chrome/content/zotero/xpcom/data_access.js b/chrome/content/zotero/xpcom/data_access.js index 06e803b82..a9549e4d2 100644 --- a/chrome/content/zotero/xpcom/data_access.js +++ b/chrome/content/zotero/xpcom/data_access.js @@ -509,6 +509,15 @@ Zotero.Item.prototype.setField = function(field, value, loadIn){ !Zotero.Date.isMultipart(value)){ value = Zotero.Date.strToMultipart(value); } + + if (fieldID == Zotero.ItemFields.getID('accessDate')) { + if (!Zotero.Date.isSQLDate(value) && + !Zotero.Date.isSQLDateTime(value)) { + Zotero.debug("Discarding invalid accessDate '" + value + + "' in Item.setField()"); + return false; + } + } } // If existing value, make sure it's actually changing diff --git a/chrome/content/zotero/xpcom/schema.js b/chrome/content/zotero/xpcom/schema.js index 0a07d09cd..0d855a582 100644 --- a/chrome/content/zotero/xpcom/schema.js +++ b/chrome/content/zotero/xpcom/schema.js @@ -771,8 +771,11 @@ Zotero.Schema = new function(){ // Firefox 2.0, so we just throw the triggers at the DB on every // userdata update and catch errors individually // - // Add in DROP statements if these need to change - var itemDataTrigger = " FOR EACH ROW WHEN NEW.fieldID=14\n" + + try { Zotero.DB.query("DROP TRIGGER insert_date_field"); } catch (e) {} + try { Zotero.DB.query("DROP TRIGGER update_date_field"); } catch (e) {} + + var itemDataTrigger = " FOR EACH ROW WHEN NEW.fieldID IN (14, 27)\n" + " BEGIN\n" + " SELECT CASE\n" + " CAST(SUBSTR(NEW.value, 1, 4) AS INT) BETWEEN 0 AND 9999 AND\n" diff --git a/userdata.sql b/userdata.sql index ef250dfa6..591c1e907 100644 --- a/userdata.sql +++ b/userdata.sql @@ -1,4 +1,4 @@ --- 17 +-- 18 -- This file creates tables containing user-specific data -- any changes -- to existing tables made here must be mirrored in transition steps in