Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | 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'; | |
ec28b24d | 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 | } | |
ec28b24d | 145 | $validCountries[] = CRM_Utils_Array::value('country_id', $states); |
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 | } | |
220 | $set[$field['column_name']] = "%{$count}"; | |
221 | $params[$count] = array($value, $type); | |
222 | $count++; | |
223 | } | |
224 | ||
225 | if (!empty($set)) { | |
226 | $setClause = array(); | |
227 | foreach ($set as $n => $v) { | |
228 | $setClause[] = "$n = $v"; | |
229 | } | |
230 | $setClause = implode(',', $setClause); | |
231 | if (!$where) { | |
232 | // do this only for insert | |
233 | $set['entity_id'] = "%{$count}"; | |
234 | $params[$count] = array($entityID, 'Integer'); | |
235 | $count++; | |
236 | ||
353ffa53 | 237 | $fieldNames = implode(',', array_keys($set)); |
6a488035 | 238 | $fieldValues = implode(',', array_values($set)); |
353ffa53 | 239 | $query = "$sqlOP ( $fieldNames ) VALUES ( $fieldValues )"; |
6a488035 TO |
240 | // for multiple values we dont do on duplicate key update |
241 | if (!$isMultiple) { | |
242 | $query .= " ON DUPLICATE KEY UPDATE $setClause"; | |
243 | } | |
244 | } | |
245 | else { | |
246 | $query = "$sqlOP SET $setClause $where"; | |
247 | } | |
248 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
249 | ||
250 | CRM_Utils_Hook::custom($hookOP, | |
251 | $hookID, | |
252 | $entityID, | |
253 | $fields | |
254 | ); | |
255 | } | |
256 | } | |
257 | } | |
258 | } | |
259 | ||
260 | /** | |
fe482240 | 261 | * Given a field return the mysql data type associated with it. |
6a488035 | 262 | * |
6a0b768e | 263 | * @param string $type |
fd31fa4c EM |
264 | * @param int $maxLength |
265 | * | |
72b3a70c CW |
266 | * @return string |
267 | * the mysql data store placeholder | |
6a488035 TO |
268 | */ |
269 | public static function fieldToSQLType($type, $maxLength = 255) { | |
270 | if (!isset($maxLength) || | |
271 | !is_numeric($maxLength) || | |
272 | $maxLength <= 0 | |
273 | ) { | |
274 | $maxLength = 255; | |
275 | } | |
276 | ||
277 | switch ($type) { | |
278 | case 'String': | |
279 | case 'Link': | |
280 | return "varchar($maxLength)"; | |
281 | ||
282 | case 'Boolean': | |
283 | return 'tinyint'; | |
284 | ||
285 | case 'Int': | |
286 | return 'int'; | |
2aa397bc | 287 | |
6a488035 TO |
288 | // the below three are FK's, and have constraints added to them |
289 | ||
290 | case 'ContactReference': | |
291 | case 'StateProvince': | |
292 | case 'Country': | |
293 | case 'File': | |
294 | return 'int unsigned'; | |
295 | ||
296 | case 'Float': | |
297 | return 'double'; | |
298 | ||
299 | case 'Money': | |
300 | return 'decimal(20,2)'; | |
301 | ||
302 | case 'Memo': | |
303 | case 'RichTextEditor': | |
304 | return 'text'; | |
305 | ||
306 | case 'Date': | |
307 | return 'datetime'; | |
308 | ||
309 | default: | |
310 | CRM_Core_Error::fatal(); | |
311 | } | |
312 | } | |
313 | ||
b5c2afd0 | 314 | /** |
c490a46a | 315 | * @param array $params |
b5c2afd0 | 316 | * @param $entityTable |
100fef9d | 317 | * @param int $entityID |
b5c2afd0 | 318 | */ |
00be9182 | 319 | public static function store(&$params, $entityTable, $entityID) { |
6a488035 TO |
320 | $cvParams = array(); |
321 | foreach ($params as $fieldID => $param) { | |
322 | foreach ($param as $index => $customValue) { | |
323 | $cvParam = array( | |
324 | 'entity_table' => $entityTable, | |
325 | 'entity_id' => $entityID, | |
326 | 'value' => $customValue['value'], | |
327 | 'type' => $customValue['type'], | |
328 | 'custom_field_id' => $customValue['custom_field_id'], | |
329 | 'custom_group_id' => $customValue['custom_group_id'], | |
330 | 'table_name' => $customValue['table_name'], | |
331 | 'column_name' => $customValue['column_name'], | |
332 | 'is_multiple' => CRM_Utils_Array::value('is_multiple', $customValue), | |
333 | 'file_id' => $customValue['file_id'], | |
334 | ); | |
335 | ||
fe482240 | 336 | // Fix Date type to be timestamp, since that is how we store in db. |
6a488035 TO |
337 | if ($cvParam['type'] == 'Date') { |
338 | $cvParam['type'] = 'Timestamp'; | |
339 | } | |
340 | ||
a7488080 | 341 | if (!empty($customValue['id'])) { |
6a488035 TO |
342 | $cvParam['id'] = $customValue['id']; |
343 | } | |
344 | if (!array_key_exists($customValue['table_name'], $cvParams)) { | |
345 | $cvParams[$customValue['table_name']] = array(); | |
346 | } | |
347 | ||
348 | if (!array_key_exists($index, $cvParams[$customValue['table_name']])) { | |
349 | $cvParams[$customValue['table_name']][$index] = array(); | |
350 | } | |
351 | ||
352 | $cvParams[$customValue['table_name']][$index][] = $cvParam; | |
353 | } | |
354 | } | |
355 | if (!empty($cvParams)) { | |
356 | self::create($cvParams); | |
357 | } | |
358 | } | |
359 | ||
b5c2afd0 | 360 | /** |
fe482240 EM |
361 | * Post process function. |
362 | * | |
c490a46a | 363 | * @param array $params |
b5c2afd0 EM |
364 | * @param $customFields |
365 | * @param $entityTable | |
100fef9d | 366 | * @param int $entityID |
b5c2afd0 EM |
367 | * @param $customFieldExtends |
368 | */ | |
00be9182 | 369 | public static function postProcess(&$params, &$customFields, $entityTable, $entityID, $customFieldExtends) { |
6a488035 TO |
370 | $customData = CRM_Core_BAO_CustomField::postProcess($params, |
371 | $customFields, | |
372 | $entityID, | |
373 | $customFieldExtends | |
374 | ); | |
375 | ||
376 | if (!empty($customData)) { | |
377 | self::store($customData, $entityTable, $entityID); | |
378 | } | |
379 | } | |
380 | ||
381 | /** | |
382 | * Return an array of all custom values associated with an entity. | |
383 | * | |
6a0b768e TO |
384 | * @param int $entityID |
385 | * Identification number of the entity. | |
386 | * @param string $entityType | |
387 | * Type of entity that the entityID corresponds to, specified. | |
6a488035 TO |
388 | * as a string with format "'<EntityName>'". Comma separated |
389 | * list may be used to specify OR matches. Allowable values | |
390 | * are enumerated types in civicrm_custom_group.extends field. | |
391 | * Optional. Default value assumes entityID references a | |
392 | * contact entity. | |
6a0b768e TO |
393 | * @param array $fieldIDs |
394 | * Optional list of fieldIDs that we want to retrieve. If this. | |
6a488035 TO |
395 | * is set the entityType is ignored |
396 | * | |
77b97be7 EM |
397 | * @param bool $formatMultiRecordField |
398 | * | |
a6c01b45 CW |
399 | * @return array |
400 | * Array of custom values for the entity with key=>value | |
6a488035 TO |
401 | * pairs specified as civicrm_custom_field.id => custom value. |
402 | * Empty array if no custom values found. | |
6a488035 TO |
403 | */ |
404 | public static function &getEntityValues($entityID, $entityType = NULL, $fieldIDs = NULL, $formatMultiRecordField = FALSE) { | |
405 | if (!$entityID) { | |
406 | // adding this here since an empty contact id could have serious repurcussions | |
407 | // like looping forever | |
408 | CRM_Core_Error::fatal('Please file an issue with the backtrace'); | |
409 | return NULL; | |
410 | } | |
411 | ||
412 | $cond = array(); | |
413 | if ($entityType) { | |
414 | $cond[] = "cg.extends IN ( '$entityType' )"; | |
415 | } | |
416 | if ($fieldIDs && | |
417 | is_array($fieldIDs) | |
418 | ) { | |
419 | $fieldIDList = implode(',', $fieldIDs); | |
420 | $cond[] = "cf.id IN ( $fieldIDList )"; | |
421 | } | |
422 | if (empty($cond)) { | |
423 | $cond[] = "cg.extends IN ( 'Contact', 'Individual', 'Household', 'Organization' )"; | |
424 | } | |
425 | $cond = implode(' AND ', $cond); | |
426 | ||
fe482240 | 427 | // First find all the fields that extend this type of entity. |
6a488035 TO |
428 | $query = " |
429 | SELECT cg.table_name, | |
430 | cg.id as groupID, | |
431 | cg.is_multiple, | |
432 | cf.column_name, | |
34f51a07 N |
433 | cf.id as fieldID, |
434 | cf.data_type as fieldDataType | |
6a488035 TO |
435 | FROM civicrm_custom_group cg, |
436 | civicrm_custom_field cf | |
437 | WHERE cf.custom_group_id = cg.id | |
438 | AND cg.is_active = 1 | |
439 | AND cf.is_active = 1 | |
440 | AND $cond | |
441 | "; | |
442 | $dao = CRM_Core_DAO::executeQuery($query); | |
443 | ||
444 | $select = $fields = $isMultiple = array(); | |
445 | ||
446 | while ($dao->fetch()) { | |
447 | if (!array_key_exists($dao->table_name, $select)) { | |
448 | $fields[$dao->table_name] = array(); | |
449 | $select[$dao->table_name] = array(); | |
450 | } | |
451 | $fields[$dao->table_name][] = $dao->fieldID; | |
452 | $select[$dao->table_name][] = "{$dao->column_name} AS custom_{$dao->fieldID}"; | |
453 | $isMultiple[$dao->table_name] = $dao->is_multiple ? TRUE : FALSE; | |
77b97be7 | 454 | $file[$dao->table_name][$dao->fieldID] = $dao->fieldDataType; |
6a488035 TO |
455 | } |
456 | ||
457 | $result = array(); | |
458 | foreach ($select as $tableName => $clauses) { | |
459 | $query = "SELECT id, " . implode(', ', $clauses) . " FROM $tableName WHERE entity_id = $entityID"; | |
460 | $dao = CRM_Core_DAO::executeQuery($query); | |
461 | while ($dao->fetch()) { | |
462 | foreach ($fields[$tableName] as $fieldID) { | |
463 | $fieldName = "custom_{$fieldID}"; | |
464 | if ($isMultiple[$tableName]) { | |
465 | if ($formatMultiRecordField) { | |
d8f34a6e | 466 | $result["{$dao->id}"]["{$fieldID}"] = $dao->$fieldName; |
fe482240 EM |
467 | } |
468 | else { | |
6a488035 TO |
469 | $result["{$fieldID}_{$dao->id}"] = $dao->$fieldName; |
470 | } | |
471 | } | |
472 | else { | |
d8f34a6e | 473 | $result[$fieldID] = $dao->$fieldName; |
6a488035 TO |
474 | } |
475 | } | |
476 | } | |
477 | } | |
478 | return $result; | |
479 | } | |
480 | ||
481 | /** | |
100fef9d | 482 | * Take in an array of entityID, custom_XXX => value |
6a488035 TO |
483 | * and set the value in the appropriate table. Should also be able |
484 | * to set the value to null. Follows api parameter/return conventions | |
485 | * | |
486 | * @array $params | |
487 | * | |
c490a46a | 488 | * @param array $params |
2a6da8d7 EM |
489 | * |
490 | * @throws Exception | |
6a488035 | 491 | * @return array |
6a488035 | 492 | */ |
00be9182 | 493 | public static function setValues(&$params) { |
6a488035 TO |
494 | |
495 | if (!isset($params['entityID']) || | |
496 | CRM_Utils_Type::escape($params['entityID'], 'Integer', FALSE) === NULL | |
497 | ) { | |
498 | return CRM_Core_Error::createAPIError(ts('entityID needs to be set and of type Integer')); | |
499 | } | |
500 | ||
501 | // first collect all the id/value pairs. The format is: | |
502 | // custom_X => value or custom_X_VALUEID => value (for multiple values), VALUEID == -1, -2 etc for new insertions | |
503 | $values = array(); | |
504 | $fieldValues = array(); | |
505 | foreach ($params as $n => $v) { | |
506 | if ($customFieldInfo = CRM_Core_BAO_CustomField::getKeyID($n, TRUE)) { | |
507 | $fieldID = (int ) $customFieldInfo[0]; | |
508 | if (CRM_Utils_Type::escape($fieldID, 'Integer', FALSE) === NULL) { | |
509 | return CRM_Core_Error::createAPIError(ts('field ID needs to be of type Integer for index %1', | |
353ffa53 TO |
510 | array(1 => $fieldID) |
511 | )); | |
6a488035 TO |
512 | } |
513 | if (!array_key_exists($fieldID, $fieldValues)) { | |
514 | $fieldValues[$fieldID] = array(); | |
515 | } | |
516 | $id = -1; | |
517 | if ($customFieldInfo[1]) { | |
518 | $id = (int ) $customFieldInfo[1]; | |
519 | } | |
520 | $fieldValues[$fieldID][] = array( | |
521 | 'value' => $v, | |
522 | 'id' => $id, | |
523 | ); | |
524 | } | |
525 | } | |
526 | ||
527 | $fieldIDList = implode(',', array_keys($fieldValues)); | |
528 | ||
529 | // format it so that we can just use create | |
530 | $sql = " | |
531 | SELECT cg.table_name as table_name , | |
532 | cg.id as cg_id , | |
533 | cg.is_multiple as is_multiple, | |
534 | cf.column_name as column_name, | |
535 | cf.id as cf_id , | |
536 | cf.data_type as data_type | |
537 | FROM civicrm_custom_group cg, | |
538 | civicrm_custom_field cf | |
539 | WHERE cf.custom_group_id = cg.id | |
540 | AND cf.id IN ( $fieldIDList ) | |
541 | "; | |
542 | ||
543 | $dao = CRM_Core_DAO::executeQuery($sql); | |
544 | $cvParams = array(); | |
545 | ||
546 | while ($dao->fetch()) { | |
547 | $dataType = $dao->data_type == 'Date' ? 'Timestamp' : $dao->data_type; | |
548 | foreach ($fieldValues[$dao->cf_id] as $fieldValue) { | |
549 | // Format null values correctly | |
550 | if ($fieldValue['value'] === NULL || $fieldValue['value'] === '') { | |
551 | switch ($dataType) { | |
552 | case 'String': | |
553 | case 'Int': | |
554 | case 'Link': | |
555 | case 'Boolean': | |
556 | $fieldValue['value'] = ''; | |
557 | break; | |
558 | ||
559 | case 'Timestamp': | |
560 | $fieldValue['value'] = NULL; | |
561 | break; | |
562 | ||
563 | case 'StateProvince': | |
564 | case 'Country': | |
565 | case 'Money': | |
566 | case 'Float': | |
2aa397bc | 567 | $fieldValue['value'] = (int) 0; |
6a488035 TO |
568 | break; |
569 | } | |
570 | } | |
571 | // Ensure that value is of the right data type | |
572 | elseif (CRM_Utils_Type::escape($fieldValue['value'], $dataType, FALSE) === NULL) { | |
573 | return CRM_Core_Error::createAPIError(ts('value: %1 is not of the right field data type: %2', | |
353ffa53 TO |
574 | array( |
575 | 1 => $fieldValue['value'], | |
576 | 2 => $dao->data_type, | |
577 | ) | |
578 | )); | |
6a488035 TO |
579 | } |
580 | ||
581 | $cvParam = array( | |
582 | 'entity_id' => $params['entityID'], | |
583 | 'value' => $fieldValue['value'], | |
584 | 'type' => $dataType, | |
585 | 'custom_field_id' => $dao->cf_id, | |
586 | 'custom_group_id' => $dao->cg_id, | |
587 | 'table_name' => $dao->table_name, | |
588 | 'column_name' => $dao->column_name, | |
589 | 'is_multiple' => $dao->is_multiple, | |
590 | ); | |
591 | ||
e7dcccf0 CW |
592 | if ($cvParam['type'] == 'File') { |
593 | $cvParam['file_id'] = $fieldValue['value']; | |
594 | } | |
595 | ||
6a488035 TO |
596 | if (!array_key_exists($dao->table_name, $cvParams)) { |
597 | $cvParams[$dao->table_name] = array(); | |
598 | } | |
599 | ||
600 | if (!array_key_exists($fieldValue['id'], $cvParams[$dao->table_name])) { | |
601 | $cvParams[$dao->table_name][$fieldValue['id']] = array(); | |
602 | } | |
603 | ||
604 | if ($fieldValue['id'] > 0) { | |
605 | $cvParam['id'] = $fieldValue['id']; | |
606 | } | |
607 | $cvParams[$dao->table_name][$fieldValue['id']][] = $cvParam; | |
608 | } | |
609 | } | |
610 | ||
611 | if (!empty($cvParams)) { | |
612 | self::create($cvParams); | |
613 | return array('is_error' => 0, 'result' => 1); | |
614 | } | |
615 | ||
616 | return CRM_Core_Error::createAPIError(ts('Unknown error')); | |
617 | } | |
618 | ||
619 | /** | |
100fef9d | 620 | * Take in an array of entityID, custom_ID |
6a488035 TO |
621 | * and gets the value from the appropriate table. |
622 | * | |
623 | * To get the values of custom fields with IDs 13 and 43 for contact ID 1327, use: | |
624 | * $params = array( 'entityID' => 1327, 'custom_13' => 1, 'custom_43' => 1 ); | |
625 | * | |
626 | * Entity Type will be infered by the custom fields you request | |
627 | * Specify $params['entityType'] if you do not supply any custom fields to return | |
628 | * and entity type is other than Contact | |
629 | * | |
630 | * @array $params | |
631 | * | |
c490a46a | 632 | * @param array $params |
2a6da8d7 EM |
633 | * |
634 | * @throws Exception | |
6a488035 | 635 | * @return array |
6a488035 | 636 | */ |
00be9182 | 637 | public static function &getValues(&$params) { |
6a488035 TO |
638 | if (empty($params)) { |
639 | return NULL; | |
640 | } | |
641 | if (!isset($params['entityID']) || | |
642 | CRM_Utils_Type::escape($params['entityID'], | |
643 | 'Integer', FALSE | |
644 | ) === NULL | |
645 | ) { | |
646 | return CRM_Core_Error::createAPIError(ts('entityID needs to be set and of type Integer')); | |
647 | } | |
648 | ||
649 | // first collect all the ids. The format is: | |
650 | // custom_ID | |
651 | $fieldIDs = array(); | |
652 | foreach ($params as $n => $v) { | |
653 | $key = $idx = NULL; | |
654 | if (substr($n, 0, 7) == 'custom_') { | |
655 | $idx = substr($n, 7); | |
656 | if (CRM_Utils_Type::escape($idx, 'Integer', FALSE) === NULL) { | |
657 | return CRM_Core_Error::createAPIError(ts('field ID needs to be of type Integer for index %1', | |
353ffa53 TO |
658 | array(1 => $idx) |
659 | )); | |
6a488035 TO |
660 | } |
661 | $fieldIDs[] = (int ) $idx; | |
662 | } | |
663 | } | |
664 | ||
665 | $default = array('Contact', 'Individual', 'Household', 'Organization'); | |
666 | if (!($type = CRM_Utils_Array::value('entityType', $params)) || | |
667 | in_array($params['entityType'], $default) | |
668 | ) { | |
669 | $type = NULL; | |
670 | } | |
671 | else { | |
672 | $entities = CRM_Core_SelectValues::customGroupExtends(); | |
673 | if (!array_key_exists($type, $entities)) { | |
674 | if (in_array($type, $entities)) { | |
675 | $type = $entities[$type]; | |
676 | if (in_array($type, $default)) { | |
677 | $type = NULL; | |
678 | } | |
679 | } | |
680 | else { | |
681 | return CRM_Core_Error::createAPIError(ts('Invalid entity type') . ': "' . $type . '"'); | |
682 | } | |
683 | } | |
684 | } | |
685 | ||
686 | $values = self::getEntityValues($params['entityID'], | |
687 | $type, | |
688 | $fieldIDs | |
689 | ); | |
690 | if (empty($values)) { | |
691 | // note that this behaviour is undesirable from an API point of view - it should return an empty array | |
692 | // since this is also called by the merger code & not sure the consequences of changing | |
693 | // are just handling undoing this in the api layer. ie. converting the error back into a success | |
694 | $result = array( | |
695 | 'is_error' => 1, | |
696 | 'error_message' => 'No values found for the specified entity ID and custom field(s).', | |
697 | ); | |
698 | return $result; | |
699 | } | |
700 | else { | |
701 | $result = array( | |
702 | 'is_error' => 0, | |
703 | 'entityID' => $params['entityID'], | |
704 | ); | |
705 | foreach ($values as $id => $value) { | |
706 | $result["custom_{$id}"] = $value; | |
707 | } | |
708 | return $result; | |
709 | } | |
710 | } | |
96025800 | 711 | |
6a488035 | 712 | } |