Fix addressbook_bo::find_contact()

This commit is contained in:
Jörg Lehrke 2010-04-21 16:02:35 +00:00
parent 69a6bf75cd
commit d11ce583fb
2 changed files with 85 additions and 163 deletions

View File

@ -101,6 +101,7 @@ class addressbook_bo extends addressbook_so
* @var boolean
*/
var $log = false;
var $logfile = '/tmp/log-addressbook_bo';
/**
* Number and message of last error or false if no error, atm. only used for saving
@ -136,6 +137,11 @@ class addressbook_bo extends addressbook_so
function __construct($contact_app='addressbook')
{
parent::__construct($contact_app);
if ($this->log)
{
$this->logfile = $GLOBALS['egw_info']['server']['temp_dir'].'/log-addressbook_bo';
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__."($contact_app)\n", 3 ,$this->logfile);
}
$this->tz_offset_s = 3600 * $GLOBALS['egw_info']['user']['preferences']['common']['tz_offset'];
$this->now_su = time() + $this->tz_offset_s;
@ -726,7 +732,8 @@ class addressbook_bo extends addressbook_so
$contact['modifier'] = $this->user;
$contact['modified'] = $this->now_su;
// set full name and fileas from the content
if (!isset($contact['n_fn']) ) {
if (!isset($contact['n_fn']))
{
$contact['n_fn'] = $this->fullname($contact);
if (isset($contact['org_name'])) $contact['n_fileas'] = $this->fileas($contact);
}
@ -1293,6 +1300,7 @@ class addressbook_bo extends addressbook_so
function merge($ids)
{
$this->error = false;
$account = null;
foreach(parent::search(array('id'=>$ids),false) as $contact) // $this->search calls the extended search from ui!
{
if ($contact['account_id'])
@ -1628,19 +1636,6 @@ class addressbook_bo extends addressbook_so
}
}
function all_empty(&$_contact, &$fields)
{
$retval = true;
foreach ($fields as $field) {
if (isset($_contact[$field]) && !empty($_contact[$field])) {
$retval = false;
break;
}
}
return $retval;
}
/**
* Try to find a matching db entry
*
@ -1650,11 +1645,14 @@ class addressbook_bo extends addressbook_so
*/
function find_contact($contact, $relax=false)
{
$empty_addr_one = $empty_addr_two = true;
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. '('. ($relax ? 'RELAX': 'EXACT') . ')[ContactData]:'
. array2string($contact));
. array2string($contact)
. "\n", 3, $this->logfile);
}
$matchingContacts = array();
@ -1663,7 +1661,8 @@ class addressbook_bo extends addressbook_so
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. '()[ContactID]: ' . $contact['id']);
. '()[ContactID]: ' . $contact['id']
. "\n", 3, $this->logfile);
}
// We only do a simple consistency check
if (!$relax || ((empty($found['n_family']) || $found['n_family'] == $contact['n_family'])
@ -1680,7 +1679,8 @@ class addressbook_bo extends addressbook_so
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. '()[ContactUID]: ' . $contact['uid']);
. '()[ContactUID]: ' . $contact['uid']
. "\n", 3, $this->logfile);
}
// Try the given UID first
$criteria = array ('contact_uid' => $contact['uid']);
@ -1707,124 +1707,75 @@ class addressbook_bo extends addressbook_so
$addr_two_fields = array('adr_two_street',
'adr_two_locality', 'adr_two_region',
'adr_two_postalcode', 'adr_two_countryname');
$no_addr_one = array();
$no_addr_two = array();
if (!empty($contact['owner']))
{
$columns_to_search += array('owner');
}
$backend =& $this->get_backend();
// define filter for empty address one
foreach ($addr_one_fields as $field)
{
if (!($db_col = array_search($field, $backend->db_cols)))
{
$db_col = $field;
}
$no_addr_one[] = "(" . $db_col . " IS NULL OR " . $db_col . " = '')";
}
// define filter for empty address two
foreach ($addr_two_fields as $field)
{
if (!($db_col = array_search($field, $backend->db_cols)))
{
$db_col = $field;
}
$no_addr_two[] = "(" . $db_col . " IS NULL OR " . $db_col . " = '')";
}
$result = false;
$criteria = array();
$empty_columns = array();
foreach ($columns_to_search as $field)
{
if (!isset($contact[$field]) || empty($contact[$field])) {
if ($relax && in_array($field, $tolerance_fields)) continue;
if (empty($contact[$field]))
{
// Not every device supports all fields
if (!in_array($field, $tolerance_fields))
{
if (!($db_col = array_search($field, $backend->db_cols)))
{
$db_col = $field;
}
$empty_columns[] = "(" . $db_col . " IS NULL OR " . $db_col . " = '')";
$criteria[$field] = '';
}
}
else
{
if (!$relax || !in_array($field, $tolerance_fields))
{
$criteria[$field] = $contact[$field];
}
$criteria[$field] = $contact[$field];
}
}
$filter = $empty_columns;
if (!$relax)
{
// We use addresses only for strong matching
if ($this->all_empty($contact, $addr_one_fields))
foreach ($addr_one_fields as $field)
{
$filter = $filter + $no_addr_one;
}
else
{
foreach ($addr_one_fields as $field)
if (empty($contact[$field]))
{
if (!isset($contact[$field]) || empty($contact[$field]))
{
if (!($db_col = array_search($field, $backend->db_cols)))
{
$db_col = $field;
}
$filter[] = "(" . $db_col . " IS NULL OR " . $db_col . " = '')";
}
else
{
$criteria[$field] = $contact[$field];
}
$criteria[$field] = '';
}
else
{
$empty_addr_one = false;
$criteria[$field] = $contact[$field];
}
}
if ($this->all_empty($contact, $addr_two_fields))
foreach ($addr_two_fields as $field)
{
$filter = $filter + $no_addr_two;
}
else
{
foreach ($addr_two_fields as $field)
if (empty($contact[$field]))
{
if (!isset($contact[$field]) || empty($contact[$field]))
{
if (!($db_col = array_search($field, $backend->db_cols)))
{
$db_col = $field;
}
$filter[] = "(" . $db_col . " IS NULL OR " . $db_col . " = '')";
}
else
{
$criteria[$field] = $contact[$field];
}
$criteria[$field] = '';
}
else
{
$empty_addr_two = false;
$criteria[$field] = $contact[$field];
}
}
}
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. '()[Addressbook FIND Step 1]: '
. 'FILTER:' . array2string($filter)
. 'CRITERIA' . array2string($criteria));
. 'CRITERIA = ' . array2string($criteria)
. "\n", 3, $this->logfile);
}
// first try full match
if (($foundContacts = parent::search($criteria, true, '', '', '', False, 'AND', false, $filter)))
if (($foundContacts = parent::search($criteria, true, '', '', '', true)))
{
foreach ($foundContacts as $egwContact)
{
@ -1833,24 +1784,26 @@ class addressbook_bo extends addressbook_so
}
// No need for more searches for relaxed matching
if (!$relax || count($matchingContacts)) return $matchingContacts;
if ($relax || count($matchingContacts)) return $matchingContacts;
if (!$this->all_empty($contact, $addr_one_fields)
&& $this->all_empty($contact, $addr_two_fields))
if (!$empty_addr_one && $empty_addr_two)
{
// try given address and ignore the second one in EGW
$filter = array_diff($filter, $no_addr_two);
foreach ($addr_two_fields as $field)
{
unset($criteria[$field]);
}
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. '()[Addressbook FIND Step 2]: '
. 'FILTER:' . array2string($filter)
. 'CRITERIA' . array2string($criteria));
. 'CRITERIA = ' . array2string($criteria)
. "\n", 3, $this->logfile);
}
if (($foundContacts = parent::search($criteria, true, '', '', '', False, 'AND', false, $filter)))
if (($foundContacts = parent::search($criteria, true, '', '', '', true)))
{
foreach ($foundContacts as $egwContact)
{
@ -1860,35 +1813,21 @@ class addressbook_bo extends addressbook_so
else
{
// try address as home address -- some devices don't qualify addresses
$filter = $empty_columns;
foreach ($addr_two_fields as $key => $field)
{
if (isset($criteria[$addr_one_fields[$key]]))
{
$criteria[$field] = $criteria[$addr_one_fields[$key]];
unset($criteria[$addr_one_fields[$key]]);
}
else
{
if (!($db_col = array_search($field,$backend->db_cols)))
{
$db_col = $field;
}
$filter[] = "(" . $db_col . " IS NULL OR " . $db_col . " = '')";
}
$criteria[$field] = $criteria[$addr_one_fields[$key]];
unset($criteria[$addr_one_fields[$key]]);
}
$filter = $filter + $no_addr_one;
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. '()[Addressbook FIND Step 3]: '
. 'FILTER:' . array2string($filter)
. 'CRITERIA' . array2string($criteria));
. 'CRITERIA = ' . array2string($criteria)
. "\n", 3, $this->logfile);
}
if (($foundContacts = parent::search($criteria, true, '', '', '', False, 'AND', false, $filter)))
if (($foundContacts = parent::search($criteria, true, '', '', '', true)))
{
foreach ($foundContacts as $egwContact)
{
@ -1897,54 +1836,23 @@ class addressbook_bo extends addressbook_so
}
}
}
else
elseif (!$empty_addr_one && !$empty_addr_two)
{ // try again after address swap
$filter = $empty_columns;
foreach ($addr_one_fields as $key => $field)
{
$_temp_set = false;
if (isset($criteria[$field]))
{
$_temp = $criteria[$field];
$_temp_set = true;
unset($criteria[$field]);
}
if (isset($criteria[$addr_two_fields[$key]]))
{
$criteria[$field] = $criteria[$addr_two_fields[$key]];
unset($criteria[$addr_two_fields[$key]]);
}
else
{
if (!($db_col = array_search($field,$backend->db_cols)))
{
$db_col = $field;
}
$filter[] = "(" . $db_col . " IS NULL OR " . $db_col . " = '')";
}
if ($_temp_set)
{
$criteria[$addr_two_fields[$key]] = $_temp;
}
else
{
if (!($db_col = array_search($addr_two_fields[$key],$backend->db_cols)))
{
$db_col = $field;
}
$filter[] = "(" . $db_col . " IS NULL OR " . $db_col . " = '')";
}
$_temp = $criteria[$field];
$criteria[$field] = $criteria[$addr_two_fields[$key]];
$criteria[$addr_two_fields[$key]] = $_temp;
}
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. '()[Addressbook FIND Step 4]: '
. 'FILTER:' . array2string($filter)
. 'CRITERIA' . array2string($criteria));
. 'CRITERIA = ' . array2string($criteria)
. "\n", 3, $this->logfile);
}
if(($foundContacts = parent::search($criteria, true, '', '', '', False, 'AND', false, $filter)))
if (($foundContacts = parent::search($criteria, true, '', '', '', true)))
{
foreach ($foundContacts as $egwContact)
{
@ -1955,7 +1863,8 @@ class addressbook_bo extends addressbook_so
if ($this->log)
{
error_log(__FILE__.'['.__LINE__.'] '.__METHOD__
. '()[FOUND]:' . array2string($matchingContacts));
. '()[FOUND]: ' . array2string($matchingContacts)
. "\n", 3, $this->logfile);
}
return $matchingContacts;
}

