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