Skip to content

Commit

Permalink
We need a normalized format for date fields when using SQL storage.
Browse files Browse the repository at this point in the history
Final fix for Bug: 11891
  • Loading branch information
mrubinsk committed Nov 20, 2013
1 parent ff22333 commit a18da4c
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 35 deletions.
36 changes: 31 additions & 5 deletions turba/lib/Driver.php
Expand Up @@ -223,6 +223,27 @@ public function getBlobs()
return $blobs;
}

/**
* Returns the attributes that represent dates.
*
* @return array List of date attributes in the array keys.
* @since 4.2.0
*/
public function getDateFields()
{
global $attributes;

$dates = array();
foreach (array_keys($this->fields) as $attribute) {
if (isset($attributes[$attribute]) &&
$attributes[$attribute]['type'] == 'monthdayyear') {
$dates[$attribute] = true;
}
}

return $dates;
}

/**
* Translates the keys of the first hash from the generalized Turba
* attributes to the driver-specific fields. The translation is based on
Expand Down Expand Up @@ -791,7 +812,8 @@ public function getObjects(array $objectIds)
$objects = $this->_read($this->map['__key'], $objectIds,
$this->getContactOwner(),
array_values($this->fields),
$this->toDriverKeys($this->getBlobs()));
$this->toDriverKeys($this->getBlobs()),
$this->toDriverKeys($this->getDateFields()));
if (!is_array($objects)) {
throw new Horde_Exception_NotFound();
}
Expand Down Expand Up @@ -868,7 +890,7 @@ public function add(array $attributes)

$attributes = $this->toDriverKeys($attributes);

$this->_add($attributes, $this->toDriverKeys($this->getBlobs()));
$this->_add($attributes, $this->toDriverKeys($this->getBlobs()), $this->toDriverKeys($this->getDateFields()));

/* Add tags. */
if (isset($attributes['__tags'])) {
Expand Down Expand Up @@ -2912,12 +2934,15 @@ protected function _search(array $criteria, array $fields, array $blobFields = a
* @param string $owner Only return contacts owned by this user.
* @param array $fields List of fields to return.
* @param array $blobFields Array of fields containing binary data.
* @param array $dateFields Array of fields containing date data.
* @since 4.2.0
*
* @return array Hash containing the search results.
* @throws Turba_Exception
*/
protected function _read($key, $ids, $owner, array $fields,
array $blobFields = array())
array $blobFields = array(),
array $dateFields = array())
{
throw new Turba_Exception(_("Reading contacts is not available."));
}
Expand All @@ -2926,11 +2951,12 @@ protected function _read($key, $ids, $owner, array $fields,
* Adds the specified contact to the addressbook.
*
* @param array $attributes The attribute values of the contact.
* @param array $blob_fields TODO
* @param array $blob_fields Fields that represent binary data.
* @param array $date_fields Fields that represent dates. @since 4.2.0
*
* @throws Turba_Exception
*/
protected function _add(array $attributes, array $blob_fields = array())
protected function _add(array $attributes, array $blob_fields = array(), array $date_fields = array())
{
throw new Turba_Exception(_("Adding contacts is not available."));
}
Expand Down
5 changes: 4 additions & 1 deletion turba/lib/Driver/Facebook.php
Expand Up @@ -134,12 +134,15 @@ protected function _search(array $criteria, array $fields, array $blobFields = a
* @param string $owner Only return contacts owned by this user.
* @param array $fields List of fields to return.
* @param array $blobFields Array of fields containing binary data.
* @param array $dateFields Array of fields containing date data.
* @since 4.2.0
*
* @return array Hash containing the search results.
* @throws Turba_Exception
*/
protected function _read($key, $ids, $owner, array $fields,
array $blobFields = array())
array $blobFields = array(),
array $dateFields = array())
{
return $this->_getEntry($ids, $fields);
}
Expand Down
5 changes: 4 additions & 1 deletion turba/lib/Driver/Favourites.php
Expand Up @@ -132,12 +132,15 @@ protected function _match($contact, $val)
* @param string $owner Only return contacts owned by this user.
* @param array $fields List of fields to return.
* @param array $blobFields Array of fields containing binary data.
* @param array $dateFields Array of fields containing date data.
* @since 4.2.0
*
* @return array Hash containing the search results.
* @throws Turba_Exception
*/
protected function _read($key, $ids, $owner, array $fields,
array $blobFields = array())
array $blobFields = array(),
array $dateFields = array())
{
$book = $this->_getAddressBook();

Expand Down
5 changes: 4 additions & 1 deletion turba/lib/Driver/Group.php
Expand Up @@ -112,12 +112,15 @@ protected function _search(array $criteria, array $fields, array $blobFields = a
* @param string $owner Only return contacts owned by this user.
* @param array $fields List of fields to return.
* @param array $blobFields Array of fields containing binary data.
* @param array $dateFields Array of fields containing date data.
* @since 4.2.0
*
* @return array Hash containing the search results.
* @throws Turba_Exception
*/
protected function _read($key, $ids, $owner, array $fields,
array $blobFields = array())
array $blobFields = array(),
array $dateFields = array())
{
$book = $this->_getAddressBook();
$results = array();
Expand Down
5 changes: 3 additions & 2 deletions turba/lib/Driver/Imsp.php
Expand Up @@ -256,11 +256,12 @@ protected function _read($key, $ids, $owner, array $fields,
* Adds the specified contact to the addressbook.
*
* @param array $attributes The attribute values of the contact.
* @param array $blob_fields TODO
* @param array $blob_fields Fields that represent binary data.
* @param array $date_fields Fields that represent dates. @since 4.2.0
*
* @throws Turba_Exception
*/
protected function _add(array $attributes, array $blob_fields = array())
protected function _add(array $attributes, array $blob_fields = array(), array $date_fields = array())
{
/* We need to map out Turba_Object_Groups back to IMSP groups before
* writing out to the server. We need to array_values() it in
Expand Down
10 changes: 7 additions & 3 deletions turba/lib/Driver/Kolab.php
Expand Up @@ -599,12 +599,15 @@ protected function _removeDuplicated($ids)
* @param string $owner Only return contacts owned by this user.
* @param array $fields List of fields to return.
* @param array $blobFields Array of fields containing binary data.
* @param array $dateFields Array of fields containing date data.
* @since 4.2.0
*
* @return array Hash containing the search results.
* @throws Turba_Exception
*/
protected function _read($key, $ids, $owner, array $fields,
array $blobFields = array())
array $blobFields = array(),
array $dateFields = array())
{
$this->connect();

Expand Down Expand Up @@ -684,11 +687,12 @@ protected function _read($key, $ids, $owner, array $fields,
* Adds the specified contact to the addressbook.
*
* @param array $attributes The attribute values of the contact.
* @param array $blob_fields TODO
* @param array $blob_fields Fields that represent binary data.
* @param array $date_fields Fields that represent dates. @since 4.2.0
*
* @throws Turba_Exception
*/
protected function _add(array $attributes, array $blob_fields = array())
protected function _add(array $attributes, array $blob_fields = array(), array $date_fields = array())
{
$this->connect();
$this->_store($attributes);
Expand Down
10 changes: 7 additions & 3 deletions turba/lib/Driver/Ldap.php
Expand Up @@ -215,12 +215,15 @@ protected function _search(array $criteria, array $fields, array $blobFields = a
* @param string $owner Only return contacts owned by this user.
* @param array $fields List of fields to return.
* @param array $blobFields Array of fields containing binary data.
* @param array $dateFields Array of fields containing date data.
* @since 4.2.0
*
* @return array Hash containing the search results.
* @throws Turba_Exception
*/
protected function _read($key, $ids, $owner, array $fields,
array $blobFields = array())
array $blobFields = array(),
array $dateFields = array())
{
/* Only DN. */
if ($key != 'dn') {
Expand Down Expand Up @@ -269,11 +272,12 @@ protected function _read($key, $ids, $owner, array $fields,
* Adds the specified contact to the addressbook.
*
* @param array $attributes The attribute values of the contact.
* @param array $blob_fields TODO
* @param array $blob_fields Fields that represent binary data.
* @param array $date_fields Fields that represent dates. @since 4.2.0
*
* @throws Turba_Exception
*/
protected function _add(array $attributes, array $blob_fields = array())
protected function _add(array $attributes, array $blob_fields = array(), array $date_fields = array())
{
if (empty($attributes['dn'])) {
throw new Turba_Exception('Tried to add an object with no dn: [' . serialize($attributes) . '].');
Expand Down
17 changes: 14 additions & 3 deletions turba/lib/Driver/Prefs.php
Expand Up @@ -33,9 +33,19 @@ protected function _search(array $criteria, array $fields, array $blobFields = a
/**
* Reads the given data from the preferences and returns the result's
* fields.
*
* @param string $key The primary key field to use.
* @param mixed $ids The ids of the contacts to load.
* @param string $owner Only return contacts owned by this user.
* @param array $fields List of fields to return.
* @param array $blobFields Array of fields containing binary data.
* @param array $dateFields Array of fields containing date data.
* @since 4.2.0
*
*/
protected function _read($key, $ids, $owner, array $fields,
array $blobFields = array())
array $blobFields = array(),
array $dateFields = array())
{
$book = $this->_getAddressBook();

Expand All @@ -57,11 +67,12 @@ protected function _read($key, $ids, $owner, array $fields,
* Adds the specified contact to the addressbook.
*
* @param array $attributes The attribute values of the contact.
* @param array $blob_fields TODO
* @param array $blob_fields Fields that represent binary data.
* @param array $date_fields Fields that represent dates. @since 4.2.0
*
* @throws Turba_Exception
*/
protected function _add(array $attributes, array $blob_fields = array())
protected function _add(array $attributes, array $blob_fields = array(), array $date_fields = array())
{
$book = $this->_getAddressBook();
$book[$attributes['id']] = $attributes;
Expand Down
16 changes: 10 additions & 6 deletions turba/lib/Driver/Share.php
Expand Up @@ -220,27 +220,31 @@ protected function _search(array $criteria, array $fields, array $blobFields = a
* @param string $owner Only return contacts owned by this user.
* @param array $fields List of fields to return.
* @param array $blobFields Array of fields containing binary data.
* @param array $dateFields Array of fields containing date data.
* @since 4.2.0
*
* @return array Hash containing the search results.
* @throws Turba_Exception
*/
protected function _read($key, $ids, $owner, array $fields,
array $blobFields = array())
array $blobFields = array(),
array $dateFields = array())
{
return $this->_driver->_read($key, $ids, $owner, $fields, $blobFields);
return $this->_driver->_read($key, $ids, $owner, $fields, $blobFields, $dateFields);
}

/**
* Adds the specified contact to the addressbook.
*
* @param array $attributes The attribute values of the contact.
* @param array $blob_fields TODO
* @param array $attributes The attribute values of the contact.
* @param array $blob_fields Fields that represent binary data.
* @param array $date_fields Fields that represent dates. @since 4.2.0
*
* @throws Turba_Exception
*/
protected function _add(array $attributes, array $blob_fields = array())
protected function _add(array $attributes, array $blob_fields = array(), array $date_fields = array())
{
return $this->_driver->_add($attributes, $blob_fields);
return $this->_driver->_add($attributes, $blob_fields, $date_fields);
}

/**
Expand Down
29 changes: 21 additions & 8 deletions turba/lib/Driver/Sql.php
Expand Up @@ -173,7 +173,7 @@ protected function _internalSearch(array $criteria, array $fields, $blobFields =
}
}

protected function _parseRead($blobFields, $result)
protected function _parseRead($blobFields, $result, $dateFields)
{
$results = array();
$columns = $this->_db->columns($this->_params['table']);
Expand All @@ -184,6 +184,11 @@ protected function _parseRead($blobFields, $result)
foreach ($row as $field => $val) {
if (isset($blobFields[$field])) {
$entry[$field] = $columns[$field]->binaryToString($val);
} elseif (isset($dateFields[$field])) {
$d = new Horde_Date($val);
$entry[$field] = $this->_convertFromDriver(
$d->strftime($GLOBALS['attributes'][$field]['params']['format_in'])
);
} else {
$entry[$field] = $this->_convertFromDriver($val);
}
Expand Down Expand Up @@ -350,12 +355,14 @@ public function searchDuplicates()
* @param string $owner Only return contacts owned by this user.
* @param array $fields List of fields to return.
* @param array $blobFields Array of fields containing binary data.
* @param array $dateFields Array of fields containing date data.
* @since 4.2.0
*
* @return array Hash containing the search results.
* @throws Turba_Exception
*/
protected function _read($key, $ids, $owner, array $fields,
array $blobFields = array())
array $blobFields = array(), array $dateFields = array())
{
$values = array();

Expand Down Expand Up @@ -386,7 +393,7 @@ protected function _read($key, $ids, $owner, array $fields,
. $this->_params['table'] . ' WHERE ' . $where;

try {
return $this->_parseRead($blobFields, $this->_db->selectAll($query, $values));
return $this->_parseRead($blobFields, $this->_db->selectAll($query, $values), $date_fields);
} catch (Horde_Db_Exception $e) {
throw new Turba_Exception($e);
}
Expand All @@ -395,13 +402,15 @@ protected function _read($key, $ids, $owner, array $fields,
/**
* Adds the specified object to the SQL database.
*
* TODO
* @param array $attributes The attribute values of the contact.
* @param array $blob_fields Fields that represent binary data.
* @param array $date_fields Fields that represent dates. @since 4.2.0
*
* @throws Turba_Exception
*/
protected function _add(array $attributes, array $blob_fields = array())
protected function _add(array $attributes, array $blob_fields = array(), array $date_fields = array())
{
list($fields, $values) = $this->_prepareWrite($attributes, $blob_fields);
list($fields, $values) = $this->_prepareWrite($attributes, $blob_fields, $date_fields);
$query = 'INSERT INTO ' . $this->_params['table']
. ' (' . implode(', ', $fields) . ')'
. ' VALUES (' . str_repeat('?, ', count($values) - 1) . '?)';
Expand All @@ -413,7 +422,7 @@ protected function _add(array $attributes, array $blob_fields = array())
}
}

protected function _prepareWrite($attributes, $blob_fields)
protected function _prepareWrite($attributes, $blob_fields, $date_fields)
{
$fields = $values = array();

Expand All @@ -422,6 +431,9 @@ protected function _prepareWrite($attributes, $blob_fields)

if (!empty($value) && isset($blob_fields[$field])) {
$values[] = new Horde_Db_Value_Binary($value);
} elseif (!empty($value) && isset($date_fields[$field])) {
$d = new Horde_Date($value);
$values[] = $d->strftime('%Y-%m-%d');
} else {
$values[] = $this->_convertToDriver($value);
}
Expand Down Expand Up @@ -510,11 +522,12 @@ protected function _save(Turba_Object $object)
list($object_key, $object_id) = each($this->toDriverKeys(array('__key' => $object->getValue('__key'))));
$attributes = $this->toDriverKeys($object->getAttributes());
$blob_fields = $this->toDriverKeys($this->getBlobs());
$date_fields = $this->toDriverKeys($this->getDateFields());

$where = $object_key . ' = ?';
unset($attributes[$object_key]);

list($fields, $values) = $this->_prepareWrite($attributes, $blob_fields);
list($fields, $values) = $this->_prepareWrite($attributes, $blob_fields, $date_fields);

$values[] = $object_id;

Expand Down

0 comments on commit a18da4c

Please sign in to comment.