version fixes
[civicrm-core.git] / CRM / Core / BAO / IM.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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 /**
fe482240 42 * Takes an associative array and adds im.
6a488035 43 *
6a0b768e
TO
44 * @param array $params
45 * (reference ) an assoc array of name/value pairs.
6a488035 46 *
a6c01b45
CW
47 * @return object
48 * CRM_Core_BAO_IM object on success, null otherwise
6a488035 49 */
00be9182 50 public static function add(&$params) {
6a488035
TO
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 *
acb1052e 66 * @param array $entityBlock input parameters to find object
6a488035 67 *
acb1052e 68 * @return bool
6a488035 69 */
00be9182 70 public static function &getValues($entityBlock) {
6a488035
TO
71 return CRM_Core_BAO_Block::getValues('im', $entityBlock);
72 }
73
74 /**
75 * Get all the ims for a specified contact_id, with the primary im being first
76 *
6a0b768e
TO
77 * @param int $id
78 * The contact id.
6a488035 79 *
77b97be7
EM
80 * @param bool $updateBlankLocInfo
81 *
a6c01b45
CW
82 * @return array
83 * the array of im details
6a488035 84 */
00be9182 85 public static function allIMs($id, $updateBlankLocInfo = FALSE) {
6a488035
TO
86 if (!$id) {
87 return NULL;
88 }
89
90 $query = "
91SELECT civicrm_im.name as im, civicrm_location_type.name as locationType, civicrm_im.is_primary as is_primary,
92civicrm_im.id as im_id, civicrm_im.location_type_id as locationTypeId,
93civicrm_im.provider_id as providerId
94FROM civicrm_contact
95LEFT JOIN civicrm_im ON ( civicrm_im.contact_id = civicrm_contact.id )
96LEFT JOIN civicrm_location_type ON ( civicrm_im.location_type_id = civicrm_location_type.id )
97WHERE
98 civicrm_contact.id = %1
99ORDER BY
100 civicrm_im.is_primary DESC, im_id ASC ";
101 $params = array(1 => array($id, 'Integer'));
102
353ffa53
TO
103 $ims = $values = array();
104 $dao = CRM_Core_DAO::executeQuery($query, $params);
6a488035
TO
105 $count = 1;
106 while ($dao->fetch()) {
107 $values = array(
108 'locationType' => $dao->locationType,
109 'is_primary' => $dao->is_primary,
110 'id' => $dao->im_id,
111 'name' => $dao->im,
112 'locationTypeId' => $dao->locationTypeId,
113 'providerId' => $dao->providerId,
114 );
115
116 if ($updateBlankLocInfo) {
117 $ims[$count++] = $values;
118 }
119 else {
120 $ims[$dao->im_id] = $values;
121 }
122 }
123 return $ims;
124 }
125
126 /**
127 * Get all the ims for a specified location_block id, with the primary im being first
128 *
6a0b768e
TO
129 * @param array $entityElements
130 * The array containing entity_id and.
16b10e64 131 * entity_table name
6a488035 132 *
a6c01b45
CW
133 * @return array
134 * the array of im details
6a488035 135 */
00be9182 136 public static function allEntityIMs(&$entityElements) {
6a488035
TO
137 if (empty($entityElements)) {
138 return NULL;
139 }
6a488035
TO
140 $entityId = $entityElements['entity_id'];
141 $entityTable = $entityElements['entity_table'];
6a488035
TO
142 $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
143FROM civicrm_loc_block loc, civicrm_im cim, civicrm_location_type ltype, {$entityTable} ev
144WHERE ev.id = %1
145AND loc.id = ev.loc_block_id
146AND cim.id IN (loc.im_id, loc.im_2_id)
147AND ltype.id = cim.location_type_id
148ORDER BY cim.is_primary DESC, im_id ASC ";
149
150 $params = array(1 => array($entityId, 'Integer'));
151
152 $ims = array();
153 $dao = CRM_Core_DAO::executeQuery($sql, $params);
154 while ($dao->fetch()) {
155 $ims[$dao->im_id] = array(
156 'locationType' => $dao->locationType,
157 'is_primary' => $dao->is_primary,
158 'id' => $dao->im_id,
159 'name' => $dao->im,
160 'locationTypeId' => $dao->locationTypeId,
161 );
162 }
163 return $ims;
164 }
12445e1c
CW
165
166 /**
fe482240 167 * Call common delete function.
12445e1c 168 */
00be9182 169 public static function del($id) {
a65e2e55 170 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('IM', $id);
12445e1c 171 }
96025800 172
6a488035 173}