$value) { $cond .= " AND " . $key . " = " . $value; } } $query = " SELECT phone, civicrm_location_type.name as locationType, civicrm_phone.is_primary as is_primary, civicrm_phone.id as phone_id, civicrm_phone.location_type_id as locationTypeId, civicrm_phone.phone_type_id as phoneTypeId FROM civicrm_contact LEFT JOIN civicrm_phone ON ( civicrm_contact.id = civicrm_phone.contact_id ) LEFT JOIN civicrm_location_type ON ( civicrm_phone.location_type_id = civicrm_location_type.id ) WHERE civicrm_contact.id = %1 $cond ORDER BY civicrm_phone.is_primary DESC, phone_id ASC "; $params = [ 1 => [ $id, 'Integer', ], ]; $numbers = $values = []; $dao = CRM_Core_DAO::executeQuery($query, $params); $count = 1; while ($dao->fetch()) { $values = [ 'locationType' => $dao->locationType, 'is_primary' => $dao->is_primary, 'id' => $dao->phone_id, 'phone' => $dao->phone, 'locationTypeId' => $dao->locationTypeId, 'phoneTypeId' => $dao->phoneTypeId, ]; if ($updateBlankLocInfo) { $numbers[$count++] = $values; } else { $numbers[$dao->phone_id] = $values; } } return $numbers; } /** * Get all the phone numbers for a specified location_block id, with the primary phone being first. * * This is called from CRM_Core_BAO_Block as a calculated function. * * @param array $entityElements * The array containing entity_id and. * entity_table name * * @param null $type * * @return array * the array of phone ids which are potential numbers */ public static function allEntityPhones($entityElements, $type = NULL) { if (empty($entityElements)) { return NULL; } $cond = NULL; if ($type) { $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id')); if ($phoneTypeId) { $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId"; } } $entityId = $entityElements['entity_id']; $entityTable = $entityElements['entity_table']; $sql = " SELECT phone, ltype.name as locationType, ph.is_primary as is_primary, ph.id as phone_id, ph.location_type_id as locationTypeId FROM civicrm_loc_block loc, civicrm_phone ph, civicrm_location_type ltype, {$entityTable} ev WHERE ev.id = %1 AND loc.id = ev.loc_block_id AND ph.id IN (loc.phone_id, loc.phone_2_id) AND ltype.id = ph.location_type_id ORDER BY ph.is_primary DESC, phone_id ASC "; $params = [ 1 => [ $entityId, 'Integer', ], ]; $numbers = []; $dao = CRM_Core_DAO::executeQuery($sql, $params); while ($dao->fetch()) { $numbers[$dao->phone_id] = [ 'locationType' => $dao->locationType, 'is_primary' => $dao->is_primary, 'id' => $dao->phone_id, 'phone' => $dao->phone, 'locationTypeId' => $dao->locationTypeId, ]; } return $numbers; } /** * Set NULL to phone, mapping, uffield * * @param $optionId * Value of option to be deleted. */ public static function setOptionToNull($optionId) { if (!$optionId) { return; } // Ensure mysql phone function exists CRM_Core_DAO::checkSqlFunctionsExist(); $tables = [ 'civicrm_phone', 'civicrm_mapping_field', 'civicrm_uf_field', ]; $params = [ 1 => [ $optionId, 'Integer', ], ]; foreach ($tables as $tableName) { $query = "UPDATE `{$tableName}` SET `phone_type_id` = NULL WHERE `phone_type_id` = %1"; CRM_Core_DAO::executeQuery($query, $params); } } /** * Call common delete function. * * @param int $id * * @return bool */ public static function del($id) { // Ensure mysql phone function exists CRM_Core_DAO::checkSqlFunctionsExist(); return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Phone', $id); } }