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