diff --git a/core/users/user_edit.php b/core/users/user_edit.php index bbee5b0db2..3d05670c86 100644 --- a/core/users/user_edit.php +++ b/core/users/user_edit.php @@ -730,13 +730,33 @@ echo " "; echo " ".$text['label-contact'].""; echo " \n"; - $sql = "select contact_uuid, contact_organization, contact_name_given, contact_name_family, contact_nickname from v_contacts "; - $sql .= "where domain_uuid = '".escape($domain_uuid)."' "; - $sql .= "and ( "; - $sql .= " contact_uuid = '".escape($contact_uuid)."' or "; - $sql .= " contact_uuid not in (select contact_uuid from v_users where domain_uuid = '".escape($domain_uuid)."') "; + $sql = "select "; + $sql .= "c.contact_uuid, "; + $sql .= "c.contact_organization, "; + $sql .= "c.contact_name_given, "; + $sql .= "c.contact_name_family, "; + $sql .= "c.contact_nickname "; + $sql .= "from "; + $sql .= "v_contacts as c "; + $sql .= "where "; + $sql .= "c.domain_uuid = '".escape($domain_uuid)."' "; + $sql .= "and not exists ( "; + $sql .= " select "; + $sql .= " contact_uuid "; + $sql .= " from "; + $sql .= " v_users as u "; + $sql .= " where "; + $sql .= " u.domain_uuid = '".escape($domain_uuid)."' "; + if (is_uuid($contact_uuid)) { //don't exclude currently assigned contact + $sql .= "and u.contact_uuid <> '".escape($contact_uuid)."' "; + } + $sql .= " and u.contact_uuid = c.contact_uuid "; $sql .= ") "; - $sql .= "order by contact_organization desc, contact_name_family asc, contact_name_given asc, contact_nickname asc "; + $sql .= "order by "; + $sql .= "lower(c.contact_organization) asc, "; + $sql .= "lower(c.contact_name_family) asc, "; + $sql .= "lower(c.contact_name_given) asc, "; + $sql .= "lower(c.contact_nickname) asc "; $prep_statement = $db->prepare(check_sql($sql)); $prep_statement->execute(); $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);