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