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