phpcs - Fix error, "CONST keyword must be lowercase; expected const but found CONST"
[civicrm-core.git] / CRM / Core / BAO / IM.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class contain function for IM handling
38 */
39class CRM_Core_BAO_IM extends CRM_Core_DAO_IM {
40
41 /**
100fef9d 42 * Takes an associative array and adds im
6a488035
TO
43 *
44 * @param array $params (reference ) an assoc array of name/value pairs
45 *
46 * @return object CRM_Core_BAO_IM object on success, null otherwise
47 * @access public
48 * @static
49 */
50 static function add(&$params) {
51 $hook = empty($params['id']) ? 'create' : 'edit';
52 CRM_Utils_Hook::pre($hook, 'IM', CRM_Utils_Array::value('id', $params), $params);
53
54 $im = new CRM_Core_DAO_IM();
55 $im->copyValues($params);
56 $im->save();
57
58 CRM_Utils_Hook::post($hook, 'IM', $im->id, $im);
59 return $im;
60 }
61
62 /**
63 * Given the list of params in the params array, fetch the object
64 * and store the values in the values array
65 *
66 * @param array entityBlock input parameters to find object
67 *
68 * @return boolean
69 * @access public
70 * @static
71 */
72 static function &getValues($entityBlock) {
73 return CRM_Core_BAO_Block::getValues('im', $entityBlock);
74 }
75
76 /**
77 * Get all the ims for a specified contact_id, with the primary im being first
78 *
79 * @param int $id the contact id
80 *
77b97be7
EM
81 * @param bool $updateBlankLocInfo
82 *
6a488035
TO
83 * @return array the array of im details
84 * @access public
85 * @static
86 */
87 static function allIMs($id, $updateBlankLocInfo = FALSE) {
88 if (!$id) {
89 return NULL;
90 }
91
92 $query = "
93SELECT civicrm_im.name as im, civicrm_location_type.name as locationType, civicrm_im.is_primary as is_primary,
94civicrm_im.id as im_id, civicrm_im.location_type_id as locationTypeId,
95civicrm_im.provider_id as providerId
96FROM civicrm_contact
97LEFT JOIN civicrm_im ON ( civicrm_im.contact_id = civicrm_contact.id )
98LEFT JOIN civicrm_location_type ON ( civicrm_im.location_type_id = civicrm_location_type.id )
99WHERE
100 civicrm_contact.id = %1
101ORDER BY
102 civicrm_im.is_primary DESC, im_id ASC ";
103 $params = array(1 => array($id, 'Integer'));
104
105 $ims = $values = array();
106 $dao = CRM_Core_DAO::executeQuery($query, $params);
107 $count = 1;
108 while ($dao->fetch()) {
109 $values = array(
110 'locationType' => $dao->locationType,
111 'is_primary' => $dao->is_primary,
112 'id' => $dao->im_id,
113 'name' => $dao->im,
114 'locationTypeId' => $dao->locationTypeId,
115 'providerId' => $dao->providerId,
116 );
117
118 if ($updateBlankLocInfo) {
119 $ims[$count++] = $values;
120 }
121 else {
122 $ims[$dao->im_id] = $values;
123 }
124 }
125 return $ims;
126 }
127
128 /**
129 * Get all the ims for a specified location_block id, with the primary im being first
130 *
131 * @param array $entityElements the array containing entity_id and
132 * entity_table name
133 *
134 * @return array the array of im details
135 * @access public
136 * @static
137 */
138 static function allEntityIMs(&$entityElements) {
139 if (empty($entityElements)) {
140 return NULL;
141 }
142
143
144 $entityId = $entityElements['entity_id'];
145 $entityTable = $entityElements['entity_table'];
146
147
148 $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
149FROM civicrm_loc_block loc, civicrm_im cim, civicrm_location_type ltype, {$entityTable} ev
150WHERE ev.id = %1
151AND loc.id = ev.loc_block_id
152AND cim.id IN (loc.im_id, loc.im_2_id)
153AND ltype.id = cim.location_type_id
154ORDER BY cim.is_primary DESC, im_id ASC ";
155
156 $params = array(1 => array($entityId, 'Integer'));
157
158 $ims = array();
159 $dao = CRM_Core_DAO::executeQuery($sql, $params);
160 while ($dao->fetch()) {
161 $ims[$dao->im_id] = array(
162 'locationType' => $dao->locationType,
163 'is_primary' => $dao->is_primary,
164 'id' => $dao->im_id,
165 'name' => $dao->im,
166 'locationTypeId' => $dao->locationTypeId,
167 );
168 }
169 return $ims;
170 }
12445e1c
CW
171
172 /**
173 * Call common delete function
174 */
175 static function del($id) {
a65e2e55 176 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('IM', $id);
12445e1c 177 }
6a488035
TO
178}
179