View File

@ -672,7 +672,21 @@ class so_sql
{
$db_col = $col;
}
if ($wildcard || $criteria[$col][0] == '!' ||
if ($val === '')
{
if (isset($this->table_def['fd'][$db_col]) &&
$this->table_def['fd'][$db_col]['type'] == 'varchar' &&
$this->table_def['fd'][$db_col]['nullable'] !== false)
{
unset($criteria[$col]);
$query[] = '(' . $db_col . ' IS NULL OR ' . $db_col . " = '')";
}
else
{
$query[$db_col] = '';
}
}
elseif ($wildcard || $criteria[$col][0] == '!' ||
is_string($criteria[$col]) && (strpos($criteria[$col],'*')!==false || strpos($criteria[$col],'?')!==false))
{
$cmp_op = ' '.$this->db->capabilities['case_insensitive_like'].' ';
@ -692,16 +706,14 @@ class so_sql
{
list($table,$only_col) = explode('.',$db_col);
$table_def = $this->db->get_table_definitions(true,$table);
if (is_array($val) && count($val) > 1)
{
array_walk($val,array($this->db,'quote'),$table_def['fd'][$only_col]['type']);
array_walk($val,array($this->db,'quote'),$this->table_def['fd'][$only_col]['type']);
$query[] = $sql = $db_col.' IN (' .implode(',',$val).')';
}
else
{
$query[] = $db_col.'='.$this->db->quote(is_array($val)?array_shift($val):$val,$table_def['fd'][$only_col]['type']);
$query[] = $db_col.'='.$this->db->quote(is_array($val)?array_shift($val):$val,$this->table_def['fd'][$only_col]['type']);
}
}
else
@ -857,6 +869,7 @@ class so_sql
$this->total = $this->db->query('SELECT FOUND_ROWS()')->fetchColumn();
}
$arr = array();
$n = 0;
if ($rs) foreach($rs as $row)
{
$data = array();