-
Notifications
You must be signed in to change notification settings - Fork 3
/
qlookup.php
77 lines (73 loc) · 2.94 KB
/
qlookup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
require_once 'qlookup.civix.php';
/**
* Implementation of hook_civicrm_config
*/
function qlookup_civicrm_config(&$config) {
_qlookup_civix_civicrm_config($config);
}
/**
* Alter fields for an event registration to make them into a demo form.
*/
function qlookup_civicrm_alterContent( &$content, $context, $tplName, &$object ) {
$pos = strpos($content, "#sort_name_navigation");
if ($pos === false) {
return;//no navigation menu
}
$url = CRM_Utils_System::url('civicrm/contact/view', 'reset=1')."&cid=";
$url = str_replace('&', '&', $url);
$str =
<<<'EOD'
<style>
.ac_results span.email {display:block;float:right;padding_left:10px;}
</style>
<script>
(function($,url){
$(function(){
$(".crm-quickSearchField").closest("ul").remove();
$("#sort_name_navigation").crmAutocomplete({action:"getttpquick"}, {
minChars:3,
width:450,
formatItem:function(data,i,max,value,term) {
if (typeof data["email"] != "undefined")
return data["sort_name"] + "<span class='email'>" + data["email"]+"</span>";
else
return data["sort_name"];
},
result:function(data){
if (data && data.id) {
document.location=url+data.id;
}
}
});
$('#id_search_block').submit (function() {
var q=$("#sort_name_navigation").val();
if (!isNaN(parseFloat(q)) && isFinite(q)) {
document.location=url+q;
return false;
}
});
});
EOD;
// wrapped in an anonymous fct that has cj and the contact view url as param
$str = $str."\n})(cj,'$url');</script>";
$content = str_replace ("#sort_name_navigation","#disabled_by_qloockup",$content).$str;
}
function aaqlookup_civicrm_contactListQuery ( &$query, $name, $context, $id ) {
//@TODO
// search on the email if name contains @
// if name contains a space, split, sort the word(s) by length and select smaller from select longer
// if nb words >2, skip the smallest
if ($context == 'navigation') {
$limit=11;
// @TODO if the search result < 11 (the limit of the autocomplete, replace the query by LIKE '%name%)
if (strlen ($name) > $fastSearchLimit) {
$query = "(SELECT c.sort_name as data, c.id FROM civicrm_contact c WHERE is_deleted=0 AND c.sort_name LIKE '$name%' OR
c.first_name LIKE '$name%' ORDER BY sort_name limit $limit ) UNION
(SELECT c.sort_name as data, c.id FROM civicrm_contact c, civicrm_email m where c.is_deleted=0 AND c.id = m.contact_id AND email like '$name%' ORDER BY sort_name limit $limit )";
//feels like it works better by searching on all emails instead of only the primary one. IMMV
} else { // we assume that more than 25 will come up on the first or last name before having to dig on the email too (union are expensive)
$query = "SELECT c.sort_name as data, c.id FROM civicrm_contact c WHERE is_deleted=0 AND c.sort_name LIKE '$name%' OR c.first_name LIKE '$name%' ORDER BY sort_name LIMIT ".$limit;
}
}
}