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