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