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