| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Class contains functions for phone |
| 38 | */ |
| 39 | class CRM_Core_BAO_Phone extends CRM_Core_DAO_Phone { |
| 40 | |
| 41 | /** |
| 42 | * Create phone object - note that the create function calls 'add' but |
| 43 | * has more business logic |
| 44 | * |
| 45 | * @param array $params |
| 46 | * |
| 47 | * @return object |
| 48 | * @throws API_Exception |
| 49 | */ |
| 50 | public static function create($params) { |
| 51 | // Ensure mysql phone function exists |
| 52 | CRM_Core_DAO::checkSqlFunctionsExist(); |
| 53 | |
| 54 | if (is_numeric(CRM_Utils_Array::value('is_primary', $params)) || |
| 55 | // if id is set & is_primary isn't we can assume no change |
| 56 | empty($params['id']) |
| 57 | ) { |
| 58 | CRM_Core_BAO_Block::handlePrimary($params, get_class()); |
| 59 | } |
| 60 | $phone = self::add($params); |
| 61 | |
| 62 | return $phone; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Takes an associative array and adds phone. |
| 67 | * |
| 68 | * @param array $params |
| 69 | * (reference ) an assoc array of name/value pairs. |
| 70 | * |
| 71 | * @return object |
| 72 | * CRM_Core_BAO_Phone object on success, null otherwise |
| 73 | */ |
| 74 | public static function add(&$params) { |
| 75 | // Ensure mysql phone function exists |
| 76 | CRM_Core_DAO::checkSqlFunctionsExist(); |
| 77 | |
| 78 | $hook = empty($params['id']) ? 'create' : 'edit'; |
| 79 | CRM_Utils_Hook::pre($hook, 'Phone', CRM_Utils_Array::value('id', $params), $params); |
| 80 | |
| 81 | $phone = new CRM_Core_DAO_Phone(); |
| 82 | $phone->copyValues($params); |
| 83 | $phone->save(); |
| 84 | |
| 85 | CRM_Utils_Hook::post($hook, 'Phone', $phone->id, $phone); |
| 86 | return $phone; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Given the list of params in the params array, fetch the object |
| 91 | * and store the values in the values array |
| 92 | * |
| 93 | * @param array $entityBlock |
| 94 | * |
| 95 | * @return array |
| 96 | * array of phone objects |
| 97 | */ |
| 98 | public static function &getValues($entityBlock) { |
| 99 | $getValues = CRM_Core_BAO_Block::getValues('phone', $entityBlock); |
| 100 | return $getValues; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get all the phone numbers for a specified contact_id, with the primary being first |
| 105 | * |
| 106 | * @param int $id |
| 107 | * The contact id. |
| 108 | * |
| 109 | * @param bool $updateBlankLocInfo |
| 110 | * @param null $type |
| 111 | * @param array $filters |
| 112 | * |
| 113 | * @return array |
| 114 | * the array of phone ids which are potential numbers |
| 115 | */ |
| 116 | public static function allPhones($id, $updateBlankLocInfo = FALSE, $type = NULL, $filters = array()) { |
| 117 | if (!$id) { |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | $cond = NULL; |
| 122 | if ($type) { |
| 123 | $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id')); |
| 124 | if ($phoneTypeId) { |
| 125 | $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId"; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if (!empty($filters) && is_array($filters)) { |
| 130 | foreach ($filters as $key => $value) { |
| 131 | $cond .= " AND " . $key . " = " . $value; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | $query = " |
| 136 | SELECT phone, civicrm_location_type.name as locationType, civicrm_phone.is_primary as is_primary, |
| 137 | civicrm_phone.id as phone_id, civicrm_phone.location_type_id as locationTypeId, |
| 138 | civicrm_phone.phone_type_id as phoneTypeId |
| 139 | FROM civicrm_contact |
| 140 | LEFT JOIN civicrm_phone ON ( civicrm_contact.id = civicrm_phone.contact_id ) |
| 141 | LEFT JOIN civicrm_location_type ON ( civicrm_phone.location_type_id = civicrm_location_type.id ) |
| 142 | WHERE civicrm_contact.id = %1 $cond |
| 143 | ORDER BY civicrm_phone.is_primary DESC, phone_id ASC "; |
| 144 | |
| 145 | $params = array( |
| 146 | 1 => array( |
| 147 | $id, |
| 148 | 'Integer', |
| 149 | ), |
| 150 | ); |
| 151 | |
| 152 | $numbers = $values = array(); |
| 153 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 154 | $count = 1; |
| 155 | while ($dao->fetch()) { |
| 156 | $values = array( |
| 157 | 'locationType' => $dao->locationType, |
| 158 | 'is_primary' => $dao->is_primary, |
| 159 | 'id' => $dao->phone_id, |
| 160 | 'phone' => $dao->phone, |
| 161 | 'locationTypeId' => $dao->locationTypeId, |
| 162 | 'phoneTypeId' => $dao->phoneTypeId, |
| 163 | ); |
| 164 | |
| 165 | if ($updateBlankLocInfo) { |
| 166 | $numbers[$count++] = $values; |
| 167 | } |
| 168 | else { |
| 169 | $numbers[$dao->phone_id] = $values; |
| 170 | } |
| 171 | } |
| 172 | return $numbers; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get all the phone numbers for a specified location_block id, with the primary phone being first |
| 177 | * |
| 178 | * @param array $entityElements |
| 179 | * The array containing entity_id and. |
| 180 | * entity_table name |
| 181 | * |
| 182 | * @param null $type |
| 183 | * |
| 184 | * @return array |
| 185 | * the array of phone ids which are potential numbers |
| 186 | */ |
| 187 | public static function allEntityPhones($entityElements, $type = NULL) { |
| 188 | if (empty($entityElements)) { |
| 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | $cond = NULL; |
| 193 | if ($type) { |
| 194 | $phoneTypeId = array_search($type, CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id')); |
| 195 | if ($phoneTypeId) { |
| 196 | $cond = " AND civicrm_phone.phone_type_id = $phoneTypeId"; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | $entityId = $entityElements['entity_id']; |
| 201 | $entityTable = $entityElements['entity_table']; |
| 202 | |
| 203 | $sql = " SELECT phone, ltype.name as locationType, ph.is_primary as is_primary, |
| 204 | ph.id as phone_id, ph.location_type_id as locationTypeId |
| 205 | FROM civicrm_loc_block loc, civicrm_phone ph, civicrm_location_type ltype, {$entityTable} ev |
| 206 | WHERE ev.id = %1 |
| 207 | AND loc.id = ev.loc_block_id |
| 208 | AND ph.id IN (loc.phone_id, loc.phone_2_id) |
| 209 | AND ltype.id = ph.location_type_id |
| 210 | ORDER BY ph.is_primary DESC, phone_id ASC "; |
| 211 | |
| 212 | $params = array( |
| 213 | 1 => array( |
| 214 | $entityId, |
| 215 | 'Integer', |
| 216 | ), |
| 217 | ); |
| 218 | $numbers = array(); |
| 219 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
| 220 | while ($dao->fetch()) { |
| 221 | $numbers[$dao->phone_id] = array( |
| 222 | 'locationType' => $dao->locationType, |
| 223 | 'is_primary' => $dao->is_primary, |
| 224 | 'id' => $dao->phone_id, |
| 225 | 'phone' => $dao->phone, |
| 226 | 'locationTypeId' => $dao->locationTypeId, |
| 227 | ); |
| 228 | } |
| 229 | return $numbers; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Set NULL to phone, mapping, uffield |
| 234 | * |
| 235 | * @param $optionId |
| 236 | * Value of option to be deleted. |
| 237 | * |
| 238 | * @return void |
| 239 | */ |
| 240 | public static function setOptionToNull($optionId) { |
| 241 | if (!$optionId) { |
| 242 | return; |
| 243 | } |
| 244 | // Ensure mysql phone function exists |
| 245 | CRM_Core_DAO::checkSqlFunctionsExist(); |
| 246 | |
| 247 | $tables = array( |
| 248 | 'civicrm_phone', |
| 249 | 'civicrm_mapping_field', |
| 250 | 'civicrm_uf_field', |
| 251 | ); |
| 252 | $params = array( |
| 253 | 1 => array( |
| 254 | $optionId, |
| 255 | 'Integer', |
| 256 | ), |
| 257 | ); |
| 258 | |
| 259 | foreach ($tables as $tableName) { |
| 260 | $query = "UPDATE `{$tableName}` SET `phone_type_id` = NULL WHERE `phone_type_id` = %1"; |
| 261 | CRM_Core_DAO::executeQuery($query, $params); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Call common delete function. |
| 267 | */ |
| 268 | public static function del($id) { |
| 269 | // Ensure mysql phone function exists |
| 270 | CRM_Core_DAO::checkSqlFunctionsExist(); |
| 271 | return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Phone', $id); |
| 272 | } |
| 273 | |
| 274 | } |