/
utils.php
523 lines (454 loc) · 19.4 KB
/
utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 3.1 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2010 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2010
* $Id$
*
*/
require_once 'CRM/Core/I18n.php';
require_once 'CRM/Core/Config.php';
require_once 'CRM/Core/Error.php';
require_once 'CRM/Utils/Array.php';
require_once 'CRM/Utils/String.php';
require_once 'CRM/Core/DAO/OptionGroup.php';
require_once 'CRM/Core/DAO/OptionValue.php';
function _crm_error( $message, $code = 8000, $level = 'Fatal', $params = null)
{
return CRM_Core_Error::createError( $message, $code, $level, $params );
}
function _crm_store_values( &$fields, &$params, &$values ) {
$valueFound = false;
foreach ($fields as $name => $field) {
// ignore all ids for now
if ( $name === 'id' || substr( $name, -1, 3 ) === '_id' ) {
continue;
}
if ( array_key_exists( $name, $params ) ) {
$values[$name] = $params[$name];
$valueFound = true;
}
}
return $valueFound;
}
function _crm_update_object(&$object, &$values)
{
// abort early if the object is an error object: CRM-500 & CRM-559
// we should trap and return somehow, or not get into this state
if ( is_a( $object, 'CRM_Core_Error' ) || ! $object ) {
return;
}
$fields =& $object->fields( );
$valueFound = false;
foreach ($fields as $name => $field) {
$key = $field['name'];
// ignore all ids for now
if ($key === 'id') {
continue;
}
if (array_key_exists( $name, $values)) {
$object->$key = $values[$name];
//if ( substr( $name, -1, 3 ) !== '_id' ) {
/* only say we've found a value if at least one is not null */
// why do we check for non-id-ness and not null-ness?
// we do want to update FKs and be able to null fields, don't we?
# if (substr($name, -3, 3) !== '_id' && $values[$name] !== null) {
$valueFound = true;
# }
}
}
// print_r($object);
if ($valueFound) {
$object->save();
}
}
function _crm_update_from_object(&$object, &$values, $empty = false, $zeroMoney = false)
{
$fields =& $object->fields();
require_once 'CRM/Utils/Type.php';
foreach ($fields as $name => $field) {
$key = $field['name'];
if (($key == 'id') or ($empty and empty($object->$key)) or
($zeroMoney and $field['type'] == CRM_Utils_Type::T_MONEY and $object->$key == '0.00')) {
continue;
}
$values[$name] = $object->$key;
// FIXME? change the dates from YYYY-MM-DD hh:mm:ss format back to YYYYMMDDhhmmss
// so the $values array is actually importable
if ($field['type'] & CRM_Utils_Type::T_DATE) {
$dropArray = array('-' => '', ':' => '', ' ' => '');
$values[$name] = strtr($values[$name], $dropArray);
}
}
}
function _crm_object_to_array( &$dao, &$values )
{
$tmpFields = $dao->fields();
$fields = array();
//rebuild $fields array to fix unique name of the fields
foreach( $tmpFields as $key => $val ) {
$fields[$val["name"]] = $val;
}
foreach( $fields as $key => $value ) {
if (array_key_exists($key, $dao)) {
$values[$key] = $dao->$key;
}
}
}
/**
* This function ensures that we have the right input contribution parameters
*
* We also need to make sure we run all the form rules on the params list
* to ensure that the params are valid
*
* @param array $params Associative array of property name/value
* pairs to insert in new contribution.
*
* @return bool|CRM_Utils_Error
* @access public
*/
function _crm_check_contrib_params( &$params ) {
static $required = array( 'contact_id', 'total_amount', 'contribution_type_id' );
// cannot create a contribution with empty params
if ( empty( $params ) ) {
return _crm_error( 'Input Parameters empty' );
}
$valid = true;
$error = '';
foreach ( $required as $field ) {
if ( ! CRM_Utils_Array::value( $field, $params ) ) {
$valid = false;
$error .= $field;
break;
}
}
if ( ! $valid ) {
return _crm_error( "Required fields not found for contribution $error" );
}
return true;
}
/**
* take the input parameter list as specified in the data model and
* convert it into the same format that we use in QF and BAO object
*
* @param array $params Associative array of property name/value
* pairs to insert in new contact.
* @param array $values The reformatted properties that we can use internally
* '
* @return array|CRM_Error
* @access public
*/
function _crm_format_contrib_params( &$params, &$values, $create=false ) {
// copy all the contribution fields as is
$fields =& CRM_Contribute_DAO_Contribution::fields( );
_crm_store_values( $fields, $params, $values );
foreach ($params as $key => $value) {
// ignore empty values or empty arrays etc
if ( CRM_Utils_System::isNull( $value ) ) {
continue;
}
switch ($key) {
case 'contribution_contact_id':
if (!CRM_Utils_Rule::integer($value)) {
return _crm_error("contact_id not valid: $value");
}
$dao =& new CRM_Core_DAO();
$qParams = array();
$svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value",
$qParams);
if (!$svq) {
return _crm_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
}
$values['contact_id'] = $values['contribution_contact_id'];
unset ($values['contribution_contact_id']);
break;
case 'receive_date':
case 'cancel_date':
case 'receipt_date':
case 'thankyou_date':
if (!CRM_Utils_Rule::date($value)) {
return _crm_error("$key not a valid date: $value");
}
break;
case 'non_deductible_amount':
case 'total_amount':
case 'fee_amount':
case 'net_amount':
if (!CRM_Utils_Rule::money($value)) {
return _crm_error("$key not a valid amount: $value");
}
break;
case 'currency':
if (!CRM_Utils_Rule::currencyCode($value)) {
return _crm_error("currency not a valid code: $value");
}
break;
case 'contribution_type':
require_once 'CRM/Contribute/PseudoConstant.php';
$values['contribution_type_id'] = CRM_Utils_Array::key( ucfirst( $value ),
CRM_Contribute_PseudoConstant::contributionType( )
);
break;
case 'payment_instrument':
require_once 'CRM/Core/OptionGroup.php';
$values['payment_instrument_id'] = CRM_Core_OptionGroup::getValue( 'payment_instrument', $value );
break;
case 'contribution_status_id':
require_once 'CRM/Core/OptionGroup.php';
$values['contribution_status_id'] = CRM_Core_OptionGroup::getValue( 'contribution_status', $value );
break;
default:
break;
}
}
if ( array_key_exists( 'note', $params ) ) {
$values['note'] = $params['note'];
}
_crm_format_custom_params( $params, $values, 'Contribution' );
if ( $create ) {
// CRM_Contribute_BAO_Contribution::add() handles contribution_source
// So, if $values contains contribution_source, convert it to source
$changes = array( 'contribution_source' => 'source' );
foreach ($changes as $orgVal => $changeVal) {
if ( isset($values[$orgVal]) ) {
$values[$changeVal] = $values[$orgVal];
unset($values[$orgVal]);
}
}
}
return null;
}
/**
* take the input parameter list as specified in the data model and
* convert it into the same format that we use in QF and BAO object
*
* @param array $params Associative array of property name/value
* pairs to insert in new contact.
* @param array $values The reformatted properties that we can use internally
*
* @param array $create Is the formatted Values array going to
* be used for CRM_Member_BAO_Membership:create()
*
* @return array|CRM_Error
* @access public
*/
function _crm_format_membership_params( &$params, &$values, $create=false)
{
require_once "CRM/Member/DAO/Membership.php";
$fields =& CRM_Member_DAO_Membership::fields( );
_crm_store_values( $fields, $params, $values );
foreach ($params as $key => $value) {
// ignore empty values or empty arrays etc
if ( CRM_Utils_System::isNull( $value ) ) {
continue;
}
switch ($key) {
case 'membership_contact_id':
if (!CRM_Utils_Rule::integer($value)) {
return _crm_error("contact_id not valid: $value");
}
$dao =& new CRM_Core_DAO();
$qParams = array();
$svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value",$qParams);
if (!$svq) {
return _crm_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
}
$values['contact_id'] = $values['membership_contact_id'];
unset($values['membership_contact_id']);
break;
case 'join_date':
case 'membership_start_date':
case 'membership_end_date':
if (!CRM_Utils_Rule::date($value)) {
return _crm_error("$key not a valid date: $value");
}
break;
case 'membership_type_id':
$id = CRM_Core_DAO::getFieldValue( "CRM_Member_DAO_MembershipType", $value, 'id', 'name' );
$values[$key] = $id;
break;
case 'status_id':
$id = CRM_Core_DAO::getFieldValue( "CRM_Member_DAO_MembershipStatus", $value, 'id', 'name' );
$values[$key] = $id;
break;
default:
break;
}
}
_crm_format_custom_params( $params, $values, 'Membership' );
if ( $create ) {
// CRM_Member_BAO_Membership::create() handles membership_start_date,
// membership_end_date and membership_source. So, if $values contains
// membership_start_date, membership_end_date or membership_source,
// convert it to start_date, end_date or source
$changes = array('membership_start_date' => 'start_date',
'membership_end_date' => 'end_date',
'membership_source' => 'source',
);
foreach ($changes as $orgVal => $changeVal) {
if ( isset($values[$orgVal]) ) {
$values[$changeVal] = $values[$orgVal];
unset($values[$orgVal]);
}
}
}
return null;
}
function _crm_format_custom_params( &$params, &$values, $extends )
{
$values['custom'] = array();
$customFields = CRM_Core_BAO_CustomField::getFields( $extends );
foreach ($params as $key => $value) {
if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
/* check if it's a valid custom field id */
if ( !array_key_exists($customFieldID, $customFields)) {
return _crm_error('Invalid custom field ID');
}
$fieldType = null;
// modified for CRM-1586
// check data type for importing custom field (labels) with data type Integer/Float/Money
/* validate the data against the CF type */
if( ( $customFields[$customFieldID]['data_type'] == "Int") ||
( $customFields[$customFieldID]['data_type'] == "Float" ) ||
( $customFields[$customFieldID]['data_type'] == "Money" ) ) {
if ( $customFields[$customFieldID]['html_type'] == "Text" ) {
$fieldType = $customFields[$customFieldID]['data_type'];
} else {
$customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID);
foreach($customOption as $customValue => $customLabel) {
//check wether $value is label or value
if ( ( strtolower($customValue) == strtolower( trim( $value ) ) ) ) {
$fieldType = "String";
} else if ( ( strtolower($customValue) == strtolower( trim( $value ) ) ) ) {
$fieldType = $customFields[$customFieldID]['data_type'];
}
}
}
} else {
//set the Field type
$fieldType = $customFields[$customFieldID]['data_type'];
}
$valid = null;
//Validate the datatype of $value
$valid = CRM_Core_BAO_CustomValue::typecheck( $fieldType, $value);
//return error, if not valid custom field
if ( ! $valid ) {
return _crm_error('Invalid value for custom field ' .
$customFields[$customFieldID][1]);
}
// fix the date field if so
if ( $customFields[$customFieldID]['data_type'] == 'Date' ) {
$value = str_replace( '-', '', $value );
}
// fixed for checkbox and multiselect
$newMulValues = array();
if ( $customFields[$customFieldID]['html_type'] == 'CheckBox' || $customFields[$customFieldID]['html_type'] =='Multi-Select') {
$value = str_replace(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR,',',trim($value,CRM_Core_BAO_CustomOption::VALUE_SEPERATOR));
$value = str_replace("|",",",$value);
$mulValues = explode( ',' , $value );
$custumOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
foreach( $mulValues as $v1 ) {
foreach($customOption as $customValue => $customLabel) {
if (( strtolower($customLabel) == strtolower(trim($v1)) )||( strtolower($customValue) == strtolower(trim($v1)) )) {
$newMulValues[] = $customValue;
}
}
}
$value = CRM_Core_BAO_CustomOption::VALUE_SEPERATOR.implode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR,$newMulValues).CRM_Core_BAO_CustomOption::VALUE_SEPERATOR;
} else if ( $customFields[$customFieldID]['html_type'] == 'Select' || $customFields[$customFieldID]['html_type'] == 'Radio' ) {
$custumOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
foreach($customOption as $customValue => $customLabel) {
if( ( strtolower($customLabel) == strtolower(trim($value)) )||( strtolower($customValue) == strtolower(trim($value)))) {
$value = $customValue;
break;
}
}
}
$values['custom'][$customFieldID] = array(
'value' => $value,
'extends' => $customFields[$customFieldID]['extends'],
'type' => $customFields[$customFieldID]['data_type'],
'custom_field_id' => $customFieldID,
);
}
}
}
function _crm_update_contribution($contribution, $values, $overwrite = true)
{
CRM_Contribute_BAO_Contribution::resolveDefaults($values, true);
if (!$overwrite) {
_crm_update_from_object($contribution, $values, true, true);
}
_crm_update_object($contribution, $values);
return $contribution;
}
/**
* This function ensures that we have the right input parameters
*
* We also need to make sure we run all the form rules on the params list
* to ensure that the params are valid
*
* @param array $params Associative array of property name/value
* pairs to insert in new history.
*
* @return bool|CRM_Utils_Error
* @access public
*/
function _crm_check_required_fields(&$params, $daoName)
{
$extends = CRM_Utils_Array::value( 'extends', $params );
if ( ( $extends == 'Activity' ||
$extends == 'Phonecall' ||
$extends == 'Meeting' ||
$extends == 'Group' ||
$extends == 'Contribution'
) &&
( $params['style'] == 'Tab' ) ) {
return _crm_error(ts("Can not create Custom Group in Tab for ". $extends));
}
require_once(str_replace('_', DIRECTORY_SEPARATOR, $daoName) . ".php");
$dao =& new $daoName();
$fields = $dao->fields();
//eval('$dao =& new CRM_Core_DAO_' . $type . 'History();');
$missing = array();
foreach ($fields as $k => $v) {
if ($k == 'id' || $k == 'domain_id') {
continue;
}
if (CRM_Utils_Array::value( 'required', $v ) && !(isset($params[$k]))) {
$missing[] = $k;
}
}
if (!empty($missing)) {
return _crm_error(ts("Required fields ". implode(',', $missing) . " for $daoName are not found"));
}
return true;
}
function _crm_initialize( ) {
$config =& CRM_Core_Config::singleton( );
}