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