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