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