Only add in the additional metadata if we are also adding them to the form
[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 * $Id$
17 *
18 */
19
20/**
21 * This class contain function for IM handling
22 */
23class CRM_Core_BAO_IM extends CRM_Core_DAO_IM {
24
25 /**
fe482240 26 * Takes an associative array and adds im.
6a488035 27 *
6a0b768e
TO
28 * @param array $params
29 * (reference ) an assoc array of name/value pairs.
6a488035 30 *
a6c01b45
CW
31 * @return object
32 * CRM_Core_BAO_IM object on success, null otherwise
6a488035 33 */
00be9182 34 public static function add(&$params) {
6a488035
TO
35 $hook = empty($params['id']) ? 'create' : 'edit';
36 CRM_Utils_Hook::pre($hook, 'IM', CRM_Utils_Array::value('id', $params), $params);
37
38 $im = new CRM_Core_DAO_IM();
39 $im->copyValues($params);
40 $im->save();
41
42 CRM_Utils_Hook::post($hook, 'IM', $im->id, $im);
43 return $im;
44 }
45
46 /**
47 * Given the list of params in the params array, fetch the object
48 * and store the values in the values array
49 *
acb1052e 50 * @param array $entityBlock input parameters to find object
6a488035 51 *
acb1052e 52 * @return bool
6a488035 53 */
00be9182 54 public static function &getValues($entityBlock) {
6a488035
TO
55 return CRM_Core_BAO_Block::getValues('im', $entityBlock);
56 }
57
58 /**
59 * Get all the ims for a specified contact_id, with the primary im being first
60 *
6a0b768e
TO
61 * @param int $id
62 * The contact id.
6a488035 63 *
77b97be7
EM
64 * @param bool $updateBlankLocInfo
65 *
a6c01b45
CW
66 * @return array
67 * the array of im details
6a488035 68 */
00be9182 69 public static function allIMs($id, $updateBlankLocInfo = FALSE) {
6a488035
TO
70 if (!$id) {
71 return NULL;
72 }
73
74 $query = "
75SELECT civicrm_im.name as im, civicrm_location_type.name as locationType, civicrm_im.is_primary as is_primary,
76civicrm_im.id as im_id, civicrm_im.location_type_id as locationTypeId,
77civicrm_im.provider_id as providerId
78FROM civicrm_contact
79LEFT JOIN civicrm_im ON ( civicrm_im.contact_id = civicrm_contact.id )
80LEFT JOIN civicrm_location_type ON ( civicrm_im.location_type_id = civicrm_location_type.id )
81WHERE
82 civicrm_contact.id = %1
83ORDER BY
84 civicrm_im.is_primary DESC, im_id ASC ";
be2fb01f 85 $params = [1 => [$id, 'Integer']];
6a488035 86
be2fb01f 87 $ims = $values = [];
353ffa53 88 $dao = CRM_Core_DAO::executeQuery($query, $params);
6a488035
TO
89 $count = 1;
90 while ($dao->fetch()) {
be2fb01f 91 $values = [
6a488035
TO
92 'locationType' => $dao->locationType,
93 'is_primary' => $dao->is_primary,
94 'id' => $dao->im_id,
95 'name' => $dao->im,
96 'locationTypeId' => $dao->locationTypeId,
97 'providerId' => $dao->providerId,
be2fb01f 98 ];
6a488035
TO
99
100 if ($updateBlankLocInfo) {
101 $ims[$count++] = $values;
102 }
103 else {
104 $ims[$dao->im_id] = $values;
105 }
106 }
107 return $ims;
108 }
109
110 /**
111 * Get all the ims for a specified location_block id, with the primary im being first
112 *
6a0b768e
TO
113 * @param array $entityElements
114 * The array containing entity_id and.
16b10e64 115 * entity_table name
6a488035 116 *
a6c01b45
CW
117 * @return array
118 * the array of im details
6a488035 119 */
00be9182 120 public static function allEntityIMs(&$entityElements) {
6a488035
TO
121 if (empty($entityElements)) {
122 return NULL;
123 }
6a488035
TO
124 $entityId = $entityElements['entity_id'];
125 $entityTable = $entityElements['entity_table'];
6a488035
TO
126 $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
127FROM civicrm_loc_block loc, civicrm_im cim, civicrm_location_type ltype, {$entityTable} ev
128WHERE ev.id = %1
129AND loc.id = ev.loc_block_id
130AND cim.id IN (loc.im_id, loc.im_2_id)
131AND ltype.id = cim.location_type_id
132ORDER BY cim.is_primary DESC, im_id ASC ";
133
be2fb01f 134 $params = [1 => [$entityId, 'Integer']];
6a488035 135
be2fb01f 136 $ims = [];
6a488035
TO
137 $dao = CRM_Core_DAO::executeQuery($sql, $params);
138 while ($dao->fetch()) {
be2fb01f 139 $ims[$dao->im_id] = [
6a488035
TO
140 'locationType' => $dao->locationType,
141 'is_primary' => $dao->is_primary,
142 'id' => $dao->im_id,
143 'name' => $dao->im,
144 'locationTypeId' => $dao->locationTypeId,
be2fb01f 145 ];
6a488035
TO
146 }
147 return $ims;
148 }
12445e1c
CW
149
150 /**
fe482240 151 * Call common delete function.
ad37ac8e 152 *
153 * @param int $id
154 *
155 * @return bool
12445e1c 156 */
00be9182 157 public static function del($id) {
a65e2e55 158 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('IM', $id);
12445e1c 159 }
96025800 160
6a488035 161}