3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2016
33 class CRM_Contact_BAO_ContactType
extends CRM_Contact_DAO_ContactType
{
36 * Fetch object based on array of properties.
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.
43 * @return CRM_Contact_BAO_ContactType|null
44 * object on success, null otherwise
46 public static function retrieve(&$params, &$defaults) {
47 $contactType = new CRM_Contact_DAO_ContactType();
48 $contactType->copyValues($params);
49 if ($contactType->find(TRUE)) {
50 CRM_Core_DAO
::storeValues($contactType, $defaults);
57 * Is this contact type active.
59 * @param string $contactType
63 public static function isActive($contactType) {
64 $contact = self
::contactTypeInfo(FALSE);
65 $active = array_key_exists($contactType, $contact) ?
TRUE : FALSE;
70 * Retrieve basic contact type information.
75 * Array of basic contact types information.
77 public static function basicTypeInfo($all = FALSE) {
78 static $_cache = NULL;
80 if ($_cache === NULL) {
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]) {
91 FROM civicrm_contact_type
92 WHERE parent_id IS NULL
95 $sql .= " AND is_active = 1";
98 $dao = CRM_Core_DAO
::executeQuery($sql,
99 CRM_Core_DAO
::$_nullArray,
101 'CRM_Contact_DAO_ContactType'
103 while ($dao->fetch()) {
105 CRM_Core_DAO
::storeValues($dao, $value);
106 $_cache[$argString][$dao->name
] = $value;
109 $cache->set($argString, $_cache[$argString]);
112 return $_cache[$argString];
116 * Retrieve all basic contact types.
121 * Array of basic contact types
123 public static function basicTypes($all = FALSE) {
124 return array_keys(self
::basicTypeInfo($all));
133 public static function basicTypePairs($all = FALSE, $key = 'name') {
134 $subtypes = self
::basicTypeInfo($all);
137 foreach ($subtypes as $name => $info) {
138 $index = ($key == 'name') ?
$name : $info[$key];
139 $pairs[$index] = $info['label'];
145 * Retrieve all subtypes Information.
147 * @param array $contactType
150 * @param bool $ignoreCache
154 * Array of sub type information
156 public static function subTypeInfo($contactType = NULL, $all = FALSE, $ignoreCache = FALSE, $reset = FALSE) {
157 static $_cache = NULL;
159 if ($reset === TRUE) {
163 if ($_cache === NULL) {
166 if ($contactType && !is_array($contactType)) {
167 $contactType = array($contactType);
170 $argString = $all ?
'CRM_CT_STI_1_' : 'CRM_CT_STI_0_';
171 if (!empty($contactType)) {
172 $argString .= implode('_', $contactType);
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();
182 if (!empty($contactType)) {
183 $ctWHERE = " AND parent.name IN ('" . implode("','", $contactType) . "')";
187 SELECT subtype.*, parent.name as parent, parent.label as parent_label
188 FROM civicrm_contact_type subtype
189 INNER JOIN civicrm_contact_type parent ON subtype.parent_id = parent.id
190 WHERE subtype.name IS NOT NULL AND subtype.parent_id IS NOT NULL {$ctWHERE}
192 if ($all === FALSE) {
193 $sql .= " AND subtype.is_active = 1 AND parent.is_active = 1 ORDER BY parent.id";
195 $dao = CRM_Core_DAO
::executeQuery($sql, array(),
196 FALSE, 'CRM_Contact_DAO_ContactType'
198 while ($dao->fetch()) {
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;
206 $cache->set($argString, $_cache[$argString]);
209 return $_cache[$argString];
214 * retrieve all subtypes
216 * @param array $contactType
219 * @param string $columnName
220 * @param bool $ignoreCache
223 * all subtypes OR list of subtypes associated to
224 * a given basic contact type
226 public static function subTypes($contactType = NULL, $all = FALSE, $columnName = 'name', $ignoreCache = FALSE) {
227 if ($columnName == 'name') {
228 return array_keys(self
::subTypeInfo($contactType, $all, $ignoreCache));
231 return array_values(self
::subTypePairs($contactType, FALSE, NULL, $ignoreCache));
237 * retrieve subtype pairs with name as 'subtype-name' and 'label' as value
239 * @param array $contactType
241 * @param string $labelPrefix
242 * @param bool $ignoreCache
245 * list of subtypes with name as 'subtype-name' and 'label' as value
247 public static function subTypePairs($contactType = NULL, $all = FALSE, $labelPrefix = '- ', $ignoreCache = FALSE) {
248 $subtypes = self
::subTypeInfo($contactType, $all, $ignoreCache);
251 foreach ($subtypes as $name => $info) {
252 $pairs[$name] = $labelPrefix . $info['label'];
259 * retrieve list of all types i.e basic + subtypes.
264 * Array of basic types + all subtypes.
266 public static function contactTypes($all = FALSE) {
267 return array_keys(self
::contactTypeInfo($all));
271 * Retrieve info array about all types i.e basic + subtypes.
277 * Array of basic types + all subtypes.
279 public static function contactTypeInfo($all = FALSE, $reset = FALSE) {
280 static $_cache = NULL;
282 if ($reset === TRUE) {
286 if ($_cache === NULL) {
290 $argString = $all ?
'CRM_CT_CTI_1' : 'CRM_CT_CTI_0';
291 if (!array_key_exists($argString, $_cache)) {
292 $cache = CRM_Utils_Cache
::singleton();
293 $_cache[$argString] = $cache->get($argString);
294 if (!$_cache[$argString]) {
295 $_cache[$argString] = array();
298 SELECT type.*, parent.name as parent, parent.label as parent_label
299 FROM civicrm_contact_type type
300 LEFT JOIN civicrm_contact_type parent ON type.parent_id = parent.id
301 WHERE type.name IS NOT NULL
303 if ($all === FALSE) {
304 $sql .= " AND type.is_active = 1";
307 $dao = CRM_Core_DAO
::executeQuery($sql,
310 'CRM_Contact_DAO_ContactType'
312 while ($dao->fetch()) {
314 CRM_Core_DAO
::storeValues($dao, $value);
315 if (array_key_exists('parent_id', $value)) {
316 $value['parent'] = $dao->parent
;
317 $value['parent_label'] = $dao->parent_label
;
319 $_cache[$argString][$dao->name
] = $value;
322 $cache->set($argString, $_cache[$argString]);
326 return $_cache[$argString];
330 * Retrieve basic type pairs with name as 'built-in name' and 'label' as value.
333 * @param null $typeName
334 * @param null $delimiter
337 * Array of basictypes with name as 'built-in name' and 'label' as value
339 public static function contactTypePairs($all = FALSE, $typeName = NULL, $delimiter = NULL) {
340 $types = self
::contactTypeInfo($all);
342 if ($typeName && !is_array($typeName)) {
343 $typeName = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, trim($typeName, CRM_Core_DAO
::VALUE_SEPARATOR
));
348 foreach ($typeName as $type) {
349 if (array_key_exists($type, $types)) {
350 $pairs[$type] = $types[$type]['label'];
355 foreach ($types as $name => $info) {
356 $pairs[$name] = $info['label'];
360 return !$delimiter ?
$pairs : implode($delimiter, $pairs);
364 * Get a list of elements for select box.
365 * Note that this used to default to using the hex(01) character - which results in an invalid character being used in form fields
366 * which was not handled well be anything that loaded & resaved the html (outside core)
367 * The use of this separator is now explicit in the calling functions as a step towards it's removal
370 * @param bool $isSeparator
371 * @param string $separator
375 public static function getSelectElements(
380 static $_cache = NULL;
382 if ($_cache === NULL) {
386 $argString = $all ?
'CRM_CT_GSE_1' : 'CRM_CT_GSE_0';
387 $argString .= $isSeparator ?
'_1' : '_0';
388 $argString .= $separator;
389 if (!array_key_exists($argString, $_cache)) {
390 $cache = CRM_Utils_Cache
::singleton();
391 $_cache[$argString] = $cache->get($argString);
393 if (!$_cache[$argString]) {
394 $_cache[$argString] = array();
397 SELECT c.name as child_name , c.label as child_label , c.id as child_id,
398 p.name as parent_name, p.label as parent_label, p.id as parent_id
399 FROM civicrm_contact_type c
400 LEFT JOIN civicrm_contact_type p ON ( c.parent_id = p.id )
401 WHERE ( c.name IS NOT NULL )
404 if ($all === FALSE) {
407 AND ( p.is_active = 1 OR p.id IS NULL )
410 $sql .= " ORDER BY c.id";
413 $dao = CRM_Core_DAO
::executeQuery($sql);
414 while ($dao->fetch()) {
415 if (!empty($dao->parent_id
)) {
416 $key = $isSeparator ?
$dao->parent_name
. $separator . $dao->child_name
: $dao->child_name
;
417 $label = "- {$dao->child_label}";
418 $pName = $dao->parent_name
;
421 $key = $dao->child_name
;
422 $label = $dao->child_label
;
423 $pName = $dao->child_name
;
426 if (!isset($values[$pName])) {
427 $values[$pName] = array();
429 $values[$pName][] = array('key' => $key, 'label' => $label);
432 $selectElements = array();
433 foreach ($values as $pName => $elements) {
434 foreach ($elements as $element) {
435 $selectElements[$element['key']] = $element['label'];
438 $_cache[$argString] = $selectElements;
440 $cache->set($argString, $_cache[$argString]);
443 return $_cache[$argString];
447 * Check if a given type is a subtype.
449 * @param string $subType
451 * @param bool $ignoreCache
454 * true if subType, false otherwise.
456 public static function isaSubType($subType, $ignoreCache = FALSE) {
457 return in_array($subType, self
::subTypes(NULL, TRUE, 'name', $ignoreCache));
461 * Retrieve the basic contact type associated with given subType.
463 * @param array /string $subType contact subType.
464 * @return array/string of basicTypes.
466 public static function getBasicType($subType) {
467 static $_cache = NULL;
468 if ($_cache === NULL) {
473 if ($subType && !is_array($subType)) {
474 $subType = array($subType);
477 $argString = implode('_', $subType);
479 if (!array_key_exists($argString, $_cache)) {
480 $_cache[$argString] = array();
483 SELECT subtype.name as contact_subtype, type.name as contact_type
484 FROM civicrm_contact_type subtype
485 INNER JOIN civicrm_contact_type type ON ( subtype.parent_id = type.id )
486 WHERE subtype.name IN ('" . implode("','", $subType) . "' )";
487 $dao = CRM_Core_DAO
::executeQuery($sql);
488 while ($dao->fetch()) {
490 $_cache[$argString] = $dao->contact_type
;
493 $_cache[$argString][$dao->contact_subtype
] = $dao->contact_type
;
496 return $_cache[$argString];
500 * Suppress all subtypes present in given array.
502 * @param array $subTypes
504 * @param bool $ignoreCache
507 * Array of suppressed subTypes.
509 public static function suppressSubTypes(&$subTypes, $ignoreCache = FALSE) {
510 $subTypes = array_diff($subTypes, self
::subTypes(NULL, TRUE, 'name', $ignoreCache));
515 * Verify if a given subtype is associated with a given basic contact type.
517 * @param string $subType
519 * @param string $contactType
521 * @param bool $ignoreCache
522 * @param string $columnName
525 * true if contact extends, false otherwise.
527 public static function isExtendsContactType($subType, $contactType, $ignoreCache = FALSE, $columnName = 'name') {
528 $subType = (array) CRM_Utils_Array
::explodePadded($subType);
529 $subtypeList = self
::subTypes($contactType, TRUE, $columnName, $ignoreCache);
530 $intersection = array_intersect($subType, $subtypeList);
531 return $subType == $intersection;
535 * Create shortcuts menu for contactTypes.
540 public static function getCreateNewList() {
541 $shortCuts = array();
542 //@todo FIXME - using the CRM_Core_DAO::VALUE_SEPARATOR creates invalid html - if you can find the form
543 // this is loaded onto then replace with something like '__' & test
544 $separator = CRM_Core_DAO
::VALUE_SEPARATOR
;
545 $contactTypes = self
::getSelectElements(FALSE, TRUE, $separator);
546 foreach ($contactTypes as $key => $value) {
548 $typeValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $key);
549 $cType = CRM_Utils_Array
::value('0', $typeValue);
550 $typeUrl = 'ct=' . $cType;
551 if ($csType = CRM_Utils_Array
::value('1', $typeValue)) {
552 $typeUrl .= "&cst=$csType";
555 'path' => 'civicrm/contact/add',
556 'query' => "$typeUrl&reset=1",
557 'ref' => "new-$value",
560 if ($csType = CRM_Utils_Array
::value('1', $typeValue)) {
561 $shortCuts[$cType]['shortCuts'][] = $shortCut;
564 $shortCuts[$cType] = $shortCut;
572 * Delete Contact SubTypes.
574 * @param int $contactTypeId
575 * ID of the Contact Subtype to be deleted.
579 public static function del($contactTypeId) {
581 if (!$contactTypeId) {
585 $params = array('id' => $contactTypeId);
586 self
::retrieve($params, $typeInfo);
587 $name = $typeInfo['name'];
588 // check if any custom group
589 $custom = new CRM_Core_DAO_CustomGroup();
590 $custom->whereAdd("extends_entity_column_value LIKE '%" .
591 CRM_Core_DAO
::VALUE_SEPARATOR
.
593 CRM_Core_DAO
::VALUE_SEPARATOR
. "%'"
595 if ($custom->find()) {
599 // remove subtype for existing contacts
601 UPDATE civicrm_contact SET contact_sub_type = NULL
602 WHERE contact_sub_type = '$name'";
603 CRM_Core_DAO
::executeQuery($sql);
605 // remove subtype from contact type table
606 $contactType = new CRM_Contact_DAO_ContactType();
607 $contactType->id
= $contactTypeId;
608 $contactType->delete();
610 // remove navigation entry if any
614 FROM civicrm_navigation
616 $params = array(1 => array("New $name", 'String'));
617 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
618 CRM_Core_BAO_Navigation
::resetNavigation();
624 * Add or update Contact SubTypes.
626 * @param array $params
627 * An assoc array of name/value pairs.
629 * @return object|void
631 public static function add(&$params) {
634 if (empty($params['id']) && empty($params['label'])) {
637 if (!empty($params['parent_id']) &&
638 !CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_ContactType', $params['parent_id'])
643 $contactType = new CRM_Contact_DAO_ContactType();
644 $contactType->copyValues($params);
645 $contactType->id
= CRM_Utils_Array
::value('id', $params);
646 $contactType->is_active
= CRM_Utils_Array
::value('is_active', $params, 0);
648 $contactType->save();
649 if ($contactType->find(TRUE)) {
650 $contactName = $contactType->name
;
651 $contact = ucfirst($contactType->label
);
652 $active = $contactType->is_active
;
655 if (!empty($params['id'])) {
656 $params = array('name' => "New $contactName");
658 'label' => "New $contact",
659 'is_active' => $active,
661 CRM_Core_BAO_Navigation
::processUpdate($params, $newParams);
664 $name = self
::getBasicType($contactName);
668 $value = array('name' => "New $name");
669 CRM_Core_BAO_Navigation
::retrieve($value, $navinfo);
671 'label' => "New $contact",
672 'name' => "New $contactName",
673 'url' => "civicrm/contact/add?ct=$name&cst=$contactName&reset=1",
674 'permission' => 'add contacts',
675 'parent_id' => $navinfo['id'],
676 'is_active' => $active,
678 CRM_Core_BAO_Navigation
::add($navigation);
680 CRM_Core_BAO_Navigation
::resetNavigation();
682 // reset the cache after adding
683 self
::subTypeInfo(NULL, FALSE, FALSE, TRUE);
689 * Update the is_active flag in the db.
692 * Id of the database record.
693 * @param bool $is_active
694 * Value we want to set the is_active field.
697 * DAO object on success, null otherwise
699 public static function setIsActive($id, $is_active) {
700 $params = array('id' => $id);
701 self
::retrieve($params, $contactinfo);
702 $params = array('name' => "New $contactinfo[name]");
703 $newParams = array('is_active' => $is_active);
704 CRM_Core_BAO_Navigation
::processUpdate($params, $newParams);
705 CRM_Core_BAO_Navigation
::resetNavigation();
706 return CRM_Core_DAO
::setFieldValue('CRM_Contact_DAO_ContactType', $id,
707 'is_active', $is_active
712 * @param string $typeName
716 public static function getLabel($typeName) {
717 $types = self
::contactTypeInfo(TRUE);
719 if (array_key_exists($typeName, $types)) {
720 return $types[$typeName]['label'];
726 * Check whether allow to change any contact's subtype
727 * on the basis of custom data and relationship of specific subtype
728 * currently used in contact/edit form amd in import validation
730 * @param int $contactId
732 * @param string $subType
737 public static function isAllowEdit($contactId, $subType = NULL) {
743 if (empty($subType)) {
744 $subType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
750 if (self
::hasCustomData($subType, $contactId) || self
::hasRelationships($contactId, $subType)) {
758 * @param $contactType
759 * @param int $contactId
763 public static function hasCustomData($contactType, $contactId = NULL) {
766 if (self
::isaSubType($contactType)) {
767 $subType = $contactType;
768 $contactType = self
::getBasicType($subType);
770 // check for empty custom data which extends subtype
771 $subTypeValue = CRM_Core_DAO
::VALUE_SEPARATOR
. $subType . CRM_Core_DAO
::VALUE_SEPARATOR
;
772 $subTypeClause = " AND extends_entity_column_value LIKE '%{$subTypeValue}%' ";
774 $query = "SELECT table_name FROM civicrm_custom_group WHERE extends = '{$contactType}' {$subTypeClause}";
776 $dao = CRM_Core_DAO
::executeQuery($query);
777 while ($dao->fetch()) {
778 $sql = "SELECT count(id) FROM {$dao->table_name}";
780 $sql .= " WHERE entity_id = {$contactId}";
784 $customDataCount = CRM_Core_DAO
::singleValueQuery($sql);
785 if (!empty($customDataCount)) {
794 * @todo what does this function do?
795 * @param int $contactId
796 * @param $contactType
800 public static function hasRelationships($contactId, $contactType) {
801 $subTypeClause = NULL;
802 if (self
::isaSubType($contactType)) {
803 $subType = $contactType;
804 $contactType = self
::getBasicType($subType);
805 $subTypeClause = " AND ( ( crt.contact_type_a = '{$contactType}' AND crt.contact_sub_type_a = '{$subType}') OR
806 ( crt.contact_type_b = '{$contactType}' AND crt.contact_sub_type_b = '{$subType}') ) ";
809 $subTypeClause = " AND ( crt.contact_type_a = '{$contactType}' OR crt.contact_type_b = '{$contactType}' ) ";
812 // check relationships for
813 $relationshipQuery = "
814 SELECT count(cr.id) FROM civicrm_relationship cr
815 INNER JOIN civicrm_relationship_type crt ON
816 ( cr.relationship_type_id = crt.id {$subTypeClause} )
817 WHERE ( cr.contact_id_a = {$contactId} OR cr.contact_id_b = {$contactId} )
820 $relationshipCount = CRM_Core_DAO
::singleValueQuery($relationshipQuery);
822 if (!empty($relationshipCount)) {
830 * @todo what does this function do?
831 * @param $contactType
832 * @param array $subtypeSet
836 public static function getSubtypeCustomPair($contactType, $subtypeSet = array()) {
837 if (empty($subtypeSet)) {
841 $customSet = $subTypeClause = array();
842 foreach ($subtypeSet as $subtype) {
843 $subtype = CRM_Utils_Type
::escape($subtype, 'String');
844 $subType = CRM_Core_DAO
::VALUE_SEPARATOR
. $subtype . CRM_Core_DAO
::VALUE_SEPARATOR
;
845 $subTypeClause[] = "extends_entity_column_value LIKE '%{$subtype}%' ";
847 $query = "SELECT table_name
848 FROM civicrm_custom_group
849 WHERE extends = %1 AND " . implode(" OR ", $subTypeClause);
850 $dao = CRM_Core_DAO
::executeQuery($query, array(1 => array($contactType, 'String')));
851 while ($dao->fetch()) {
852 $customSet[] = $dao->table_name
;
854 return array_unique($customSet);
858 * Function that does something.
859 * @todo what does this function do?
861 * @param int $contactID
862 * @param $contactType
863 * @param array $oldSubtypeSet
864 * @param array $newSubtypeSet
868 public static function deleteCustomSetForSubtypeMigration(
871 $oldSubtypeSet = array(),
872 $newSubtypeSet = array()
874 $oldCustomSet = self
::getSubtypeCustomPair($contactType, $oldSubtypeSet);
875 $newCustomSet = self
::getSubtypeCustomPair($contactType, $newSubtypeSet);
877 $customToBeRemoved = array_diff($oldCustomSet, $newCustomSet);
878 foreach ($customToBeRemoved as $customTable) {
879 self
::deleteCustomRowsForEntityID($customTable, $contactID);
885 * Delete content / rows of a custom table specific to a subtype for a given custom-group.
886 * This function currently works for contact subtypes only and could be later improved / genralized
887 * to work for other subtypes as well.
891 * @param array $subtypes
892 * List of subtypes related to which entry is to be removed.
896 public static function deleteCustomRowsOfSubtype($gID, $subtypes = array(), $subtypesToPreserve = array()) {
897 if (!$gID or empty($subtypes)) {
901 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $gID, 'table_name');
903 // drop triggers CRM-13587
904 CRM_Core_DAO
::dropTriggers($tableName);
906 foreach ($subtypesToPreserve as $subtypeToPreserve) {
907 $subtypeToPreserve = CRM_Utils_Type
::escape($subtypeToPreserve, 'String');
908 $subtypesToPreserveClause[] = "(civicrm_contact.contact_sub_type NOT LIKE '%" . CRM_Core_DAO
::VALUE_SEPARATOR
. $subtypeToPreserve . CRM_Core_DAO
::VALUE_SEPARATOR
. "%')";
910 $subtypesToPreserveClause = implode(' AND ', $subtypesToPreserveClause);
912 $subtypeClause = array();
913 foreach ($subtypes as $subtype) {
914 $subtype = CRM_Utils_Type
::escape($subtype, 'String');
915 $subtypeClause[] = "( civicrm_contact.contact_sub_type LIKE '%" . CRM_Core_DAO
::VALUE_SEPARATOR
. $subtype . CRM_Core_DAO
::VALUE_SEPARATOR
. "%'"
916 . " AND " . $subtypesToPreserveClause . ")";
918 $subtypeClause = implode(' OR ', $subtypeClause);
920 $query = "DELETE custom.*
921 FROM {$tableName} custom
922 INNER JOIN civicrm_contact ON civicrm_contact.id = custom.entity_id
923 WHERE ($subtypeClause)";
925 CRM_Core_DAO
::singleValueQuery($query);
927 // rebuild triggers CRM-13587
928 CRM_Core_DAO
::triggerRebuild($tableName);
932 * Delete content / rows of a custom table specific entity-id for a given custom-group table.
934 * @param int $customTable
936 * @param int $entityID
939 * @return null|string
941 public static function deleteCustomRowsForEntityID($customTable, $entityID) {
942 $customTable = CRM_Utils_Type
::escape($customTable, 'String');
943 $query = "DELETE FROM {$customTable} WHERE entity_id = %1";
944 return CRM_Core_DAO
::singleValueQuery($query, array(1 => array($entityID, 'Integer')));