Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Core_BAO_CustomValueTable { | |
36 | ||
b5c2afd0 | 37 | /** |
100fef9d | 38 | * @param array $customParams |
b5c2afd0 EM |
39 | * |
40 | * @throws Exception | |
41 | */ | |
00be9182 | 42 | public static function create(&$customParams) { |
6a488035 TO |
43 | if (empty($customParams) || |
44 | !is_array($customParams) | |
45 | ) { | |
46 | return; | |
47 | } | |
8ef12e64 | 48 | |
6a488035 TO |
49 | foreach ($customParams as $tableName => $tables) { |
50 | foreach ($tables as $index => $fields) { | |
353ffa53 TO |
51 | $sqlOP = NULL; |
52 | $hookID = NULL; | |
53 | $hookOP = NULL; | |
54 | $entityID = NULL; | |
6a488035 | 55 | $isMultiple = FALSE; |
353ffa53 TO |
56 | $set = array(); |
57 | $params = array(); | |
58 | $count = 1; | |
6a488035 TO |
59 | foreach ($fields as $field) { |
60 | if (!$sqlOP) { | |
353ffa53 TO |
61 | $entityID = $field['entity_id']; |
62 | $hookID = $field['custom_group_id']; | |
6a488035 TO |
63 | $isMultiple = $field['is_multiple']; |
64 | if (array_key_exists('id', $field)) { | |
353ffa53 TO |
65 | $sqlOP = "UPDATE $tableName "; |
66 | $where = " WHERE id = %{$count}"; | |
6a488035 TO |
67 | $params[$count] = array($field['id'], 'Integer'); |
68 | $count++; | |
69 | $hookOP = 'edit'; | |
70 | } | |
71 | else { | |
353ffa53 TO |
72 | $sqlOP = "INSERT INTO $tableName "; |
73 | $where = NULL; | |
6a488035 TO |
74 | $hookOP = 'create'; |
75 | } | |
76 | } | |
77 | ||
78 | // fix the value before we store it | |
79 | $value = $field['value']; | |
80 | $type = $field['type']; | |
81 | switch ($type) { | |
82 | case 'StateProvince': | |
83 | $type = 'Integer'; | |
84 | if (is_array($value)) { | |
85 | $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $value) . CRM_Core_DAO::VALUE_SEPARATOR; | |
86 | $type = 'String'; | |
87 | } | |
ec28b24d | 88 | elseif (!is_numeric($value) && !strstr($value, CRM_Core_DAO::VALUE_SEPARATOR)) { |
6a488035 TO |
89 | //fix for multi select state, CRM-3437 |
90 | $mulValues = explode(',', $value); | |
91 | $validStates = array(); | |
92 | foreach ($mulValues as $key => $stateVal) { | |
93 | $states = array(); | |
94 | $states['state_province'] = trim($stateVal); | |
95 | ||
96 | CRM_Utils_Array::lookupValue($states, 'state_province', | |
97 | CRM_Core_PseudoConstant::stateProvince(), TRUE | |
98 | ); | |
a7488080 | 99 | if (empty($states['state_province_id'])) { |
6a488035 TO |
100 | CRM_Utils_Array::lookupValue($states, 'state_province', |
101 | CRM_Core_PseudoConstant::stateProvinceAbbreviation(), TRUE | |
102 | ); | |
103 | } | |
ec28b24d | 104 | $validStates[] = CRM_Utils_Array::value('state_province_id', $states); |
6a488035 TO |
105 | } |
106 | $value = implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
107 | $validStates | |
108 | ); | |
109 | $type = 'String'; | |
110 | } | |
111 | elseif (!$value) { | |
112 | // CRM-3415 | |
113 | // using type of timestamp allows us to sneak in a null into db | |
114 | // gross but effective hack | |
115 | $value = NULL; | |
116 | $type = 'Timestamp'; | |
117 | } | |
ec28b24d | 118 | else { |
119 | $type = 'String'; | |
120 | } | |
6a488035 TO |
121 | break; |
122 | ||
123 | case 'Country': | |
124 | $type = 'Integer'; | |
8007b543 | 125 | $mulValues = explode(',', $value); |
6a488035 TO |
126 | if (is_array($value)) { |
127 | $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $value) . CRM_Core_DAO::VALUE_SEPARATOR; | |
128 | $type = 'String'; | |
129 | } | |
ec28b24d | 130 | elseif (!is_numeric($value) && !strstr($value, CRM_Core_DAO::VALUE_SEPARATOR)) { |
6a488035 TO |
131 | //fix for multi select country, CRM-3437 |
132 | $mulValues = explode(',', $value); | |
133 | $validCountries = array(); | |
134 | foreach ($mulValues as $key => $countryVal) { | |
135 | $countries = array(); | |
136 | $countries['country'] = trim($countryVal); | |
137 | CRM_Utils_Array::lookupValue($countries, 'country', | |
138 | CRM_Core_PseudoConstant::country(), TRUE | |
139 | ); | |
a7488080 | 140 | if (empty($countries['country_id'])) { |
6a488035 TO |
141 | CRM_Utils_Array::lookupValue($countries, 'country', |
142 | CRM_Core_PseudoConstant::countryIsoCode(), TRUE | |
143 | ); | |
144 | } | |
8415b361 | 145 | $validCountries[] = CRM_Utils_Array::value('country_id', $countries); |
6a488035 TO |
146 | } |
147 | $value = implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
148 | $validCountries | |
149 | ); | |
150 | $type = 'String'; | |
151 | } | |
152 | elseif (!$value) { | |
153 | // CRM-3415 | |
154 | // using type of timestamp allows us to sneak in a null into db | |
155 | // gross but effective hack | |
156 | $value = NULL; | |
157 | $type = 'Timestamp'; | |
158 | } | |
ec28b24d | 159 | else { |
160 | $type = 'String'; | |
161 | } | |
6a488035 TO |
162 | break; |
163 | ||
164 | case 'File': | |
165 | if (!$field['file_id']) { | |
166 | CRM_Core_Error::fatal(); | |
167 | } | |
168 | ||
169 | // need to add/update civicrm_entity_file | |
170 | $entityFileDAO = new CRM_Core_DAO_EntityFile(); | |
171 | $entityFileDAO->file_id = $field['file_id']; | |
172 | $entityFileDAO->find(TRUE); | |
173 | ||
174 | $entityFileDAO->entity_table = $field['table_name']; | |
175 | $entityFileDAO->entity_id = $field['entity_id']; | |
176 | $entityFileDAO->file_id = $field['file_id']; | |
177 | $entityFileDAO->save(); | |
178 | $entityFileDAO->free(); | |
179 | $value = $field['file_id']; | |
180 | $type = 'String'; | |
181 | break; | |
182 | ||
183 | case 'Date': | |
184 | $value = CRM_Utils_Date::isoToMysql($value); | |
185 | break; | |
186 | ||
187 | case 'Int': | |
188 | if (is_numeric($value)) { | |
189 | $type = 'Integer'; | |
190 | } | |
191 | else { | |
192 | $type = 'Timestamp'; | |
193 | } | |
194 | break; | |
195 | ||
196 | case 'ContactReference': | |
197 | if ($value == NULL) { | |
198 | $type = 'Timestamp'; | |
199 | } | |
200 | else { | |
201 | $type = 'Integer'; | |
202 | } | |
203 | break; | |
204 | ||
205 | case 'RichTextEditor': | |
206 | $type = 'String'; | |
207 | break; | |
208 | ||
209 | case 'Boolean': | |
210 | //fix for CRM-3290 | |
211 | $value = CRM_Utils_String::strtoboolstr($value); | |
212 | if ($value === FALSE) { | |
213 | $type = 'Timestamp'; | |
214 | } | |
215 | break; | |
216 | ||
217 | default: | |
218 | break; | |
219 | } | |
fd630ef9 | 220 | if (strtolower($value) === "null") { |
221 | // when unsetting a value to null, we don't need to validate the type | |
222 | // https://projectllr.atlassian.net/browse/VGQBMP-20 | |
223 | $set[$field['column_name']] = $value; | |
f931b74c | 224 | } |
225 | else { | |
fd630ef9 | 226 | $set[$field['column_name']] = "%{$count}"; |
227 | $params[$count] = array($value, $type); | |
228 | $count++; | |
229 | } | |
6a488035 TO |
230 | } |
231 | ||
232 | if (!empty($set)) { | |
233 | $setClause = array(); | |
234 | foreach ($set as $n => $v) { | |
235 | $setClause[] = "$n = $v"; | |
236 | } | |
237 | $setClause = implode(',', $setClause); | |
238 | if (!$where) { | |
239 | // do this only for insert | |
240 | $set['entity_id'] = "%{$count}"; | |
241 | $params[$count] = array($entityID, 'Integer'); | |
242 | $count++; | |
243 | ||
353ffa53 | 244 | $fieldNames = implode(',', array_keys($set)); |
6a488035 | 245 | $fieldValues = implode(',', array_values($set)); |
353ffa53 | 246 | $query = "$sqlOP ( $fieldNames ) VALUES ( $fieldValues )"; |
6a488035 TO |
247 | // for multiple values we dont do on duplicate key update |
248 | if (!$isMultiple) { | |
249 | $query .= " ON DUPLICATE KEY UPDATE $setClause"; | |
250 | } | |
251 | } | |
252 | else { | |
253 | $query = "$sqlOP SET $setClause $where"; | |
254 | } | |
255 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
256 | ||
257 | CRM_Utils_Hook::custom($hookOP, | |
258 | $hookID, | |
259 | $entityID, | |
260 | $fields | |
261 | ); | |
262 | } | |
263 | } | |
264 | } | |
265 | } | |
266 | ||
267 | /** | |
fe482240 | 268 | * Given a field return the mysql data type associated with it. |
6a488035 | 269 | * |
6a0b768e | 270 | * @param string $type |
fd31fa4c EM |
271 | * @param int $maxLength |
272 | * | |
72b3a70c CW |
273 | * @return string |
274 | * the mysql data store placeholder | |
6a488035 TO |
275 | */ |
276 | public static function fieldToSQLType($type, $maxLength = 255) { | |
277 | if (!isset($maxLength) || | |
278 | !is_numeric($maxLength) || | |
279 | $maxLength <= 0 | |
280 | ) { | |
281 | $maxLength = 255; | |
282 | } | |
283 | ||
284 | switch ($type) { | |
285 | case 'String': | |
286 | case 'Link': | |
287 | return "varchar($maxLength)"; | |
288 | ||
289 | case 'Boolean': | |
290 | return 'tinyint'; | |
291 | ||
292 | case 'Int': | |
293 | return 'int'; | |
2aa397bc | 294 | |
6a488035 TO |
295 | // the below three are FK's, and have constraints added to them |
296 | ||
297 | case 'ContactReference': | |
298 | case 'StateProvince': | |
299 | case 'Country': | |
300 | case 'File': | |
301 | return 'int unsigned'; | |
302 | ||
303 | case 'Float': | |
304 | return 'double'; | |
305 | ||
306 | case 'Money': | |
307 | return 'decimal(20,2)'; | |
308 | ||
309 | case 'Memo': | |
310 | case 'RichTextEditor': | |
311 | return 'text'; | |
312 | ||
313 | case 'Date': | |
314 | return 'datetime'; | |
315 | ||
316 | default: | |
317 | CRM_Core_Error::fatal(); | |
318 | } | |
319 | } | |
320 | ||
b5c2afd0 | 321 | /** |
c490a46a | 322 | * @param array $params |
b5c2afd0 | 323 | * @param $entityTable |
100fef9d | 324 | * @param int $entityID |
b5c2afd0 | 325 | */ |
00be9182 | 326 | public static function store(&$params, $entityTable, $entityID) { |
6a488035 TO |
327 | $cvParams = array(); |
328 | foreach ($params as $fieldID => $param) { | |
329 | foreach ($param as $index => $customValue) { | |
330 | $cvParam = array( | |
331 | 'entity_table' => $entityTable, | |
332 | 'entity_id' => $entityID, | |
333 | 'value' => $customValue['value'], | |
334 | 'type' => $customValue['type'], | |
335 | 'custom_field_id' => $customValue['custom_field_id'], | |
336 | 'custom_group_id' => $customValue['custom_group_id'], | |
337 | 'table_name' => $customValue['table_name'], | |
338 | 'column_name' => $customValue['column_name'], | |
339 | 'is_multiple' => CRM_Utils_Array::value('is_multiple', $customValue), | |
340 | 'file_id' => $customValue['file_id'], | |
341 | ); | |
342 | ||
fe482240 | 343 | // Fix Date type to be timestamp, since that is how we store in db. |
6a488035 TO |
344 | if ($cvParam['type'] == 'Date') { |
345 | $cvParam['type'] = 'Timestamp'; | |
346 | } | |
347 | ||
a7488080 | 348 | if (!empty($customValue['id'])) { |
6a488035 TO |
349 | $cvParam['id'] = $customValue['id']; |
350 | } | |
351 | if (!array_key_exists($customValue['table_name'], $cvParams)) { | |
352 | $cvParams[$customValue['table_name']] = array(); | |
353 | } | |
354 | ||
355 | if (!array_key_exists($index, $cvParams[$customValue['table_name']])) { | |
356 | $cvParams[$customValue['table_name']][$index] = array(); | |
357 | } | |
358 | ||
359 | $cvParams[$customValue['table_name']][$index][] = $cvParam; | |
360 | } | |
361 | } | |
362 | if (!empty($cvParams)) { | |
363 | self::create($cvParams); | |
364 | } | |
365 | } | |
366 | ||
b5c2afd0 | 367 | /** |
fe482240 EM |
368 | * Post process function. |
369 | * | |
c490a46a | 370 | * @param array $params |
b5c2afd0 | 371 | * @param $entityTable |
100fef9d | 372 | * @param int $entityID |
b5c2afd0 EM |
373 | * @param $customFieldExtends |
374 | */ | |
5fc3ea24 | 375 | public static function postProcess(&$params, $entityTable, $entityID, $customFieldExtends) { |
6a488035 | 376 | $customData = CRM_Core_BAO_CustomField::postProcess($params, |
6a488035 TO |
377 | $entityID, |
378 | $customFieldExtends | |
379 | ); | |
380 | ||
381 | if (!empty($customData)) { | |
382 | self::store($customData, $entityTable, $entityID); | |
383 | } | |
384 | } | |
385 | ||
386 | /** | |
387 | * Return an array of all custom values associated with an entity. | |
388 | * | |
6a0b768e TO |
389 | * @param int $entityID |
390 | * Identification number of the entity. | |
391 | * @param string $entityType | |
392 | * Type of entity that the entityID corresponds to, specified. | |
6a488035 TO |
393 | * as a string with format "'<EntityName>'". Comma separated |
394 | * list may be used to specify OR matches. Allowable values | |
395 | * are enumerated types in civicrm_custom_group.extends field. | |
396 | * Optional. Default value assumes entityID references a | |
397 | * contact entity. | |
6a0b768e TO |
398 | * @param array $fieldIDs |
399 | * Optional list of fieldIDs that we want to retrieve. If this. | |
6a488035 TO |
400 | * is set the entityType is ignored |
401 | * | |
77b97be7 | 402 | * @param bool $formatMultiRecordField |
c693f065 | 403 | * @param array $DTparams - CRM-17810 dataTable params for the multiValued custom fields. |
77b97be7 | 404 | * |
a6c01b45 CW |
405 | * @return array |
406 | * Array of custom values for the entity with key=>value | |
6a488035 TO |
407 | * pairs specified as civicrm_custom_field.id => custom value. |
408 | * Empty array if no custom values found. | |
6a488035 | 409 | */ |
c693f065 | 410 | public static function &getEntityValues($entityID, $entityType = NULL, $fieldIDs = NULL, $formatMultiRecordField = FALSE, $DTparams = NULL) { |
6a488035 TO |
411 | if (!$entityID) { |
412 | // adding this here since an empty contact id could have serious repurcussions | |
413 | // like looping forever | |
414 | CRM_Core_Error::fatal('Please file an issue with the backtrace'); | |
415 | return NULL; | |
416 | } | |
417 | ||
418 | $cond = array(); | |
419 | if ($entityType) { | |
420 | $cond[] = "cg.extends IN ( '$entityType' )"; | |
421 | } | |
422 | if ($fieldIDs && | |
423 | is_array($fieldIDs) | |
424 | ) { | |
425 | $fieldIDList = implode(',', $fieldIDs); | |
426 | $cond[] = "cf.id IN ( $fieldIDList )"; | |
427 | } | |
428 | if (empty($cond)) { | |
429 | $cond[] = "cg.extends IN ( 'Contact', 'Individual', 'Household', 'Organization' )"; | |
430 | } | |
431 | $cond = implode(' AND ', $cond); | |
432 | ||
e87c8fb7 | 433 | $limit = $orderBy = ''; |
c693f065 | 434 | if (!empty($DTparams['rowCount']) && $DTparams['rowCount'] > 0) { |
d51e02d3 | 435 | $limit = " LIMIT " . CRM_Utils_Type::escape($DTparams['offset'], 'Integer') . ", " . CRM_Utils_Type::escape($DTparams['rowCount'], 'Integer'); |
c693f065 | 436 | } |
c693f065 | 437 | if (!empty($DTparams['sort'])) { |
438 | $orderBy = ' ORDER BY ' . CRM_Utils_Type::escape($DTparams['sort'], 'String'); | |
439 | } | |
440 | ||
fe482240 | 441 | // First find all the fields that extend this type of entity. |
6a488035 TO |
442 | $query = " |
443 | SELECT cg.table_name, | |
444 | cg.id as groupID, | |
445 | cg.is_multiple, | |
446 | cf.column_name, | |
34f51a07 N |
447 | cf.id as fieldID, |
448 | cf.data_type as fieldDataType | |
6a488035 TO |
449 | FROM civicrm_custom_group cg, |
450 | civicrm_custom_field cf | |
451 | WHERE cf.custom_group_id = cg.id | |
452 | AND cg.is_active = 1 | |
453 | AND cf.is_active = 1 | |
454 | AND $cond | |
455 | "; | |
456 | $dao = CRM_Core_DAO::executeQuery($query); | |
457 | ||
458 | $select = $fields = $isMultiple = array(); | |
459 | ||
460 | while ($dao->fetch()) { | |
461 | if (!array_key_exists($dao->table_name, $select)) { | |
462 | $fields[$dao->table_name] = array(); | |
463 | $select[$dao->table_name] = array(); | |
464 | } | |
465 | $fields[$dao->table_name][] = $dao->fieldID; | |
466 | $select[$dao->table_name][] = "{$dao->column_name} AS custom_{$dao->fieldID}"; | |
467 | $isMultiple[$dao->table_name] = $dao->is_multiple ? TRUE : FALSE; | |
77b97be7 | 468 | $file[$dao->table_name][$dao->fieldID] = $dao->fieldDataType; |
6a488035 TO |
469 | } |
470 | ||
471 | $result = array(); | |
472 | foreach ($select as $tableName => $clauses) { | |
c693f065 | 473 | $query = "SELECT SQL_CALC_FOUND_ROWS id, " . implode(', ', $clauses) . " FROM $tableName WHERE entity_id = $entityID {$orderBy} {$limit}"; |
6a488035 | 474 | $dao = CRM_Core_DAO::executeQuery($query); |
e87c8fb7 | 475 | if (!empty($DTparams)) { |
476 | $result['count'] = CRM_Core_DAO::singleValueQuery('SELECT FOUND_ROWS()'); | |
477 | } | |
6a488035 TO |
478 | while ($dao->fetch()) { |
479 | foreach ($fields[$tableName] as $fieldID) { | |
480 | $fieldName = "custom_{$fieldID}"; | |
481 | if ($isMultiple[$tableName]) { | |
482 | if ($formatMultiRecordField) { | |
d8f34a6e | 483 | $result["{$dao->id}"]["{$fieldID}"] = $dao->$fieldName; |
fe482240 EM |
484 | } |
485 | else { | |
6a488035 TO |
486 | $result["{$fieldID}_{$dao->id}"] = $dao->$fieldName; |
487 | } | |
488 | } | |
489 | else { | |
d8f34a6e | 490 | $result[$fieldID] = $dao->$fieldName; |
6a488035 TO |
491 | } |
492 | } | |
493 | } | |
494 | } | |
495 | return $result; | |
496 | } | |
497 | ||
498 | /** | |
100fef9d | 499 | * Take in an array of entityID, custom_XXX => value |
6a488035 TO |
500 | * and set the value in the appropriate table. Should also be able |
501 | * to set the value to null. Follows api parameter/return conventions | |
502 | * | |
503 | * @array $params | |
504 | * | |
c490a46a | 505 | * @param array $params |
2a6da8d7 EM |
506 | * |
507 | * @throws Exception | |
6a488035 | 508 | * @return array |
6a488035 | 509 | */ |
00be9182 | 510 | public static function setValues(&$params) { |
6a488035 TO |
511 | |
512 | if (!isset($params['entityID']) || | |
513 | CRM_Utils_Type::escape($params['entityID'], 'Integer', FALSE) === NULL | |
514 | ) { | |
515 | return CRM_Core_Error::createAPIError(ts('entityID needs to be set and of type Integer')); | |
516 | } | |
517 | ||
518 | // first collect all the id/value pairs. The format is: | |
519 | // custom_X => value or custom_X_VALUEID => value (for multiple values), VALUEID == -1, -2 etc for new insertions | |
520 | $values = array(); | |
521 | $fieldValues = array(); | |
522 | foreach ($params as $n => $v) { | |
523 | if ($customFieldInfo = CRM_Core_BAO_CustomField::getKeyID($n, TRUE)) { | |
524 | $fieldID = (int ) $customFieldInfo[0]; | |
525 | if (CRM_Utils_Type::escape($fieldID, 'Integer', FALSE) === NULL) { | |
526 | return CRM_Core_Error::createAPIError(ts('field ID needs to be of type Integer for index %1', | |
353ffa53 TO |
527 | array(1 => $fieldID) |
528 | )); | |
6a488035 TO |
529 | } |
530 | if (!array_key_exists($fieldID, $fieldValues)) { | |
531 | $fieldValues[$fieldID] = array(); | |
532 | } | |
533 | $id = -1; | |
534 | if ($customFieldInfo[1]) { | |
535 | $id = (int ) $customFieldInfo[1]; | |
536 | } | |
537 | $fieldValues[$fieldID][] = array( | |
538 | 'value' => $v, | |
539 | 'id' => $id, | |
540 | ); | |
541 | } | |
542 | } | |
543 | ||
544 | $fieldIDList = implode(',', array_keys($fieldValues)); | |
545 | ||
546 | // format it so that we can just use create | |
547 | $sql = " | |
548 | SELECT cg.table_name as table_name , | |
549 | cg.id as cg_id , | |
550 | cg.is_multiple as is_multiple, | |
551 | cf.column_name as column_name, | |
552 | cf.id as cf_id , | |
553 | cf.data_type as data_type | |
554 | FROM civicrm_custom_group cg, | |
555 | civicrm_custom_field cf | |
556 | WHERE cf.custom_group_id = cg.id | |
557 | AND cf.id IN ( $fieldIDList ) | |
558 | "; | |
559 | ||
560 | $dao = CRM_Core_DAO::executeQuery($sql); | |
561 | $cvParams = array(); | |
562 | ||
563 | while ($dao->fetch()) { | |
564 | $dataType = $dao->data_type == 'Date' ? 'Timestamp' : $dao->data_type; | |
565 | foreach ($fieldValues[$dao->cf_id] as $fieldValue) { | |
566 | // Format null values correctly | |
567 | if ($fieldValue['value'] === NULL || $fieldValue['value'] === '') { | |
568 | switch ($dataType) { | |
569 | case 'String': | |
570 | case 'Int': | |
571 | case 'Link': | |
572 | case 'Boolean': | |
573 | $fieldValue['value'] = ''; | |
574 | break; | |
575 | ||
576 | case 'Timestamp': | |
577 | $fieldValue['value'] = NULL; | |
578 | break; | |
579 | ||
580 | case 'StateProvince': | |
581 | case 'Country': | |
582 | case 'Money': | |
583 | case 'Float': | |
2aa397bc | 584 | $fieldValue['value'] = (int) 0; |
6a488035 TO |
585 | break; |
586 | } | |
587 | } | |
588 | // Ensure that value is of the right data type | |
589 | elseif (CRM_Utils_Type::escape($fieldValue['value'], $dataType, FALSE) === NULL) { | |
590 | return CRM_Core_Error::createAPIError(ts('value: %1 is not of the right field data type: %2', | |
353ffa53 TO |
591 | array( |
592 | 1 => $fieldValue['value'], | |
593 | 2 => $dao->data_type, | |
594 | ) | |
595 | )); | |
6a488035 TO |
596 | } |
597 | ||
598 | $cvParam = array( | |
599 | 'entity_id' => $params['entityID'], | |
600 | 'value' => $fieldValue['value'], | |
601 | 'type' => $dataType, | |
602 | 'custom_field_id' => $dao->cf_id, | |
603 | 'custom_group_id' => $dao->cg_id, | |
604 | 'table_name' => $dao->table_name, | |
605 | 'column_name' => $dao->column_name, | |
606 | 'is_multiple' => $dao->is_multiple, | |
607 | ); | |
608 | ||
e7dcccf0 CW |
609 | if ($cvParam['type'] == 'File') { |
610 | $cvParam['file_id'] = $fieldValue['value']; | |
611 | } | |
612 | ||
6a488035 TO |
613 | if (!array_key_exists($dao->table_name, $cvParams)) { |
614 | $cvParams[$dao->table_name] = array(); | |
615 | } | |
616 | ||
617 | if (!array_key_exists($fieldValue['id'], $cvParams[$dao->table_name])) { | |
618 | $cvParams[$dao->table_name][$fieldValue['id']] = array(); | |
619 | } | |
620 | ||
621 | if ($fieldValue['id'] > 0) { | |
622 | $cvParam['id'] = $fieldValue['id']; | |
623 | } | |
624 | $cvParams[$dao->table_name][$fieldValue['id']][] = $cvParam; | |
625 | } | |
626 | } | |
627 | ||
628 | if (!empty($cvParams)) { | |
629 | self::create($cvParams); | |
630 | return array('is_error' => 0, 'result' => 1); | |
631 | } | |
632 | ||
633 | return CRM_Core_Error::createAPIError(ts('Unknown error')); | |
634 | } | |
635 | ||
636 | /** | |
100fef9d | 637 | * Take in an array of entityID, custom_ID |
6a488035 TO |
638 | * and gets the value from the appropriate table. |
639 | * | |
640 | * To get the values of custom fields with IDs 13 and 43 for contact ID 1327, use: | |
641 | * $params = array( 'entityID' => 1327, 'custom_13' => 1, 'custom_43' => 1 ); | |
642 | * | |
b44e3f84 | 643 | * Entity Type will be inferred by the custom fields you request |
6a488035 TO |
644 | * Specify $params['entityType'] if you do not supply any custom fields to return |
645 | * and entity type is other than Contact | |
646 | * | |
647 | * @array $params | |
648 | * | |
c490a46a | 649 | * @param array $params |
2a6da8d7 EM |
650 | * |
651 | * @throws Exception | |
6a488035 | 652 | * @return array |
6a488035 | 653 | */ |
00be9182 | 654 | public static function &getValues(&$params) { |
6a488035 TO |
655 | if (empty($params)) { |
656 | return NULL; | |
657 | } | |
658 | if (!isset($params['entityID']) || | |
659 | CRM_Utils_Type::escape($params['entityID'], | |
660 | 'Integer', FALSE | |
661 | ) === NULL | |
662 | ) { | |
663 | return CRM_Core_Error::createAPIError(ts('entityID needs to be set and of type Integer')); | |
664 | } | |
665 | ||
666 | // first collect all the ids. The format is: | |
667 | // custom_ID | |
668 | $fieldIDs = array(); | |
669 | foreach ($params as $n => $v) { | |
670 | $key = $idx = NULL; | |
671 | if (substr($n, 0, 7) == 'custom_') { | |
672 | $idx = substr($n, 7); | |
673 | if (CRM_Utils_Type::escape($idx, 'Integer', FALSE) === NULL) { | |
674 | return CRM_Core_Error::createAPIError(ts('field ID needs to be of type Integer for index %1', | |
353ffa53 TO |
675 | array(1 => $idx) |
676 | )); | |
6a488035 TO |
677 | } |
678 | $fieldIDs[] = (int ) $idx; | |
679 | } | |
680 | } | |
681 | ||
682 | $default = array('Contact', 'Individual', 'Household', 'Organization'); | |
683 | if (!($type = CRM_Utils_Array::value('entityType', $params)) || | |
684 | in_array($params['entityType'], $default) | |
685 | ) { | |
686 | $type = NULL; | |
687 | } | |
688 | else { | |
689 | $entities = CRM_Core_SelectValues::customGroupExtends(); | |
690 | if (!array_key_exists($type, $entities)) { | |
691 | if (in_array($type, $entities)) { | |
692 | $type = $entities[$type]; | |
693 | if (in_array($type, $default)) { | |
694 | $type = NULL; | |
695 | } | |
696 | } | |
697 | else { | |
698 | return CRM_Core_Error::createAPIError(ts('Invalid entity type') . ': "' . $type . '"'); | |
699 | } | |
700 | } | |
701 | } | |
702 | ||
703 | $values = self::getEntityValues($params['entityID'], | |
704 | $type, | |
705 | $fieldIDs | |
706 | ); | |
707 | if (empty($values)) { | |
708 | // note that this behaviour is undesirable from an API point of view - it should return an empty array | |
709 | // since this is also called by the merger code & not sure the consequences of changing | |
710 | // are just handling undoing this in the api layer. ie. converting the error back into a success | |
711 | $result = array( | |
712 | 'is_error' => 1, | |
713 | 'error_message' => 'No values found for the specified entity ID and custom field(s).', | |
714 | ); | |
715 | return $result; | |
716 | } | |
717 | else { | |
718 | $result = array( | |
719 | 'is_error' => 0, | |
720 | 'entityID' => $params['entityID'], | |
721 | ); | |
722 | foreach ($values as $id => $value) { | |
723 | $result["custom_{$id}"] = $value; | |
724 | } | |
725 | return $result; | |
726 | } | |
727 | } | |
96025800 | 728 | |
6a488035 | 729 | } |