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