Merge pull request #23893 from eileenmcnaughton/user_two
[civicrm-core.git] / CRM / Contribute / Import / Field.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Contribute_Import_Field {
18
19 /**
20 * #@+
21 * @var string
22 */
23
24 /**
25 * Name of the field
26 * @var string
27 */
28 public $_name;
29
30 /**
31 * Title of the field to be used in display
32 * @var string
33 */
34 public $_title;
35
36 /**
37 * Type of field
38 * @var int
39 */
40 public $_type;
41
42 /**
43 * Is this field required.
44 *
45 * @var bool
46 */
47 public $_required;
48
49 /**
50 * Data to be carried for use by a derived class
51 * @var object
52 */
53 public $_payload;
54
55 /**
56 * Regexp to match the CSV header of this column/field
57 * @var string
58 */
59 public $_headerPattern;
60
61 /**
62 * Regexp to match the pattern of data from various column/fields
63 * @var string
64 */
65 public $_dataPattern;
66
67 /**
68 * Value of this field
69 * @var string|null
70 */
71 public $_value;
72
73 /**
74 * This is soft credit field
75 * @var string
76 */
77 public $_softCreditField;
78
79 /**
80 * @param string $name
81 * @param string $title
82 * @param int $type
83 * @param string $headerPattern
84 * @param string $dataPattern
85 * @param null $softCreditField
86 */
87 public function __construct($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//', $softCreditField = NULL) {
88 $this->_name = $name;
89 $this->_title = $title;
90 $this->_type = $type;
91 $this->_headerPattern = $headerPattern;
92 $this->_dataPattern = $dataPattern;
93 $this->_softCreditField = $softCreditField;
94 $this->_value = NULL;
95 }
96
97 public function resetValue() {
98 $this->_value = NULL;
99 }
100
101 /**
102 * Set a value.
103 *
104 * The value is in string format. Convert the value to the type of this field
105 * and set the field value with the appropriate type
106 *
107 * @param string $value
108 */
109 public function setValue($value) {
110 $this->_value = $value;
111 }
112
113 /**
114 * Validate a field.
115 *
116 * @return bool
117 */
118 public function validate() {
119
120 if (CRM_Utils_System::isNull($this->_value)) {
121 return TRUE;
122 }
123
124 switch ($this->_name) {
125 case 'contact_id':
126 // note: we validate existence of the contact in API, upon
127 // insert (it would be too costly to do a db call here)
128 return CRM_Utils_Rule::integer($this->_value);
129
130 case 'receive_date':
131 case 'cancel_date':
132 case 'receipt_date':
133 case 'thankyou_date':
134 return CRM_Utils_Rule::date($this->_value);
135
136 case 'non_deductible_amount':
137 case 'total_amount':
138 case 'fee_amount':
139 case 'net_amount':
140 return CRM_Utils_Rule::money($this->_value);
141
142 case 'trxn_id':
143 static $seenTrxnIds = [];
144 if (in_array($this->_value, $seenTrxnIds)) {
145 return FALSE;
146 }
147 elseif ($this->_value) {
148 $seenTrxnIds[] = $this->_value;
149 return TRUE;
150 }
151 else {
152 $this->_value = NULL;
153 return TRUE;
154 }
155 break;
156
157 case 'currency':
158 return CRM_Utils_Rule::currencyCode($this->_value);
159
160 case 'financial_type':
161 static $contributionTypes = NULL;
162 if (!$contributionTypes) {
163 $contributionTypes = CRM_Contribute_PseudoConstant::financialType();
164 }
165 if (in_array($this->_value, $contributionTypes)) {
166 return TRUE;
167 }
168 else {
169 return FALSE;
170 }
171 break;
172
173 case 'payment_instrument':
174 static $paymentInstruments = NULL;
175 if (!$paymentInstruments) {
176 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument();
177 }
178 if (in_array($this->_value, $paymentInstruments)) {
179 return TRUE;
180 }
181 else {
182 return FALSE;
183 }
184 break;
185
186 default:
187 break;
188 }
189
190 // check whether that's a valid custom field id
191 // and if so, check the contents' validity
192 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($this->_name)) {
193 static $customFields = NULL;
194 if (!$customFields) {
195 $customFields = CRM_Core_BAO_CustomField::getFields('Contribution');
196 }
197 if (!array_key_exists($customFieldID, $customFields)) {
198 return FALSE;
199 }
200 return CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID]['data_type'], $this->_value);
201 }
202
203 return TRUE;
204 }
205
206 }