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