Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Contribute_Import_Field { | |
18 | ||
1330f57a SL |
19 | /** |
20 | * #@+ | |
6a488035 TO |
21 | * @var string |
22 | */ | |
23 | ||
24 | /** | |
100fef9d | 25 | * Name of the field |
1330f57a | 26 | * @var string |
6a488035 TO |
27 | */ |
28 | public $_name; | |
29 | ||
30 | /** | |
100fef9d | 31 | * Title of the field to be used in display |
1330f57a | 32 | * @var string |
6a488035 TO |
33 | */ |
34 | public $_title; | |
35 | ||
36 | /** | |
100fef9d | 37 | * Type of field |
5ebfe76b | 38 | * @var int |
6a488035 TO |
39 | */ |
40 | public $_type; | |
41 | ||
42 | /** | |
b67daa72 | 43 | * Is this field required. |
44 | * | |
45 | * @var bool | |
6a488035 TO |
46 | */ |
47 | public $_required; | |
48 | ||
49 | /** | |
100fef9d | 50 | * Data to be carried for use by a derived class |
6a488035 TO |
51 | * @var object |
52 | */ | |
53 | public $_payload; | |
54 | ||
55 | /** | |
100fef9d | 56 | * Regexp to match the CSV header of this column/field |
6a488035 TO |
57 | * @var string |
58 | */ | |
59 | public $_headerPattern; | |
60 | ||
61 | /** | |
100fef9d | 62 | * Regexp to match the pattern of data from various column/fields |
6a488035 TO |
63 | * @var string |
64 | */ | |
65 | public $_dataPattern; | |
66 | ||
67 | /** | |
100fef9d | 68 | * Value of this field |
5ebfe76b | 69 | * @var string|null |
6a488035 TO |
70 | */ |
71 | public $_value; | |
72 | ||
73 | /** | |
100fef9d | 74 | * This is soft credit field |
6a488035 TO |
75 | * @var string |
76 | */ | |
430ae6dd TO |
77 | public $_softCreditField; |
78 | ||
186c9c17 | 79 | /** |
100fef9d | 80 | * @param string $name |
5ebfe76b | 81 | * @param string $title |
186c9c17 EM |
82 | * @param int $type |
83 | * @param string $headerPattern | |
84 | * @param string $dataPattern | |
85 | * @param null $softCreditField | |
86 | */ | |
00be9182 | 87 | public function __construct($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//', $softCreditField = NULL) { |
6a488035 TO |
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 | ||
00be9182 | 97 | public function resetValue() { |
6a488035 TO |
98 | $this->_value = NULL; |
99 | } | |
100 | ||
101 | /** | |
74ab7ba8 EM |
102 | * Set a value. |
103 | * | |
104 | * The value is in string format. Convert the value to the type of this field | |
6a488035 | 105 | * and set the field value with the appropriate type |
74ab7ba8 | 106 | * |
5ebfe76b | 107 | * @param string $value |
6a488035 | 108 | */ |
00be9182 | 109 | public function setValue($value) { |
6a488035 TO |
110 | $this->_value = $value; |
111 | } | |
112 | ||
186c9c17 | 113 | /** |
74ab7ba8 EM |
114 | * Validate a field. |
115 | * | |
186c9c17 EM |
116 | * @return bool |
117 | */ | |
00be9182 | 118 | public function validate() { |
6a488035 TO |
119 | |
120 | if (CRM_Utils_System::isNull($this->_value)) { | |
121 | return TRUE; | |
122 | } | |
123 | ||
124 | switch ($this->_name) { | |
125 | case 'contact_id': | |
91bb24a7 | 126 | // note: we validate existence of the contact in API, upon |
127 | // insert (it would be too costly to do a db call here) | |
6a488035 TO |
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': | |
be2fb01f | 143 | static $seenTrxnIds = []; |
6a488035 TO |
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 | ||
874c9be7 | 160 | case 'financial_type': |
6a488035 TO |
161 | static $contributionTypes = NULL; |
162 | if (!$contributionTypes) { | |
874c9be7 | 163 | $contributionTypes = CRM_Contribute_PseudoConstant::financialType(); |
6a488035 TO |
164 | } |
165 | if (in_array($this->_value, $contributionTypes)) { | |
166 | return TRUE; | |
167 | } | |
168 | else { | |
169 | return FALSE; | |
170 | } | |
d3e86119 | 171 | break; |
6a488035 TO |
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 | } | |
96025800 | 205 | |
6a488035 | 206 | } |