CRM-16065 - Custom field set data not being saved if contact has more than one contac...
[civicrm-core.git] / CRM / Contact / BAO / ContactType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
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';
6a488035
TO
389 if (!array_key_exists($argString, $_cache)) {
390 $cache = CRM_Utils_Cache::singleton();
391 $_cache[$argString] = $cache->get($argString);
392
393 if (!$_cache[$argString]) {
394 $_cache[$argString] = array();
395
396 $sql = "
397SELECT 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
399FROM civicrm_contact_type c
400LEFT JOIN civicrm_contact_type p ON ( c.parent_id = p.id )
401WHERE ( c.name IS NOT NULL )
402";
403
404 if ($all === FALSE) {
405 $sql .= "
406AND c.is_active = 1
407AND ( p.is_active = 1 OR p.id IS NULL )
408";
409 }
410 $sql .= " ORDER BY c.id";
411
412 $values = array();
413 $dao = CRM_Core_DAO::executeQuery($sql);
414 while ($dao->fetch()) {
415 if (!empty($dao->parent_id)) {
353ffa53 416 $key = $isSeparator ? $dao->parent_name . $separator . $dao->child_name : $dao->child_name;
bee6039a 417 $label = "- {$dao->child_label}";
6a488035
TO
418 $pName = $dao->parent_name;
419 }
420 else {
353ffa53 421 $key = $dao->child_name;
6a488035
TO
422 $label = $dao->child_label;
423 $pName = $dao->child_name;
424 }
425
426 if (!isset($values[$pName])) {
427 $values[$pName] = array();
428 }
429 $values[$pName][] = array('key' => $key, 'label' => $label);
430 }
431
432 $selectElements = array();
433 foreach ($values as $pName => $elements) {
434 foreach ($elements as $element) {
435 $selectElements[$element['key']] = $element['label'];
436 }
437 }
438 $_cache[$argString] = $selectElements;
439
440 $cache->set($argString, $_cache[$argString]);
441 }
442 }
443 return $_cache[$argString];
444 }
445
446 /**
fe482240 447 * Check if a given type is a subtype.
6a488035 448 *
77c5b619
TO
449 * @param string $subType
450 * Contact subType.
f4aaa82a 451 * @param bool $ignoreCache
6a488035 452 *
bed98343 453 * @return bool
a6c01b45 454 * true if subType, false otherwise.
6a488035 455 */
00be9182 456 public static function isaSubType($subType, $ignoreCache = FALSE) {
6a488035
TO
457 return in_array($subType, self::subTypes(NULL, TRUE, 'name', $ignoreCache));
458 }
459
460 /**
100fef9d 461 * Retrieve the basic contact type associated with given subType.
6a488035 462 *
353ffa53 463 * @param array /string $subType contact subType.
51ccfbbe 464 * @return array/string of basicTypes.
6a488035 465 */
00be9182 466 public static function getBasicType($subType) {
6a488035
TO
467 static $_cache = NULL;
468 if ($_cache === NULL) {
469 $_cache = array();
470 }
471
472 $isArray = TRUE;
473 if ($subType && !is_array($subType)) {
474 $subType = array($subType);
475 $isArray = FALSE;
476 }
477 $argString = implode('_', $subType);
478
479 if (!array_key_exists($argString, $_cache)) {
480 $_cache[$argString] = array();
481
482 $sql = "
483SELECT subtype.name as contact_subtype, type.name as contact_type
484FROM civicrm_contact_type subtype
485INNER JOIN civicrm_contact_type type ON ( subtype.parent_id = type.id )
486WHERE subtype.name IN ('" . implode("','", $subType) . "' )";
487 $dao = CRM_Core_DAO::executeQuery($sql);
488 while ($dao->fetch()) {
489 if (!$isArray) {
490 $_cache[$argString] = $dao->contact_type;
491 break;
492 }
493 $_cache[$argString][$dao->contact_subtype] = $dao->contact_type;
494 }
495 }
496 return $_cache[$argString];
497 }
498
499 /**
c490a46a 500 * Suppress all subtypes present in given array.
6a488035 501 *
77c5b619
TO
502 * @param array $subTypes
503 * Contact subTypes.
f4aaa82a 504 * @param bool $ignoreCache
6a488035 505 *
a6c01b45 506 * @return array
16b10e64 507 * Array of suppressed subTypes.
6a488035 508 */
00be9182 509 public static function suppressSubTypes(&$subTypes, $ignoreCache = FALSE) {
6a488035
TO
510 $subTypes = array_diff($subTypes, self::subTypes(NULL, TRUE, 'name', $ignoreCache));
511 return $subTypes;
512 }
513
514 /**
100fef9d 515 * Verify if a given subtype is associated with a given basic contact type.
6a488035 516 *
77c5b619
TO
517 * @param string $subType
518 * Contact subType.
519 * @param string $contactType
520 * Contact Type.
f4aaa82a
EM
521 * @param bool $ignoreCache
522 * @param string $columnName
6a488035 523 *
bed98343 524 * @return bool
a6c01b45 525 * true if contact extends, false otherwise.
6a488035 526 */
00be9182 527 public static function isExtendsContactType($subType, $contactType, $ignoreCache = FALSE, $columnName = 'name') {
6a488035
TO
528 if (!is_array($subType)) {
529 $subType = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($subType, CRM_Core_DAO::VALUE_SEPARATOR));
530 }
531 $subtypeList = self::subTypes($contactType, TRUE, $columnName, $ignoreCache);
532 $intersection = array_intersect($subType, $subtypeList);
533 return $subType == $intersection;
534 }
535
536 /**
fe482240 537 * Create shortcuts menu for contactTypes.
6a488035 538 *
a6c01b45
CW
539 * @return array
540 * of contactTypes
6a488035 541 */
00be9182 542 public static function getCreateNewList() {
6a488035 543 $shortCuts = array();
b500fbea
EM
544 //@todo FIXME - using the CRM_Core_DAO::VALUE_SEPARATOR creates invalid html - if you can find the form
545 // this is loaded onto then replace with something like '__' & test
546 $separator = CRM_Core_DAO::VALUE_SEPARATOR;
547 $contactTypes = self::getSelectElements(FALSE, TRUE, $separator);
6a488035
TO
548 foreach ($contactTypes as $key => $value) {
549 if ($key) {
550 $typeValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, $key);
7926b999 551 $cType = CRM_Utils_Array::value('0', $typeValue);
552 $typeUrl = 'ct=' . $cType;
6a488035
TO
553 if ($csType = CRM_Utils_Array::value('1', $typeValue)) {
554 $typeUrl .= "&cst=$csType";
555 }
7926b999 556 $shortCut = array(
6a488035
TO
557 'path' => 'civicrm/contact/add',
558 'query' => "$typeUrl&reset=1",
559 'ref' => "new-$value",
560 'title' => $value,
561 );
7926b999 562 if ($csType = CRM_Utils_Array::value('1', $typeValue)) {
563 $shortCuts[$cType]['shortCuts'][] = $shortCut;
564 }
565 else {
566 $shortCuts[$cType] = $shortCut;
567 }
6a488035
TO
568 }
569 }
570 return $shortCuts;
571 }
572
573 /**
fe482240 574 * Delete Contact SubTypes.
6a488035 575 *
77c5b619
TO
576 * @param int $contactTypeId
577 * ID of the Contact Subtype to be deleted.
6a488035 578 *
f4aaa82a 579 * @return bool
6a488035 580 */
00be9182 581 public static function del($contactTypeId) {
6a488035
TO
582
583 if (!$contactTypeId) {
584 return FALSE;
585 }
586
587 $params = array('id' => $contactTypeId);
588 self::retrieve($params, $typeInfo);
589 $name = $typeInfo['name'];
590 // check if any custom group
591 $custom = new CRM_Core_DAO_CustomGroup();
592 $custom->whereAdd("extends_entity_column_value LIKE '%" .
593 CRM_Core_DAO::VALUE_SEPARATOR .
594 $name .
595 CRM_Core_DAO::VALUE_SEPARATOR . "%'"
596 );
597 if ($custom->find()) {
598 return FALSE;
599 }
600
601 // remove subtype for existing contacts
602 $sql = "
603UPDATE civicrm_contact SET contact_sub_type = NULL
604WHERE contact_sub_type = '$name'";
605 CRM_Core_DAO::executeQuery($sql);
606
607 // remove subtype from contact type table
608 $contactType = new CRM_Contact_DAO_ContactType();
609 $contactType->id = $contactTypeId;
610 $contactType->delete();
611
612 // remove navigation entry if any
613 if ($name) {
614 $sql = "
615DELETE
616FROM civicrm_navigation
617WHERE name = %1";
618 $params = array(1 => array("New $name", 'String'));
619 $dao = CRM_Core_DAO::executeQuery($sql, $params);
620 CRM_Core_BAO_Navigation::resetNavigation();
621 }
622 return TRUE;
623 }
624
625 /**
fe482240 626 * Add or update Contact SubTypes.
6a488035 627 *
77c5b619
TO
628 * @param array $params
629 * An assoc array of name/value pairs.
6a488035 630 *
bed98343 631 * @return object|void
6a488035 632 */
00be9182 633 public static function add(&$params) {
6a488035
TO
634
635 // label or name
0f77a625 636 if (empty($params['id']) && empty($params['label'])) {
bed98343 637 return NULL;
6a488035 638 }
a7488080 639 if (!empty($params['parent_id']) &&
6a488035
TO
640 !CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_ContactType', $params['parent_id'])
641 ) {
bed98343 642 return NULL;
6a488035
TO
643 }
644
645 $contactType = new CRM_Contact_DAO_ContactType();
646 $contactType->copyValues($params);
647 $contactType->id = CRM_Utils_Array::value('id', $params);
648 $contactType->is_active = CRM_Utils_Array::value('is_active', $params, 0);
649
6a488035
TO
650 $contactType->save();
651 if ($contactType->find(TRUE)) {
652 $contactName = $contactType->name;
353ffa53
TO
653 $contact = ucfirst($contactType->label);
654 $active = $contactType->is_active;
6a488035
TO
655 }
656
a7488080 657 if (!empty($params['id'])) {
6a488035
TO
658 $params = array('name' => "New $contactName");
659 $newParams = array(
660 'label' => "New $contact",
661 'is_active' => $active,
662 );
663 CRM_Core_BAO_Navigation::processUpdate($params, $newParams);
664 }
665 else {
666 $name = self::getBasicType($contactName);
667 if (!$name) {
668 return;
669 }
670 $value = array('name' => "New $name");
671 CRM_Core_BAO_Navigation::retrieve($value, $navinfo);
672 $navigation = array(
673 'label' => "New $contact",
674 'name' => "New $contactName",
15a7ea79 675 'url' => "civicrm/contact/add?ct=$name&cst=$contactName&reset=1",
6a488035
TO
676 'permission' => 'add contacts',
677 'parent_id' => $navinfo['id'],
678 'is_active' => $active,
679 );
680 CRM_Core_BAO_Navigation::add($navigation);
681 }
682 CRM_Core_BAO_Navigation::resetNavigation();
683
684 // reset the cache after adding
685 self::subTypeInfo(NULL, FALSE, FALSE, TRUE);
686
687 return $contactType;
688 }
689
690 /**
fe482240 691 * Update the is_active flag in the db.
6a488035 692 *
77c5b619
TO
693 * @param int $id
694 * Id of the database record.
695 * @param bool $is_active
696 * Value we want to set the is_active field.
6a488035 697 *
a6c01b45
CW
698 * @return Object
699 * DAO object on success, null otherwise
6a488035 700 */
00be9182 701 public static function setIsActive($id, $is_active) {
6a488035
TO
702 $params = array('id' => $id);
703 self::retrieve($params, $contactinfo);
704 $params = array('name' => "New $contactinfo[name]");
705 $newParams = array('is_active' => $is_active);
706 CRM_Core_BAO_Navigation::processUpdate($params, $newParams);
707 CRM_Core_BAO_Navigation::resetNavigation();
708 return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_ContactType', $id,
709 'is_active', $is_active
710 );
711 }
712
86538308 713 /**
100fef9d 714 * @param string $typeName
86538308
EM
715 *
716 * @return mixed
717 */
00be9182 718 public static function getLabel($typeName) {
6a488035
TO
719 $types = self::contactTypeInfo(TRUE);
720
721 if (array_key_exists($typeName, $types)) {
722 return $types[$typeName]['label'];
723 }
724 return $typeName;
725 }
726
727 /**
100fef9d 728 * Check whether allow to change any contact's subtype
6a488035
TO
729 * on the basis of custom data and relationship of specific subtype
730 * currently used in contact/edit form amd in import validation
731 *
77c5b619
TO
732 * @param int $contactId
733 * Contact id.
734 * @param string $subType
735 * Subtype.
6a488035 736 *
bed98343 737 * @return bool
6a488035 738 */
00be9182 739 public static function isAllowEdit($contactId, $subType = NULL) {
6a488035
TO
740
741 if (!$contactId) {
742 return TRUE;
743 }
744
745 if (empty($subType)) {
746 $subType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
747 $contactId,
748 'contact_sub_type'
749 );
750 }
751
752 if (self::hasCustomData($subType, $contactId) || self::hasRelationships($contactId, $subType)) {
753 return FALSE;
754 }
755
756 return TRUE;
757 }
758
86538308
EM
759 /**
760 * @param $contactType
100fef9d 761 * @param int $contactId
86538308
EM
762 *
763 * @return bool
764 */
00be9182 765 public static function hasCustomData($contactType, $contactId = NULL) {
6a488035
TO
766 $subTypeClause = '';
767
768 if (self::isaSubType($contactType)) {
769 $subType = $contactType;
770 $contactType = self::getBasicType($subType);
771
772 // check for empty custom data which extends subtype
773 $subTypeValue = CRM_Core_DAO::VALUE_SEPARATOR . $subType . CRM_Core_DAO::VALUE_SEPARATOR;
774 $subTypeClause = " AND extends_entity_column_value LIKE '%{$subTypeValue}%' ";
775 }
776 $query = "SELECT table_name FROM civicrm_custom_group WHERE extends = '{$contactType}' {$subTypeClause}";
777
778 $dao = CRM_Core_DAO::executeQuery($query);
779 while ($dao->fetch()) {
780 $sql = "SELECT count(id) FROM {$dao->table_name}";
781 if ($contactId) {
782 $sql .= " WHERE entity_id = {$contactId}";
783 }
784 $sql .= " LIMIT 1";
785
786 $customDataCount = CRM_Core_DAO::singleValueQuery($sql);
787 if (!empty($customDataCount)) {
788 $dao->free();
789 return TRUE;
790 }
791 }
792 return FALSE;
793 }
794
f4aaa82a
EM
795 /**
796 * @todo what does this function do?
100fef9d 797 * @param int $contactId
f4aaa82a
EM
798 * @param $contactType
799 *
800 * @return bool
801 */
00be9182 802 public static function hasRelationships($contactId, $contactType) {
6a488035
TO
803 $subTypeClause = NULL;
804 if (self::isaSubType($contactType)) {
353ffa53
TO
805 $subType = $contactType;
806 $contactType = self::getBasicType($subType);
6a488035
TO
807 $subTypeClause = " AND ( ( crt.contact_type_a = '{$contactType}' AND crt.contact_sub_type_a = '{$subType}') OR
808 ( crt.contact_type_b = '{$contactType}' AND crt.contact_sub_type_b = '{$subType}') ) ";
809 }
810 else {
811 $subTypeClause = " AND ( crt.contact_type_a = '{$contactType}' OR crt.contact_type_b = '{$contactType}' ) ";
812 }
813
814 // check relationships for
815 $relationshipQuery = "
816SELECT count(cr.id) FROM civicrm_relationship cr
817INNER JOIN civicrm_relationship_type crt ON
818( cr.relationship_type_id = crt.id {$subTypeClause} )
819WHERE ( cr.contact_id_a = {$contactId} OR cr.contact_id_b = {$contactId} )
820LIMIT 1";
821
822 $relationshipCount = CRM_Core_DAO::singleValueQuery($relationshipQuery);
823
824 if (!empty($relationshipCount)) {
825 return TRUE;
826 }
827
828 return FALSE;
829 }
830
f4aaa82a
EM
831 /**
832 * @todo what does this function do?
833 * @param $contactType
834 * @param array $subtypeSet
835 *
836 * @return array
837 */
00be9182 838 public static function getSubtypeCustomPair($contactType, $subtypeSet = array()) {
6a488035
TO
839 if (empty($subtypeSet)) {
840 return $subtypeSet;
841 }
842
843 $customSet = $subTypeClause = array();
844 foreach ($subtypeSet as $subtype) {
353ffa53
TO
845 $subtype = CRM_Utils_Type::escape($subtype, 'String');
846 $subType = CRM_Core_DAO::VALUE_SEPARATOR . $subtype . CRM_Core_DAO::VALUE_SEPARATOR;
6a488035
TO
847 $subTypeClause[] = "extends_entity_column_value LIKE '%{$subtype}%' ";
848 }
849 $query = "SELECT table_name
850FROM civicrm_custom_group
851WHERE extends = %1 AND " . implode(" OR ", $subTypeClause);
852 $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($contactType, 'String')));
853 while ($dao->fetch()) {
854 $customSet[] = $dao->table_name;
855 }
856 return array_unique($customSet);
857 }
858
f4aaa82a 859 /**
fe482240 860 * Function that does something.
f4aaa82a
EM
861 * @todo what does this function do?
862 *
100fef9d 863 * @param int $contactID
f4aaa82a
EM
864 * @param $contactType
865 * @param array $oldSubtypeSet
866 * @param array $newSubtypeSet
867 *
868 * @return bool
869 */
bed98343 870 public static function deleteCustomSetForSubtypeMigration(
51ccfbbe 871 $contactID,
6a488035
TO
872 $contactType,
873 $oldSubtypeSet = array(),
874 $newSubtypeSet = array()
875 ) {
876 $oldCustomSet = self::getSubtypeCustomPair($contactType, $oldSubtypeSet);
877 $newCustomSet = self::getSubtypeCustomPair($contactType, $newSubtypeSet);
878
879 $customToBeRemoved = array_diff($oldCustomSet, $newCustomSet);
880 foreach ($customToBeRemoved as $customTable) {
881 self::deleteCustomRowsForEntityID($customTable, $contactID);
882 }
883 return TRUE;
884 }
885
886 /**
887 * Delete content / rows of a custom table specific to a subtype for a given custom-group.
888 * This function currently works for contact subtypes only and could be later improved / genralized
889 * to work for other subtypes as well.
890 *
77c5b619
TO
891 * @param int $gID
892 * Custom group id.
893 * @param array $subtypes
894 * List of subtypes related to which entry is to be removed.
6a488035
TO
895 *
896 * @return void
6a488035 897 */
66ed0bee 898 public static function deleteCustomRowsOfSubtype($gID, $subtypes = array()) {
6a488035
TO
899 if (!$gID or empty($subtypes)) {
900 return FALSE;
901 }
902
903 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $gID, 'table_name');
904
66ed0bee 905 // drop triggers CRM-13587
906 CRM_Core_DAO::dropTriggers($tableName);
907
6a488035
TO
908 $subtypeClause = array();
909 foreach ($subtypes as $subtype) {
910 $subtype = CRM_Utils_Type::escape($subtype, 'String');
911 $subtypeClause[] = "civicrm_contact.contact_sub_type LIKE '%" . CRM_Core_DAO::VALUE_SEPARATOR . $subtype . CRM_Core_DAO::VALUE_SEPARATOR . "%'";
912 }
913 $subtypeClause = implode(' OR ', $subtypeClause);
914
915 $query = "DELETE custom.*
916FROM {$tableName} custom
917INNER JOIN civicrm_contact ON civicrm_contact.id = custom.entity_id
918WHERE ($subtypeClause)";
66ed0bee 919
920 CRM_Core_DAO::singleValueQuery($query);
921
922 // rebuild triggers CRM-13587
923 CRM_Core_DAO::triggerRebuild($tableName);
6a488035
TO
924 }
925
926 /**
927 * Delete content / rows of a custom table specific entity-id for a given custom-group table.
928 *
77c5b619
TO
929 * @param int $customTable
930 * Custom table name.
931 * @param int $entityID
932 * Entity id.
6a488035
TO
933 *
934 * @return void
6a488035 935 */
d9ef38cc 936 public static function deleteCustomRowsForEntityID($customTable, $entityID) {
6a488035
TO
937 $customTable = CRM_Utils_Type::escape($customTable, 'String');
938 $query = "DELETE FROM {$customTable} WHERE entity_id = %1";
939 return CRM_Core_DAO::singleValueQuery($query, array(1 => array($entityID, 'Integer')));
940 }
96025800 941
6a488035 942}