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