Merge pull request #17008 from ivan-compucorp/CPS-70-fix-radio-value
[civicrm-core.git] / CRM / Core / BAO / IM.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 * $Id$
17 *
18 */
19
20/**
21 * This class contain function for IM handling
22 */
23class CRM_Core_BAO_IM extends CRM_Core_DAO_IM {
24
25 /**
6a5fec96 26 * Create or update IM record.
6a488035 27 *
6a0b768e 28 * @param array $params
6a5fec96 29 * @return CRM_Core_DAO_IM
6a488035 30 */
6a5fec96
CW
31 public static function add($params) {
32 return self::writeRecord($params);
6a488035
TO
33 }
34
35 /**
36 * Given the list of params in the params array, fetch the object
37 * and store the values in the values array
38 *
acb1052e 39 * @param array $entityBlock input parameters to find object
6a488035 40 *
acb1052e 41 * @return bool
6a488035 42 */
00be9182 43 public static function &getValues($entityBlock) {
6a488035
TO
44 return CRM_Core_BAO_Block::getValues('im', $entityBlock);
45 }
46
47 /**
48 * Get all the ims for a specified contact_id, with the primary im being first
49 *
6a0b768e
TO
50 * @param int $id
51 * The contact id.
6a488035 52 *
77b97be7
EM
53 * @param bool $updateBlankLocInfo
54 *
a6c01b45
CW
55 * @return array
56 * the array of im details
6a488035 57 */
00be9182 58 public static function allIMs($id, $updateBlankLocInfo = FALSE) {
6a488035
TO
59 if (!$id) {
60 return NULL;
61 }
62
63 $query = "
64SELECT civicrm_im.name as im, civicrm_location_type.name as locationType, civicrm_im.is_primary as is_primary,
65civicrm_im.id as im_id, civicrm_im.location_type_id as locationTypeId,
66civicrm_im.provider_id as providerId
67FROM civicrm_contact
68LEFT JOIN civicrm_im ON ( civicrm_im.contact_id = civicrm_contact.id )
69LEFT JOIN civicrm_location_type ON ( civicrm_im.location_type_id = civicrm_location_type.id )
70WHERE
71 civicrm_contact.id = %1
72ORDER BY
73 civicrm_im.is_primary DESC, im_id ASC ";
be2fb01f 74 $params = [1 => [$id, 'Integer']];
6a488035 75
be2fb01f 76 $ims = $values = [];
353ffa53 77 $dao = CRM_Core_DAO::executeQuery($query, $params);
6a488035
TO
78 $count = 1;
79 while ($dao->fetch()) {
be2fb01f 80 $values = [
6a488035
TO
81 'locationType' => $dao->locationType,
82 'is_primary' => $dao->is_primary,
83 'id' => $dao->im_id,
84 'name' => $dao->im,
85 'locationTypeId' => $dao->locationTypeId,
86 'providerId' => $dao->providerId,
be2fb01f 87 ];
6a488035
TO
88
89 if ($updateBlankLocInfo) {
90 $ims[$count++] = $values;
91 }
92 else {
93 $ims[$dao->im_id] = $values;
94 }
95 }
96 return $ims;
97 }
98
99 /**
100 * Get all the ims for a specified location_block id, with the primary im being first
101 *
6a0b768e
TO
102 * @param array $entityElements
103 * The array containing entity_id and.
16b10e64 104 * entity_table name
6a488035 105 *
a6c01b45
CW
106 * @return array
107 * the array of im details
6a488035 108 */
00be9182 109 public static function allEntityIMs(&$entityElements) {
6a488035
TO
110 if (empty($entityElements)) {
111 return NULL;
112 }
6a488035
TO
113 $entityId = $entityElements['entity_id'];
114 $entityTable = $entityElements['entity_table'];
6a488035
TO
115 $sql = "SELECT cim.name as im, ltype.name as locationType, cim.is_primary as is_primary, cim.id as im_id, cim.location_type_id as locationTypeId
116FROM civicrm_loc_block loc, civicrm_im cim, civicrm_location_type ltype, {$entityTable} ev
117WHERE ev.id = %1
118AND loc.id = ev.loc_block_id
119AND cim.id IN (loc.im_id, loc.im_2_id)
120AND ltype.id = cim.location_type_id
121ORDER BY cim.is_primary DESC, im_id ASC ";
122
be2fb01f 123 $params = [1 => [$entityId, 'Integer']];
6a488035 124
be2fb01f 125 $ims = [];
6a488035
TO
126 $dao = CRM_Core_DAO::executeQuery($sql, $params);
127 while ($dao->fetch()) {
be2fb01f 128 $ims[$dao->im_id] = [
6a488035
TO
129 'locationType' => $dao->locationType,
130 'is_primary' => $dao->is_primary,
131 'id' => $dao->im_id,
132 'name' => $dao->im,
133 'locationTypeId' => $dao->locationTypeId,
be2fb01f 134 ];
6a488035
TO
135 }
136 return $ims;
137 }
12445e1c
CW
138
139 /**
fe482240 140 * Call common delete function.
ad37ac8e 141 *
142 * @param int $id
143 *
144 * @return bool
12445e1c 145 */
00be9182 146 public static function del($id) {
a65e2e55 147 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('IM', $id);
12445e1c 148 }
96025800 149
6a488035 150}