Merge pull request #4809 from totten/master-cs2
[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 (reference ) an assoc array of name/value pairs
45 *
46 * @return object CRM_Core_BAO_IM object on success, null otherwise
47 * @static
48 */
49 public static function add(&$params) {
50 $hook = empty($params['id']) ? 'create' : 'edit';
51 CRM_Utils_Hook::pre($hook, 'IM', CRM_Utils_Array::value('id', $params), $params);
52
53 $im = new CRM_Core_DAO_IM();
54 $im->copyValues($params);
55 $im->save();
56
57 CRM_Utils_Hook::post($hook, 'IM', $im->id, $im);
58 return $im;
59 }
60
61 /**
62 * Given the list of params in the params array, fetch the object
63 * and store the values in the values array
64 *
65 * @param array entityBlock input parameters to find object
66 *
67 * @return boolean
68 * @static
69 */
70 public static function &getValues($entityBlock) {
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 *
77 * @param int $id the contact id
78 *
79 * @param bool $updateBlankLocInfo
80 *
81 * @return array the array of im details
82 * @static
83 */
84 public static function allIMs($id, $updateBlankLocInfo = FALSE) {
85 if (!$id) {
86 return NULL;
87 }
88
89 $query = "
90 SELECT civicrm_im.name as im, civicrm_location_type.name as locationType, civicrm_im.is_primary as is_primary,
91 civicrm_im.id as im_id, civicrm_im.location_type_id as locationTypeId,
92 civicrm_im.provider_id as providerId
93 FROM civicrm_contact
94 LEFT JOIN civicrm_im ON ( civicrm_im.contact_id = civicrm_contact.id )
95 LEFT JOIN civicrm_location_type ON ( civicrm_im.location_type_id = civicrm_location_type.id )
96 WHERE
97 civicrm_contact.id = %1
98 ORDER BY
99 civicrm_im.is_primary DESC, im_id ASC ";
100 $params = array(1 => array($id, 'Integer'));
101
102 $ims = $values = array();
103 $dao = CRM_Core_DAO::executeQuery($query, $params);
104 $count = 1;
105 while ($dao->fetch()) {
106 $values = array(
107 'locationType' => $dao->locationType,
108 'is_primary' => $dao->is_primary,
109 'id' => $dao->im_id,
110 'name' => $dao->im,
111 'locationTypeId' => $dao->locationTypeId,
112 'providerId' => $dao->providerId,
113 );
114
115 if ($updateBlankLocInfo) {
116 $ims[$count++] = $values;
117 }
118 else {
119 $ims[$dao->im_id] = $values;
120 }
121 }
122 return $ims;
123 }
124
125 /**
126 * Get all the ims for a specified location_block id, with the primary im being first
127 *
128 * @param array $entityElements the array containing entity_id and
129 * entity_table name
130 *
131 * @return array the array of im details
132 * @static
133 */
134 public static function allEntityIMs(&$entityElements) {
135 if (empty($entityElements)) {
136 return NULL;
137 }
138
139
140 $entityId = $entityElements['entity_id'];
141 $entityTable = $entityElements['entity_table'];
142
143
144 $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
145 FROM civicrm_loc_block loc, civicrm_im cim, civicrm_location_type ltype, {$entityTable} ev
146 WHERE ev.id = %1
147 AND loc.id = ev.loc_block_id
148 AND cim.id IN (loc.im_id, loc.im_2_id)
149 AND ltype.id = cim.location_type_id
150 ORDER BY cim.is_primary DESC, im_id ASC ";
151
152 $params = array(1 => array($entityId, 'Integer'));
153
154 $ims = array();
155 $dao = CRM_Core_DAO::executeQuery($sql, $params);
156 while ($dao->fetch()) {
157 $ims[$dao->im_id] = array(
158 'locationType' => $dao->locationType,
159 'is_primary' => $dao->is_primary,
160 'id' => $dao->im_id,
161 'name' => $dao->im,
162 'locationTypeId' => $dao->locationTypeId,
163 );
164 }
165 return $ims;
166 }
167
168 /**
169 * Call common delete function
170 */
171 public static function del($id) {
172 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('IM', $id);
173 }
174 }