Merge pull request #17358 from seamuslee001/d8_prevnext_cache_test_fix
[civicrm-core.git] / CRM / Core / BAO / IM.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19
20 /**
21 * This class contain function for IM handling
22 */
23 class CRM_Core_BAO_IM extends CRM_Core_DAO_IM {
24
25 /**
26 * Create or update IM record.
27 *
28 * @param array $params
29 * @return CRM_Core_DAO_IM
30 */
31 public static function add($params) {
32 return self::writeRecord($params);
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 *
39 * @param array $entityBlock input parameters to find object
40 *
41 * @return bool
42 */
43 public static function &getValues($entityBlock) {
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 *
50 * @param int $id
51 * The contact id.
52 *
53 * @param bool $updateBlankLocInfo
54 *
55 * @return array
56 * the array of im details
57 */
58 public static function allIMs($id, $updateBlankLocInfo = FALSE) {
59 if (!$id) {
60 return NULL;
61 }
62
63 $query = "
64 SELECT civicrm_im.name as im, civicrm_location_type.name as locationType, civicrm_im.is_primary as is_primary,
65 civicrm_im.id as im_id, civicrm_im.location_type_id as locationTypeId,
66 civicrm_im.provider_id as providerId
67 FROM civicrm_contact
68 LEFT JOIN civicrm_im ON ( civicrm_im.contact_id = civicrm_contact.id )
69 LEFT JOIN civicrm_location_type ON ( civicrm_im.location_type_id = civicrm_location_type.id )
70 WHERE
71 civicrm_contact.id = %1
72 ORDER BY
73 civicrm_im.is_primary DESC, im_id ASC ";
74 $params = [1 => [$id, 'Integer']];
75
76 $ims = $values = [];
77 $dao = CRM_Core_DAO::executeQuery($query, $params);
78 $count = 1;
79 while ($dao->fetch()) {
80 $values = [
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,
87 ];
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 *
102 * @param array $entityElements
103 * The array containing entity_id and.
104 * entity_table name
105 *
106 * @return array
107 * the array of im details
108 */
109 public static function allEntityIMs(&$entityElements) {
110 if (empty($entityElements)) {
111 return NULL;
112 }
113 $entityId = $entityElements['entity_id'];
114 $entityTable = $entityElements['entity_table'];
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
116 FROM civicrm_loc_block loc, civicrm_im cim, civicrm_location_type ltype, {$entityTable} ev
117 WHERE ev.id = %1
118 AND loc.id = ev.loc_block_id
119 AND cim.id IN (loc.im_id, loc.im_2_id)
120 AND ltype.id = cim.location_type_id
121 ORDER BY cim.is_primary DESC, im_id ASC ";
122
123 $params = [1 => [$entityId, 'Integer']];
124
125 $ims = [];
126 $dao = CRM_Core_DAO::executeQuery($sql, $params);
127 while ($dao->fetch()) {
128 $ims[$dao->im_id] = [
129 'locationType' => $dao->locationType,
130 'is_primary' => $dao->is_primary,
131 'id' => $dao->im_id,
132 'name' => $dao->im,
133 'locationTypeId' => $dao->locationTypeId,
134 ];
135 }
136 return $ims;
137 }
138
139 /**
140 * Call common delete function.
141 *
142 * @param int $id
143 *
144 * @return bool
145 */
146 public static function del($id) {
147 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('IM', $id);
148 }
149
150 }