Import from SVN (r45945, r596)
[civicrm-core.git] / CRM / Member / Import / Field.php
CommitLineData
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 */
35class CRM_Member_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; function __construct($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') {
87 $this->_name = $name;
88 $this->_title = $title;
89 $this->_type = $type;
90 $this->_headerPattern = $headerPattern;
91 $this->_dataPattern = $dataPattern;
92
93 $this->_value = NULL;
94 }
95
96 function resetValue() {
97 $this->_value = NULL;
98 }
99
100 /**
101 * the value is in string format. convert the value to the type of this field
102 * and set the field value with the appropriate type
103 */
104 function setValue($value) {
105 $this->_value = $value;
106 }
107
108 function validate() {
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':
133 static $seenTrxnIds = array();
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 }
190 return CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID]['data_type'], $this->_value);
191 }
192
193 return TRUE;
194 }
195}
196