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