add status preference dao to ignore list
[civicrm-core.git] / CRM / Contact / BAO / ContactType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Contact_BAO_ContactType extends CRM_Contact_DAO_ContactType {
36
37 /**
fe482240 38 * Fetch object based on array of properties.
6a488035 39 *
77c5b619
TO
40 * @param array $params
41 * (reference ) an assoc array of name/value pairs.
42 * @param array $defaults
43 * (reference ) an assoc array to hold the flattened values.
6a488035 44 *
16b10e64
CW
45 * @return CRM_Contact_BAO_ContactType|null
46 * object on success, null otherwise
6a488035 47 */
00be9182 48 public static function retrieve(&$params, &$defaults) {
6a488035
TO
49 $contactType = new CRM_Contact_DAO_ContactType();
50 $contactType->copyValues($params);
51 if ($contactType->find(TRUE)) {
52 CRM_Core_DAO::storeValues($contactType, $defaults);
53 return $contactType;
54 }
55 return NULL;
56 }
57
86538308
EM
58 /**
59 * @param $contactType
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 */
00be9182 77 public static function &basicTypeInfo($all = FALSE) {
6a488035
TO
78 static $_cache = NULL;
79
80 if ($_cache === NULL) {
81 $_cache = array();
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 = "
90SELECT *
91FROM civicrm_contact_type
92WHERE parent_id IS NULL
93";
94 if ($all === FALSE) {
95 $sql .= " AND is_active = 1";
96 }
97
98 $dao = CRM_Core_DAO::executeQuery($sql,
99 CRM_Core_DAO::$_nullArray,
100 FALSE,
101 'CRM_Contact_DAO_ContactType'
102 );
103 while ($dao->fetch()) {
104 $value = array();
105 CRM_Core_DAO::storeValues($dao, $value);
106 $_cache[$argString][$dao->name] = $value;
107 }
108
109 $cache->set($argString, $_cache[$argString]);
110 }
111 }
112 return $_cache[$argString];
113 }
114
115 /**
c490a46a 116 * Retrieve all basic contact types.
6a488035 117 *
da6b46f4 118 * @param bool $all
6a488035 119 *
a6c01b45 120 * @return array
16b10e64 121 * Array of basic contact types
6a488035 122 */
00be9182 123 public static function basicTypes($all = FALSE) {
6a488035
TO
124 return array_keys(self::basicTypeInfo($all));
125 }
126
86538308
EM
127 /**
128 * @param bool $all
129 * @param string $key
130 *
131 * @return array
132 */
00be9182 133 public static function basicTypePairs($all = FALSE, $key = 'name') {
6a488035
TO
134 $subtypes = self::basicTypeInfo($all);
135
136 $pairs = array();
137 foreach ($subtypes as $name => $info) {
138 $index = ($key == 'name') ? $name : $info[$key];
139 $pairs[$index] = $info['label'];
140 }
141 return $pairs;
142 }
143
144 /**
c490a46a 145 * Retrieve all subtypes Information.
6a488035 146 *
77c5b619
TO
147 * @param array $contactType
148 * ..
fd31fa4c
EM
149 * @param bool $all
150 * @param bool $ignoreCache
151 * @param bool $reset
6a488035 152 *
a6c01b45 153 * @return array
16b10e64 154 * Array of sub type information
6a488035 155 */
00be9182 156 public static function &subTypeInfo($contactType = NULL, $all = FALSE, $ignoreCache = FALSE, $reset = FALSE) {
6a488035
TO
157 static $_cache = NULL;
158
159 if ($reset === TRUE) {
160 $_cache = NULL;
161 }
162
163 if ($_cache === NULL) {
164 $_cache = array();
165 }
166 if ($contactType && !is_array($contactType)) {
167 $contactType = array($contactType);
168 }
169
170 $argString = $all ? 'CRM_CT_STI_1_' : 'CRM_CT_STI_0_';
171 if (!empty($contactType)) {
172 $argString .= implode('_', $contactType);
173 }
174
175 if ((!array_key_exists($argString, $_cache)) || $ignoreCache) {
176 $cache = CRM_Utils_Cache::singleton();
177 $_cache[$argString] = $cache->get($argString);
178 if (!$_cache[$argString] || $ignoreCache) {
179 $_cache[$argString] = array();
180
181 $ctWHERE = '';
182 if (!empty($contactType)) {
183 $ctWHERE = " AND parent.name IN ('" . implode("','", $contactType) . "')";
184 }
185
186 $sql = "
187SELECT subtype.*, parent.name as parent, parent.label as parent_label
188FROM civicrm_contact_type subtype
189INNER JOIN civicrm_contact_type parent ON subtype.parent_id = parent.id
190WHERE subtype.name IS NOT NULL AND subtype.parent_id IS NOT NULL {$ctWHERE}
191";
192 if ($all === FALSE) {
193 $sql .= " AND subtype.is_active = 1 AND parent.is_active = 1 ORDER BY parent.id";
194 }
195 $dao = CRM_Core_DAO::executeQuery($sql, array(),
196 FALSE, 'CRM_Contact_DAO_ContactType'
197 );
198 while ($dao->fetch()) {
199 $value = array();
200 CRM_Core_DAO::storeValues($dao, $value);
201 $value['parent'] = $dao->parent;
202 $value['parent_label'] = $dao->parent_label;
203 $_cache[$argString][$dao->name] = $value;
204 }
205
206 $cache->set($argString, $_cache[$argString]);
207 }
208 }
209 return $_cache[$argString];
210 }
211
212 /**
213 *
c490a46a 214 * retrieve all subtypes
6a488035 215 *
77c5b619
TO
216 * @param array $contactType
217 * ..
f4aaa82a
EM
218 * @param bool $all
219 * @param string $columnName
220 * @param bool $ignoreCache
6a488035 221 *
a6c01b45 222 * @return array
16b10e64
CW
223 * all subtypes OR list of subtypes associated to
224 * a given basic contact type
6a488035 225 */
00be9182 226 public static function subTypes($contactType = NULL, $all = FALSE, $columnName = 'name', $ignoreCache = FALSE) {
6a488035
TO
227 if ($columnName == 'name') {
228 return array_keys(self::subTypeInfo($contactType, $all, $ignoreCache));
229 }
230 else {
231 return array_values(self::subTypePairs($contactType, FALSE, NULL, $ignoreCache));
232 }
233 }
234
235 /**
236 *
c490a46a 237 * retrieve subtype pairs with name as 'subtype-name' and 'label' as value
6a488035 238 *
77c5b619 239 * @param array $contactType
f4aaa82a
EM
240 * @param bool $all
241 * @param string $labelPrefix
242 * @param bool $ignoreCache
6a488035 243 *
72b3a70c
CW
244 * @return array
245 * list of subtypes with name as 'subtype-name' and 'label' as value
6a488035 246 */
00be9182 247 public static function subTypePairs($contactType = NULL, $all = FALSE, $labelPrefix = '- ', $ignoreCache = FALSE) {
6a488035
TO
248 $subtypes = self::subTypeInfo($contactType, $all, $ignoreCache);
249
250 $pairs = array();
251 foreach ($subtypes as $name => $info) {
252 $pairs[$name] = $labelPrefix . $info['label'];
253 }
254 return $pairs;
255 }
256
257 /**
258 *
c490a46a 259 * retrieve list of all types i.e basic + subtypes.
6a488035 260 *
f4aaa82a 261 * @param bool $all
6a488035 262 *
a6c01b45 263 * @return array
16b10e64 264 * Array of basic types + all subtypes.
6a488035 265 */
00be9182 266 public static function contactTypes($all = FALSE) {
6a488035
TO
267 return array_keys(self::contactTypeInfo($all));
268 }
269
270 /**
271 *
c490a46a 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) {
288 $_cache = array();
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]) {
296 $_cache[$argString] = array();
297
298 $sql = "
299SELECT type.*, parent.name as parent, parent.label as parent_label
300FROM civicrm_contact_type type
301LEFT JOIN civicrm_contact_type parent ON type.parent_id = parent.id
302WHERE 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,
215d1f2c 309 array(),
6a488035
TO
310 FALSE,
311 'CRM_Contact_DAO_ContactType'
312 );
313 while ($dao->fetch()) {
314 $value = array();
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 /**
c490a46a 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
347 $pairs = array();
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) {
384 $_cache = array();
385 }
386
387 $argString = $all ? 'CRM_CT_GSE_1' : 'CRM_CT_GSE_0';
b500fbea 388 $argString .= $isSeparator ? '_1' : '_0';
e9567ca3 389 $argString .= $separator;
6a488035
TO
390 if (!array_key_exists($argString, $_cache)) {
391 $cache = CRM_Utils_Cache::singleton();
392 $_cache[$argString] = $cache->get($argString);
393
394 if (!$_cache[$argString]) {
395 $_cache[$argString] = array();
396
397 $sql = "
398SELECT c.name as child_name , c.label as child_label , c.id as child_id,
399 p.name as parent_name, p.label as parent_label, p.id as parent_id
400FROM civicrm_contact_type c
401LEFT JOIN civicrm_contact_type p ON ( c.parent_id = p.id )
402WHERE ( c.name IS NOT NULL )
403";
404
405 if ($all === FALSE) {
406 $sql .= "
407AND c.is_active = 1
408AND ( p.is_active = 1 OR p.id IS NULL )
409";
410 }
411 $sql .= " ORDER BY c.id";
412
413 $values = array();
414 $dao = CRM_Core_DAO::executeQuery($sql);
415 while ($dao->fetch()) {
416 if (!empty($dao->parent_id)) {
353ffa53 417 $key = $isSeparator ? $dao->parent_name . $separator . $dao->child_name : $dao->child_name;
bee6039a 418 $label = "- {$dao->child_label}";
6a488035
TO
419 $pName = $dao->parent_name;
420 }
421 else {
353ffa53 422 $key = $dao->child_name;
6a488035
TO
423 $label = $dao->child_label;
424 $pName = $dao->child_name;
425 }
426
427 if (!isset($values[$pName])) {
428 $values[$pName] = array();
429 }
430 $values[$pName][] = array('key' => $key, 'label' => $label);
431 }
432
433 $selectElements = array();
434 foreach ($values as $pName => $elements) {
435 foreach ($elements as $element) {
436 $selectElements[$element['key']] = $element['label'];
437 }
438 }
439 $_cache[$argString] = $selectElements;
440
441 $cache->set($argString, $_cache[$argString]);
442 }
443 }
444 return $_cache[$argString];
445 }
446
447 /**
fe482240 448 * Check if a given type is a subtype.
6a488035 449 *
77c5b619
TO
450 * @param string $subType
451 * Contact subType.
f4aaa82a 452 * @param bool $ignoreCache
6a488035 453 *
bed98343 454 * @return bool
a6c01b45 455 * true if subType, false otherwise.
6a488035 456 */
00be9182 457 public static function isaSubType($subType, $ignoreCache = FALSE) {
6a488035
TO
458 return in_array($subType, self::subTypes(NULL, TRUE, 'name', $ignoreCache));
459 }
460
461 /**
100fef9d 462 * Retrieve the basic contact type associated with given subType.
6a488035 463 *
353ffa53 464 * @param array /string $subType contact subType.
51ccfbbe 465 * @return array/string of basicTypes.
6a488035 466 */
00be9182 467 public static function getBasicType($subType) {
6a488035
TO
468 static $_cache = NULL;
469 if ($_cache === NULL) {
470 $_cache = array();
471 }
472
473 $isArray = TRUE;
474 if ($subType && !is_array($subType)) {
475 $subType = array($subType);
476 $isArray = FALSE;
477 }
478 $argString = implode('_', $subType);
479
480 if (!array_key_exists($argString, $_cache)) {
481 $_cache[$argString] = array();
482
483 $sql = "
484SELECT subtype.name as contact_subtype, type.name as contact_type
485FROM civicrm_contact_type subtype
486INNER JOIN civicrm_contact_type type ON ( subtype.parent_id = type.id )
487WHERE subtype.name IN ('" . implode("','", $subType) . "' )";
488 $dao = CRM_Core_DAO::executeQuery($sql);
489 while ($dao->fetch()) {
490 if (!$isArray) {
491 $_cache[$argString] = $dao->contact_type;
492 break;
493 }
494 $_cache[$argString][$dao->contact_subtype] = $dao->contact_type;
495 }
496 }
497 return $_cache[$argString];
498 }
499
500 /**
c490a46a 501 * Suppress all subtypes present in given array.
6a488035 502 *
77c5b619
TO
503 * @param array $subTypes
504 * Contact subTypes.
f4aaa82a 505 * @param bool $ignoreCache
6a488035 506 *
a6c01b45 507 * @return array
16b10e64 508 * Array of suppressed subTypes.
6a488035 509 */
00be9182 510 public static function suppressSubTypes(&$subTypes, $ignoreCache = FALSE) {
6a488035
TO
511 $subTypes = array_diff($subTypes, self::subTypes(NULL, TRUE, 'name', $ignoreCache));
512 return $subTypes;
513 }
514
515 /**
100fef9d 516 * Verify if a given subtype is associated with a given basic contact type.
6a488035 517 *
77c5b619
TO
518 * @param string $subType
519 * Contact subType.
520 * @param string $contactType
521 * Contact Type.
f4aaa82a
EM
522 * @param bool $ignoreCache
523 * @param string $columnName
6a488035 524 *
bed98343 525 * @return bool
a6c01b45 526 * true if contact extends, false otherwise.
6a488035 527 */
00be9182 528 public static function isExtendsContactType($subType, $contactType, $ignoreCache = FALSE, $columnName = 'name') {
6a488035
TO
529 if (!is_array($subType)) {
530 $subType = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($subType, CRM_Core_DAO::VALUE_SEPARATOR));
531 }
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() {
6a488035 544 $shortCuts = array();
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 }
7926b999 557 $shortCut = array(
6a488035
TO
558 'path' => 'civicrm/contact/add',
559 'query' => "$typeUrl&reset=1",
560 'ref' => "new-$value",
561 'title' => $value,
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
588 $params = array('id' => $contactTypeId);
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 = "
604UPDATE civicrm_contact SET contact_sub_type = NULL
605WHERE 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 = "
616DELETE
617FROM civicrm_navigation
618WHERE name = %1";
619 $params = array(1 => array("New $name", 'String'));
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'])) {
6a488035
TO
659 $params = array('name' => "New $contactName");
660 $newParams = array(
661 'label' => "New $contact",
662 'is_active' => $active,
663 );
664 CRM_Core_BAO_Navigation::processUpdate($params, $newParams);
665 }
666 else {
667 $name = self::getBasicType($contactName);
668 if (!$name) {
669 return;
670 }
671 $value = array('name' => "New $name");
672 CRM_Core_BAO_Navigation::retrieve($value, $navinfo);
673 $navigation = array(
674 'label' => "New $contact",
675 'name' => "New $contactName",
15a7ea79 676 'url' => "civicrm/contact/add?ct=$name&cst=$contactName&reset=1",
6a488035
TO
677 'permission' => 'add contacts',
678 'parent_id' => $navinfo['id'],
679 'is_active' => $active,
680 );
681 CRM_Core_BAO_Navigation::add($navigation);
682 }
683 CRM_Core_BAO_Navigation::resetNavigation();
684
685 // reset the cache after adding
686 self::subTypeInfo(NULL, FALSE, FALSE, TRUE);
687
688 return $contactType;
689 }
690
691 /**
fe482240 692 * Update the is_active flag in the db.
6a488035 693 *
77c5b619
TO
694 * @param int $id
695 * Id of the database record.
696 * @param bool $is_active
697 * Value we want to set the is_active field.
6a488035 698 *
a6c01b45
CW
699 * @return Object
700 * DAO object on success, null otherwise
6a488035 701 */
00be9182 702 public static function setIsActive($id, $is_active) {
6a488035
TO
703 $params = array('id' => $id);
704 self::retrieve($params, $contactinfo);
705 $params = array('name' => "New $contactinfo[name]");
706 $newParams = array('is_active' => $is_active);
707 CRM_Core_BAO_Navigation::processUpdate($params, $newParams);
708 CRM_Core_BAO_Navigation::resetNavigation();
709 return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_ContactType', $id,
710 'is_active', $is_active
711 );
712 }
713
86538308 714 /**
100fef9d 715 * @param string $typeName
86538308
EM
716 *
717 * @return mixed
718 */
00be9182 719 public static function getLabel($typeName) {
6a488035
TO
720 $types = self::contactTypeInfo(TRUE);
721
722 if (array_key_exists($typeName, $types)) {
723 return $types[$typeName]['label'];
724 }
725 return $typeName;
726 }
727
728 /**
100fef9d 729 * Check whether allow to change any contact's subtype
6a488035
TO
730 * on the basis of custom data and relationship of specific subtype
731 * currently used in contact/edit form amd in import validation
732 *
77c5b619
TO
733 * @param int $contactId
734 * Contact id.
735 * @param string $subType
736 * Subtype.
6a488035 737 *
bed98343 738 * @return bool
6a488035 739 */
00be9182 740 public static function isAllowEdit($contactId, $subType = NULL) {
6a488035
TO
741
742 if (!$contactId) {
743 return TRUE;
744 }
745
746 if (empty($subType)) {
747 $subType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
748 $contactId,
749 'contact_sub_type'
750 );
751 }
752
753 if (self::hasCustomData($subType, $contactId) || self::hasRelationships($contactId, $subType)) {
754 return FALSE;
755 }
756
757 return TRUE;
758 }
759
86538308
EM
760 /**
761 * @param $contactType
100fef9d 762 * @param int $contactId
86538308
EM
763 *
764 * @return bool
765 */
00be9182 766 public static function hasCustomData($contactType, $contactId = NULL) {
6a488035
TO
767 $subTypeClause = '';
768
769 if (self::isaSubType($contactType)) {
770 $subType = $contactType;
771 $contactType = self::getBasicType($subType);
772
773 // check for empty custom data which extends subtype
774 $subTypeValue = CRM_Core_DAO::VALUE_SEPARATOR . $subType . CRM_Core_DAO::VALUE_SEPARATOR;
775 $subTypeClause = " AND extends_entity_column_value LIKE '%{$subTypeValue}%' ";
776 }
777 $query = "SELECT table_name FROM civicrm_custom_group WHERE extends = '{$contactType}' {$subTypeClause}";
778
779 $dao = CRM_Core_DAO::executeQuery($query);
780 while ($dao->fetch()) {
781 $sql = "SELECT count(id) FROM {$dao->table_name}";
782 if ($contactId) {
783 $sql .= " WHERE entity_id = {$contactId}";
784 }
785 $sql .= " LIMIT 1";
786
787 $customDataCount = CRM_Core_DAO::singleValueQuery($sql);
788 if (!empty($customDataCount)) {
789 $dao->free();
790 return TRUE;
791 }
792 }
793 return FALSE;
794 }
795
f4aaa82a
EM
796 /**
797 * @todo what does this function do?
100fef9d 798 * @param int $contactId
f4aaa82a
EM
799 * @param $contactType
800 *
801 * @return bool
802 */
00be9182 803 public static function hasRelationships($contactId, $contactType) {
6a488035
TO
804 $subTypeClause = NULL;
805 if (self::isaSubType($contactType)) {
353ffa53
TO
806 $subType = $contactType;
807 $contactType = self::getBasicType($subType);
6a488035
TO
808 $subTypeClause = " AND ( ( crt.contact_type_a = '{$contactType}' AND crt.contact_sub_type_a = '{$subType}') OR
809 ( crt.contact_type_b = '{$contactType}' AND crt.contact_sub_type_b = '{$subType}') ) ";
810 }
811 else {
812 $subTypeClause = " AND ( crt.contact_type_a = '{$contactType}' OR crt.contact_type_b = '{$contactType}' ) ";
813 }
814
815 // check relationships for
816 $relationshipQuery = "
817SELECT count(cr.id) FROM civicrm_relationship cr
818INNER JOIN civicrm_relationship_type crt ON
819( cr.relationship_type_id = crt.id {$subTypeClause} )
820WHERE ( cr.contact_id_a = {$contactId} OR cr.contact_id_b = {$contactId} )
821LIMIT 1";
822
823 $relationshipCount = CRM_Core_DAO::singleValueQuery($relationshipQuery);
824
825 if (!empty($relationshipCount)) {
826 return TRUE;
827 }
828
829 return FALSE;
830 }
831
f4aaa82a
EM
832 /**
833 * @todo what does this function do?
834 * @param $contactType
835 * @param array $subtypeSet
836 *
837 * @return array
838 */
00be9182 839 public static function getSubtypeCustomPair($contactType, $subtypeSet = array()) {
6a488035
TO
840 if (empty($subtypeSet)) {
841 return $subtypeSet;
842 }
843
844 $customSet = $subTypeClause = array();
845 foreach ($subtypeSet as $subtype) {
353ffa53
TO
846 $subtype = CRM_Utils_Type::escape($subtype, 'String');
847 $subType = CRM_Core_DAO::VALUE_SEPARATOR . $subtype . CRM_Core_DAO::VALUE_SEPARATOR;
6a488035
TO
848 $subTypeClause[] = "extends_entity_column_value LIKE '%{$subtype}%' ";
849 }
850 $query = "SELECT table_name
851FROM civicrm_custom_group
852WHERE extends = %1 AND " . implode(" OR ", $subTypeClause);
853 $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($contactType, 'String')));
854 while ($dao->fetch()) {
855 $customSet[] = $dao->table_name;
856 }
857 return array_unique($customSet);
858 }
859
f4aaa82a 860 /**
fe482240 861 * Function that does something.
f4aaa82a
EM
862 * @todo what does this function do?
863 *
100fef9d 864 * @param int $contactID
f4aaa82a
EM
865 * @param $contactType
866 * @param array $oldSubtypeSet
867 * @param array $newSubtypeSet
868 *
869 * @return bool
870 */
bed98343 871 public static function deleteCustomSetForSubtypeMigration(
51ccfbbe 872 $contactID,
6a488035
TO
873 $contactType,
874 $oldSubtypeSet = array(),
875 $newSubtypeSet = array()
876 ) {
877 $oldCustomSet = self::getSubtypeCustomPair($contactType, $oldSubtypeSet);
878 $newCustomSet = self::getSubtypeCustomPair($contactType, $newSubtypeSet);
879
880 $customToBeRemoved = array_diff($oldCustomSet, $newCustomSet);
881 foreach ($customToBeRemoved as $customTable) {
882 self::deleteCustomRowsForEntityID($customTable, $contactID);
883 }
884 return TRUE;
885 }
886
887 /**
888 * Delete content / rows of a custom table specific to a subtype for a given custom-group.
889 * This function currently works for contact subtypes only and could be later improved / genralized
890 * to work for other subtypes as well.
891 *
77c5b619
TO
892 * @param int $gID
893 * Custom group id.
894 * @param array $subtypes
895 * List of subtypes related to which entry is to be removed.
6a488035
TO
896 *
897 * @return void
6a488035 898 */
66ed0bee 899 public static function deleteCustomRowsOfSubtype($gID, $subtypes = array()) {
6a488035
TO
900 if (!$gID or empty($subtypes)) {
901 return FALSE;
902 }
903
904 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $gID, 'table_name');
905
66ed0bee 906 // drop triggers CRM-13587
907 CRM_Core_DAO::dropTriggers($tableName);
908
6a488035
TO
909 $subtypeClause = array();
910 foreach ($subtypes as $subtype) {
911 $subtype = CRM_Utils_Type::escape($subtype, 'String');
912 $subtypeClause[] = "civicrm_contact.contact_sub_type LIKE '%" . CRM_Core_DAO::VALUE_SEPARATOR . $subtype . CRM_Core_DAO::VALUE_SEPARATOR . "%'";
913 }
914 $subtypeClause = implode(' OR ', $subtypeClause);
915
916 $query = "DELETE custom.*
917FROM {$tableName} custom
918INNER JOIN civicrm_contact ON civicrm_contact.id = custom.entity_id
919WHERE ($subtypeClause)";
66ed0bee 920
921 CRM_Core_DAO::singleValueQuery($query);
922
923 // rebuild triggers CRM-13587
924 CRM_Core_DAO::triggerRebuild($tableName);
6a488035
TO
925 }
926
927 /**
928 * Delete content / rows of a custom table specific entity-id for a given custom-group table.
929 *
77c5b619
TO
930 * @param int $customTable
931 * Custom table name.
932 * @param int $entityID
933 * Entity id.
6a488035
TO
934 *
935 * @return void
6a488035 936 */
d9ef38cc 937 public static function deleteCustomRowsForEntityID($customTable, $entityID) {
6a488035
TO
938 $customTable = CRM_Utils_Type::escape($customTable, 'String');
939 $query = "DELETE FROM {$customTable} WHERE entity_id = %1";
940 return CRM_Core_DAO::singleValueQuery($query, array(1 => array($entityID, 'Integer')));
941 }
96025800 942
6a488035 943}