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