Merge pull request #23232 from braders/nodefaults-tab-links
[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 */
17
18 /**
19 * This class contain function for IM handling
20 */
21 class CRM_Core_BAO_IM extends CRM_Core_DAO_IM {
22 use CRM_Contact_AccessTrait;
23
24 /**
25 * Create or update IM record.
26 *
27 * @param array $params
28 *
29 * @return \CRM_Core_DAO|\CRM_Core_DAO_IM
30 * @throws \CRM_Core_Exception
31 * @throws \API_Exception
32 */
33 public static function create($params) {
34 CRM_Core_BAO_Block::handlePrimary($params, __CLASS__);
35 return self::writeRecord($params);
36 }
37
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
54 /**
55 * Given the list of params in the params array, fetch the object
56 * and store the values in the values array
57 *
58 * @param array $entityBlock input parameters to find object
59 *
60 * @return bool
61 */
62 public static function &getValues($entityBlock) {
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 *
69 * @param int $id
70 * The contact id.
71 *
72 * @param bool $updateBlankLocInfo
73 *
74 * @return array
75 * the array of im details
76 */
77 public static function allIMs($id, $updateBlankLocInfo = FALSE) {
78 if (!$id) {
79 return NULL;
80 }
81
82 $query = "
83 SELECT civicrm_im.name as im, civicrm_location_type.name as locationType, civicrm_im.is_primary as is_primary,
84 civicrm_im.id as im_id, civicrm_im.location_type_id as locationTypeId,
85 civicrm_im.provider_id as providerId
86 FROM civicrm_contact
87 LEFT JOIN civicrm_im ON ( civicrm_im.contact_id = civicrm_contact.id )
88 LEFT JOIN civicrm_location_type ON ( civicrm_im.location_type_id = civicrm_location_type.id )
89 WHERE
90 civicrm_contact.id = %1
91 ORDER BY
92 civicrm_im.is_primary DESC, im_id ASC ";
93 $params = [1 => [$id, 'Integer']];
94
95 $ims = $values = [];
96 $dao = CRM_Core_DAO::executeQuery($query, $params);
97 $count = 1;
98 while ($dao->fetch()) {
99 $values = [
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,
106 ];
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 *
121 * @param array $entityElements
122 * The array containing entity_id and.
123 * entity_table name
124 *
125 * @return array
126 * the array of im details
127 */
128 public static function allEntityIMs(&$entityElements) {
129 if (empty($entityElements)) {
130 return NULL;
131 }
132 $entityId = $entityElements['entity_id'];
133 $entityTable = $entityElements['entity_table'];
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
135 FROM civicrm_loc_block loc, civicrm_im cim, civicrm_location_type ltype, {$entityTable} ev
136 WHERE ev.id = %1
137 AND loc.id = ev.loc_block_id
138 AND cim.id IN (loc.im_id, loc.im_2_id)
139 AND ltype.id = cim.location_type_id
140 ORDER BY cim.is_primary DESC, im_id ASC ";
141
142 $params = [1 => [$entityId, 'Integer']];
143
144 $ims = [];
145 $dao = CRM_Core_DAO::executeQuery($sql, $params);
146 while ($dao->fetch()) {
147 $ims[$dao->im_id] = [
148 'locationType' => $dao->locationType,
149 'is_primary' => $dao->is_primary,
150 'id' => $dao->im_id,
151 'name' => $dao->im,
152 'locationTypeId' => $dao->locationTypeId,
153 ];
154 }
155 return $ims;
156 }
157
158 /**
159 * Call common delete function.
160 *
161 * @see \CRM_Contact_BAO_Contact::on_hook_civicrm_post
162 *
163 * @param int $id
164 * @deprecated
165 * @return bool
166 */
167 public static function del($id) {
168 return (bool) self::deleteRecord(['id' => $id]);
169 }
170
171 }