INFRA-132 - Fix spacing of @return tag in comments
[civicrm-core.git] / CRM / Core / BAO / IM.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class contain function for IM handling
38 */
39 class CRM_Core_BAO_IM extends CRM_Core_DAO_IM {
40
41 /**
42 * Takes an associative array and adds im
43 *
44 * @param array $params
45 * (reference ) an assoc array of name/value pairs.
46 *
47 * @return object
48 * CRM_Core_BAO_IM object on success, null otherwise
49 * @static
50 */
51 public static function add(&$params) {
52 $hook = empty($params['id']) ? 'create' : 'edit';
53 CRM_Utils_Hook::pre($hook, 'IM', CRM_Utils_Array::value('id', $params), $params);
54
55 $im = new CRM_Core_DAO_IM();
56 $im->copyValues($params);
57 $im->save();
58
59 CRM_Utils_Hook::post($hook, 'IM', $im->id, $im);
60 return $im;
61 }
62
63 /**
64 * Given the list of params in the params array, fetch the object
65 * and store the values in the values array
66 *
67 * @param array entityBlock input parameters to find object
68 *
69 * @return boolean
70 * @static
71 */
72 public 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
80 * The contact id.
81 *
82 * @param bool $updateBlankLocInfo
83 *
84 * @return array
85 * the array of im details
86 * @static
87 */
88 public static function allIMs($id, $updateBlankLocInfo = FALSE) {
89 if (!$id) {
90 return NULL;
91 }
92
93 $query = "
94 SELECT civicrm_im.name as im, civicrm_location_type.name as locationType, civicrm_im.is_primary as is_primary,
95 civicrm_im.id as im_id, civicrm_im.location_type_id as locationTypeId,
96 civicrm_im.provider_id as providerId
97 FROM civicrm_contact
98 LEFT JOIN civicrm_im ON ( civicrm_im.contact_id = civicrm_contact.id )
99 LEFT JOIN civicrm_location_type ON ( civicrm_im.location_type_id = civicrm_location_type.id )
100 WHERE
101 civicrm_contact.id = %1
102 ORDER BY
103 civicrm_im.is_primary DESC, im_id ASC ";
104 $params = array(1 => array($id, 'Integer'));
105
106 $ims = $values = array();
107 $dao = CRM_Core_DAO::executeQuery($query, $params);
108 $count = 1;
109 while ($dao->fetch()) {
110 $values = array(
111 'locationType' => $dao->locationType,
112 'is_primary' => $dao->is_primary,
113 'id' => $dao->im_id,
114 'name' => $dao->im,
115 'locationTypeId' => $dao->locationTypeId,
116 'providerId' => $dao->providerId,
117 );
118
119 if ($updateBlankLocInfo) {
120 $ims[$count++] = $values;
121 }
122 else {
123 $ims[$dao->im_id] = $values;
124 }
125 }
126 return $ims;
127 }
128
129 /**
130 * Get all the ims for a specified location_block id, with the primary im being first
131 *
132 * @param array $entityElements
133 * The array containing entity_id and.
134 * entity_table name
135 *
136 * @return array
137 * the array of im details
138 * @static
139 */
140 public static function allEntityIMs(&$entityElements) {
141 if (empty($entityElements)) {
142 return NULL;
143 }
144
145
146 $entityId = $entityElements['entity_id'];
147 $entityTable = $entityElements['entity_table'];
148
149
150 $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
151 FROM civicrm_loc_block loc, civicrm_im cim, civicrm_location_type ltype, {$entityTable} ev
152 WHERE ev.id = %1
153 AND loc.id = ev.loc_block_id
154 AND cim.id IN (loc.im_id, loc.im_2_id)
155 AND ltype.id = cim.location_type_id
156 ORDER BY cim.is_primary DESC, im_id ASC ";
157
158 $params = array(1 => array($entityId, 'Integer'));
159
160 $ims = array();
161 $dao = CRM_Core_DAO::executeQuery($sql, $params);
162 while ($dao->fetch()) {
163 $ims[$dao->im_id] = array(
164 'locationType' => $dao->locationType,
165 'is_primary' => $dao->is_primary,
166 'id' => $dao->im_id,
167 'name' => $dao->im,
168 'locationTypeId' => $dao->locationTypeId,
169 );
170 }
171 return $ims;
172 }
173
174 /**
175 * Call common delete function
176 */
177 public static function del($id) {
178 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('IM', $id);
179 }
180 }