3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Contact_BAO_ContactType
extends CRM_Contact_DAO_ContactType
{
20 * Fetch object based on array of properties.
22 * @param array $params
23 * (reference ) an assoc array of name/value pairs.
24 * @param array $defaults
25 * (reference ) an assoc array to hold the flattened values.
27 * @return CRM_Contact_BAO_ContactType|null
28 * object on success, null otherwise
30 public static function retrieve(&$params, &$defaults) {
31 $contactType = new CRM_Contact_DAO_ContactType();
32 $contactType->copyValues($params);
33 if ($contactType->find(TRUE)) {
34 CRM_Core_DAO
::storeValues($contactType, $defaults);
41 * Is this contact type active.
43 * @param string $contactType
47 public static function isActive($contactType) {
48 $contact = self
::contactTypeInfo(FALSE);
49 $active = array_key_exists($contactType, $contact) ?
TRUE : FALSE;
54 * Retrieve basic contact type information.
59 * Array of basic contact types information.
61 public static function basicTypeInfo($all = FALSE) {
62 static $_cache = NULL;
64 if ($_cache === NULL) {
68 $argString = $all ?
'CRM_CT_BTI_1' : 'CRM_CT_BTI_0';
69 if (!array_key_exists($argString, $_cache)) {
70 $cache = CRM_Utils_Cache
::singleton();
71 $_cache[$argString] = $cache->get($argString);
72 if (!$_cache[$argString]) {
75 FROM civicrm_contact_type
76 WHERE parent_id IS NULL
79 $sql .= " AND is_active = 1";
83 $dao = CRM_Core_DAO
::executeQuery($sql,
86 'CRM_Contact_DAO_ContactType'
88 while ($dao->fetch()) {
90 CRM_Core_DAO
::storeValues($dao, $value);
91 $_cache[$argString][$dao->name
] = $value;
94 $cache->set($argString, $_cache[$argString]);
97 return $_cache[$argString];
101 * Retrieve all basic contact types.
106 * Array of basic contact types
108 public static function basicTypes($all = FALSE) {
109 return array_keys(self
::basicTypeInfo($all));
118 public static function basicTypePairs($all = FALSE, $key = 'name') {
119 $subtypes = self
::basicTypeInfo($all);
122 foreach ($subtypes as $name => $info) {
123 $index = ($key == 'name') ?
$name : $info[$key];
124 $pairs[$index] = $info['label'];
130 * Retrieve all subtypes Information.
132 * @param array $contactType
135 * @param bool $ignoreCache
138 * Array of sub type information
140 public static function subTypeInfo($contactType = NULL, $all = FALSE, $ignoreCache = FALSE) {
141 $argString = $all ?
'CRM_CT_STI_1_' : 'CRM_CT_STI_0_';
142 if (!empty($contactType)) {
143 $contactType = (array) $contactType;
144 $argString .= implode('_', $contactType);
146 if (!Civi
::cache('contactTypes')->has($argString) ||
$ignoreCache) {
148 if (!empty($contactType)) {
149 $ctWHERE = " AND parent.name IN ('" . implode("','", $contactType) . "')";
153 SELECT subtype.*, parent.name as parent, parent.label as parent_label
154 FROM civicrm_contact_type subtype
155 INNER JOIN civicrm_contact_type parent ON subtype.parent_id = parent.id
156 WHERE subtype.name IS NOT NULL AND subtype.parent_id IS NOT NULL {$ctWHERE}
158 if ($all === FALSE) {
159 $sql .= " AND subtype.is_active = 1 AND parent.is_active = 1 ORDER BY parent.id";
161 $dao = CRM_Core_DAO
::executeQuery($sql, [],
162 FALSE, 'CRM_Contact_DAO_ContactType'
165 while ($dao->fetch()) {
167 CRM_Core_DAO
::storeValues($dao, $value);
168 $value['parent'] = $dao->parent
;
169 $value['parent_label'] = $dao->parent_label
;
170 $values[$dao->name
] = $value;
172 Civi
::cache('contactTypes')->set($argString, $values);
174 return Civi
::cache('contactTypes')->get($argString);
179 * retrieve all subtypes
181 * @param array $contactType
184 * @param string $columnName
185 * @param bool $ignoreCache
188 * all subtypes OR list of subtypes associated to
189 * a given basic contact type
191 public static function subTypes($contactType = NULL, $all = FALSE, $columnName = 'name', $ignoreCache = FALSE) {
192 if ($columnName == 'name') {
193 return array_keys(self
::subTypeInfo($contactType, $all, $ignoreCache));
196 return array_values(self
::subTypePairs($contactType, FALSE, NULL, $ignoreCache));
202 * retrieve subtype pairs with name as 'subtype-name' and 'label' as value
204 * @param array $contactType
206 * @param string $labelPrefix
207 * @param bool $ignoreCache
210 * list of subtypes with name as 'subtype-name' and 'label' as value
212 public static function subTypePairs($contactType = NULL, $all = FALSE, $labelPrefix = '- ', $ignoreCache = FALSE) {
213 $subtypes = self
::subTypeInfo($contactType, $all, $ignoreCache);
216 foreach ($subtypes as $name => $info) {
217 $pairs[$name] = $labelPrefix . $info['label'];
224 * retrieve list of all types i.e basic + subtypes.
229 * Array of basic types + all subtypes.
231 public static function contactTypes($all = FALSE) {
232 return array_keys(self
::contactTypeInfo($all));
236 * Retrieve info array about all types i.e basic + subtypes.
242 * Array of basic types + all subtypes.
244 public static function contactTypeInfo($all = FALSE, $reset = FALSE) {
245 static $_cache = NULL;
247 if ($reset === TRUE) {
251 if ($_cache === NULL) {
255 $argString = $all ?
'CRM_CT_CTI_1' : 'CRM_CT_CTI_0';
256 if (!array_key_exists($argString, $_cache)) {
257 $cache = CRM_Utils_Cache
::singleton();
258 $_cache[$argString] = $cache->get($argString);
259 if (!$_cache[$argString]) {
260 $_cache[$argString] = [];
263 SELECT type.*, parent.name as parent, parent.label as parent_label
264 FROM civicrm_contact_type type
265 LEFT JOIN civicrm_contact_type parent ON type.parent_id = parent.id
266 WHERE type.name IS NOT NULL
268 if ($all === FALSE) {
269 $sql .= " AND type.is_active = 1";
272 $dao = CRM_Core_DAO
::executeQuery($sql,
275 'CRM_Contact_DAO_ContactType'
277 while ($dao->fetch()) {
279 CRM_Core_DAO
::storeValues($dao, $value);
280 if (array_key_exists('parent_id', $value)) {
281 $value['parent'] = $dao->parent
;
282 $value['parent_label'] = $dao->parent_label
;
284 $_cache[$argString][$dao->name
] = $value;
287 $cache->set($argString, $_cache[$argString]);
291 return $_cache[$argString];
295 * Retrieve basic type pairs with name as 'built-in name' and 'label' as value.
298 * @param null $typeName
299 * @param null $delimiter
302 * Array of basictypes with name as 'built-in name' and 'label' as value
304 public static function contactTypePairs($all = FALSE, $typeName = NULL, $delimiter = NULL) {
305 $types = self
::contactTypeInfo($all);
307 if ($typeName && !is_array($typeName)) {
308 $typeName = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, trim($typeName, CRM_Core_DAO
::VALUE_SEPARATOR
));
313 foreach ($typeName as $type) {
314 if (array_key_exists($type, $types)) {
315 $pairs[$type] = $types[$type]['label'];
320 foreach ($types as $name => $info) {
321 $pairs[$name] = $info['label'];
325 return !$delimiter ?
$pairs : implode($delimiter, $pairs);
329 * Get a list of elements for select box.
330 * Note that this used to default to using the hex(01) character - which results in an invalid character being used in form fields
331 * which was not handled well be anything that loaded & resaved the html (outside core)
332 * The use of this separator is now explicit in the calling functions as a step towards it's removal
335 * @param bool $isSeparator
336 * @param string $separator
340 public static function getSelectElements(
345 // @todo - use Cache class - ie like Civi::cache('contactTypes')
346 static $_cache = NULL;
348 if ($_cache === NULL) {
352 $argString = $all ?
'CRM_CT_GSE_1' : 'CRM_CT_GSE_0';
353 $argString .= $isSeparator ?
'_1' : '_0';
354 $argString .= $separator;
355 $argString = CRM_Utils_Cache
::cleanKey($argString);
356 if (!array_key_exists($argString, $_cache)) {
357 $cache = CRM_Utils_Cache
::singleton();
358 $_cache[$argString] = $cache->get($argString);
360 if (!$_cache[$argString]) {
361 $_cache[$argString] = [];
364 SELECT c.name as child_name , c.label as child_label , c.id as child_id,
365 p.name as parent_name, p.label as parent_label, p.id as parent_id
366 FROM civicrm_contact_type c
367 LEFT JOIN civicrm_contact_type p ON ( c.parent_id = p.id )
368 WHERE ( c.name IS NOT NULL )
371 if ($all === FALSE) {
374 AND ( p.is_active = 1 OR p.id IS NULL )
377 $sql .= " ORDER BY c.id";
380 $dao = CRM_Core_DAO
::executeQuery($sql);
381 while ($dao->fetch()) {
382 if (!empty($dao->parent_id
)) {
383 $key = $isSeparator ?
$dao->parent_name
. $separator . $dao->child_name
: $dao->child_name
;
384 $label = "- {$dao->child_label}";
385 $pName = $dao->parent_name
;
388 $key = $dao->child_name
;
389 $label = $dao->child_label
;
390 $pName = $dao->child_name
;
393 if (!isset($values[$pName])) {
394 $values[$pName] = [];
396 $values[$pName][] = ['key' => $key, 'label' => $label];
399 $selectElements = [];
400 foreach ($values as $pName => $elements) {
401 foreach ($elements as $element) {
402 $selectElements[$element['key']] = $element['label'];
405 $_cache[$argString] = $selectElements;
407 $cache->set($argString, $_cache[$argString]);
410 return $_cache[$argString];
414 * Check if a given type is a subtype.
416 * @param string $subType
418 * @param bool $ignoreCache
421 * true if subType, false otherwise.
423 public static function isaSubType($subType, $ignoreCache = FALSE) {
424 return in_array($subType, self
::subTypes(NULL, TRUE, 'name', $ignoreCache));
428 * Retrieve the basic contact type associated with given subType.
430 * @param array|string $subType contact subType.
431 * @return array|string
434 public static function getBasicType($subType) {
435 // @todo - use Cache class - ie like Civi::cache('contactTypes')
436 static $_cache = NULL;
437 if ($_cache === NULL) {
442 if ($subType && !is_array($subType)) {
443 $subType = [$subType];
446 $argString = implode('_', $subType);
448 if (!array_key_exists($argString, $_cache)) {
449 $_cache[$argString] = [];
452 SELECT subtype.name as contact_subtype, type.name as contact_type
453 FROM civicrm_contact_type subtype
454 INNER JOIN civicrm_contact_type type ON ( subtype.parent_id = type.id )
455 WHERE subtype.name IN ('" . implode("','", $subType) . "' )";
456 $dao = CRM_Core_DAO
::executeQuery($sql);
457 while ($dao->fetch()) {
459 $_cache[$argString] = $dao->contact_type
;
462 $_cache[$argString][$dao->contact_subtype
] = $dao->contact_type
;
465 return $_cache[$argString];
469 * Suppress all subtypes present in given array.
471 * @param array $subTypes
473 * @param bool $ignoreCache
476 * Array of suppressed subTypes.
478 public static function suppressSubTypes(&$subTypes, $ignoreCache = FALSE) {
479 $subTypes = array_diff($subTypes, self
::subTypes(NULL, TRUE, 'name', $ignoreCache));
484 * Verify if a given subtype is associated with a given basic contact type.
486 * @param string $subType
488 * @param string $contactType
490 * @param bool $ignoreCache
491 * @param string $columnName
494 * true if contact extends, false otherwise.
496 public static function isExtendsContactType($subType, $contactType, $ignoreCache = FALSE, $columnName = 'name') {
497 $subType = (array) CRM_Utils_Array
::explodePadded($subType);
498 $subtypeList = self
::subTypes($contactType, TRUE, $columnName, $ignoreCache);
499 $intersection = array_intersect($subType, $subtypeList);
500 return $subType == $intersection;
504 * Create shortcuts menu for contactTypes.
509 public static function getCreateNewList() {
511 //@todo FIXME - using the CRM_Core_DAO::VALUE_SEPARATOR creates invalid html - if you can find the form
512 // this is loaded onto then replace with something like '__' & test
513 $separator = CRM_Core_DAO
::VALUE_SEPARATOR
;
514 $contactTypes = self
::getSelectElements(FALSE, TRUE, $separator);
515 foreach ($contactTypes as $key => $value) {
517 $typeValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $key);
518 $cType = CRM_Utils_Array
::value('0', $typeValue);
519 $typeUrl = 'ct=' . $cType;
520 if ($csType = CRM_Utils_Array
::value('1', $typeValue)) {
521 $typeUrl .= "&cst=$csType";
524 'path' => 'civicrm/contact/add',
525 'query' => "$typeUrl&reset=1",
526 'ref' => "new-$value",
529 if ($csType = CRM_Utils_Array
::value('1', $typeValue)) {
530 $shortCuts[$cType]['shortCuts'][] = $shortCut;
533 $shortCuts[$cType] = $shortCut;
541 * Delete Contact SubTypes.
543 * @param int $contactTypeId
544 * ID of the Contact Subtype to be deleted.
548 public static function del($contactTypeId) {
550 if (!$contactTypeId) {
554 $params = ['id' => $contactTypeId];
555 self
::retrieve($params, $typeInfo);
556 $name = $typeInfo['name'];
557 // check if any custom group
558 $custom = new CRM_Core_DAO_CustomGroup();
559 $custom->whereAdd("extends_entity_column_value LIKE '%" .
560 CRM_Core_DAO
::VALUE_SEPARATOR
.
562 CRM_Core_DAO
::VALUE_SEPARATOR
. "%'"
564 if ($custom->find()) {
568 // remove subtype for existing contacts
570 UPDATE civicrm_contact SET contact_sub_type = NULL
571 WHERE contact_sub_type = '$name'";
572 CRM_Core_DAO
::executeQuery($sql);
574 // remove subtype from contact type table
575 $contactType = new CRM_Contact_DAO_ContactType();
576 $contactType->id
= $contactTypeId;
577 $contactType->delete();
579 // remove navigation entry if any
583 FROM civicrm_navigation
585 $params = [1 => ["New $name", 'String']];
586 CRM_Core_DAO
::executeQuery($sql, $params);
587 CRM_Core_BAO_Navigation
::resetNavigation();
588 Civi
::cache('contactTypes')->clear();
594 * Add or update Contact SubTypes.
596 * @param array $params
597 * An assoc array of name/value pairs.
599 * @return object|void
601 public static function add(&$params) {
604 if (empty($params['id']) && empty($params['label'])) {
607 if (!empty($params['parent_id']) &&
608 !CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_ContactType', $params['parent_id'])
613 $contactType = new CRM_Contact_DAO_ContactType();
614 $contactType->copyValues($params);
615 $contactType->id
= CRM_Utils_Array
::value('id', $params);
616 $contactType->is_active
= CRM_Utils_Array
::value('is_active', $params, 0);
618 $contactType->save();
619 if ($contactType->find(TRUE)) {
620 $contactName = $contactType->name
;
621 $contact = ucfirst($contactType->label
);
622 $active = $contactType->is_active
;
625 if (!empty($params['id'])) {
627 'label' => "New $contact",
628 'is_active' => $active,
630 CRM_Core_BAO_Navigation
::processUpdate(['name' => "New $contactName"], $newParams);
633 $name = self
::getBasicType($contactName);
637 $value = ['name' => "New $name"];
638 CRM_Core_BAO_Navigation
::retrieve($value, $navinfo);
640 'label' => "New $contact",
641 'name' => "New $contactName",
642 'url' => "civicrm/contact/add?ct=$name&cst=$contactName&reset=1",
643 'permission' => 'add contacts',
644 'parent_id' => $navinfo['id'],
645 'is_active' => $active,
647 CRM_Core_BAO_Navigation
::add($navigation);
649 CRM_Core_BAO_Navigation
::resetNavigation();
650 Civi
::cache('contactTypes')->clear();
656 * Update the is_active flag in the db.
659 * Id of the database record.
660 * @param bool $is_active
661 * Value we want to set the is_active field.
664 * true if we found and updated the object, else false
666 public static function setIsActive($id, $is_active) {
667 $params = ['id' => $id];
668 self
::retrieve($params, $contactinfo);
669 $params = ['name' => "New $contactinfo[name]"];
670 $newParams = ['is_active' => $is_active];
671 CRM_Core_BAO_Navigation
::processUpdate($params, $newParams);
672 CRM_Core_BAO_Navigation
::resetNavigation();
673 return CRM_Core_DAO
::setFieldValue('CRM_Contact_DAO_ContactType', $id,
674 'is_active', $is_active
679 * @param string $typeName
683 public static function getLabel($typeName) {
684 $types = self
::contactTypeInfo(TRUE);
686 if (array_key_exists($typeName, $types)) {
687 return $types[$typeName]['label'];
693 * Check whether allow to change any contact's subtype
694 * on the basis of custom data and relationship of specific subtype
695 * currently used in contact/edit form amd in import validation
697 * @param int $contactId
699 * @param string $subType
704 public static function isAllowEdit($contactId, $subType = NULL) {
710 if (empty($subType)) {
711 $subType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
717 if (self
::hasCustomData($subType, $contactId) || self
::hasRelationships($contactId, $subType)) {
725 * @param $contactType
726 * @param int $contactId
730 public static function hasCustomData($contactType, $contactId = NULL) {
733 if (self
::isaSubType($contactType)) {
734 $subType = $contactType;
735 $contactType = self
::getBasicType($subType);
737 // check for empty custom data which extends subtype
738 $subTypeValue = CRM_Core_DAO
::VALUE_SEPARATOR
. $subType . CRM_Core_DAO
::VALUE_SEPARATOR
;
739 $subTypeClause = " AND extends_entity_column_value LIKE '%{$subTypeValue}%' ";
741 $query = "SELECT table_name FROM civicrm_custom_group WHERE extends = '{$contactType}' {$subTypeClause}";
743 $dao = CRM_Core_DAO
::executeQuery($query);
744 while ($dao->fetch()) {
745 $sql = "SELECT count(id) FROM {$dao->table_name}";
747 $sql .= " WHERE entity_id = {$contactId}";
751 $customDataCount = CRM_Core_DAO
::singleValueQuery($sql);
752 if (!empty($customDataCount)) {
760 * @todo what does this function do?
761 * @param int $contactId
762 * @param $contactType
766 public static function hasRelationships($contactId, $contactType) {
767 $subTypeClause = NULL;
768 if (self
::isaSubType($contactType)) {
769 $subType = $contactType;
770 $contactType = self
::getBasicType($subType);
771 $subTypeClause = " AND ( ( crt.contact_type_a = '{$contactType}' AND crt.contact_sub_type_a = '{$subType}') OR
772 ( crt.contact_type_b = '{$contactType}' AND crt.contact_sub_type_b = '{$subType}') ) ";
775 $subTypeClause = " AND ( crt.contact_type_a = '{$contactType}' OR crt.contact_type_b = '{$contactType}' ) ";
778 // check relationships for
779 $relationshipQuery = "
780 SELECT count(cr.id) FROM civicrm_relationship cr
781 INNER JOIN civicrm_relationship_type crt ON
782 ( cr.relationship_type_id = crt.id {$subTypeClause} )
783 WHERE ( cr.contact_id_a = {$contactId} OR cr.contact_id_b = {$contactId} )
786 $relationshipCount = CRM_Core_DAO
::singleValueQuery($relationshipQuery);
788 if (!empty($relationshipCount)) {
796 * @todo what does this function do?
797 * @param $contactType
798 * @param array $subtypeSet
802 public static function getSubtypeCustomPair($contactType, $subtypeSet = []) {
803 if (empty($subtypeSet)) {
807 $customSet = $subTypeClause = [];
808 foreach ($subtypeSet as $subtype) {
809 $subtype = CRM_Utils_Type
::escape($subtype, 'String');
810 $subtype = CRM_Core_DAO
::VALUE_SEPARATOR
. $subtype . CRM_Core_DAO
::VALUE_SEPARATOR
;
811 $subTypeClause[] = "extends_entity_column_value LIKE '%{$subtype}%' ";
813 $query = "SELECT table_name
814 FROM civicrm_custom_group
815 WHERE extends = %1 AND " . implode(" OR ", $subTypeClause);
816 $dao = CRM_Core_DAO
::executeQuery($query, [1 => [$contactType, 'String']]);
817 while ($dao->fetch()) {
818 $customSet[] = $dao->table_name
;
820 return array_unique($customSet);
824 * Function that does something.
825 * @todo what does this function do?
827 * @param int $contactID
828 * @param $contactType
829 * @param array $oldSubtypeSet
830 * @param array $newSubtypeSet
834 public static function deleteCustomSetForSubtypeMigration(
840 $oldCustomSet = self
::getSubtypeCustomPair($contactType, $oldSubtypeSet);
841 $newCustomSet = self
::getSubtypeCustomPair($contactType, $newSubtypeSet);
843 $customToBeRemoved = array_diff($oldCustomSet, $newCustomSet);
844 foreach ($customToBeRemoved as $customTable) {
845 self
::deleteCustomRowsForEntityID($customTable, $contactID);
851 * Delete content / rows of a custom table specific to a subtype for a given custom-group.
852 * This function currently works for contact subtypes only and could be later improved / genralized
853 * to work for other subtypes as well.
857 * @param array $subtypes
858 * List of subtypes related to which entry is to be removed.
859 * @param array $subtypesToPreserve
863 public static function deleteCustomRowsOfSubtype($gID, $subtypes = [], $subtypesToPreserve = []) {
864 if (!$gID or empty($subtypes)) {
868 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $gID, 'table_name');
870 // drop triggers CRM-13587
871 CRM_Core_DAO
::dropTriggers($tableName);
873 foreach ($subtypesToPreserve as $subtypeToPreserve) {
874 $subtypeToPreserve = CRM_Utils_Type
::escape($subtypeToPreserve, 'String');
875 $subtypesToPreserveClause[] = "(civicrm_contact.contact_sub_type NOT LIKE '%" . CRM_Core_DAO
::VALUE_SEPARATOR
. $subtypeToPreserve . CRM_Core_DAO
::VALUE_SEPARATOR
. "%')";
877 $subtypesToPreserveClause = implode(' AND ', $subtypesToPreserveClause);
880 foreach ($subtypes as $subtype) {
881 $subtype = CRM_Utils_Type
::escape($subtype, 'String');
882 $subtypeClause[] = "( civicrm_contact.contact_sub_type LIKE '%" . CRM_Core_DAO
::VALUE_SEPARATOR
. $subtype . CRM_Core_DAO
::VALUE_SEPARATOR
. "%'"
883 . " AND " . $subtypesToPreserveClause . ")";
885 $subtypeClause = implode(' OR ', $subtypeClause);
887 $query = "DELETE custom.*
888 FROM {$tableName} custom
889 INNER JOIN civicrm_contact ON civicrm_contact.id = custom.entity_id
890 WHERE ($subtypeClause)";
892 CRM_Core_DAO
::singleValueQuery($query);
894 // rebuild triggers CRM-13587
895 CRM_Core_DAO
::triggerRebuild($tableName);
899 * Delete content / rows of a custom table specific entity-id for a given custom-group table.
901 * @param int $customTable
903 * @param int $entityID
906 * @return null|string
908 public static function deleteCustomRowsForEntityID($customTable, $entityID) {
909 $customTable = CRM_Utils_Type
::escape($customTable, 'String');
910 $query = "DELETE FROM {$customTable} WHERE entity_id = %1";
911 return CRM_Core_DAO
::singleValueQuery($query, [1 => [$entityID, 'Integer']]);