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_Contribute_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 | */ | |
86 | public $_value; | |
87 | ||
88 | /** | |
100fef9d | 89 | * This is soft credit field |
6a488035 TO |
90 | * @var string |
91 | */ | |
430ae6dd TO |
92 | public $_softCreditField; |
93 | ||
186c9c17 | 94 | /** |
100fef9d | 95 | * @param string $name |
186c9c17 EM |
96 | * @param $title |
97 | * @param int $type | |
98 | * @param string $headerPattern | |
99 | * @param string $dataPattern | |
100 | * @param null $softCreditField | |
101 | */ | |
430ae6dd | 102 | function __construct($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//', $softCreditField = NULL) { |
6a488035 TO |
103 | $this->_name = $name; |
104 | $this->_title = $title; | |
105 | $this->_type = $type; | |
106 | $this->_headerPattern = $headerPattern; | |
107 | $this->_dataPattern = $dataPattern; | |
108 | $this->_softCreditField = $softCreditField; | |
109 | $this->_value = NULL; | |
110 | } | |
111 | ||
112 | function resetValue() { | |
113 | $this->_value = NULL; | |
114 | } | |
115 | ||
116 | /** | |
100fef9d | 117 | * The value is in string format. convert the value to the type of this field |
6a488035 TO |
118 | * and set the field value with the appropriate type |
119 | */ | |
120 | function setValue($value) { | |
121 | $this->_value = $value; | |
122 | } | |
123 | ||
186c9c17 EM |
124 | /** |
125 | * @return bool | |
126 | */ | |
6a488035 TO |
127 | function validate() { |
128 | ||
129 | if (CRM_Utils_System::isNull($this->_value)) { | |
130 | return TRUE; | |
131 | } | |
132 | ||
133 | switch ($this->_name) { | |
134 | case 'contact_id': | |
91bb24a7 | 135 | // note: we validate existence of the contact in API, upon |
136 | // insert (it would be too costly to do a db call here) | |
6a488035 TO |
137 | return CRM_Utils_Rule::integer($this->_value); |
138 | ||
139 | case 'receive_date': | |
140 | case 'cancel_date': | |
141 | case 'receipt_date': | |
142 | case 'thankyou_date': | |
143 | return CRM_Utils_Rule::date($this->_value); | |
144 | ||
145 | case 'non_deductible_amount': | |
146 | case 'total_amount': | |
147 | case 'fee_amount': | |
148 | case 'net_amount': | |
149 | return CRM_Utils_Rule::money($this->_value); | |
150 | ||
151 | case 'trxn_id': | |
152 | static $seenTrxnIds = array(); | |
153 | if (in_array($this->_value, $seenTrxnIds)) { | |
154 | return FALSE; | |
155 | } | |
156 | elseif ($this->_value) { | |
157 | $seenTrxnIds[] = $this->_value; | |
158 | return TRUE; | |
159 | } | |
160 | else { | |
161 | $this->_value = NULL; | |
162 | return TRUE; | |
163 | } | |
164 | break; | |
165 | ||
166 | case 'currency': | |
167 | return CRM_Utils_Rule::currencyCode($this->_value); | |
168 | ||
169 | case 'financial_type': | |
170 | static $contributionTypes = NULL; | |
171 | if (!$contributionTypes) { | |
172 | $contributionTypes = CRM_Contribute_PseudoConstant::financialType(); | |
173 | } | |
174 | if (in_array($this->_value, $contributionTypes)) { | |
175 | return TRUE; | |
176 | } | |
177 | else { | |
178 | return FALSE; | |
179 | } | |
180 | break; | |
181 | ||
182 | case 'payment_instrument': | |
183 | static $paymentInstruments = NULL; | |
184 | if (!$paymentInstruments) { | |
185 | $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
186 | } | |
187 | if (in_array($this->_value, $paymentInstruments)) { | |
188 | return TRUE; | |
189 | } | |
190 | else { | |
191 | return FALSE; | |
192 | } | |
193 | break; | |
194 | ||
195 | default: | |
196 | break; | |
197 | } | |
198 | ||
199 | // check whether that's a valid custom field id | |
200 | // and if so, check the contents' validity | |
201 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($this->_name)) { | |
202 | static $customFields = NULL; | |
203 | if (!$customFields) { | |
204 | $customFields = CRM_Core_BAO_CustomField::getFields('Contribution'); | |
205 | } | |
206 | if (!array_key_exists($customFieldID, $customFields)) { | |
207 | return FALSE; | |
208 | } | |
209 | return CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID]['data_type'], $this->_value); | |
210 | } | |
211 | ||
212 | return TRUE; | |
213 | } | |
214 | } | |
215 |