Merge pull request #18712 from eileenmcnaughton/locev3
[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 */
17
18/**
19 * This class contain function for IM handling
20 */
21class CRM_Core_BAO_IM extends CRM_Core_DAO_IM {
22
23 /**
6a5fec96 24 * Create or update IM record.
6a488035 25 *
6a0b768e 26 * @param array $params
f04b3586 27 *
28 * @return \CRM_Core_DAO|\CRM_Core_DAO_IM
29 * @throws \CRM_Core_Exception
30 * @throws \API_Exception
6a488035 31 */
9f8f650e 32 public static function create($params) {
3bec4854 33 CRM_Core_BAO_Block::handlePrimary($params, __CLASS__);
6a5fec96 34 return self::writeRecord($params);
6a488035
TO
35 }
36
9f8f650e 37 /**
38 * Create or update IM record.
39 *
40 * @deprecated
41 *
42 * @param array $params
43 *
44 * @return \CRM_Core_DAO|\CRM_Core_DAO_IM
45 * @throws \CRM_Core_Exception
46 * @throws \API_Exception
47 */
48 public static function add($params) {
49 CRM_Core_Error::deprecatedFunctionWarning('use the v4 api');
50 return self::create($params);
51 }
52
6a488035
TO
53 /**
54 * Given the list of params in the params array, fetch the object
55 * and store the values in the values array
56 *
acb1052e 57 * @param array $entityBlock input parameters to find object
6a488035 58 *
acb1052e 59 * @return bool
6a488035 60 */
00be9182 61 public static function &getValues($entityBlock) {
6a488035
TO
62 return CRM_Core_BAO_Block::getValues('im', $entityBlock);
63 }
64
65 /**
66 * Get all the ims for a specified contact_id, with the primary im being first
67 *
6a0b768e
TO
68 * @param int $id
69 * The contact id.
6a488035 70 *
77b97be7
EM
71 * @param bool $updateBlankLocInfo
72 *
a6c01b45
CW
73 * @return array
74 * the array of im details
6a488035 75 */
00be9182 76 public static function allIMs($id, $updateBlankLocInfo = FALSE) {
6a488035
TO
77 if (!$id) {
78 return NULL;
79 }
80
81 $query = "
82SELECT civicrm_im.name as im, civicrm_location_type.name as locationType, civicrm_im.is_primary as is_primary,
83civicrm_im.id as im_id, civicrm_im.location_type_id as locationTypeId,
84civicrm_im.provider_id as providerId
85FROM civicrm_contact
86LEFT JOIN civicrm_im ON ( civicrm_im.contact_id = civicrm_contact.id )
87LEFT JOIN civicrm_location_type ON ( civicrm_im.location_type_id = civicrm_location_type.id )
88WHERE
89 civicrm_contact.id = %1
90ORDER BY
91 civicrm_im.is_primary DESC, im_id ASC ";
be2fb01f 92 $params = [1 => [$id, 'Integer']];
6a488035 93
be2fb01f 94 $ims = $values = [];
353ffa53 95 $dao = CRM_Core_DAO::executeQuery($query, $params);
6a488035
TO
96 $count = 1;
97 while ($dao->fetch()) {
be2fb01f 98 $values = [
6a488035
TO
99 'locationType' => $dao->locationType,
100 'is_primary' => $dao->is_primary,
101 'id' => $dao->im_id,
102 'name' => $dao->im,
103 'locationTypeId' => $dao->locationTypeId,
104 'providerId' => $dao->providerId,
be2fb01f 105 ];
6a488035
TO
106
107 if ($updateBlankLocInfo) {
108 $ims[$count++] = $values;
109 }
110 else {
111 $ims[$dao->im_id] = $values;
112 }
113 }
114 return $ims;
115 }
116
117 /**
118 * Get all the ims for a specified location_block id, with the primary im being first
119 *
6a0b768e
TO
120 * @param array $entityElements
121 * The array containing entity_id and.
16b10e64 122 * entity_table name
6a488035 123 *
a6c01b45
CW
124 * @return array
125 * the array of im details
6a488035 126 */
00be9182 127 public static function allEntityIMs(&$entityElements) {
6a488035
TO
128 if (empty($entityElements)) {
129 return NULL;
130 }
6a488035
TO
131 $entityId = $entityElements['entity_id'];
132 $entityTable = $entityElements['entity_table'];
6a488035
TO
133 $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
134FROM civicrm_loc_block loc, civicrm_im cim, civicrm_location_type ltype, {$entityTable} ev
135WHERE ev.id = %1
136AND loc.id = ev.loc_block_id
137AND cim.id IN (loc.im_id, loc.im_2_id)
138AND ltype.id = cim.location_type_id
139ORDER BY cim.is_primary DESC, im_id ASC ";
140
be2fb01f 141 $params = [1 => [$entityId, 'Integer']];
6a488035 142
be2fb01f 143 $ims = [];
6a488035
TO
144 $dao = CRM_Core_DAO::executeQuery($sql, $params);
145 while ($dao->fetch()) {
be2fb01f 146 $ims[$dao->im_id] = [
6a488035
TO
147 'locationType' => $dao->locationType,
148 'is_primary' => $dao->is_primary,
149 'id' => $dao->im_id,
150 'name' => $dao->im,
151 'locationTypeId' => $dao->locationTypeId,
be2fb01f 152 ];
6a488035
TO
153 }
154 return $ims;
155 }
12445e1c
CW
156
157 /**
fe482240 158 * Call common delete function.
ad37ac8e 159 *
160 * @param int $id
161 *
162 * @return bool
12445e1c 163 */
00be9182 164 public static function del($id) {
a65e2e55 165 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('IM', $id);
12445e1c 166 }
96025800 167
6a488035 168}