Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | class CRM_Contact_BAO_ContactType extends CRM_Contact_DAO_ContactType { | |
34 | ||
35 | /** | |
fe482240 | 36 | * Fetch object based on array of properties. |
6a488035 | 37 | * |
77c5b619 TO |
38 | * @param array $params |
39 | * (reference ) an assoc array of name/value pairs. | |
40 | * @param array $defaults | |
41 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 42 | * |
16b10e64 CW |
43 | * @return CRM_Contact_BAO_ContactType|null |
44 | * object on success, null otherwise | |
6a488035 | 45 | */ |
00be9182 | 46 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
47 | $contactType = new CRM_Contact_DAO_ContactType(); |
48 | $contactType->copyValues($params); | |
49 | if ($contactType->find(TRUE)) { | |
50 | CRM_Core_DAO::storeValues($contactType, $defaults); | |
51 | return $contactType; | |
52 | } | |
53 | return NULL; | |
54 | } | |
55 | ||
86538308 | 56 | /** |
db01bf2f | 57 | * Is this contact type active. |
58 | * | |
59 | * @param string $contactType | |
86538308 EM |
60 | * |
61 | * @return bool | |
62 | */ | |
00be9182 | 63 | public static function isActive($contactType) { |
6a488035 TO |
64 | $contact = self::contactTypeInfo(FALSE); |
65 | $active = array_key_exists($contactType, $contact) ? TRUE : FALSE; | |
66 | return $active; | |
67 | } | |
68 | ||
69 | /** | |
c490a46a | 70 | * Retrieve basic contact type information. |
6a488035 | 71 | * |
dd244018 | 72 | * @param bool $all |
6a488035 | 73 | * |
a6c01b45 | 74 | * @return array |
16b10e64 | 75 | * Array of basic contact types information. |
6a488035 | 76 | */ |
93ecb8e8 | 77 | public static function basicTypeInfo($all = FALSE) { |
6a488035 TO |
78 | static $_cache = NULL; |
79 | ||
80 | if ($_cache === NULL) { | |
be2fb01f | 81 | $_cache = []; |
6a488035 TO |
82 | } |
83 | ||
84 | $argString = $all ? 'CRM_CT_BTI_1' : 'CRM_CT_BTI_0'; | |
85 | if (!array_key_exists($argString, $_cache)) { | |
86 | $cache = CRM_Utils_Cache::singleton(); | |
87 | $_cache[$argString] = $cache->get($argString); | |
88 | if (!$_cache[$argString]) { | |
89 | $sql = " | |
90 | SELECT * | |
91 | FROM civicrm_contact_type | |
92 | WHERE parent_id IS NULL | |
93 | "; | |
94 | if ($all === FALSE) { | |
95 | $sql .= " AND is_active = 1"; | |
96 | } | |
97 | ||
be2fb01f | 98 | $params = []; |
6a488035 | 99 | $dao = CRM_Core_DAO::executeQuery($sql, |
8e74c5fa | 100 | $params, |
6a488035 TO |
101 | FALSE, |
102 | 'CRM_Contact_DAO_ContactType' | |
103 | ); | |
104 | while ($dao->fetch()) { | |
be2fb01f | 105 | $value = []; |
6a488035 TO |
106 | CRM_Core_DAO::storeValues($dao, $value); |
107 | $_cache[$argString][$dao->name] = $value; | |
108 | } | |
109 | ||
110 | $cache->set($argString, $_cache[$argString]); | |
111 | } | |
112 | } | |
113 | return $_cache[$argString]; | |
114 | } | |
115 | ||
116 | /** | |
c490a46a | 117 | * Retrieve all basic contact types. |
6a488035 | 118 | * |
da6b46f4 | 119 | * @param bool $all |
6a488035 | 120 | * |
a6c01b45 | 121 | * @return array |
16b10e64 | 122 | * Array of basic contact types |
6a488035 | 123 | */ |
00be9182 | 124 | public static function basicTypes($all = FALSE) { |
6a488035 TO |
125 | return array_keys(self::basicTypeInfo($all)); |
126 | } | |
127 | ||
86538308 EM |
128 | /** |
129 | * @param bool $all | |
130 | * @param string $key | |
131 | * | |
132 | * @return array | |
133 | */ | |
00be9182 | 134 | public static function basicTypePairs($all = FALSE, $key = 'name') { |
6a488035 TO |
135 | $subtypes = self::basicTypeInfo($all); |
136 | ||
be2fb01f | 137 | $pairs = []; |
6a488035 TO |
138 | foreach ($subtypes as $name => $info) { |
139 | $index = ($key == 'name') ? $name : $info[$key]; | |
140 | $pairs[$index] = $info['label']; | |
141 | } | |
142 | return $pairs; | |
143 | } | |
144 | ||
145 | /** | |
c490a46a | 146 | * Retrieve all subtypes Information. |
6a488035 | 147 | * |
77c5b619 TO |
148 | * @param array $contactType |
149 | * .. | |
fd31fa4c EM |
150 | * @param bool $all |
151 | * @param bool $ignoreCache | |
152 | * @param bool $reset | |
6a488035 | 153 | * |
a6c01b45 | 154 | * @return array |
16b10e64 | 155 | * Array of sub type information |
6a488035 | 156 | */ |
93ecb8e8 | 157 | public static function subTypeInfo($contactType = NULL, $all = FALSE, $ignoreCache = FALSE, $reset = FALSE) { |
6a488035 TO |
158 | static $_cache = NULL; |
159 | ||
160 | if ($reset === TRUE) { | |
161 | $_cache = NULL; | |
162 | } | |
163 | ||
164 | if ($_cache === NULL) { | |
be2fb01f | 165 | $_cache = []; |
6a488035 TO |
166 | } |
167 | if ($contactType && !is_array($contactType)) { | |
be2fb01f | 168 | $contactType = [$contactType]; |
6a488035 TO |
169 | } |
170 | ||
171 | $argString = $all ? 'CRM_CT_STI_1_' : 'CRM_CT_STI_0_'; | |
172 | if (!empty($contactType)) { | |
173 | $argString .= implode('_', $contactType); | |
174 | } | |
175 | ||
176 | if ((!array_key_exists($argString, $_cache)) || $ignoreCache) { | |
177 | $cache = CRM_Utils_Cache::singleton(); | |
178 | $_cache[$argString] = $cache->get($argString); | |
179 | if (!$_cache[$argString] || $ignoreCache) { | |
be2fb01f | 180 | $_cache[$argString] = []; |
6a488035 TO |
181 | |
182 | $ctWHERE = ''; | |
183 | if (!empty($contactType)) { | |
184 | $ctWHERE = " AND parent.name IN ('" . implode("','", $contactType) . "')"; | |
185 | } | |
186 | ||
187 | $sql = " | |
188 | SELECT subtype.*, parent.name as parent, parent.label as parent_label | |
189 | FROM civicrm_contact_type subtype | |
190 | INNER JOIN civicrm_contact_type parent ON subtype.parent_id = parent.id | |
191 | WHERE subtype.name IS NOT NULL AND subtype.parent_id IS NOT NULL {$ctWHERE} | |
192 | "; | |
193 | if ($all === FALSE) { | |
194 | $sql .= " AND subtype.is_active = 1 AND parent.is_active = 1 ORDER BY parent.id"; | |
195 | } | |
be2fb01f | 196 | $dao = CRM_Core_DAO::executeQuery($sql, [], |
6a488035 TO |
197 | FALSE, 'CRM_Contact_DAO_ContactType' |
198 | ); | |
199 | while ($dao->fetch()) { | |
be2fb01f | 200 | $value = []; |
6a488035 TO |
201 | CRM_Core_DAO::storeValues($dao, $value); |
202 | $value['parent'] = $dao->parent; | |
203 | $value['parent_label'] = $dao->parent_label; | |
204 | $_cache[$argString][$dao->name] = $value; | |
205 | } | |
206 | ||
207 | $cache->set($argString, $_cache[$argString]); | |
208 | } | |
209 | } | |
210 | return $_cache[$argString]; | |
211 | } | |
212 | ||
213 | /** | |
214 | * | |
c490a46a | 215 | * retrieve all subtypes |
6a488035 | 216 | * |
77c5b619 TO |
217 | * @param array $contactType |
218 | * .. | |
f4aaa82a EM |
219 | * @param bool $all |
220 | * @param string $columnName | |
221 | * @param bool $ignoreCache | |
6a488035 | 222 | * |
a6c01b45 | 223 | * @return array |
16b10e64 CW |
224 | * all subtypes OR list of subtypes associated to |
225 | * a given basic contact type | |
6a488035 | 226 | */ |
00be9182 | 227 | public static function subTypes($contactType = NULL, $all = FALSE, $columnName = 'name', $ignoreCache = FALSE) { |
6a488035 TO |
228 | if ($columnName == 'name') { |
229 | return array_keys(self::subTypeInfo($contactType, $all, $ignoreCache)); | |
230 | } | |
231 | else { | |
232 | return array_values(self::subTypePairs($contactType, FALSE, NULL, $ignoreCache)); | |
233 | } | |
234 | } | |
235 | ||
236 | /** | |
237 | * | |
c490a46a | 238 | * retrieve subtype pairs with name as 'subtype-name' and 'label' as value |
6a488035 | 239 | * |
77c5b619 | 240 | * @param array $contactType |
f4aaa82a EM |
241 | * @param bool $all |
242 | * @param string $labelPrefix | |
243 | * @param bool $ignoreCache | |
6a488035 | 244 | * |
72b3a70c CW |
245 | * @return array |
246 | * list of subtypes with name as 'subtype-name' and 'label' as value | |
6a488035 | 247 | */ |
00be9182 | 248 | public static function subTypePairs($contactType = NULL, $all = FALSE, $labelPrefix = '- ', $ignoreCache = FALSE) { |
6a488035 TO |
249 | $subtypes = self::subTypeInfo($contactType, $all, $ignoreCache); |
250 | ||
be2fb01f | 251 | $pairs = []; |
6a488035 TO |
252 | foreach ($subtypes as $name => $info) { |
253 | $pairs[$name] = $labelPrefix . $info['label']; | |
254 | } | |
255 | return $pairs; | |
256 | } | |
257 | ||
258 | /** | |
259 | * | |
c490a46a | 260 | * retrieve list of all types i.e basic + subtypes. |
6a488035 | 261 | * |
f4aaa82a | 262 | * @param bool $all |
6a488035 | 263 | * |
a6c01b45 | 264 | * @return array |
16b10e64 | 265 | * Array of basic types + all subtypes. |
6a488035 | 266 | */ |
00be9182 | 267 | public static function contactTypes($all = FALSE) { |
6a488035 TO |
268 | return array_keys(self::contactTypeInfo($all)); |
269 | } | |
270 | ||
271 | /** | |
db01bf2f | 272 | * Retrieve info array about all types i.e basic + subtypes. |
6a488035 | 273 | * |
f4aaa82a EM |
274 | * @param bool $all |
275 | * @param bool $reset | |
6a488035 | 276 | * |
a6c01b45 | 277 | * @return array |
16b10e64 | 278 | * Array of basic types + all subtypes. |
6a488035 | 279 | */ |
00be9182 | 280 | public static function contactTypeInfo($all = FALSE, $reset = FALSE) { |
6a488035 TO |
281 | static $_cache = NULL; |
282 | ||
283 | if ($reset === TRUE) { | |
284 | $_cache = NULL; | |
285 | } | |
286 | ||
287 | if ($_cache === NULL) { | |
be2fb01f | 288 | $_cache = []; |
6a488035 TO |
289 | } |
290 | ||
291 | $argString = $all ? 'CRM_CT_CTI_1' : 'CRM_CT_CTI_0'; | |
292 | if (!array_key_exists($argString, $_cache)) { | |
293 | $cache = CRM_Utils_Cache::singleton(); | |
294 | $_cache[$argString] = $cache->get($argString); | |
295 | if (!$_cache[$argString]) { | |
be2fb01f | 296 | $_cache[$argString] = []; |
6a488035 TO |
297 | |
298 | $sql = " | |
299 | SELECT type.*, parent.name as parent, parent.label as parent_label | |
300 | FROM civicrm_contact_type type | |
301 | LEFT JOIN civicrm_contact_type parent ON type.parent_id = parent.id | |
302 | WHERE type.name IS NOT NULL | |
303 | "; | |
304 | if ($all === FALSE) { | |
305 | $sql .= " AND type.is_active = 1"; | |
306 | } | |
307 | ||
308 | $dao = CRM_Core_DAO::executeQuery($sql, | |
be2fb01f | 309 | [], |
6a488035 TO |
310 | FALSE, |
311 | 'CRM_Contact_DAO_ContactType' | |
312 | ); | |
313 | while ($dao->fetch()) { | |
be2fb01f | 314 | $value = []; |
6a488035 TO |
315 | CRM_Core_DAO::storeValues($dao, $value); |
316 | if (array_key_exists('parent_id', $value)) { | |
317 | $value['parent'] = $dao->parent; | |
318 | $value['parent_label'] = $dao->parent_label; | |
319 | } | |
320 | $_cache[$argString][$dao->name] = $value; | |
321 | } | |
322 | ||
323 | $cache->set($argString, $_cache[$argString]); | |
324 | } | |
325 | } | |
326 | ||
327 | return $_cache[$argString]; | |
328 | } | |
329 | ||
330 | /** | |
db01bf2f | 331 | * Retrieve basic type pairs with name as 'built-in name' and 'label' as value. |
6a488035 | 332 | * |
f4aaa82a EM |
333 | * @param bool $all |
334 | * @param null $typeName | |
335 | * @param null $delimiter | |
6a488035 | 336 | * |
a6c01b45 | 337 | * @return array |
16b10e64 | 338 | * Array of basictypes with name as 'built-in name' and 'label' as value |
6a488035 | 339 | */ |
00be9182 | 340 | public static function contactTypePairs($all = FALSE, $typeName = NULL, $delimiter = NULL) { |
6a488035 TO |
341 | $types = self::contactTypeInfo($all); |
342 | ||
343 | if ($typeName && !is_array($typeName)) { | |
344 | $typeName = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($typeName, CRM_Core_DAO::VALUE_SEPARATOR)); | |
345 | } | |
346 | ||
be2fb01f | 347 | $pairs = []; |
6a488035 TO |
348 | if ($typeName) { |
349 | foreach ($typeName as $type) { | |
350 | if (array_key_exists($type, $types)) { | |
351 | $pairs[$type] = $types[$type]['label']; | |
352 | } | |
353 | } | |
354 | } | |
355 | else { | |
356 | foreach ($types as $name => $info) { | |
357 | $pairs[$name] = $info['label']; | |
358 | } | |
359 | } | |
360 | ||
361 | return !$delimiter ? $pairs : implode($delimiter, $pairs); | |
362 | } | |
363 | ||
b500fbea | 364 | /** |
fe482240 | 365 | * Get a list of elements for select box. |
b500fbea EM |
366 | * Note that this used to default to using the hex(01) character - which results in an invalid character being used in form fields |
367 | * which was not handled well be anything that loaded & resaved the html (outside core) | |
368 | * The use of this separator is now explicit in the calling functions as a step towards it's removal | |
369 | * | |
370 | * @param bool $all | |
371 | * @param bool $isSeparator | |
372 | * @param string $separator | |
373 | * | |
374 | * @return mixed | |
375 | */ | |
bed98343 | 376 | public static function getSelectElements( |
51ccfbbe | 377 | $all = FALSE, |
b500fbea EM |
378 | $isSeparator = TRUE, |
379 | $separator = '__' | |
6a488035 TO |
380 | ) { |
381 | static $_cache = NULL; | |
382 | ||
383 | if ($_cache === NULL) { | |
be2fb01f | 384 | $_cache = []; |
6a488035 TO |
385 | } |
386 | ||
387 | $argString = $all ? 'CRM_CT_GSE_1' : 'CRM_CT_GSE_0'; | |
b500fbea | 388 | $argString .= $isSeparator ? '_1' : '_0'; |
e9567ca3 | 389 | $argString .= $separator; |
77f080cb | 390 | $argString = CRM_Utils_Cache::cleanKey($argString); |
6a488035 TO |
391 | if (!array_key_exists($argString, $_cache)) { |
392 | $cache = CRM_Utils_Cache::singleton(); | |
393 | $_cache[$argString] = $cache->get($argString); | |
394 | ||
395 | if (!$_cache[$argString]) { | |
be2fb01f | 396 | $_cache[$argString] = []; |
6a488035 TO |
397 | |
398 | $sql = " | |
399 | SELECT c.name as child_name , c.label as child_label , c.id as child_id, | |
400 | p.name as parent_name, p.label as parent_label, p.id as parent_id | |
401 | FROM civicrm_contact_type c | |
402 | LEFT JOIN civicrm_contact_type p ON ( c.parent_id = p.id ) | |
403 | WHERE ( c.name IS NOT NULL ) | |
404 | "; | |
405 | ||
406 | if ($all === FALSE) { | |
407 | $sql .= " | |
408 | AND c.is_active = 1 | |
409 | AND ( p.is_active = 1 OR p.id IS NULL ) | |
410 | "; | |
411 | } | |
412 | $sql .= " ORDER BY c.id"; | |
413 | ||
be2fb01f | 414 | $values = []; |
6a488035 TO |
415 | $dao = CRM_Core_DAO::executeQuery($sql); |
416 | while ($dao->fetch()) { | |
417 | if (!empty($dao->parent_id)) { | |
353ffa53 | 418 | $key = $isSeparator ? $dao->parent_name . $separator . $dao->child_name : $dao->child_name; |
bee6039a | 419 | $label = "- {$dao->child_label}"; |
6a488035 TO |
420 | $pName = $dao->parent_name; |
421 | } | |
422 | else { | |
353ffa53 | 423 | $key = $dao->child_name; |
6a488035 TO |
424 | $label = $dao->child_label; |
425 | $pName = $dao->child_name; | |
426 | } | |
427 | ||
428 | if (!isset($values[$pName])) { | |
be2fb01f | 429 | $values[$pName] = []; |
6a488035 | 430 | } |
be2fb01f | 431 | $values[$pName][] = ['key' => $key, 'label' => $label]; |
6a488035 TO |
432 | } |
433 | ||
be2fb01f | 434 | $selectElements = []; |
6a488035 TO |
435 | foreach ($values as $pName => $elements) { |
436 | foreach ($elements as $element) { | |
437 | $selectElements[$element['key']] = $element['label']; | |
438 | } | |
439 | } | |
440 | $_cache[$argString] = $selectElements; | |
441 | ||
442 | $cache->set($argString, $_cache[$argString]); | |
443 | } | |
444 | } | |
445 | return $_cache[$argString]; | |
446 | } | |
447 | ||
448 | /** | |
fe482240 | 449 | * Check if a given type is a subtype. |
6a488035 | 450 | * |
77c5b619 TO |
451 | * @param string $subType |
452 | * Contact subType. | |
f4aaa82a | 453 | * @param bool $ignoreCache |
6a488035 | 454 | * |
bed98343 | 455 | * @return bool |
a6c01b45 | 456 | * true if subType, false otherwise. |
6a488035 | 457 | */ |
00be9182 | 458 | public static function isaSubType($subType, $ignoreCache = FALSE) { |
6a488035 TO |
459 | return in_array($subType, self::subTypes(NULL, TRUE, 'name', $ignoreCache)); |
460 | } | |
461 | ||
462 | /** | |
100fef9d | 463 | * Retrieve the basic contact type associated with given subType. |
6a488035 | 464 | * |
683bf891 SL |
465 | * @param array|string $subType contact subType. |
466 | * @return array|string | |
467 | * basicTypes. | |
6a488035 | 468 | */ |
00be9182 | 469 | public static function getBasicType($subType) { |
6a488035 TO |
470 | static $_cache = NULL; |
471 | if ($_cache === NULL) { | |
be2fb01f | 472 | $_cache = []; |
6a488035 TO |
473 | } |
474 | ||
475 | $isArray = TRUE; | |
476 | if ($subType && !is_array($subType)) { | |
be2fb01f | 477 | $subType = [$subType]; |
6a488035 TO |
478 | $isArray = FALSE; |
479 | } | |
480 | $argString = implode('_', $subType); | |
481 | ||
482 | if (!array_key_exists($argString, $_cache)) { | |
be2fb01f | 483 | $_cache[$argString] = []; |
6a488035 TO |
484 | |
485 | $sql = " | |
486 | SELECT subtype.name as contact_subtype, type.name as contact_type | |
487 | FROM civicrm_contact_type subtype | |
488 | INNER JOIN civicrm_contact_type type ON ( subtype.parent_id = type.id ) | |
489 | WHERE subtype.name IN ('" . implode("','", $subType) . "' )"; | |
490 | $dao = CRM_Core_DAO::executeQuery($sql); | |
491 | while ($dao->fetch()) { | |
492 | if (!$isArray) { | |
493 | $_cache[$argString] = $dao->contact_type; | |
494 | break; | |
495 | } | |
496 | $_cache[$argString][$dao->contact_subtype] = $dao->contact_type; | |
497 | } | |
498 | } | |
499 | return $_cache[$argString]; | |
500 | } | |
501 | ||
502 | /** | |
c490a46a | 503 | * Suppress all subtypes present in given array. |
6a488035 | 504 | * |
77c5b619 TO |
505 | * @param array $subTypes |
506 | * Contact subTypes. | |
f4aaa82a | 507 | * @param bool $ignoreCache |
6a488035 | 508 | * |
a6c01b45 | 509 | * @return array |
16b10e64 | 510 | * Array of suppressed subTypes. |
6a488035 | 511 | */ |
00be9182 | 512 | public static function suppressSubTypes(&$subTypes, $ignoreCache = FALSE) { |
6a488035 TO |
513 | $subTypes = array_diff($subTypes, self::subTypes(NULL, TRUE, 'name', $ignoreCache)); |
514 | return $subTypes; | |
515 | } | |
516 | ||
517 | /** | |
100fef9d | 518 | * Verify if a given subtype is associated with a given basic contact type. |
6a488035 | 519 | * |
77c5b619 TO |
520 | * @param string $subType |
521 | * Contact subType. | |
522 | * @param string $contactType | |
523 | * Contact Type. | |
f4aaa82a EM |
524 | * @param bool $ignoreCache |
525 | * @param string $columnName | |
6a488035 | 526 | * |
bed98343 | 527 | * @return bool |
a6c01b45 | 528 | * true if contact extends, false otherwise. |
6a488035 | 529 | */ |
00be9182 | 530 | public static function isExtendsContactType($subType, $contactType, $ignoreCache = FALSE, $columnName = 'name') { |
1b23469d | 531 | $subType = (array) CRM_Utils_Array::explodePadded($subType); |
6a488035 TO |
532 | $subtypeList = self::subTypes($contactType, TRUE, $columnName, $ignoreCache); |
533 | $intersection = array_intersect($subType, $subtypeList); | |
534 | return $subType == $intersection; | |
535 | } | |
536 | ||
537 | /** | |
fe482240 | 538 | * Create shortcuts menu for contactTypes. |
6a488035 | 539 | * |
a6c01b45 CW |
540 | * @return array |
541 | * of contactTypes | |
6a488035 | 542 | */ |
00be9182 | 543 | public static function getCreateNewList() { |
be2fb01f | 544 | $shortCuts = []; |
b500fbea EM |
545 | //@todo FIXME - using the CRM_Core_DAO::VALUE_SEPARATOR creates invalid html - if you can find the form |
546 | // this is loaded onto then replace with something like '__' & test | |
547 | $separator = CRM_Core_DAO::VALUE_SEPARATOR; | |
548 | $contactTypes = self::getSelectElements(FALSE, TRUE, $separator); | |
6a488035 TO |
549 | foreach ($contactTypes as $key => $value) { |
550 | if ($key) { | |
551 | $typeValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, $key); | |
7926b999 | 552 | $cType = CRM_Utils_Array::value('0', $typeValue); |
553 | $typeUrl = 'ct=' . $cType; | |
6a488035 TO |
554 | if ($csType = CRM_Utils_Array::value('1', $typeValue)) { |
555 | $typeUrl .= "&cst=$csType"; | |
556 | } | |
be2fb01f | 557 | $shortCut = [ |
6a488035 TO |
558 | 'path' => 'civicrm/contact/add', |
559 | 'query' => "$typeUrl&reset=1", | |
560 | 'ref' => "new-$value", | |
561 | 'title' => $value, | |
be2fb01f | 562 | ]; |
7926b999 | 563 | if ($csType = CRM_Utils_Array::value('1', $typeValue)) { |
564 | $shortCuts[$cType]['shortCuts'][] = $shortCut; | |
565 | } | |
566 | else { | |
567 | $shortCuts[$cType] = $shortCut; | |
568 | } | |
6a488035 TO |
569 | } |
570 | } | |
571 | return $shortCuts; | |
572 | } | |
573 | ||
574 | /** | |
fe482240 | 575 | * Delete Contact SubTypes. |
6a488035 | 576 | * |
77c5b619 TO |
577 | * @param int $contactTypeId |
578 | * ID of the Contact Subtype to be deleted. | |
6a488035 | 579 | * |
f4aaa82a | 580 | * @return bool |
6a488035 | 581 | */ |
00be9182 | 582 | public static function del($contactTypeId) { |
6a488035 TO |
583 | |
584 | if (!$contactTypeId) { | |
585 | return FALSE; | |
586 | } | |
587 | ||
be2fb01f | 588 | $params = ['id' => $contactTypeId]; |
6a488035 TO |
589 | self::retrieve($params, $typeInfo); |
590 | $name = $typeInfo['name']; | |
591 | // check if any custom group | |
592 | $custom = new CRM_Core_DAO_CustomGroup(); | |
593 | $custom->whereAdd("extends_entity_column_value LIKE '%" . | |
594 | CRM_Core_DAO::VALUE_SEPARATOR . | |
595 | $name . | |
596 | CRM_Core_DAO::VALUE_SEPARATOR . "%'" | |
597 | ); | |
598 | if ($custom->find()) { | |
599 | return FALSE; | |
600 | } | |
601 | ||
602 | // remove subtype for existing contacts | |
603 | $sql = " | |
604 | UPDATE civicrm_contact SET contact_sub_type = NULL | |
605 | WHERE contact_sub_type = '$name'"; | |
606 | CRM_Core_DAO::executeQuery($sql); | |
607 | ||
608 | // remove subtype from contact type table | |
609 | $contactType = new CRM_Contact_DAO_ContactType(); | |
610 | $contactType->id = $contactTypeId; | |
611 | $contactType->delete(); | |
612 | ||
613 | // remove navigation entry if any | |
614 | if ($name) { | |
615 | $sql = " | |
616 | DELETE | |
617 | FROM civicrm_navigation | |
618 | WHERE name = %1"; | |
be2fb01f | 619 | $params = [1 => ["New $name", 'String']]; |
6a488035 TO |
620 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
621 | CRM_Core_BAO_Navigation::resetNavigation(); | |
622 | } | |
623 | return TRUE; | |
624 | } | |
625 | ||
626 | /** | |
fe482240 | 627 | * Add or update Contact SubTypes. |
6a488035 | 628 | * |
77c5b619 TO |
629 | * @param array $params |
630 | * An assoc array of name/value pairs. | |
6a488035 | 631 | * |
bed98343 | 632 | * @return object|void |
6a488035 | 633 | */ |
00be9182 | 634 | public static function add(&$params) { |
6a488035 TO |
635 | |
636 | // label or name | |
0f77a625 | 637 | if (empty($params['id']) && empty($params['label'])) { |
bed98343 | 638 | return NULL; |
6a488035 | 639 | } |
a7488080 | 640 | if (!empty($params['parent_id']) && |
6a488035 TO |
641 | !CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_ContactType', $params['parent_id']) |
642 | ) { | |
bed98343 | 643 | return NULL; |
6a488035 TO |
644 | } |
645 | ||
646 | $contactType = new CRM_Contact_DAO_ContactType(); | |
647 | $contactType->copyValues($params); | |
648 | $contactType->id = CRM_Utils_Array::value('id', $params); | |
649 | $contactType->is_active = CRM_Utils_Array::value('is_active', $params, 0); | |
650 | ||
6a488035 TO |
651 | $contactType->save(); |
652 | if ($contactType->find(TRUE)) { | |
653 | $contactName = $contactType->name; | |
353ffa53 TO |
654 | $contact = ucfirst($contactType->label); |
655 | $active = $contactType->is_active; | |
6a488035 TO |
656 | } |
657 | ||
a7488080 | 658 | if (!empty($params['id'])) { |
be2fb01f | 659 | $newParams = [ |
6a488035 TO |
660 | 'label' => "New $contact", |
661 | 'is_active' => $active, | |
be2fb01f | 662 | ]; |
1237d8d7 | 663 | CRM_Core_BAO_Navigation::processUpdate(['name' => "New $contactName"], $newParams); |
6a488035 TO |
664 | } |
665 | else { | |
666 | $name = self::getBasicType($contactName); | |
667 | if (!$name) { | |
683bf891 | 668 | return NULL; |
6a488035 | 669 | } |
be2fb01f | 670 | $value = ['name' => "New $name"]; |
6a488035 | 671 | CRM_Core_BAO_Navigation::retrieve($value, $navinfo); |
be2fb01f | 672 | $navigation = [ |
6a488035 TO |
673 | 'label' => "New $contact", |
674 | 'name' => "New $contactName", | |
15a7ea79 | 675 | 'url' => "civicrm/contact/add?ct=$name&cst=$contactName&reset=1", |
6a488035 TO |
676 | 'permission' => 'add contacts', |
677 | 'parent_id' => $navinfo['id'], | |
678 | 'is_active' => $active, | |
be2fb01f | 679 | ]; |
6a488035 TO |
680 | CRM_Core_BAO_Navigation::add($navigation); |
681 | } | |
682 | CRM_Core_BAO_Navigation::resetNavigation(); | |
683 | ||
684 | // reset the cache after adding | |
685 | self::subTypeInfo(NULL, FALSE, FALSE, TRUE); | |
686 | ||
687 | return $contactType; | |
688 | } | |
689 | ||
690 | /** | |
fe482240 | 691 | * Update the is_active flag in the db. |
6a488035 | 692 | * |
77c5b619 TO |
693 | * @param int $id |
694 | * Id of the database record. | |
695 | * @param bool $is_active | |
696 | * Value we want to set the is_active field. | |
6a488035 | 697 | * |
8a4fede3 | 698 | * @return bool |
699 | * true if we found and updated the object, else false | |
6a488035 | 700 | */ |
00be9182 | 701 | public static function setIsActive($id, $is_active) { |
be2fb01f | 702 | $params = ['id' => $id]; |
6a488035 | 703 | self::retrieve($params, $contactinfo); |
be2fb01f CW |
704 | $params = ['name' => "New $contactinfo[name]"]; |
705 | $newParams = ['is_active' => $is_active]; | |
6a488035 TO |
706 | CRM_Core_BAO_Navigation::processUpdate($params, $newParams); |
707 | CRM_Core_BAO_Navigation::resetNavigation(); | |
708 | return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_ContactType', $id, | |
709 | 'is_active', $is_active | |
710 | ); | |
711 | } | |
712 | ||
86538308 | 713 | /** |
100fef9d | 714 | * @param string $typeName |
86538308 EM |
715 | * |
716 | * @return mixed | |
717 | */ | |
00be9182 | 718 | public static function getLabel($typeName) { |
6a488035 TO |
719 | $types = self::contactTypeInfo(TRUE); |
720 | ||
721 | if (array_key_exists($typeName, $types)) { | |
722 | return $types[$typeName]['label']; | |
723 | } | |
724 | return $typeName; | |
725 | } | |
726 | ||
727 | /** | |
100fef9d | 728 | * Check whether allow to change any contact's subtype |
6a488035 TO |
729 | * on the basis of custom data and relationship of specific subtype |
730 | * currently used in contact/edit form amd in import validation | |
731 | * | |
77c5b619 TO |
732 | * @param int $contactId |
733 | * Contact id. | |
734 | * @param string $subType | |
735 | * Subtype. | |
6a488035 | 736 | * |
bed98343 | 737 | * @return bool |
6a488035 | 738 | */ |
00be9182 | 739 | public static function isAllowEdit($contactId, $subType = NULL) { |
6a488035 TO |
740 | |
741 | if (!$contactId) { | |
742 | return TRUE; | |
743 | } | |
744 | ||
745 | if (empty($subType)) { | |
746 | $subType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
747 | $contactId, | |
748 | 'contact_sub_type' | |
749 | ); | |
750 | } | |
751 | ||
752 | if (self::hasCustomData($subType, $contactId) || self::hasRelationships($contactId, $subType)) { | |
753 | return FALSE; | |
754 | } | |
755 | ||
756 | return TRUE; | |
757 | } | |
758 | ||
86538308 EM |
759 | /** |
760 | * @param $contactType | |
100fef9d | 761 | * @param int $contactId |
86538308 EM |
762 | * |
763 | * @return bool | |
764 | */ | |
00be9182 | 765 | public static function hasCustomData($contactType, $contactId = NULL) { |
6a488035 TO |
766 | $subTypeClause = ''; |
767 | ||
768 | if (self::isaSubType($contactType)) { | |
769 | $subType = $contactType; | |
770 | $contactType = self::getBasicType($subType); | |
771 | ||
772 | // check for empty custom data which extends subtype | |
773 | $subTypeValue = CRM_Core_DAO::VALUE_SEPARATOR . $subType . CRM_Core_DAO::VALUE_SEPARATOR; | |
774 | $subTypeClause = " AND extends_entity_column_value LIKE '%{$subTypeValue}%' "; | |
775 | } | |
776 | $query = "SELECT table_name FROM civicrm_custom_group WHERE extends = '{$contactType}' {$subTypeClause}"; | |
777 | ||
778 | $dao = CRM_Core_DAO::executeQuery($query); | |
779 | while ($dao->fetch()) { | |
780 | $sql = "SELECT count(id) FROM {$dao->table_name}"; | |
781 | if ($contactId) { | |
782 | $sql .= " WHERE entity_id = {$contactId}"; | |
783 | } | |
784 | $sql .= " LIMIT 1"; | |
785 | ||
786 | $customDataCount = CRM_Core_DAO::singleValueQuery($sql); | |
787 | if (!empty($customDataCount)) { | |
6a488035 TO |
788 | return TRUE; |
789 | } | |
790 | } | |
791 | return FALSE; | |
792 | } | |
793 | ||
f4aaa82a EM |
794 | /** |
795 | * @todo what does this function do? | |
100fef9d | 796 | * @param int $contactId |
f4aaa82a EM |
797 | * @param $contactType |
798 | * | |
799 | * @return bool | |
800 | */ | |
00be9182 | 801 | public static function hasRelationships($contactId, $contactType) { |
6a488035 TO |
802 | $subTypeClause = NULL; |
803 | if (self::isaSubType($contactType)) { | |
353ffa53 TO |
804 | $subType = $contactType; |
805 | $contactType = self::getBasicType($subType); | |
6a488035 TO |
806 | $subTypeClause = " AND ( ( crt.contact_type_a = '{$contactType}' AND crt.contact_sub_type_a = '{$subType}') OR |
807 | ( crt.contact_type_b = '{$contactType}' AND crt.contact_sub_type_b = '{$subType}') ) "; | |
808 | } | |
809 | else { | |
810 | $subTypeClause = " AND ( crt.contact_type_a = '{$contactType}' OR crt.contact_type_b = '{$contactType}' ) "; | |
811 | } | |
812 | ||
813 | // check relationships for | |
814 | $relationshipQuery = " | |
815 | SELECT count(cr.id) FROM civicrm_relationship cr | |
816 | INNER JOIN civicrm_relationship_type crt ON | |
817 | ( cr.relationship_type_id = crt.id {$subTypeClause} ) | |
818 | WHERE ( cr.contact_id_a = {$contactId} OR cr.contact_id_b = {$contactId} ) | |
819 | LIMIT 1"; | |
820 | ||
821 | $relationshipCount = CRM_Core_DAO::singleValueQuery($relationshipQuery); | |
822 | ||
823 | if (!empty($relationshipCount)) { | |
824 | return TRUE; | |
825 | } | |
826 | ||
827 | return FALSE; | |
828 | } | |
829 | ||
f4aaa82a EM |
830 | /** |
831 | * @todo what does this function do? | |
832 | * @param $contactType | |
833 | * @param array $subtypeSet | |
834 | * | |
835 | * @return array | |
836 | */ | |
be2fb01f | 837 | public static function getSubtypeCustomPair($contactType, $subtypeSet = []) { |
6a488035 TO |
838 | if (empty($subtypeSet)) { |
839 | return $subtypeSet; | |
840 | } | |
841 | ||
be2fb01f | 842 | $customSet = $subTypeClause = []; |
6a488035 | 843 | foreach ($subtypeSet as $subtype) { |
353ffa53 | 844 | $subtype = CRM_Utils_Type::escape($subtype, 'String'); |
8ed93e75 | 845 | $subtype = CRM_Core_DAO::VALUE_SEPARATOR . $subtype . CRM_Core_DAO::VALUE_SEPARATOR; |
6a488035 TO |
846 | $subTypeClause[] = "extends_entity_column_value LIKE '%{$subtype}%' "; |
847 | } | |
848 | $query = "SELECT table_name | |
849 | FROM civicrm_custom_group | |
850 | WHERE extends = %1 AND " . implode(" OR ", $subTypeClause); | |
be2fb01f | 851 | $dao = CRM_Core_DAO::executeQuery($query, [1 => [$contactType, 'String']]); |
6a488035 TO |
852 | while ($dao->fetch()) { |
853 | $customSet[] = $dao->table_name; | |
854 | } | |
855 | return array_unique($customSet); | |
856 | } | |
857 | ||
f4aaa82a | 858 | /** |
fe482240 | 859 | * Function that does something. |
f4aaa82a EM |
860 | * @todo what does this function do? |
861 | * | |
100fef9d | 862 | * @param int $contactID |
f4aaa82a EM |
863 | * @param $contactType |
864 | * @param array $oldSubtypeSet | |
865 | * @param array $newSubtypeSet | |
866 | * | |
867 | * @return bool | |
868 | */ | |
bed98343 | 869 | public static function deleteCustomSetForSubtypeMigration( |
51ccfbbe | 870 | $contactID, |
6a488035 | 871 | $contactType, |
be2fb01f CW |
872 | $oldSubtypeSet = [], |
873 | $newSubtypeSet = [] | |
6a488035 TO |
874 | ) { |
875 | $oldCustomSet = self::getSubtypeCustomPair($contactType, $oldSubtypeSet); | |
876 | $newCustomSet = self::getSubtypeCustomPair($contactType, $newSubtypeSet); | |
877 | ||
878 | $customToBeRemoved = array_diff($oldCustomSet, $newCustomSet); | |
879 | foreach ($customToBeRemoved as $customTable) { | |
880 | self::deleteCustomRowsForEntityID($customTable, $contactID); | |
881 | } | |
882 | return TRUE; | |
883 | } | |
884 | ||
885 | /** | |
886 | * Delete content / rows of a custom table specific to a subtype for a given custom-group. | |
887 | * This function currently works for contact subtypes only and could be later improved / genralized | |
888 | * to work for other subtypes as well. | |
889 | * | |
77c5b619 TO |
890 | * @param int $gID |
891 | * Custom group id. | |
892 | * @param array $subtypes | |
893 | * List of subtypes related to which entry is to be removed. | |
683bf891 | 894 | * @param array $subtypesToPreserve |
6a488035 | 895 | * |
67d19299 | 896 | * @return bool |
6a488035 | 897 | */ |
be2fb01f | 898 | public static function deleteCustomRowsOfSubtype($gID, $subtypes = [], $subtypesToPreserve = []) { |
6a488035 TO |
899 | if (!$gID or empty($subtypes)) { |
900 | return FALSE; | |
901 | } | |
902 | ||
903 | $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $gID, 'table_name'); | |
904 | ||
66ed0bee | 905 | // drop triggers CRM-13587 |
906 | CRM_Core_DAO::dropTriggers($tableName); | |
3d5ac964 | 907 | |
3dfaea8d | 908 | foreach ($subtypesToPreserve as $subtypeToPreserve) { |
89f0019f | 909 | $subtypeToPreserve = CRM_Utils_Type::escape($subtypeToPreserve, 'String'); |
910 | $subtypesToPreserveClause[] = "(civicrm_contact.contact_sub_type NOT LIKE '%" . CRM_Core_DAO::VALUE_SEPARATOR . $subtypeToPreserve . CRM_Core_DAO::VALUE_SEPARATOR . "%')"; | |
3dfaea8d | 911 | } |
912 | $subtypesToPreserveClause = implode(' AND ', $subtypesToPreserveClause); | |
66ed0bee | 913 | |
be2fb01f | 914 | $subtypeClause = []; |
6a488035 TO |
915 | foreach ($subtypes as $subtype) { |
916 | $subtype = CRM_Utils_Type::escape($subtype, 'String'); | |
3dfaea8d | 917 | $subtypeClause[] = "( civicrm_contact.contact_sub_type LIKE '%" . CRM_Core_DAO::VALUE_SEPARATOR . $subtype . CRM_Core_DAO::VALUE_SEPARATOR . "%'" |
3d5ac964 | 918 | . " AND " . $subtypesToPreserveClause . ")"; |
6a488035 TO |
919 | } |
920 | $subtypeClause = implode(' OR ', $subtypeClause); | |
921 | ||
922 | $query = "DELETE custom.* | |
923 | FROM {$tableName} custom | |
924 | INNER JOIN civicrm_contact ON civicrm_contact.id = custom.entity_id | |
925 | WHERE ($subtypeClause)"; | |
66ed0bee | 926 | |
927 | CRM_Core_DAO::singleValueQuery($query); | |
928 | ||
929 | // rebuild triggers CRM-13587 | |
930 | CRM_Core_DAO::triggerRebuild($tableName); | |
6a488035 TO |
931 | } |
932 | ||
933 | /** | |
934 | * Delete content / rows of a custom table specific entity-id for a given custom-group table. | |
935 | * | |
77c5b619 TO |
936 | * @param int $customTable |
937 | * Custom table name. | |
938 | * @param int $entityID | |
939 | * Entity id. | |
6a488035 | 940 | * |
67d19299 | 941 | * @return null|string |
6a488035 | 942 | */ |
d9ef38cc | 943 | public static function deleteCustomRowsForEntityID($customTable, $entityID) { |
6a488035 TO |
944 | $customTable = CRM_Utils_Type::escape($customTable, 'String'); |
945 | $query = "DELETE FROM {$customTable} WHERE entity_id = %1"; | |
be2fb01f | 946 | return CRM_Core_DAO::singleValueQuery($query, [1 => [$entityID, 'Integer']]); |
6a488035 | 947 | } |
96025800 | 948 | |
6a488035 | 949 | } |