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