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