-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcivicrm_addressbook_instance.php
318 lines (293 loc) · 9.92 KB
/
civicrm_addressbook_instance.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
class civicrm_addressbook_instance extends rcube_addressbook {
public $primary_key = 'ID';
public $groups = true;
public $readonly = true;
public $ready = true;
public $list_page = 1;
public $page_size = 50;
public $sort_col = 'name';
public $sort_order = 'ASC';
public $date_cols = array();
public $coltypes = array(
'name' => array('limit'=>1),
'email' => array('limit'=>1),
'phone' => array('limit'=>1),
);
private $filter;
private $result;
private $gid = 0;
/**
* Returns addressbook name (e.g. for addressbooks listing)
*/
function get_name() {
return 'civicrm';
}
/**
* Save a search string for future listings
*
* @param mixed Search params to use in listing method, obtained by get_search_set()
*/
function set_search_set($filter) {
$this->filter = $filter;
}
/**
* Getter for saved search properties
*
* @return mixed Search properties used by this class
*/
function get_search_set() {
return $this->filter;
}
/**
* Reset saved results and search parameters
*/
function reset() {
$this->result = null;
$this->filter = null;
}
/**
* List the current set of contact records
*
* @param array List of cols to show
* @param int Only return this number of records, use negative values for tail
* @return array Indexed list of contact records, each a hash array
*/
function list_records($cols=null, $subset=0) {
$db = $this->getDb();
$result = $this->count();
$offset = $this->page_size * ($this->list_page-1);
$clause = $this->filter ? (' (' . $this->filter . ') '):' 1 ';
$groupJoin = '';
$groupClause = '';
if (!empty($this->gid)) {
$groupJoin = " INNER JOIN civicrm_group_contact ON civicrm_group_contact.contact_id = civicrm_contact.id ";
$groupClause = " AND (civicrm_group_contact.status = 'Added' AND civicrm_group_contact.group_id = ".$db->escape($this->gid)." )";
}
$db->query("
SELECT civicrm_contact.id as id, civicrm_contact.display_name, civicrm_email.email, civicrm_phone.phone
FROM civicrm_contact
INNER JOIN civicrm_email ON civicrm_contact.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1
{$groupJoin}
LEFT JOIN civicrm_phone ON civicrm_contact.id = civicrm_phone.contact_id AND civicrm_phone.is_primary = 1
WHERE {$clause}
{$groupClause}
AND civicrm_contact.is_deleted = 0 AND civicrm_contact.is_deceased = 0
ORDER BY civicrm_contact.display_name, civicrm_email.email
LIMIT {$offset}, {$this->page_size}
");
while ($sql_arr = $db->fetch_assoc()) {
$record = array(
'ID' => $sql_arr['id'],
'name' => $sql_arr['display_name'],
'email' => $sql_arr['email'],
'phone' => $sql_arr['phone'],
);
$result->add($record);
}
return $result;
}
/**
* Search records
*
* @param array List of fields to search in
* @param string Search value
* @param int Search mode. Sum of self::SEARCH_*.
* @param boolean True if results are requested, False if count only
* @param boolean True to skip the count query (select only)
* @param array List of fields that cannot be empty
*
* @return object rcube_result_set List of contact records and 'count' value
*/
function search($fields, $value, $mode=0, $select=true, $nocount=false, $required=array()) {
$db = $this->getDb();
if ($fields == '*') {
$this->set_search_set($db->ilike('civicrm_contact.display_name', $value . '%')." OR ".$db->ilike('civicrm_email.email', $value . '%'));
} elseif ($fields == 'ID') {
$ids = !is_array($value) ? explode(self::SEPARATOR, $value) : $value;
$ids = $db->array2list($ids, 'integer');
$this->set_search_set('`civicrm_contact`.`id` IN ('.$ids.')');
} else {
$wheres = array();
foreach($fields as $fieldName) {
switch($fieldName) {
case 'name':
$wheres[] = $db->ilike('civicrm_contact.display_name', $value . '%');
break;
case 'email':
$wheres[] = $db->ilike('civicrm_email.email', $value . '%');
break;
}
}
if (count($wheres)) {
$this->set_search_set(join(' OR ', $wheres));
}
}
return $this->list_records();
}
/**
* Count number of available contacts in database
*
* @return rcube_result_set Result set with values for 'count' and 'first'
*/
function count() {
$db = $this->getDb();
$clause = $this->filter ? (' (' . $this->filter . ') '):' 1 ';
$groupJoin = '';
$groupClause = '';
if (!empty($this->gid)) {
$groupJoin = " INNER JOIN civicrm_group_contact ON civicrm_group_contact.contact_id = civicrm_contact.id ";
$groupClause = " AND (civicrm_group_contact.status = 'Added' AND civicrm_group_contact.group_id = ".$db->escape($this->gid)." )";
}
$db->query("
SELECT COUNT(*) as total
FROM civicrm_contact
INNER JOIN civicrm_email ON civicrm_contact.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1
{$groupJoin}
LEFT JOIN civicrm_phone ON civicrm_contact.id = civicrm_phone.contact_id AND civicrm_phone.is_primary = 1
WHERE {$clause}
{$groupClause}
AND civicrm_contact.is_deleted = 0 AND civicrm_contact.is_deceased = 0
ORDER BY civicrm_contact.display_name, civicrm_email.email
");
if ($sql_arr = $db->fetch_assoc()) {
$count = $sql_arr['total'];
}
return new rcube_result_set($count, ($this->list_page-1) * $this->page_size);
}
/**
* Return the last result set
*
* @return rcube_result_set Current result set or NULL if nothing selected yet
*/
function get_result() {
return $this->result;
}
/**
* Get a specific contact record
*
* @param mixed Record identifier(s)
* @param boolean True to return record as associative array, otherwise a result set is returned
*
* @return rcube_result_set|array Result object with all record fields
*/
function get_record($id, $assoc=false) {
$this->result = new rcube_result_set(0);
$record = false;
$db = $this->getDb();
$db->query("
SELECT civicrm_contact.id as id, civicrm_contact.display_name, civicrm_email.email, civicrm_phone.phone
FROM civicrm_contact
INNER JOIN civicrm_email ON civicrm_contact.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1
LEFT JOIN civicrm_phone ON civicrm_contact.id = civicrm_phone.contact_id AND civicrm_phone.is_primary = 1
WHERE civicrm_contact.is_deleted = 0 AND civicrm_contact.is_deceased = 0 AND civicrm_contact.id=? ", $id);
if ($sql_arr = $db->fetch_assoc()) {
$this->result = new rcube_result_set(0);
$record = array(
'ID' => $sql_arr['id'],
'name' => $sql_arr['display_name'],
'email' => $sql_arr['email'],
'phone' => $sql_arr['phone'],
);
$this->result->add($record);
}
return $assoc && $record ? $record : $this->result;
}
/**
* Setter for the current group
* (empty, has to be re-implemented by extending class)
*/
function set_group($gid) {
$this->gid = $gid;
}
/**
* List all active contact groups of this source
*
* @param string Optional search string to match group name
* @param int Search mode. Sum of self::SEARCH_*
*
* @return array Indexed list of contact groups, each a hash array
*/
function list_groups($search = null, $mode = 0)
{
$groups = array();
$db = $this->getDb();
$clause = "";
if ($search) {
$clause = " AND " . $db->ilike('title', $search . '%');
}
$db->query("SELECT * FROM civicrm_group WHERE is_active = 1 and is_hidden = 0 {$clause} ORDER BY title");
while ($sql_arr = $db->fetch_assoc()) {
$record = array(
'ID' => $sql_arr['id'],
'name' => $sql_arr['title']
);
$groups[] = $record;
}
return $groups;
}
/**
* Get group properties such as name and email address(es)
*
* @param string Group identifier
* @return array Group properties as hash array
*/
function get_group($group_id)
{
$groups = array();
$db = $this->getDb();
$db->query("
SELECT civicrm_contact.id as id, civicrm_contact.display_name, civicrm_email.email, civicrm_phone.phone
FROM civicrm_contact
INNER JOIN civicrm_email ON civicrm_contact.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1
INNER JOIN civicrm_group_contact ON civicrm_group_contact.contact_id = civicrm_contact.id
INNER JOIN civicrm_group ON civicrm_group.id = civicrm_group_contact.group_id
LEFT JOIN civicrm_phone ON civicrm_contact.id = civicrm_phone.contact_id AND civicrm_phone.is_primary = 1
WHERE civicrm_group.is_active = 1
AND civicrm_group_contact.status = 'Added'
AND civicrm_group.id=?
ORDER BY title", $group_id);
while ($sql_arr = $db->fetch_assoc()) {
$record = array(
'ID' => $sql_arr['id'],
'name' => $sql_arr['display_name'],
'email' => $sql_arr['email'],
'phone' => $sql_arr['phone'],
);
$groups[$group_id][] = $record;
}
return $groups;
}
/**
* Get group assignments of a specific contact record
*
* @param mixed Record identifier
*
* @return array List of assigned groups as ID=>Name pairs
* @since 0.5-beta
*/
function get_record_groups($id)
{
$groups = array();
$db = $this->getDb();
$db->query("
SELECT civicrm_group.title, civicrm_group.id
FROM civicrm_group_contact
INNER JOIN civicrm_group ON civicrm_group.id = civicrm_group_contact.group_id
WHERE civicrm_group.is_active = 1
AND civicrm_group_contact.status = 'Added'
AND civicrm_group_contact.contact_id=?
ORDER BY title", $id);
while ($sql_arr = $db->fetch_assoc()) {
$groups[$sql_arr['id']] = $sql_arr['title'];
}
return $groups;
}
private function getDb() {
$rcmail = rcmail::get_instance();
$dsn = $rcmail->config->get('civicrm_db_dsn');
$db = rcube_db::factory($dsn, '', false);
return $db;
}
}
?>