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